The indexer hears the runner's run-started and pipeline-ended on their own streams and writes candidates.run-<id>: one pull per member, its repository and directory from its registration, its manifest and fingerprint from a take-in of its commit read through the seat git, over its branch's revision view when its registration watches the branch, else the trunk's. The state runs keeps each run's members and ended modules, so a restart between two ends still drops the candidate. The try candidates, the checker's checks consumer, checker_tries and the three try tools go; IsTryGroup takes run-<id> beside try-<digits>. Builds: design 47 G §64, §190, §321, §66, A-160, A-174 Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
302 lines
11 KiB
Go
302 lines
11 KiB
Go
package index
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"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-forge-bridge/forgemsg"
|
|
"git.novox.be/novox/mesh-sdk/go/keys"
|
|
"git.novox.be/novox/mesh-sdk/go/process"
|
|
)
|
|
|
|
// The indexer's two control records (novox/hq ADR 0565 §2, issue 1953 D2, D3): the catalogue's predefined list, read from the
|
|
// forge bridge's merged message, and each open pull request's config declarations, read from its pull message's manifests
|
|
// against the trunk's manifests the indexer holds. mesh-config reads both in place of the controller's mirror and its
|
|
// `pull-declares`; a key's write is its event, nothing polls.
|
|
|
|
// The two states' buckets.
|
|
var (
|
|
PredefinedBucket = Module + "_" + PredefinedState
|
|
ConfigCandidatesBucket = Module + "_" + ConfigCandidatesState
|
|
)
|
|
|
|
// writePredefined keeps the predefined list a merged message carries, unless the key names a later merge: a merge older
|
|
// than the one it names never replaces it (ADR 0348 §7). The same list at the same merge writes nothing (ADR 0289 §1).
|
|
func (ix *Indexer) writePredefined(ctx context.Context, f forgemsg.Forge) error {
|
|
if ix.Predefined == nil || len(f.Predefined) == 0 {
|
|
return nil
|
|
}
|
|
return ix.notYet(PredefinedBucket, write(ctx, ix.Predefined, PredefinedListKey, func(value []byte, found bool) ([]byte, bool, error) {
|
|
if found {
|
|
var cur struct { // the list is compared as it was kept, byte for byte
|
|
Commit string `json:"commit"`
|
|
List json.RawMessage `json:"list"`
|
|
}
|
|
if err := json.Unmarshal(value, &cur); err == nil {
|
|
if cur.Commit == f.Merge && string(cur.List) == string(f.Predefined) {
|
|
return nil, false, nil
|
|
}
|
|
older, err := ix.older(ctx, f.Repository, f.Merge, cur.Commit)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
if older {
|
|
ix.logf("%s: the predefined list of %.12s is older than the one kept (%.12s): left", f.Repository, f.Merge, cur.Commit)
|
|
return nil, false, nil
|
|
}
|
|
}
|
|
}
|
|
next, err := json.Marshal(meshcatalog.PredefinedByListValue{V: keys.Version, Commit: f.Merge, At: ptr(ix.now()), List: f.Predefined})
|
|
return next, false, err
|
|
}))
|
|
}
|
|
|
|
// notYet says a state not on the bus yet (it comes with this delivery, bound when it appears) and lets the message go:
|
|
// what it would have written is written at the next message that carries it.
|
|
func (ix *Indexer) notYet(bucket string, err error) error {
|
|
if errors.Is(err, process.ErrNotYet) {
|
|
ix.logf("the state %s is not on the bus yet: nothing is written to it for this message", bucket)
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (ix *Indexer) now() time.Time {
|
|
if ix.Now != nil {
|
|
return ix.Now()
|
|
}
|
|
return time.Now()
|
|
}
|
|
|
|
// PullName is a pull request as mesh-config names it, `owner/repository#number`.
|
|
func PullName(repository string, number int) string { return repository + "#" + strconv.Itoa(number) }
|
|
|
|
// DeclarationsOf is a manifest's config declarations by key, in the layout of the controller's declared-config mirror: the
|
|
// config form's `config`, else the settings form's `settings`, each default and enum encoded the one way.
|
|
func DeclarationsOf(raw string) (map[string]meshcatalog.ConfigCandidatesByPullValueDeclaresValueValue, error) {
|
|
var m struct {
|
|
Config map[string]meshcatalog.ConfigCandidatesByPullValueDeclaresValueValue `json:"config"`
|
|
Settings map[string]meshcatalog.ConfigCandidatesByPullValueDeclaresValueValue `json:"settings"`
|
|
}
|
|
if err := json.Unmarshal([]byte(raw), &m); err != nil {
|
|
return nil, err
|
|
}
|
|
declared := m.Config
|
|
if declared == nil {
|
|
declared = m.Settings
|
|
}
|
|
// A default and an enum decoded into the contract's `any` are written again in the one canonical way (keys sorted).
|
|
out := make(map[string]meshcatalog.ConfigCandidatesByPullValueDeclaresValueValue, len(declared))
|
|
for key, d := range declared {
|
|
out[key] = d
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// changedDeclarations are the declarations head adds or changes against trunk.
|
|
func changedDeclarations(head, trunk map[string]meshcatalog.ConfigCandidatesByPullValueDeclaresValueValue) map[string]meshcatalog.ConfigCandidatesByPullValueDeclaresValueValue {
|
|
out := map[string]meshcatalog.ConfigCandidatesByPullValueDeclaresValueValue{}
|
|
for key, d := range head {
|
|
if before, there := trunk[key]; !there || !reflect.DeepEqual(before, d) {
|
|
out[key] = d
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// trunkManifest is the manifest the indexer holds for a module on the trunk, "" when it holds none.
|
|
func (ix *Indexer) trunkManifest(ctx context.Context, held map[string]meshcatalog.ModulesByModuleValue, module string) (string, error) {
|
|
m, ok := held[module]
|
|
if !ok || m.Manifest == "" || ix.Manifests == nil {
|
|
return "", nil
|
|
}
|
|
raw, _, found, err := ix.Manifests.Get(ctx, m.Manifest)
|
|
if err != nil || !found {
|
|
return "", err
|
|
}
|
|
kept, err := meshcatalogclient.DecodeManifest(raw)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return kept.Manifest, nil
|
|
}
|
|
|
|
// candidateDeclares are, per module of a pull request's head, the declarations its manifest adds or changes against the
|
|
// trunk's (an empty set for a module that adds none): heads are the head's manifests by module.
|
|
func (ix *Indexer) candidateDeclares(ctx context.Context, held map[string]meshcatalog.ModulesByModuleValue, heads map[string]string) (map[string]map[string]meshcatalog.ConfigCandidatesByPullValueDeclaresValueValue, error) {
|
|
out := map[string]map[string]meshcatalog.ConfigCandidatesByPullValueDeclaresValueValue{}
|
|
for module, raw := range heads {
|
|
head, err := DeclarationsOf(raw)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: the manifest of %s at the head: %v", ErrUnreadable, module, err)
|
|
}
|
|
trunkRaw, err := ix.trunkManifest(ctx, held, module)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
trunk := map[string]meshcatalog.ConfigCandidatesByPullValueDeclaresValueValue{}
|
|
if trunkRaw != "" {
|
|
if trunk, err = DeclarationsOf(trunkRaw); err != nil {
|
|
return nil, fmt.Errorf("the trunk's manifest of %s: %w", module, err)
|
|
}
|
|
}
|
|
if changed := changedDeclarations(head, trunk); len(changed) > 0 {
|
|
out[module] = changed
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// writeConfigCandidate writes a pull request's key of config-candidates from its head's manifests by module: written when
|
|
// what it says differs (its time apart), never for the same head and declarations twice.
|
|
func (ix *Indexer) writeConfigCandidate(ctx context.Context, repository string, number int, head string, held map[string]meshcatalog.ModulesByModuleValue, heads map[string]string) error {
|
|
if ix.ConfigCandidates == nil {
|
|
return nil
|
|
}
|
|
pull := PullName(repository, number)
|
|
key, err := ConfigCandidateKey(pull)
|
|
if err != nil {
|
|
return fmt.Errorf("%w: %v", ErrUnreadable, err)
|
|
}
|
|
declares, err := ix.candidateDeclares(ctx, held, heads)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return ix.notYet(ConfigCandidatesBucket, write(ctx, ix.ConfigCandidates, key, func(value []byte, found bool) ([]byte, bool, error) {
|
|
if found {
|
|
var cur meshcatalog.ConfigCandidatesByPullValue
|
|
if json.Unmarshal(value, &cur) == nil && cur.Head == head && reflect.DeepEqual(cur.Declares, declares) {
|
|
return nil, false, nil
|
|
}
|
|
}
|
|
next, err := json.Marshal(meshcatalog.ConfigCandidatesByPullValue{V: keys.Version, Pull: pull, Head: head, Declares: declares, At: ptr(ix.now())})
|
|
return next, false, err
|
|
}))
|
|
}
|
|
|
|
// dropConfigCandidate deletes a pull request's key: it merged or closed.
|
|
func (ix *Indexer) dropConfigCandidate(ctx context.Context, repository string, number int) error {
|
|
if ix.ConfigCandidates == nil {
|
|
return nil
|
|
}
|
|
key, err := ConfigCandidateKey(PullName(repository, number))
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return ix.notYet(ConfigCandidatesBucket, write(ctx, ix.ConfigCandidates, key, func(_ []byte, found bool) ([]byte, bool, error) { return nil, found, nil }))
|
|
}
|
|
|
|
// configCandidateOfPull is a pull message's part in config-candidates: a head whose manifests the message carries is written,
|
|
// a closed one deleted. A head held again without its directories leaves the key as it is.
|
|
func (ix *Indexer) configCandidateOfPull(ctx context.Context, f forgemsg.Forge, held map[string]meshcatalog.ModulesByModuleValue) error {
|
|
if f.Action == forgemsg.PullClosed {
|
|
return ix.dropConfigCandidate(ctx, f.Repository, f.Number)
|
|
}
|
|
if !f.DirsKnown {
|
|
return nil
|
|
}
|
|
heads := map[string]string{}
|
|
for dir, raw := range f.Manifests {
|
|
heads[moduleOfDir(held, f, dir)] = raw
|
|
}
|
|
return ix.writeConfigCandidate(ctx, f.Repository, f.Number, f.Head, held, heads)
|
|
}
|
|
|
|
// configCandidatesAfterMerge writes again every open pull request's key that names a module the merge changed, since what
|
|
// it adds is judged against the trunk (ADR 0565 §2), from the head's manifests its candidate keeps; and deletes the merged
|
|
// pull request's.
|
|
func (ix *Indexer) configCandidatesAfterMerge(ctx context.Context, f forgemsg.Forge, changed []string) error {
|
|
if ix.ConfigCandidates == nil {
|
|
return nil
|
|
}
|
|
if err := ix.dropConfigCandidate(ctx, f.Repository, f.Number); err != nil {
|
|
return err
|
|
}
|
|
if len(changed) == 0 || ix.Store == nil || ix.Manifests == nil {
|
|
return nil
|
|
}
|
|
held, err := ix.heldModules(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := ix.ConfigCandidates.Keys(ctx); errors.Is(err, process.ErrNotYet) {
|
|
return nil
|
|
}
|
|
groups, err := ix.Store.Keys(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var errs []error
|
|
for _, g := range groups {
|
|
if meshcatalogclient.IsTryGroup(g) {
|
|
continue // a run's candidate holds no pull request's config declarations (runcandidates.go)
|
|
}
|
|
raw, _, found, err := ix.Store.Get(ctx, g)
|
|
if err != nil || !found {
|
|
continue
|
|
}
|
|
c, err := DecodeCandidate(raw)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, p := range c.Pulls {
|
|
if !touchesAny(p, changed) {
|
|
continue
|
|
}
|
|
heads := map[string]string{}
|
|
for module, fp := range p.Fps {
|
|
mraw, _, found, err := ix.Manifests.Get(ctx, fp.Manifest)
|
|
if err != nil || !found {
|
|
continue
|
|
}
|
|
if kept, err := meshcatalogclient.DecodeManifest(mraw); err == nil {
|
|
heads[module] = kept.Manifest
|
|
}
|
|
}
|
|
if err := ix.writeConfigCandidate(ctx, p.Repository, int(p.Number), p.Head, held, heads); err != nil {
|
|
errs = append(errs, err)
|
|
}
|
|
}
|
|
}
|
|
return errors.Join(errs...)
|
|
}
|
|
|
|
func touchesAny(p meshcatalog.CandidatesByGroupValuePullsItem, modules []string) bool {
|
|
for _, m := range modules {
|
|
if _, ok := p.Fps[m]; ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// writtenModules are the modules a merged message wrote, from its written keys (`modules.<module>`).
|
|
func writtenModules(written []string) []string {
|
|
var out []string
|
|
for _, k := range written {
|
|
out = append(out, strings.TrimPrefix(k, "modules."))
|
|
}
|
|
return out
|
|
}
|
|
|
|
func ptr[T any](v T) *T { return &v }
|
|
|
|
// ConfigCandidateKey is a pull request's key in config-candidates, `owner/repository#number` with `/` and `#` as dots, the form
|
|
// mesh-config keeps a pull request's values under.
|
|
func ConfigCandidateKey(pull string) (string, error) {
|
|
owner, rest, ok := strings.Cut(strings.TrimSpace(pull), "/")
|
|
repo, number, ok2 := strings.Cut(rest, "#")
|
|
if !ok || !ok2 || owner == "" || repo == "" || number == "" || strings.ContainsAny(owner+repo+number, ".*> ") {
|
|
return "", fmt.Errorf("index: %q is not a pull request, owner/repository#number", pull)
|
|
}
|
|
return meshcatalog.ConfigCandidatesByPullKey(owner + "." + repo + "." + number), nil
|
|
}
|