package main import ( "context" "fmt" "sort" "strings" "github.com/novox/mesh-controller/internal/inventory" "github.com/novox/mesh-controller/internal/link" ) // **A module moves only when its program changes** (novox/hq issue 721). A merge check builds every module the // delivery plan moves and compares each with the build the mesh holds of it; a walk is cut without a module whose // check found its whole image the same as that build, while the mesh still holds it, and says so among the modules // it left unchanged. A merge of one sentence in a package a program imports but never reaches moves no build of it. // sameAsRunning are the modules of a walk its checks found the same as the build the mesh holds, each with what its // check said. **Only when every pull request the walk answers was checked at the head it merged, and every one of // those checks built the module and found it the same as the build the mesh still holds**: a merge the walk answers // without a pull request, a check of another head, a check that built it differently, or a held build that moved // since, leaves the module in the walk. func sameAsRunning(plan *inventory.Plan, builds []link.CheckedBuild, held map[string]string) map[string]string { if plan.Delivery == nil || len(plan.Delivery.Merges) == 0 { return nil } for _, m := range plan.Delivery.Merges { if _, ok := pullOf(m); !ok { return nil } } pulls := reuseAskOf(plan).Pulls heads := mergedHeadsOf(plan) out := map[string]string{} for name := range plan.Modules { said := "" for _, pull := range pulls { b, ok := checkedBuildOf(builds, pull, heads[pull], name) if !ok || !stillHeld(b.Built, name, held) { said = "" break } if said == "" { said = b.Built.Same } } if said != "" { out[name] = said } } if len(out) == 0 { return nil } return out } // checkedBuildOf is the newest build a check of the pull request at head made of the module; false when none did. func checkedBuildOf(builds []link.CheckedBuild, pull link.PullRef, head, module string) (link.CheckedBuild, bool) { if head == "" { return link.CheckedBuild{}, false } for _, b := range builds { if b.Pull == pull && b.Built.Module == module && link.SameHead(b.Head, head) { return b, true } } return link.CheckedBuild{}, false } // stillHeld is whether a check found every artifact of the module the same as a build the mesh still holds. func stillHeld(b link.CheckBuilt, module string, held map[string]string) bool { if b.Same == "" || b.NotBuilt != "" || len(b.Made) == 0 || len(b.SameAs) != len(b.Made) { return false } for _, m := range b.Made { was := pinnedDigest(b.SameAs[m.Name]) if was == "" || was != pinnedDigest(held[module+"/"+m.Name]) { return false } } return true } // pinnedDigest is a reference's pinned digest, empty when it is not pinned. func pinnedDigest(ref string) string { if i := strings.LastIndex(ref, "@"); i >= 0 { return ref[i+1:] } return "" } // leaveSameAsRunning takes from a walk being cut every module its checks found the same as the build the mesh holds // (sameAsRunning): what was taken, with why. **Anything not read leaves the walk as it was**, said. func leaveSameAsRunning(ctx context.Context, inv *inventory.Inventory, plan *inventory.Plan) map[string]string { if plan.Delivery == nil || len(plan.Modules) == 0 { return nil } pulls := reuseAskOf(plan).Pulls if len(pulls) == 0 { return nil } builds, err := checkedBuildsOf(ctx, pulls) if err != nil { fmt.Printf(" the checks' builds could not be read, so every module moves: %v\n", err) return nil } held, err := inv.Held(ctx) if err != nil { fmt.Printf(" what the mesh holds could not be read, so every module moves: %v\n", err) return nil } same := sameAsRunning(plan, builds, held) if len(same) == 0 { return nil } names := make([]string, 0, len(same)) for n := range same { names = append(names, n) } sort.Strings(names) withhold(plan, names, nil) for _, n := range names { fmt.Printf(" %s left out: %s\n", n, same[n]) } return same }