AskSteps gains SayImports, called once at the end of a build that read its imports (any branch; the indexer judges the fingerprint), with the module, the fingerprint asked, the commit and the imports; SayImportsOn publishes it on mesh.seat.<seat>.event.imports-reported.<node> (buildstep.ImportsReportedSubject), id per module and fingerprint. A failed say is journalled and leaves the answer standing; BuildAnswer.Imports stays as the say's input. Builds: design 47 H2 H-A-517, H-A-518, H-A-532 DQ-2, DQ-7, DMC-2
367 lines
16 KiB
Go
367 lines
16 KiB
Go
package buildstep
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
meshkeys "git.novox.be/novox/mesh-sdk/go/keys"
|
|
)
|
|
|
|
// **A pipeline step, once.** The runner (seat mesh-pipeline-runner) asks each lint and test step of an artifact on the
|
|
// serving build seat's stream check, and the seat's holder says its end as the seat's event step-ended on its own node's
|
|
// subject (novox/hq ADR 0666 §7, §8, §21; design 47 *Pipeline steps and the policy*). These are the neutral forms of the
|
|
// part build-step's StepRun and StepEnded, which the three build modules share; the publish itself is the holder's.
|
|
|
|
// The pipeline steps a build seat runs.
|
|
const (
|
|
StepLint = "lint"
|
|
StepUnit = "unit"
|
|
StepIntegration = "integration"
|
|
// StepInstall is the Node build seat's install of a module's npm tree, asked before the integration level; its end
|
|
// names the archive (design 47 H2 §800, H-A-379).
|
|
StepInstall = "install"
|
|
)
|
|
|
|
// PipelineSteps are the steps a build seat serves, in a pipeline's order.
|
|
var PipelineSteps = []string{StepLint, StepUnit, StepInstall, StepIntegration}
|
|
|
|
// The outcomes a build seat says of a step: never passed for a step that ran nothing.
|
|
const (
|
|
OutcomePassed = "passed"
|
|
OutcomeFailed = "failed"
|
|
OutcomeNothingToRun = "nothing to run"
|
|
OutcomeCouldNotRun = "could not run"
|
|
// OutcomeDeclined is a pipeline step the holder's node lacks a need of, said with Needs and Lacks: the runner puts it
|
|
// again on a node that has the need (design 47 H2 H-A-375, H-A-445).
|
|
OutcomeDeclined = "declined"
|
|
)
|
|
|
|
// StepOutcomes are the outcomes a build seat may say.
|
|
var StepOutcomes = []string{OutcomePassed, OutcomeFailed, OutcomeNothingToRun, OutcomeCouldNotRun, OutcomeDeclined}
|
|
|
|
// StepEndedVersion is the layout version of a step's end.
|
|
const StepEndedVersion = 1
|
|
|
|
// StepEndedInlineMost is the most bytes of a run's answer a step-ended carries inline as `output`: the bus's message limit
|
|
// less the room its headers take and 16 KiB for the end's other fields. An answer over it is kept by the holder as an object,
|
|
// which `answer-object` names and the seat's verb `answer` answers (design 47 G A-254, H2 H-A-412).
|
|
const StepEndedInlineMost = (1 << 20) - (8 << 10) - (16 << 10)
|
|
|
|
// AnswerObjectName is the name of the object a build seat's holder keeps a step's end over what its key or event carries
|
|
// in, by the ask's id, in its steps objects (design 47 H2 H-A-412 addendum).
|
|
func AnswerObjectName(ask string) string { return "answer." + ask }
|
|
|
|
// KeptResult is a package's passing result at the digest of what its tests read (ADR 0666 §8).
|
|
type KeptResult struct {
|
|
Package string `json:"package"`
|
|
Digest string `json:"digest"`
|
|
Level string `json:"level"`
|
|
Toolchain *ToolchainUsed `json:"toolchain,omitempty"`
|
|
Pipeline string `json:"pipeline,omitempty"`
|
|
}
|
|
|
|
// TestCounts are how many tests a step ran and carried, per level.
|
|
type TestCounts struct {
|
|
Unit int `json:"unit,omitempty"`
|
|
Integration int `json:"integration,omitempty"`
|
|
}
|
|
|
|
// StepRun is one pipeline step of one artifact at one commit.
|
|
type StepRun struct {
|
|
Pipeline string `json:"pipeline"`
|
|
Step string `json:"step"`
|
|
Module string `json:"module"`
|
|
Artifact string `json:"artifact"`
|
|
Branch string `json:"branch,omitempty"`
|
|
Commit string `json:"commit"`
|
|
Revision int `json:"revision,omitempty"`
|
|
Fingerprint string `json:"fingerprint"`
|
|
Fetch Fetch `json:"fetch"`
|
|
Toolchain *ToolchainUsed `json:"toolchain,omitempty"`
|
|
Kept []KeptResult `json:"kept,omitempty"`
|
|
Rerun bool `json:"rerun,omitempty"`
|
|
By string `json:"by,omitempty"`
|
|
// Archive is the reference of the install archive in the artifact store, which the Go build module fetches and
|
|
// unpacks into the workspace before the step (design 47 H2 §800, H-A-399 (2)).
|
|
Archive string `json:"archive,omitempty"`
|
|
}
|
|
|
|
// Skips says whether the step runs no tests of pkg at digest, its kept result holding them.
|
|
func (r StepRun) Skips(pkg, digest string) bool {
|
|
return slices.ContainsFunc(r.Kept, func(k KeptResult) bool {
|
|
return k.Package == pkg && k.Digest == digest && k.Level == r.Step
|
|
})
|
|
}
|
|
|
|
func (r StepRun) check(id string) error {
|
|
if !slices.Contains(PipelineSteps, r.Step) {
|
|
return fmt.Errorf("buildstep: the step ask %s names the step %q, not %s", id, r.Step, strings.Join(PipelineSteps, ", "))
|
|
}
|
|
if r.Pipeline == "" {
|
|
return fmt.Errorf("buildstep: the step ask %s names no pipeline", id)
|
|
}
|
|
if err := meshkeys.CheckName("module", r.Module); err != nil {
|
|
return fmt.Errorf("the step ask %s: %w", id, err)
|
|
}
|
|
if mod, art, ok := strings.Cut(r.Artifact, ":"); !ok || mod != r.Module || art == "" {
|
|
return fmt.Errorf("buildstep: the step ask %s names the artifact %q, not %s:<artifact>", id, r.Artifact, r.Module)
|
|
}
|
|
if !askCommitOK.MatchString(r.Commit) {
|
|
return fmt.Errorf("buildstep: the step ask %s names no full commit (%q)", id, r.Commit)
|
|
}
|
|
if r.Fingerprint == "" {
|
|
return fmt.Errorf("buildstep: the step ask %s names no build-input fingerprint", id)
|
|
}
|
|
if err := r.Fetch.check(id); err != nil {
|
|
return err
|
|
}
|
|
if r.Rerun != (r.By != "") {
|
|
return fmt.Errorf("buildstep: the step ask %s is a rerun with no caller, or names a caller and is no rerun", id)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// StepEnded is an ask's end, said by the build seat's holder on the node that ran it, whatever its asker (design 47 G
|
|
// §602): a pipeline step's end names its pipeline, step, module, artifact, commit and fingerprint; the end of any other
|
|
// ask names its asker, the ask, the node, the outcome and when, and neither commit nor fingerprint (G §548).
|
|
type StepEnded struct {
|
|
V int `json:"v"`
|
|
ID string `json:"id"`
|
|
Asker string `json:"asker"`
|
|
Ask string `json:"ask"`
|
|
Pipeline string `json:"pipeline,omitempty"`
|
|
Step string `json:"step,omitempty"`
|
|
Module string `json:"module,omitempty"`
|
|
Artifact string `json:"artifact,omitempty"`
|
|
Commit string `json:"commit,omitempty"`
|
|
Fingerprint string `json:"fingerprint,omitempty"`
|
|
Node string `json:"node"`
|
|
Outcome string `json:"outcome"`
|
|
Tests *TestCounts `json:"tests,omitempty"`
|
|
Carried []KeptResult `json:"carried,omitempty"`
|
|
Kept []KeptResult `json:"kept,omitempty"`
|
|
Said string `json:"said,omitempty"`
|
|
// Needs and Lacks are, for a declined step, the seats the package needs the node to hold and what the node lacked of
|
|
// them (design 47 H2 H-A-445).
|
|
Needs []string `json:"needs,omitempty"`
|
|
Lacks []string `json:"lacks,omitempty"`
|
|
Output string `json:"output,omitempty"`
|
|
AnswerObject string `json:"answer-object,omitempty"`
|
|
// NotRun are the tests that did not run, each with its reason, counted apart from passed and failed (design 47 H2
|
|
// §800).
|
|
NotRun []NotRun `json:"not-run,omitempty"`
|
|
// Archive is, for an install, the reference the installed tree's archive was published under in the artifact store
|
|
// (the seat oci-artifact-registry; design 47 H2 §800, H-A-399 (2)).
|
|
Archive string `json:"archive,omitempty"`
|
|
Rerun bool `json:"rerun,omitempty"`
|
|
By string `json:"by,omitempty"`
|
|
Started time.Time `json:"started,omitzero"`
|
|
Ended time.Time `json:"ended,omitzero"`
|
|
At time.Time `json:"at,omitzero"`
|
|
}
|
|
|
|
// FailingTest is a failing test and its words, as a run's output names it (design 47 H2 §791).
|
|
type FailingTest struct {
|
|
Test string `json:"test"`
|
|
Words string `json:"words,omitempty"`
|
|
}
|
|
|
|
// OutputAnswer is what a build seat's verb output answers (design 47 H2 §791, H-A-367 (1), H-A-412): the ask, the run's
|
|
// test output's last 64 KB, its whole size and the failing tests with their words, or NoLongerKept once the output is past
|
|
// the outputs objects' max-age.
|
|
type OutputAnswer struct {
|
|
Ask string `json:"ask"`
|
|
Output string `json:"output,omitempty"`
|
|
Size int `json:"size,omitempty"`
|
|
Failing []FailingTest `json:"failing,omitempty"`
|
|
NoLongerKept bool `json:"no-longer-kept,omitempty"`
|
|
}
|
|
|
|
// NotRun is a test that did not run and why (design 47 H2 §800).
|
|
type NotRun struct {
|
|
Test string `json:"test"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
|
|
// InstallAsk is the job of an ask of the kind install the seat node-build-node accepts (design 47 H2 §800, H-A-403): the
|
|
// npm tree a module's package lock names, installed with no install scripts and published to the artifact store as the
|
|
// module's archive <module>:node-modules under the ask's version, which the builder's reservation covers.
|
|
type InstallAsk struct {
|
|
ID string `json:"id"`
|
|
Asker string `json:"asker"`
|
|
Pipeline string `json:"pipeline"`
|
|
Module string `json:"module"`
|
|
Commit string `json:"commit"`
|
|
// Fingerprint is the module's build-input fingerprint at the commit (H-A-403).
|
|
Fingerprint string `json:"fingerprint"`
|
|
// Lock is the package lock's text at the commit.
|
|
Lock string `json:"lock"`
|
|
}
|
|
|
|
// InstallArtifact is the artifact the install archive of module is published as (H-A-403): <module>:node-modules.
|
|
func InstallArtifact(module string) string { return module + ":node-modules" }
|
|
|
|
func (i InstallAsk) check(a StepAsk) error {
|
|
switch {
|
|
case i.ID != a.ID || i.Asker != a.Asker:
|
|
return fmt.Errorf("buildstep: the install ask %s names the id %q and the asker %q, not its ask's", a.ID, i.ID, i.Asker)
|
|
case i.Pipeline == "":
|
|
return fmt.Errorf("buildstep: the install ask %s names no pipeline", a.ID)
|
|
case !askCommitOK.MatchString(i.Commit):
|
|
return fmt.Errorf("buildstep: the install ask %s names no full commit (%q)", a.ID, i.Commit)
|
|
case i.Fingerprint == "":
|
|
return fmt.Errorf("buildstep: the install ask %s names no build-input fingerprint", a.ID)
|
|
case strings.TrimSpace(i.Lock) == "":
|
|
return fmt.Errorf("buildstep: the install ask %s holds no package lock", a.ID)
|
|
case a.Version == "":
|
|
return fmt.Errorf("buildstep: the install ask %s names no version, so its archive could be published under no reservation (H-A-373, H-A-403)", a.ID)
|
|
}
|
|
if err := meshkeys.CheckName("module", i.Module); err != nil {
|
|
return fmt.Errorf("the install ask %s: %w", a.ID, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EndOf is the end of the step a holds, on node, with outcome: the ask's names copied, so the runner can match the ask,
|
|
// the commit and the fingerprint (ADR 0666 §21).
|
|
func EndOf(a StepAsk, node, outcome string) (StepEnded, error) {
|
|
if i := a.Install; i != nil {
|
|
// An install's end names its pipeline, the step install, its module, commit and fingerprint (H-A-379, H-A-403).
|
|
e := StepEnded{V: StepEndedVersion, ID: a.ID, Asker: a.Asker, Ask: a.ID, Pipeline: i.Pipeline, Step: StepInstall, Module: i.Module,
|
|
Commit: i.Commit, Fingerprint: i.Fingerprint, Node: node, Outcome: outcome}
|
|
return e, e.check()
|
|
}
|
|
if a.Step == nil {
|
|
return StepEnded{}, fmt.Errorf("buildstep: the ask %s is no pipeline step", a.ID)
|
|
}
|
|
s := a.Step
|
|
e := StepEnded{V: StepEndedVersion, ID: a.ID, Asker: a.Asker, Ask: a.ID, Pipeline: s.Pipeline, Step: s.Step, Module: s.Module, Artifact: s.Artifact,
|
|
Commit: s.Commit, Fingerprint: s.Fingerprint, Node: node, Outcome: outcome, Rerun: s.Rerun, By: s.By}
|
|
return e, e.check()
|
|
}
|
|
|
|
func (e StepEnded) check() error {
|
|
if err := CheckAskID(e.ID); err != nil {
|
|
return err
|
|
}
|
|
if err := checkNode(e.Node); err != nil {
|
|
return err
|
|
}
|
|
if e.Output != "" && e.AnswerObject != "" {
|
|
return fmt.Errorf("buildstep: the end of %s carries its answer inline and as an object", e.ID)
|
|
}
|
|
if e.Pipeline == "" {
|
|
// G §548, §602: the end of an ask outside a pipeline names its asker, the ask and when, and no commit or
|
|
// fingerprint.
|
|
if !slices.Contains(StepOutcomes, e.Outcome) {
|
|
return fmt.Errorf("buildstep: the end of %s says %q, not one of %s", e.ID, e.Outcome, strings.Join(StepOutcomes, ", "))
|
|
}
|
|
if e.Asker == "" || e.Ask == "" || e.At.IsZero() {
|
|
return fmt.Errorf("buildstep: the end of %s, an ask outside a pipeline, names no asker, ask or time", e.ID)
|
|
}
|
|
if e.Commit != "" || e.Fingerprint != "" {
|
|
return fmt.Errorf("buildstep: the end of %s names a commit or a fingerprint and no pipeline", e.ID)
|
|
}
|
|
return nil
|
|
}
|
|
if !slices.Contains(PipelineSteps, e.Step) {
|
|
return fmt.Errorf("buildstep: the end of %s names the step %q", e.ID, e.Step)
|
|
}
|
|
if !slices.Contains(StepOutcomes, e.Outcome) {
|
|
return fmt.Errorf("buildstep: the end of %s says %q, not one of %s", e.ID, e.Outcome, strings.Join(StepOutcomes, ", "))
|
|
}
|
|
if e.Step == StepInstall {
|
|
// An install's ask names its pipeline, module and commit and no artifact or fingerprint (design 47 H2 §800): its
|
|
// end copies those and names its archive.
|
|
if e.Module == "" || !askCommitOK.MatchString(e.Commit) {
|
|
return fmt.Errorf("buildstep: the install's end %s names no module or full commit", e.ID)
|
|
}
|
|
return nil
|
|
}
|
|
if e.Pipeline == "" || e.Module == "" || e.Artifact == "" || !askCommitOK.MatchString(e.Commit) || e.Fingerprint == "" {
|
|
return fmt.Errorf("buildstep: the end of %s names no pipeline, module, artifact, full commit or fingerprint", e.ID)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EncodeStepEnded is a step's end as its event's body; one that could not run or failed says why.
|
|
func EncodeStepEnded(e StepEnded) ([]byte, error) {
|
|
e.V = StepEndedVersion
|
|
if err := e.check(); err != nil {
|
|
return nil, err
|
|
}
|
|
if e.Outcome == OutcomeDeclined && (e.Pipeline == "" || len(e.Lacks) == 0) {
|
|
return nil, fmt.Errorf("buildstep: the end of %s says declined and is no pipeline step's or names nothing its node lacks", e.ID)
|
|
}
|
|
if (e.Outcome == OutcomeCouldNotRun || e.Outcome == OutcomeFailed || e.Outcome == OutcomeDeclined) && e.Said == "" {
|
|
return nil, fmt.Errorf("buildstep: the end of %s says %s and not why", e.ID, e.Outcome)
|
|
}
|
|
return json.Marshal(e)
|
|
}
|
|
|
|
// DecodeStepEnded reads a step's end heard on subject: the node it names must be the subject's, the one node whose
|
|
// holder's account may publish there (ADR 0363 §2, ADR 0666 §21).
|
|
func DecodeStepEnded(subject string, data []byte) (StepEnded, error) {
|
|
_, node, err := ParseStepEndedSubject(subject)
|
|
if err != nil {
|
|
return StepEnded{}, err
|
|
}
|
|
var e StepEnded
|
|
if err := json.Unmarshal(data, &e); err != nil {
|
|
return StepEnded{}, fmt.Errorf("buildstep: a step's end does not fit its shape: %w", err)
|
|
}
|
|
if e.V != StepEndedVersion {
|
|
return StepEnded{}, fmt.Errorf("%w: a step's end of version %d, this reader reads %d", ErrUnknownVersion, e.V, StepEndedVersion)
|
|
}
|
|
if err := e.check(); err != nil {
|
|
return StepEnded{}, err
|
|
}
|
|
if e.Node != node {
|
|
return StepEnded{}, fmt.Errorf("buildstep: the end of %s names the node %s and was said on %s's subject", e.ID, e.Node, node)
|
|
}
|
|
return e, nil
|
|
}
|
|
|
|
// StepEndedSubject is where the holder of a language's build seat on node says a step's end.
|
|
func StepEndedSubject(language, node string) (string, error) {
|
|
seat, err := BuildSeat(language)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if err := checkNode(node); err != nil {
|
|
return "", err
|
|
}
|
|
return "mesh.seat." + seat + ".event.step-ended." + node, nil
|
|
}
|
|
|
|
// ImportsReportedSubject is where the holder of a language's build seat on node says a build's import set, the event
|
|
// imports-reported of the part build-step (design 47 H2 H-A-517, H-A-532 DQ-2).
|
|
func ImportsReportedSubject(language, node string) (string, error) {
|
|
seat, err := BuildSeat(language)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if err := checkNode(node); err != nil {
|
|
return "", err
|
|
}
|
|
return "mesh.seat." + seat + ".event.imports-reported." + node, nil
|
|
}
|
|
|
|
// ParseStepEndedSubject is the inverse of StepEndedSubject.
|
|
func ParseStepEndedSubject(subject string) (language, node string, err error) {
|
|
rest, ok := strings.CutPrefix(subject, "mesh.seat.node-build-")
|
|
if ok {
|
|
language, node, ok = strings.Cut(rest, ".event.step-ended.")
|
|
}
|
|
if !ok {
|
|
return "", "", fmt.Errorf("buildstep: %q is not a build seat's step-ended subject", subject)
|
|
}
|
|
if want, err := StepEndedSubject(language, node); err != nil || want != subject {
|
|
return "", "", fmt.Errorf("buildstep: %q is not a build seat's step-ended subject: %v", subject, err)
|
|
}
|
|
return language, node, nil
|
|
}
|