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 / or branch (%q, %q)", e.Repository, e.Branch) } return m, nil } // Repository is `/`. 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...` (A-236), apart from a pull request's `..`. 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 }