402 lines
17 KiB
Go
402 lines
17 KiB
Go
package check
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"mesh-delivery-checker/rules"
|
|
"slices"
|
|
"strings"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/buildstep"
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/composer"
|
|
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
|
|
|
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/packagename"
|
|
)
|
|
|
|
// A change's manifests are judged by the indexer's judge (novox/hq ADR 0578 §2): the checker asks the Go build seat to
|
|
// build the judge's command (mesh-delivery's modules/mesh-delivery-indexer, cmd/mesh-judge) at mesh-delivery's trunk, or
|
|
// at the group's mesh-delivery member head when that member changes the judge, and to run it in its sandbox over the
|
|
// catalogue as the change leaves it, exactly as the composer is run (buildstep.ComposeJob, ADR 0482 §4). A manifest the
|
|
// change touches that the judge refuses fails the check, naming the manifest and each problem. The checker links no
|
|
// judge of its own.
|
|
|
|
// The judge the checker runs.
|
|
const (
|
|
DeliveryRepository = "novox/mesh-delivery"
|
|
JudgeDir = "modules/mesh-delivery-indexer"
|
|
JudgePackage = "mesh-delivery-indexer/cmd/mesh-judge"
|
|
// JudgeCase is the case of a judge request holding the catalogue as the change leaves it, and JudgeTrunk the one
|
|
// holding it as the trunk holds it: a problem the change case has and the trunk case lacks is the change's (ADR 0579 §2).
|
|
JudgeCase = "change"
|
|
JudgeTrunk = "trunk"
|
|
// judgeBetween is how the judge names the problems between manifests that no one module's removal ends.
|
|
judgeBetween = rules.BetweenManifests
|
|
)
|
|
|
|
// judgePaths are the paths of mesh-delivery whose change changes the judge: a group whose mesh-delivery member changes
|
|
// one is judged by that member's head.
|
|
var judgePaths = []string{JudgeDir + "/judge/", JudgeDir + "/cmd/mesh-judge/", JudgeDir + "/go.mod", JudgeDir + "/go.sum"}
|
|
|
|
// changesJudge says whether a merge of mesh-delivery with these changed paths changes the judge.
|
|
func changesJudge(repository string, merged []Fetched) bool {
|
|
if repository != DeliveryRepository {
|
|
return false
|
|
}
|
|
for _, f := range merged {
|
|
for _, p := range f.Changed {
|
|
for _, j := range judgePaths {
|
|
if p == j || strings.HasPrefix(p, j) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// TrunkManifests says the manifest digest of every module the indexer holds (its modules state), by module.
|
|
type TrunkManifests interface {
|
|
ManifestDigests() map[string]string
|
|
// ManifestFingerprints is the fingerprint of the entry of every such module in the trunk revision, by module, whose
|
|
// catalog graph entry holds that manifest (design 47 G A-310).
|
|
ManifestFingerprints() map[string]string
|
|
}
|
|
|
|
// SeatList gives the seat definitions the manifests are judged against, whole, as raw definitions (a request's Seats): the
|
|
// list the seat list reader holds. It is an error when the list has not been read from the seat yet, since a manifest judged
|
|
// against the bootstrapped seats alone would be refused for a seat it names that the mesh defines (novox/hq ADR 0636 §1).
|
|
type SeatList interface {
|
|
Definitions() ([]json.RawMessage, error)
|
|
}
|
|
|
|
// PredefinedList is the catalogue's list of predefined keys the indexer keeps (its state predefined, key list), the one a
|
|
// config value falling back to a key is judged against (ADR 0348 §7); nil when it keeps none.
|
|
type PredefinedList interface {
|
|
PredefinedList(ctx context.Context) (json.RawMessage, error)
|
|
}
|
|
|
|
// judgeRequest is the judge's request over the catalogue as the case leaves it: every trunk manifest by its digest, each
|
|
// one a pull request touches replaced by the digest its candidate names; touched are the modules whose manifest the
|
|
// change adds or changes (a digest the trunk does not hold for it), sorted (novox/hq ADR 0579 §2).
|
|
func judgeRequest(trunk, trunkFps map[string]string, pulls []Pull) (req composer.Request, touched []string) {
|
|
manifests, before := map[string]composer.Manifest{}, map[string]composer.Manifest{}
|
|
for module, digest := range trunk {
|
|
if digest != "" {
|
|
manifests[module] = composer.Manifest{Digest: digest, Fingerprint: trunkFps[module]}
|
|
before[module] = composer.Manifest{Digest: digest, Fingerprint: trunkFps[module]}
|
|
}
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, p := range pulls {
|
|
for module, cm := range p.Fps {
|
|
if cm.Manifest == "" {
|
|
continue
|
|
}
|
|
manifests[module] = composer.Manifest{Digest: cm.Manifest, Fingerprint: cm.Fp2}
|
|
if trunk[module] != cm.Manifest {
|
|
seen[module] = true
|
|
}
|
|
}
|
|
}
|
|
return composer.Request{V: composer.Version, Cases: []composer.Case{{Name: JudgeCase, Manifests: manifests},
|
|
{Name: JudgeTrunk, Manifests: before}}}, slices.Sorted(maps.Keys(seen))
|
|
}
|
|
|
|
// judgeManifests runs the indexer's judge over the case's manifests and records its answers as a fact the rules judge
|
|
// (rules.JudgeManifests), with the trunk validator's answer on a new indexer (ADR 0633 clause 6). A case that touches no
|
|
// manifest, or an examiner that reads no trunk manifests, judges nothing; a judge that cannot be asked or does not answer
|
|
// is a failure, said, never a pass.
|
|
func (st *state) judgeManifests(ctx context.Context) {
|
|
if st.j.Manifests == nil || st.narrowTry() {
|
|
return
|
|
}
|
|
req, touched := judgeRequest(st.j.Manifests.ManifestDigests(), st.j.Manifests.ManifestFingerprints(), st.c.Pulls)
|
|
if len(touched) == 0 {
|
|
return
|
|
}
|
|
commit, at := st.judgeHead, "the group's mesh-delivery head "+st.judgeHead
|
|
if commit == "" {
|
|
if st.j.Trunks != nil {
|
|
commit = st.j.Trunks.Heads()[DeliveryRepository]
|
|
}
|
|
at = "mesh-delivery's trunk " + commit
|
|
}
|
|
if commit == "" {
|
|
st.fail(st.goModule(), "the change's manifests cannot be judged: the trunk head of "+DeliveryRepository+" is not known")
|
|
return
|
|
}
|
|
// The list of predefined keys the manifests are judged against (ADR 0578 §2): the catalogue member's predefined.json
|
|
// when the change alters it, else the one the indexer keeps.
|
|
if list, from := st.predefinedList(ctx); list != nil {
|
|
req.Predefined = list
|
|
st.c.note("the manifests are judged against the predefined keys of " + from)
|
|
}
|
|
req.Packages = st.knownPackages(ctx)
|
|
// The heads' seat list, for the change case and the trunk case both (ADR 0636 §4).
|
|
req.Seats = st.c.Seats
|
|
cv, trunkCase, ok := st.runJudge(ctx, req, touched, commit, at, st.packagesOf(DeliveryRepository))
|
|
if !ok {
|
|
return
|
|
}
|
|
st.judgedVerdicts(cv, at)
|
|
m := rules.Manifests{Touched: touched, At: at, Change: cv, Trunk: trunkCase}
|
|
if !st.indexerOf(ctx, req, touched, cv, at, &m) {
|
|
return
|
|
}
|
|
st.facts = append(st.facts, rules.Step{Module: st.goModule(), Manifests: &m})
|
|
}
|
|
|
|
// runJudge asks the Go build seat to build the judge at the commit and run it over the request, and reads its change and
|
|
// trunk cases; false, the failure recorded, when it cannot be asked or does not answer.
|
|
func (st *state) runJudge(ctx context.Context, req composer.Request, touched []string, commit, at string, versions buildstep.PackageVersions) (change, trunk composer.CaseVerdict, ok bool) {
|
|
raw, err := json.Marshal(req)
|
|
if err != nil {
|
|
st.fail(st.goModule(), err.Error())
|
|
return change, trunk, false
|
|
}
|
|
job := buildstep.ComposeJob{Fetch: buildstep.Fetch{Repository: DeliveryRepository, Commit: commit, Dir: JudgeDir, Packages: versions}, Dir: JudgeDir,
|
|
Package: JudgePackage, Request: raw, Reader: "the indexer's judge at " + at}
|
|
a := buildstep.StepAsk{ID: buildstep.NewID(), Kind: buildstep.AskCheck, Language: buildstep.AskGo, Asker: st.j.Module,
|
|
At: st.j.now().UTC(), Compose: &job}
|
|
st.c.Progress.Set(fmt.Sprintf("the judge ask %s of the manifests of %s", a.ID, strings.Join(touched, ", ")), "")
|
|
st.c.Asked(a.ID)
|
|
if err := st.j.Asks.Put(ctx, a); err != nil {
|
|
st.fail(st.goModule(), "the judge of the change's manifests could not be asked: "+err.Error())
|
|
return change, trunk, false
|
|
}
|
|
step, err := st.j.Asks.Wait(ctx, a)
|
|
if err != nil {
|
|
st.fail(st.goModule(), "the judge of the change's manifests: "+err.Error())
|
|
return change, trunk, false
|
|
}
|
|
change, trunk, why := judgeAnswer(step)
|
|
if why != "" {
|
|
st.fail(st.goModule(), fmt.Sprintf("the indexer's judge at %s did not judge the change's manifests: %s", at, why))
|
|
return change, trunk, false
|
|
}
|
|
return change, trunk, true
|
|
}
|
|
|
|
// CatalogSeat is the seat the indexer holds (rules.CatalogSeat).
|
|
const CatalogSeat = rules.CatalogSeat
|
|
|
|
// indexerOf finds the touched module whose manifest claims the seat mesh-catalog, the indexer, and puts in m the trunk's
|
|
// manifest validator's change case on it, the one the running parts hold (ADR 0633 clause 6): when the change judges
|
|
// with its own head's validator, or at other package versions than the latest published, the trunk's is asked as well.
|
|
// False, the failure recorded, when a manifest cannot be read or the trunk's validator cannot be asked.
|
|
func (st *state) indexerOf(ctx context.Context, req composer.Request, touched []string, cv composer.CaseVerdict, judgedAt string, m *rules.Manifests) bool {
|
|
if st.j.Read == nil {
|
|
return true
|
|
}
|
|
var indexer string
|
|
for _, module := range touched {
|
|
man, err := st.j.Read.Manifest(ctx, req.Cases[0].Manifests[module].Digest)
|
|
if err != nil {
|
|
st.fail(st.goModule(), fmt.Sprintf("the manifest of %s cannot be read, so the indexer cannot be ruled out: %v", module, err))
|
|
return false
|
|
}
|
|
for _, c := range man.Claims {
|
|
if c.Name == CatalogSeat {
|
|
indexer = module
|
|
}
|
|
}
|
|
}
|
|
if indexer == "" {
|
|
return true
|
|
}
|
|
// The validator the running parts hold is the trunk's, built at the latest published versions alone, never at the group's
|
|
// heads (ADR 0633 clause 6): a group's mesh-sdk or mesh-contracts head must not change the validator that judges the new
|
|
// indexer. The head's run is reused when it was that validator already.
|
|
if trunkVersions := st.trunkJudgeVersions(); st.judgeHead != "" || !maps.Equal(st.packagesOf(DeliveryRepository), trunkVersions) {
|
|
trunk := ""
|
|
if st.j.Trunks != nil {
|
|
trunk = st.j.Trunks.Heads()[DeliveryRepository]
|
|
}
|
|
if trunk == "" {
|
|
st.fail(st.goModule(), "the new indexer cannot be judged by the trunk's manifest validator: the trunk head of "+DeliveryRepository+" is not known")
|
|
return false
|
|
}
|
|
judgedAt = "mesh-delivery's trunk " + trunk
|
|
var ok bool
|
|
if cv, _, ok = st.runJudge(ctx, req, touched, trunk, judgedAt, trunkVersions); !ok {
|
|
return false
|
|
}
|
|
}
|
|
m.Indexer, m.IndexerAt, m.OfIndexer = indexer, judgedAt, &cv
|
|
return true
|
|
}
|
|
|
|
// judgeAnswer reads the judge's change and trunk cases from its step, or why it cannot.
|
|
func judgeAnswer(step buildstep.Step) (change, trunk composer.CaseVerdict, why string) {
|
|
if step.State != buildstep.StepPassed {
|
|
return change, trunk, fmt.Sprintf("its step %s: %s", step.State, step.Reason)
|
|
}
|
|
var a buildstep.ComposeAnswer
|
|
if err := json.Unmarshal(step.Result, &a); err != nil {
|
|
return change, trunk, "its step's answer cannot be read: " + err.Error()
|
|
}
|
|
var v composer.Verdict
|
|
if err := json.Unmarshal(a.Verdict, &v); err != nil || v.V != composer.Version {
|
|
return change, trunk, fmt.Sprintf("its verdict cannot be read (version %d): %v", v.V, err)
|
|
}
|
|
found := 0
|
|
for _, c := range v.Cases {
|
|
switch c.Name {
|
|
case JudgeCase:
|
|
change, found = c, found+1
|
|
case JudgeTrunk:
|
|
trunk, found = c, found+1
|
|
}
|
|
}
|
|
if found != 2 {
|
|
return change, trunk, "its verdict does not hold both the cases " + JudgeCase + " and " + JudgeTrunk
|
|
}
|
|
return change, trunk, ""
|
|
}
|
|
|
|
// predefinedList is the list of predefined keys a case's manifests are judged against, and where it comes from: the
|
|
// catalogue member's predefined.json at its head when its merge changes it, else the one the indexer keeps; nil when
|
|
// neither can be read, said, and the judge then takes each fallback as its value declares it.
|
|
func (st *state) predefinedList(ctx context.Context) (catalogue.PredefinedList, string) {
|
|
read := func(raw []byte, from string) (catalogue.PredefinedList, string) {
|
|
list, err := catalogue.ParsePredefined(raw)
|
|
if err != nil {
|
|
st.c.note("the predefined keys of " + from + " cannot be read, so fallbacks are judged as their values declare them: " + err.Error())
|
|
return nil, ""
|
|
}
|
|
return list, from
|
|
}
|
|
if st.predefinedHead != "" && st.j.Forge != nil {
|
|
text, err := st.j.Forge.File(ctx, catalogueRepository, catalogue.PredefinedFile, st.predefinedHead)
|
|
if err == nil {
|
|
return read([]byte(text), "the catalogue member's head "+st.predefinedHead)
|
|
}
|
|
st.c.note("the catalogue member's predefined.json cannot be read: " + err.Error())
|
|
}
|
|
if st.j.Predefined == nil {
|
|
return nil, ""
|
|
}
|
|
raw, err := st.j.Predefined.PredefinedList(ctx)
|
|
if err != nil || raw == nil {
|
|
if err != nil {
|
|
st.c.note("the indexer's predefined keys cannot be read: " + err.Error())
|
|
}
|
|
return nil, ""
|
|
}
|
|
return read(raw, "the indexer's kept list")
|
|
}
|
|
|
|
// judgedByMember notes the group's mesh-delivery member head as the judge's when its merge changes the judge, and the
|
|
// catalogue member's head as where the predefined keys are read when its merge changes predefined.json.
|
|
func (st *state) judgedByMember(p Pull, merged []Fetched) {
|
|
if changesJudge(p.Repository, merged) {
|
|
st.judgeHead = p.Head
|
|
}
|
|
if p.Repository == catalogueRepository {
|
|
for _, f := range merged {
|
|
if slices.Contains(f.Changed, catalogue.PredefinedFile) {
|
|
st.predefinedHead = p.Head
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// knownPackages are the packages a manifest's `packages` may name, sorted (DLV-I14, design 32): every name the indexer's
|
|
// packages state holds, and, of each package a member of the case declares that the state does not know by its
|
|
// `<module>/<artifact>`, the name read at that member's head through the forge (packagename.Of, the indexer's own reader
|
|
// at a merge). A name that cannot be read is said and left out, so a manifest naming it is refused naming it. Nil, which
|
|
// judges no name, when the checker follows no packages state or has not read it yet (PackageNames nil); an answered
|
|
// state that holds no name is an empty list, never nil, so every name is judged against it.
|
|
func (st *state) knownPackages(ctx context.Context) []string {
|
|
if st.j.KnownPackages == nil {
|
|
return nil
|
|
}
|
|
names := st.j.KnownPackages.PackageNames()
|
|
if names == nil {
|
|
return nil
|
|
}
|
|
built := map[string]bool{}
|
|
for _, b := range names {
|
|
built[b] = true
|
|
}
|
|
known := make([]string, 0, len(names))
|
|
known = append(known, slices.Collect(maps.Keys(names))...)
|
|
declared, err := st.declaredPackages(ctx)
|
|
if err != nil {
|
|
st.c.note("the packages the group's members declare are not known, so only the packages state's names are known to the judge: " + err.Error())
|
|
}
|
|
for _, d := range declared {
|
|
if built[d.module+"/"+d.name] {
|
|
continue
|
|
}
|
|
if st.j.Forge == nil {
|
|
st.c.note(fmt.Sprintf("the name of the package %s/%s new in the group is not read: this checker reads no forge", d.module, d.name))
|
|
continue
|
|
}
|
|
read := func(ctx context.Context, repository, commit, file string) ([]byte, error) {
|
|
text, err := st.j.Forge.File(ctx, repository, file, commit)
|
|
if errors.Is(err, ErrNotFound) {
|
|
return nil, nil
|
|
}
|
|
return []byte(text), err
|
|
}
|
|
dirs := []string{d.dir}
|
|
if d.dir == "" {
|
|
dirs = []string{"modules/" + d.module, meshcatalogclient.RootPath}
|
|
}
|
|
var why []string
|
|
for _, dir := range dirs {
|
|
a := catalogue.Artifact{Name: d.name, Kind: catalogue.ArtifactPackage, Language: d.language, Paths: d.paths}
|
|
name, err := packagename.Of(ctx, read, d.pull.Repository, d.pull.Head, dir, a)
|
|
if err == nil {
|
|
known, why = append(known, name), nil
|
|
break
|
|
}
|
|
why = append(why, err.Error())
|
|
}
|
|
if len(why) > 0 {
|
|
st.c.note(fmt.Sprintf("the name of the package %s/%s new in the group is not read at the head of %s: %s", d.module, d.name, pullKey(d.pull), strings.Join(why, "; ")))
|
|
}
|
|
}
|
|
slices.Sort(known)
|
|
return slices.Compact(known)
|
|
}
|
|
|
|
// trunkJudgeVersions are the versions the trunk's manifest validator is built at: the latest published version of each
|
|
// package, with no group member's head (ADR 0633 clause 6).
|
|
func (st *state) trunkJudgeVersions() buildstep.PackageVersions {
|
|
return packagesFor(st.j.Published, &Case{}, DeliveryRepository)
|
|
}
|
|
|
|
// judgedVerdicts puts the check's own judge's verdict on each manifest the change brings on the case, as the seat
|
|
// mesh-catalog's verdict value, for the composer's request (ADR 0578 §2, §3; F5 of milestone 47/G): accepted, or refused
|
|
// with its words; a manifest the judge gave no verdict on carries none, which the composer reads as accepted.
|
|
func (st *state) judgedVerdicts(cv composer.CaseVerdict, at string) {
|
|
for _, p := range st.c.Pulls {
|
|
for name, fp := range p.Fps {
|
|
read, ok := cv.Manifests[name]
|
|
if fp.Manifest == "" || !ok || (!read.Accepted && read.Refused == "") {
|
|
continue
|
|
}
|
|
v := meshcatalog.VerdictsByDigestValue{Module: name, State: meshcatalogclient.VerdictAccepted, Judge: at}
|
|
if !read.Accepted {
|
|
v.State, v.Problems = meshcatalogclient.VerdictRefused, []string{read.Refused}
|
|
}
|
|
raw, err := json.Marshal(v)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if st.c.Judged == nil {
|
|
st.c.Judged = map[string]json.RawMessage{}
|
|
}
|
|
st.c.Judged[name] = raw
|
|
}
|
|
}
|
|
}
|