Files
mesh-delivery/modules/mesh-delivery-forge-bridge/internal/bridge/moved.go
T
jschoubben 7ac48fd882 B2, B3: the forge bridge says head-moved and carries the one model
B2: on every branch-moved of a watched branch, and at its start for every watched branch's head,
the bridge reads the head through the seat git's verb branch, as a kept call that waits on the
holder's return while the branch is watched, and writes one head-moved (repository, branch,
head, before the head held in its state heads, start) on its stream forge; a head equal to the
one held writes nothing. It reads no pull request, no tree, no file and no protection: the
merged and pushed messages, their inputs and the state inputs, the call of git.pull, the finding
branch-unprotected with its held finding writer, and the ten-second redelivery go. A move whose
head cannot be read is left unacknowledged for the bus to deliver again.

B3: the pull-message path (Convert, the open pull requests at start and reconnect, the
forge-delete message), the groups (internal/groups' group part, forge.group.merged, end-asks,
bridge_groups, bridge_group_end_again), bridge_merged_message with the forge-made reader, the
checker's owed and the merge act of a switched key (with the switch's bookkeeping read) go, with
their consumes, invokes, states and requires. The gate's owed path stays (H-A-71), its catch-up
from the gate's verb groups gone with that verb.

The two meet in main.go and the package compiles only with both, as it did not since B1. The
unit suites pass; the whole test (integration) is rewritten in B4.

Builds: design 47 H §739, §764 (the forge bridge; the branch protection); lead answers H-A-23, H-A-27, H-A-48, H-A-71

Claude-Session: https://claude.ai/code/session_01JG73KHUnBze48rdua8u2sy
2026-11-20 03:31:04 +01:00

81 lines
3.4 KiB
Go

package bridge
import (
"context"
"encoding/json"
"fmt"
"strings"
git "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/git"
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
"git.novox.be/novox/mesh-sdk/go/keys"
)
// BranchMovedSubject is the seat git's branch-moved (design 47 G §11): a branch's new head, which a merge and an admitted
// push alike say, kept on the seat's stream SEAT_git_branch-moved and heard there through a durable consumer.
const BranchMovedSubject = git.SubjectBranchMoved
// Moved is a branch-moved as the bridge reads it: the repository's two halves and the branch. The head the event says is
// never taken: the commit is read from the forge's own record by the seat's verb branch (§11).
type Moved struct {
Owner, Repo, Branch string
}
// MovedOf reads a branch-moved's body.
func MovedOf(data []byte) (Moved, error) {
var e git.BranchMovedEvent
if err := json.Unmarshal(data, &e); err != nil {
return Moved{}, unreadable(BranchMovedSubject, "the body is not json: %v", err)
}
m := Moved{Branch: e.Branch}
m.Owner, m.Repo, _ = strings.Cut(e.Repository, "/")
if m.Owner == "" || m.Repo == "" || strings.ContainsAny(m.Owner+m.Repo, "./ *>") || m.Branch == "" {
return Moved{}, unreadable(BranchMovedSubject, "no repository <owner>/<name> or branch (%q, %q)", e.Repository, e.Branch)
}
return m, nil
}
// Repository is `<owner>/<name>`.
func (m Moved) Repository() string { return repository(m.Owner, m.Repo) }
// HeadKey is the key of a watched branch's last head written in the state heads: `branch.<owner>.<name>.<escaped
// branch>` (A-236), apart from a pull request's `<owner>.<name>.<number>`.
func (m Moved) HeadKey() string {
return "branch." + m.Owner + "." + m.Repo + "." + keys.EscapeName(m.Branch)
}
// ReadHead reads a branch's head through the seat git's verb branch (design 47 H §739): never the event's body, and no
// protection (ADR 0682 §13). An answer that says an error, or names no 40-hex head, is an error, never a branch with none.
func ReadHead(ctx context.Context, call Caller, m Moved) (string, error) {
a, err := askGit[git.BranchAnswer](ctx, call, git.VerbBranch, git.BranchInput{Repository: m.Repository(), Branch: m.Branch})
if err != nil {
return "", err
}
if !sha40.MatchString(a.Head) {
return "", fmt.Errorf("the seat git's branch %s of %s names no 40-hex head (%q)", m.Branch, m.Repository(), a.Head)
}
return a.Head, nil
}
// HeadMoved is the one message of a move of a watched branch's head (design 47 H §739): its subject on the stream forge
// and its body, before the head the bridge held for the branch (empty the first time), start true when said at the
// bridge's start. A head equal to before writes nothing, which the caller decides before it asks.
func HeadMoved(m Moved, head, before string, start bool) (Message, error) {
event, err := forgemsg.HeadMovedEvent(m.Repository(), m.Branch)
if err != nil {
return Message{}, err
}
data, err := forgemsg.EncodeHeadMoved(forgemsg.HeadMoved{Repository: m.Repository(), Branch: m.Branch, Head: head, Before: before, Start: start})
if err != nil {
return Message{}, err
}
said := fmt.Sprintf("%s of %s moved to %s", m.Branch, m.Repository(), head)
if before != "" {
said += " from " + before
}
if start {
said += ", read at the start"
}
return Message{Event: event, Data: data, Said: said}, nil
}