The finding pipeline-failed has scope delivery and id <module>.<branch>, one per mover whose required step failed or could not run, naming the pipeline and the steps, owned by the commit's author as before; that module's next passed pipeline on that branch clears it. Unit tests 84 to 84 (the newer-revision test replaced). Builds: design 47 H §770; lead answers H-A-19, H-A-59 Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
157 lines
6.6 KiB
Go
157 lines
6.6 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
mcc "git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
|
condseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
|
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshpipelinerunner"
|
|
)
|
|
|
|
// PipelineFailedKind is the kind of the runner's finding of a failed head (design 47 G §33, §540).
|
|
const PipelineFailedKind = "pipeline-failed"
|
|
|
|
// forgeOwner is the form of an owner that names a commit's author as the forge does (design 47 G §540, §678).
|
|
var forgeOwner = regexp.MustCompile(`^forge:[A-Za-z0-9._-]+$`)
|
|
|
|
// Findings is the runner's finding writer as the failures use it: mesh-sdk's held writer of the seat mesh-conditions' finding.
|
|
type Findings interface {
|
|
Write(ctx context.Context, f condseat.Finding) error
|
|
Clear(ctx context.Context, scope, id, kind, why string) error
|
|
Held() []condseat.Finding
|
|
}
|
|
|
|
// Failures says a failed head (design 47 G §33, §540, A-191): a `required` step that failed, or ended `could not run`, is the
|
|
// finding `pipeline-failed` of its commit, naming the module, the branch, the commit and the steps, with the commit's author as
|
|
// its owner `forge:<account>`; a newer revision of that branch passing each of those steps clears it. The condition keeper
|
|
// raises it as the author's, and the operator's after two hours (§540).
|
|
type Failures struct {
|
|
Findings Findings
|
|
// Observer is the runner's own module name.
|
|
Observer string
|
|
// Author reads the forge's login of the commit's author through the seat git (the verb pull, by the commit that merged
|
|
// it); an error is a commit no pull request merged or a read that failed.
|
|
Author func(repository, commit string) (string, error)
|
|
Now func() time.Time
|
|
Logf func(string, ...any)
|
|
}
|
|
|
|
func (f *Failures) logf(format string, args ...any) {
|
|
if f.Logf != nil {
|
|
f.Logf(format, args...)
|
|
}
|
|
}
|
|
|
|
// failedSteps are the required steps of p that failed or could not run, the approval excepted: an approval's end is the
|
|
// operator's, not a failure of the commit.
|
|
func failedSteps(p seat.Pipeline, module string) []seat.StepResult {
|
|
var out []seat.StepResult
|
|
for _, s := range p.Steps {
|
|
if s.Module == module && s.Status == seat.StepResultStatusRequired && s.Step != seat.StepResultStepApproval &&
|
|
(s.Outcome == seat.StepResultOutcomeFailed || s.Outcome == seat.StepResultOutcomeCouldNotRun) {
|
|
out = append(out, s)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// findingID is a mover's finding's id, `<module>.<branch>`, each escaped as a key's name is (design 47 H §770).
|
|
func findingID(module, branch string) string { return About(module, branch) }
|
|
|
|
// held is the finding pipeline-failed of module on branch the findings hold, nil when none (§770).
|
|
func (f *Failures) held(module, branch string) *condseat.Finding {
|
|
id := findingID(module, branch)
|
|
for _, h := range f.Findings.Held() {
|
|
if h.Scope == mcc.ScopeDelivery && h.ID == id && h.Kind == PipelineFailedKind {
|
|
h := h
|
|
return &h
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// StepEnded raises, for the step s of p just ended, the finding pipeline-failed of its mover when a required step of that
|
|
// mover failed or could not run: one finding per failing mover, id `<module>.<branch>` (design 47 H §770, H-A-19, H-A-59).
|
|
func (f *Failures) StepEnded(store Updater, p seat.Pipeline, s seat.StepResult) {
|
|
m, ok := MoverOf(p, s.Module)
|
|
if f == nil || f.Findings == nil || !ok {
|
|
return
|
|
}
|
|
if len(failedSteps(p, m.Module)) > 0 {
|
|
f.write(context.Background(), p, m)
|
|
}
|
|
}
|
|
|
|
// Ended clears, for a pipeline that ended passed, each of its movers' finding pipeline-failed on its branch: that module's
|
|
// next passed pipeline on that branch clears it (§770).
|
|
func (f *Failures) Ended(p seat.Pipeline) {
|
|
if f == nil || f.Findings == nil || p.Outcome != seat.PipelineOutcomePassed {
|
|
return
|
|
}
|
|
for _, m := range p.Movers {
|
|
if f.held(m.Module, p.Branch) == nil {
|
|
continue
|
|
}
|
|
if err := f.Findings.Clear(context.Background(), mcc.ScopeDelivery, findingID(m.Module, p.Branch), PipelineFailedKind,
|
|
"the pipeline "+p.Pipeline+" of "+p.Branch+" passed"); err != nil {
|
|
f.logf("the finding %s of %s on %s is not cleared: %v", PipelineFailedKind, m.Module, p.Branch, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// write raises or rewrites the mover m's finding, naming the pipeline and the steps (§770).
|
|
func (f *Failures) write(ctx context.Context, p seat.Pipeline, m seat.Mover) {
|
|
steps := failedSteps(p, m.Module)
|
|
var names []string
|
|
for _, s := range steps {
|
|
names = append(names, strings.TrimSpace(s.Artifact+" "+s.Step)+" ("+s.Outcome+")")
|
|
}
|
|
now := time.Now().UTC()
|
|
if f.Now != nil {
|
|
now = f.Now().UTC()
|
|
}
|
|
owner, why := "", ""
|
|
if old := f.held(m.Module, p.Branch); old != nil {
|
|
owner = old.Owner
|
|
}
|
|
if owner == "" {
|
|
owner, why = f.author(m)
|
|
}
|
|
explanation := fmt.Sprintf("The required steps %s of %s did not pass in the pipeline %s on the branch %s, at the commit %s. The module's next passed pipeline on the branch clears this.",
|
|
strings.Join(names, ", "), m.Module, p.Pipeline, p.Branch, m.Commit)
|
|
if why != "" {
|
|
explanation += " " + why
|
|
}
|
|
id := findingID(m.Module, p.Branch)
|
|
err := f.Findings.Write(ctx, condseat.Finding{Scope: mcc.ScopeDelivery, ID: id, Kind: PipelineFailedKind, Severity: condseat.FindingSeverityWarning,
|
|
Summary: fmt.Sprintf("%s on %s in %s: %s", m.Module, p.Branch, p.Pipeline, strings.Join(names, ", ")),
|
|
Headline: fmt.Sprintf("A required step of %s failed on %s", m.Module, p.Branch),
|
|
Explanation: explanation, Needs: "a passed pipeline of " + m.Module + " on " + p.Branch,
|
|
Owner: owner, Source: "pipeline-runner." + PipelineFailedKind, Observer: f.Observer, At: now,
|
|
Subject: condseat.Subject{Scope: mcc.ScopeDelivery, ID: id}})
|
|
if err != nil {
|
|
f.logf("the finding %s of %s on %s is not written: %v", PipelineFailedKind, m.Module, p.Branch, err)
|
|
}
|
|
}
|
|
|
|
// author is the forge account of the mover's commit's author, read through the forge (§33).
|
|
func (f *Failures) author(m seat.Mover) (string, string) {
|
|
if f.Author == nil {
|
|
return "", "The commit's author is not read, so the finding has no owner."
|
|
}
|
|
account, err := f.Author(m.Repository, m.Commit)
|
|
if err != nil {
|
|
f.logf("the author of %s in %s is not read, so its finding %s has no owner: %v", m.Commit, m.Repository, PipelineFailedKind, err)
|
|
return "", "The commit's author is not known (" + err.Error() + "), so the finding has no owner."
|
|
}
|
|
if !forgeOwner.MatchString("forge:" + account) {
|
|
f.logf("the author %q of %s is no forge account name, so its finding %s has no owner", account, m.Commit, PipelineFailedKind)
|
|
return "", "The commit's author is not known as a forge account, so the finding has no owner."
|
|
}
|
|
return "forge:" + account, ""
|
|
}
|