Files

52 lines
1.5 KiB
Go

package main
// The platform's answer to a posted scan: what the pipeline does with the
// quality gate the code-quality server evaluated (POST /api/v1/scans/sast,
// `gate`). The platform decides from the tenant's policy for the stage the
// pipeline's token belongs to; this module only acts on it:
//
// - pass — nothing to do;
// - warn — reported as `gate-decision`; the Jenkins stage turns red and the
// pipeline goes on;
// - block — Scan fails, so the stage and the pipeline stop (a UAT pull
// request is then closed by the job's failure handler).
//
// An answer without a `gate` (a platform older than the policy, or a reply this
// module cannot read) is `pass`: the module never invents a stricter rule than
// the platform asked for, and the deploy gate still judges the stored verdict.
import (
"encoding/json"
"fmt"
)
type platformGate struct {
Decision string `json:"decision"`
Reason string `json:"reason"`
}
func gateFromResponse(body []byte) platformGate {
var answer struct {
Gate *platformGate `json:"gate"`
}
if err := json.Unmarshal(body, &answer); err != nil || answer.Gate == nil {
return platformGate{Decision: "pass"}
}
switch answer.Gate.Decision {
case "pass", "warn", "block":
return *answer.Gate
}
return platformGate{Decision: "pass"}
}
func (g platformGate) err() error {
if g.Decision != "block" {
return nil
}
reason := g.Reason
if reason == "" {
reason = "the quality gate failed"
}
return fmt.Errorf("IntegraCI stopped the pipeline: %s", reason)
}