gate: the source of a watched branch follows every merge into it (A-464, A-464b)
The forge bridge's merged moves the repository's head and says source-moved; at its start each head is compared with the forge's branch head; gate_source_follow does it for one repository by hand. Builds: design 47 G-22 A-464, A-464b
This commit is contained in:
@@ -32,7 +32,7 @@ func (l *listening) get() string { l.mu.Lock(); defer l.mu.Unlock(); return l.n
|
||||
|
||||
// The events this owner follows: the forge bridge's word on an act owed, and the step events the delivery records are built
|
||||
// from (consumer.go).
|
||||
var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), pullUpdatedEvent, pullMergedEvent, pullClosedEvent, registrationChangedEvent)
|
||||
var followed = append(append([]string{owedEndedEvent}, stepPatterns()...), pullUpdatedEvent, pullMergedEvent, pullClosedEvent, registrationChangedEvent, forgeMergedEvent)
|
||||
|
||||
func main() {
|
||||
settingsFile := os.Getenv("MESH_DELIVERY_GATE_SETTINGS")
|
||||
@@ -118,7 +118,13 @@ func run(h *Holder, l *listening) {
|
||||
l.set("listening")
|
||||
logf("[mesh-delivery] listening: %s", strings.Join(followed, ", "))
|
||||
// A watched branch no source key holds starts from the forge, once at each start and at each registration change (A-401).
|
||||
go startSourcesAtStart(h)
|
||||
// then each head is compared with the forge's, and a merge made outside the gate is followed (A-464).
|
||||
go func() {
|
||||
startSourcesAtStart(h)
|
||||
if _, err := h.FollowAtStart(); err != nil {
|
||||
logf("[mesh-delivery] the sources were not all compared with the forge's heads: %v", err)
|
||||
}
|
||||
}()
|
||||
// A missed pull event is put right from the forge's own record, once at each start (§716).
|
||||
if err := h.ReadForge(); err != nil {
|
||||
logf("[mesh-delivery] the open pull requests were not read from the forge, they are read again at the next start: %v", err)
|
||||
@@ -179,6 +185,9 @@ func take(h *Holder, e stdio.Envelope) error {
|
||||
_, err := h.StartSources()
|
||||
return err
|
||||
}
|
||||
if forgeMergedEvent != "" && stdio.TopicMatches(forgeMergedEvent, e.Key) {
|
||||
return h.ForgeMerged(e.Body)
|
||||
}
|
||||
if e.Key == pullClosedEvent {
|
||||
return errors.Join(h.DeliveryClosed(e.Body), h.pullEnded(e.Body, "closed"))
|
||||
}
|
||||
|
||||
@@ -141,6 +141,9 @@ func TestItClaimsTheDeliverySeatAndFollowsWhatItHandles(t *testing.T) {
|
||||
t.Fatalf("consumes %v, follows %v", m.Consumes, followed)
|
||||
}
|
||||
for _, c := range m.Consumes {
|
||||
if c == forgeMergedEvent {
|
||||
continue // A-464: the source follows every merge, so the gate hears the forge bridge's merged message, and it alone
|
||||
}
|
||||
for _, gone := range []string{".forge.", "teller", "view", "forge-pull", "forge-status"} {
|
||||
if strings.Contains(c, gone) {
|
||||
t.Errorf("the manifest still consumes %s, which G's gate does not hear (§431, §592, §593)", c)
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
package main
|
||||
|
||||
// The gate's source of a watched branch follows every merge into that branch, whoever made it (design 47 G A-464, A-464b, amending
|
||||
// A-463): on the forge bridge's `merged` of a repository the source names, at the branch the source watches, the gate moves that
|
||||
// repository's head to the merge and says source-moved, as a take-in moves it; at its start it compares each head with the
|
||||
// forge's branch head, asked of the seat git, and moves every one behind it the same way, so a merge whose message was lost is
|
||||
// followed too; gate_source_follow does the same for one repository now. A head the source does not name, or a branch it does
|
||||
// not watch, is left: only a start or a registration change adds them (sourcestart.go).
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
seat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/trunk"
|
||||
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||
)
|
||||
|
||||
// forgeMergedEvent is the forge bridge's merged message of any repository, as the manifest consumes it.
|
||||
var forgeMergedEvent = declared("forge.*.*.merged")
|
||||
|
||||
// moveHead moves the head the branch's source names for repository to head, and says source-moved with the key's revision. It
|
||||
// answers false, and writes nothing, when the branch has no source key, the key does not name the repository or names the head
|
||||
// already. group is the group that made the merge, empty for a merge no group of the gate made.
|
||||
func (h *Holder) moveHead(branch, repository, head, group string) (moved bool, err error) {
|
||||
if h.Source == nil || head == "" {
|
||||
return false, nil
|
||||
}
|
||||
rev, err := h.Source.Set(branch, func(prev *Source) (Source, bool) {
|
||||
moved = false
|
||||
if prev == nil {
|
||||
return Source{}, false
|
||||
}
|
||||
name := ""
|
||||
for r := range prev.Heads {
|
||||
if trunk.SameRepository(r, repository) {
|
||||
name = r
|
||||
}
|
||||
}
|
||||
if name == "" || prev.Heads[name] == head {
|
||||
return Source{}, false
|
||||
}
|
||||
next := Source{Group: group, Heads: map[string]string{}}
|
||||
for r, c := range prev.Heads {
|
||||
next.Heads[r] = c
|
||||
}
|
||||
next.Heads[name] = head
|
||||
moved = true
|
||||
return next, true
|
||||
})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("source.%s cannot be moved: %w", branch, err)
|
||||
}
|
||||
if !moved || h.SaySeat == nil {
|
||||
return moved, nil
|
||||
}
|
||||
at := h.Now().UTC()
|
||||
return true, h.SaySeat(seat.SubjectSourceMoved(keys.EscapeName(branch)),
|
||||
seat.SourceMovedEvent{Branch: branch, Group: group, Revision: int64(rev), At: &at},
|
||||
"source-moved."+keys.EscapeName(branch)+"."+strconv.FormatUint(rev, 10))
|
||||
}
|
||||
|
||||
// ForgeMerged takes one merged message of the forge bridge: the head of its repository in the source of the branch it landed on
|
||||
// moves to the merge. A merge no delivery group made (the message names no group) is said so. A message that cannot be read, or
|
||||
// names no branch or merge, is said and taken: heard again it would be refused again.
|
||||
func (h *Holder) ForgeMerged(body json.RawMessage) error {
|
||||
f, err := forgemsg.DecodeForge(body)
|
||||
if err != nil {
|
||||
h.logf("[mesh-delivery] a merged message of the forge bridge is not readable, the source does not follow it: %v", err)
|
||||
return nil
|
||||
}
|
||||
if f.Kind != forgemsg.ForgeMerged {
|
||||
return nil
|
||||
}
|
||||
if f.Base == "" || f.Merge == "" {
|
||||
h.logf("[mesh-delivery] the merged message of %s names no branch or no merge, the source does not follow it", f.Repository)
|
||||
return nil
|
||||
}
|
||||
moved, err := h.moveHead(f.Base, f.Repository, f.Merge, f.Group)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if moved {
|
||||
if f.Group == "" { // A-464b: a merge whose message names no delivery group is one the gate did not make
|
||||
h.logf("[mesh-delivery] source.%s follows the merge %.12s of %s: not made by a delivery group of the gate", f.Base, f.Merge, f.Repository)
|
||||
} else {
|
||||
h.logf("[mesh-delivery] source.%s follows the merge %.12s of %s, made by the delivery group %s", f.Base, f.Merge, f.Repository, f.Group)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FollowAtStart compares each head of every source key with the forge's head of that branch and moves every one behind it, once
|
||||
// at each start. A head the forge cannot read is said and left. It answers the heads moved as "<repository>@<branch>".
|
||||
func (h *Holder) FollowAtStart() ([]string, error) {
|
||||
if h.Source == nil || h.Forge == nil {
|
||||
return nil, errors.New("this gate keeps no source or reads no forge")
|
||||
}
|
||||
branches, err := h.Source.Branches()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var moved []string
|
||||
var errs []error
|
||||
for _, branch := range branches {
|
||||
held, _, err := h.Source.Read(branch)
|
||||
if err != nil || held == nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
for repository, head := range held.Heads {
|
||||
ok, err := h.followForge(branch, repository, head)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
h.logf("[mesh-delivery] source.%s: %s is not compared with the forge's head: %v", branch, repository, err)
|
||||
continue
|
||||
}
|
||||
if ok {
|
||||
moved = append(moved, repository+"@"+branch)
|
||||
}
|
||||
}
|
||||
}
|
||||
h.logf("[mesh-delivery] the sources compared with the forge's heads at the start: moved %v", moved)
|
||||
return moved, errors.Join(errs...)
|
||||
}
|
||||
|
||||
// followForge moves one repository's head in a branch's source to the forge's head of the branch when they differ.
|
||||
func (h *Holder) followForge(branch, repository, held string) (bool, error) {
|
||||
a, err := h.Forge.Branch(repository, branch)
|
||||
if err == nil && a.Error != "" {
|
||||
err = errors.New(a.Error)
|
||||
}
|
||||
if err == nil && a.Head == "" {
|
||||
err = errors.New("the forge named no head")
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if sameCommit(held, a.Head) && len(held) == len(a.Head) {
|
||||
return false, nil
|
||||
}
|
||||
moved, err := h.moveHead(branch, repository, a.Head, "")
|
||||
if moved {
|
||||
h.logf("[mesh-delivery] source.%s follows the forge: %s from %.12s to %.12s", branch, repository, held, a.Head)
|
||||
}
|
||||
return moved, err
|
||||
}
|
||||
|
||||
// SourceFollow moves one repository's head to the forge's head of the branch by hand, after the hand-act log took it.
|
||||
func (h *Holder) SourceFollow(branch, repository, why, by string, acts handActs) (map[string]any, error) {
|
||||
if strings.TrimSpace(branch) == "" || strings.TrimSpace(repository) == "" || strings.TrimSpace(why) == "" {
|
||||
return nil, errors.New("say the branch, the repository and why its head follows the forge")
|
||||
}
|
||||
if h.Source == nil || h.Forge == nil {
|
||||
return nil, errors.New("this gate keeps no source or reads no forge")
|
||||
}
|
||||
if acts == nil {
|
||||
return nil, errors.New("the hand-act log cannot be reached here, so nothing was moved")
|
||||
}
|
||||
held, _, err := h.Source.Read(branch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if held == nil {
|
||||
return nil, fmt.Errorf("the gate keeps no source of the branch %s", branch)
|
||||
}
|
||||
name := ""
|
||||
for r := range held.Heads {
|
||||
if trunk.SameRepository(r, repository) {
|
||||
name = r
|
||||
}
|
||||
}
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("the source of %s names no head of %s", branch, repository)
|
||||
}
|
||||
if _, err := acts.Record(meshhandactsclient.Request{What: "moved the head of " + repository + " in the source of the branch " + branch + " to the forge's head",
|
||||
Why: why, Cause: "gate_source_follow", Verb: "source-follow"}); err != nil {
|
||||
return nil, fmt.Errorf("the hand act was not recorded through mesh-hand-acts, so nothing was moved: %w", err)
|
||||
}
|
||||
moved, err := h.followForge(branch, name, held.Heads[name])
|
||||
return map[string]any{"branch": branch, "repository": name, "moved": moved, "by": by}, err
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
)
|
||||
|
||||
// Design 47 G A-464, A-464b: the forge bridge's merged message moves the head of its repository in the source of the branch it
|
||||
// landed on and says source-moved; a merge naming no delivery group is said not made by one; a branch no source key holds, a
|
||||
// repository the key does not name, and a head the key holds already move nothing.
|
||||
func TestAMergedMessageMovesTheSourceOfItsBranchWhoeverMadeTheMerge(t *testing.T) {
|
||||
old, merge := strings.Repeat("a", 40), strings.Repeat("b", 40)
|
||||
body := func(repository, base, group string) []byte {
|
||||
f := forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: repository, Base: base, Merge: merge, Head: merge, Number: 14, Root: strings.Repeat("d", 40)}
|
||||
data, err := forgemsg.EncodeForge(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
for _, c := range []struct {
|
||||
name, repository, base string
|
||||
moved bool
|
||||
}{
|
||||
{"watched branch, named repository", "novox/mesh-contracts", "main", true},
|
||||
{"a branch no source key holds", "novox/mesh-contracts", "other", false},
|
||||
{"a repository the key does not name", "novox/other", "main", false},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
src := &memSource{vals: map[string]Source{"main": {Group: "g", Heads: map[string]string{"novox/mesh-contracts": old}}}}
|
||||
var said []string
|
||||
var logged []string
|
||||
h := &Holder{Now: time.Now, Source: src,
|
||||
Logf: func(f string, a ...any) { logged = append(logged, f) },
|
||||
SaySeat: func(subject string, _ any, id string) error { said = append(said, id); return nil }}
|
||||
if err := h.ForgeMerged(body(c.repository, c.base, "")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := src.vals["main"]
|
||||
if c.moved != (got.Heads["novox/mesh-contracts"] == merge) || c.moved != (len(said) == 1) || (c.moved && got.Group != "") {
|
||||
t.Fatalf("source %+v said %v: moved wanted %v", got, said, c.moved)
|
||||
}
|
||||
if c.moved && !strings.Contains(strings.Join(logged, "\n"), "not made by a delivery group of the gate") {
|
||||
t.Errorf("a merge no group made is not said so: %v", logged)
|
||||
}
|
||||
// Heard again, the key names the head already: nothing is moved or said twice.
|
||||
if err := h.ForgeMerged(body(c.repository, c.base, "")); err != nil || len(said) > 1 {
|
||||
t.Errorf("heard twice: %v, said %v", err, said)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-464: at its start the gate compares each head with the forge's branch head and moves every one behind it; a head
|
||||
// the forge cannot read is said and left; gate_source_follow does it for one repository after the hand-act log took it.
|
||||
func TestTheStartAndTheToolMoveAHeadBehindTheForge(t *testing.T) {
|
||||
old, now := strings.Repeat("a", 40), strings.Repeat("c", 40)
|
||||
mk := func() (*Holder, *branchSource, *[]string) {
|
||||
src := &branchSource{memSource{vals: map[string]Source{"main": {Heads: map[string]string{"o/f1": old, "o/f2": old, "o/f3": now}}}}}
|
||||
var said []string
|
||||
return &Holder{Now: time.Now, Source: src, Logf: func(string, ...any) {},
|
||||
Forge: &fakeForge{heads: map[string]string{"o/f1@main": now, "o/f3@main": now}}, // o/f2 cannot be read
|
||||
SaySeat: func(_ string, _ any, id string) error { said = append(said, id); return nil }}, src, &said
|
||||
}
|
||||
h, src, said := mk()
|
||||
moved, err := h.FollowAtStart()
|
||||
if err == nil || len(moved) != 1 || moved[0] != "o/f1@main" || src.vals["main"].Heads["o/f1"] != now || src.vals["main"].Heads["o/f2"] != old || len(*said) != 1 {
|
||||
t.Fatalf("start: moved %v err %v heads %v said %v: want only o/f1 moved, o/f2 left with an error said", moved, err, src.vals["main"].Heads, *said)
|
||||
}
|
||||
h, src, said = mk()
|
||||
acts := &handActsFake{refuse: errors.New("log down")}
|
||||
if _, err := h.SourceFollow("main", "o/f1", "missed a merge", "me", acts); err == nil || src.vals["main"].Heads["o/f1"] != old {
|
||||
t.Fatalf("moved without the log: %v", err)
|
||||
}
|
||||
acts.refuse = nil
|
||||
if _, err := h.SourceFollow("main", "o/f1", "", "me", acts); err == nil {
|
||||
t.Fatal("moved with no reason")
|
||||
}
|
||||
out, err := h.SourceFollow("main", "o/f1", "missed a merge", "me", acts)
|
||||
if err != nil || out["moved"] != true || len(acts.took) != 1 || acts.took[0].Cause != "gate_source_follow" || src.vals["main"].Heads["o/f1"] != now {
|
||||
t.Fatalf("tool: %v %v log %+v heads %v", out, err, acts.took, src.vals["main"].Heads)
|
||||
}
|
||||
}
|
||||
|
||||
// Design 47 G A-464: the gate takes the forge bridge's merged event of any repository, by the manifest's own pattern.
|
||||
func TestTheGateTakesTheForgeBridgesMergedEventOfAnyRepository(t *testing.T) {
|
||||
merge := strings.Repeat("b", 40)
|
||||
data, err := forgemsg.EncodeForge(forgemsg.Forge{V: 1, Kind: forgemsg.ForgeMerged, Repository: "novox/mesh-contracts", Base: "main", Merge: merge, Head: merge, Number: 14, Root: strings.Repeat("d", 40)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
src := &memSource{vals: map[string]Source{"main": {Heads: map[string]string{"novox/mesh-contracts": strings.Repeat("a", 40)}}}}
|
||||
h := &Holder{Now: time.Now, Source: src, Logf: func(string, ...any) {}}
|
||||
key := strings.Replace(strings.Replace(forgeMergedEvent, "*", "novox", 1), "*", "mesh-contracts", 1)
|
||||
if err := take(h, stdio.Envelope{Key: key, Body: data}); err != nil || src.vals["main"].Heads["novox/mesh-contracts"] != merge {
|
||||
t.Fatalf("event %q: %v, source %+v", key, err, src.vals["main"])
|
||||
}
|
||||
}
|
||||
@@ -89,5 +89,16 @@ func sourceTools(h *Holder, acts handActs) []stdio.Tool {
|
||||
Run: func(a map[string]any) (any, error) {
|
||||
return h.SourceStart(strArg(a, "branch"), strArg(a, "why"), stdio.CallerOf(a), acts)
|
||||
}},
|
||||
{Name: "gate_source_follow",
|
||||
Description: "Move the head one watched branch's source names for one repository to the forge's head of that branch now, as " +
|
||||
"the gate does when it hears a merge and at its start (design 47 G A-464); recorded in the hand-act log of the seat " +
|
||||
"mesh-hand-acts first. Nothing is moved when the source already names the forge's head.",
|
||||
Input: map[string]any{"type": "object", "properties": map[string]any{
|
||||
"branch": str("the watched branch"), "repository": str("the repository, owner/name"),
|
||||
"why": str("why its head follows the forge by hand, in plain words")},
|
||||
"required": []string{"branch", "repository", "why"}},
|
||||
Run: func(a map[string]any) (any, error) {
|
||||
return h.SourceFollow(strArg(a, "branch"), strArg(a, "repository"), strArg(a, "why"), stdio.CallerOf(a), acts)
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
"git.pull-merged",
|
||||
"git.pull-closed",
|
||||
"mesh-catalog.registration-changed",
|
||||
"mesh-delivery-forge-bridge.forge.*.*.merged",
|
||||
"mesh-conditions.findings-asked"
|
||||
],
|
||||
"emits": [
|
||||
@@ -383,6 +384,7 @@
|
||||
"gate_records",
|
||||
"gate_source",
|
||||
"gate_source_again",
|
||||
"gate_source_follow",
|
||||
"gate_source_start"
|
||||
],
|
||||
"config": {
|
||||
|
||||
Reference in New Issue
Block a user