FB6a: the forge bridge reads trees, files, pulls and tags through the seat git and hears its pull and delete events
Taken over from the join's 473f9323, 5f40d230, 159a679b; the events are the seat's pull-updated, pull-merged, pull-closed and forge-delete. Builds: design 47 G §252, §310, §312, §317 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// Command mesh-delivery-forge-bridge-tools serves the forge bridge's tools (novox/hq ADR 0633 §4, issue 1585; design 51):
|
||||
// the runtime launches it for a call. bridge_tags reads a package's git tags from the forge through the forge module's
|
||||
// gitea_api, for the builder's floor. bridge_groups reads the delivery groups the gate holds and what the bridge kept of their ends;
|
||||
// the runtime launches it for a call. bridge_tags reads a package's git tags through the seat git's verb tags, for the
|
||||
// builder's floor. bridge_groups reads the delivery groups the gate holds and what the bridge kept of their ends;
|
||||
// bridge_group_end_again writes an ask into the bridge's state end-asks and waits for the bridge's answer there, since the
|
||||
// bridge is the other program and serves no request subject (ADR 0437 §4).
|
||||
package main
|
||||
@@ -43,18 +43,18 @@ const (
|
||||
type asker func(key string, value []byte) (groups.EndAsk, error)
|
||||
|
||||
// Tools are the bridge's tools: groupsRead reads the groups the gate holds with what the bridge kept of them.
|
||||
func Tools(ask asker, now func() time.Time, groupsRead func() ([]groups.Row, []string, error), merged func(repository, merge string, number int) (json.RawMessage, bool, error), api bridge.API) []stdio.Tool {
|
||||
func Tools(ask asker, now func() time.Time, groupsRead func() ([]groups.Row, []string, error), merged func(repository, merge string, number int) (json.RawMessage, bool, error), forge bridge.Caller) []stdio.Tool {
|
||||
str := func(args map[string]any, k string) string { s, _ := args[k].(string); return strings.TrimSpace(s) }
|
||||
return []stdio.Tool{
|
||||
{Name: bridge.TagsTool,
|
||||
Description: "A package's git tags on the forge, for the builder's floor (design 51): every tag of repository whose name starts " +
|
||||
"with prefix (go/v for a Go package in the go directory, v for one at the root), sorted. Read-only; read from the forge " +
|
||||
"through the forge module's gitea_api, at most " + fmt.Sprint(bridge.TagsMaxPages*bridge.TagsPageSize) + " tags (more says so).",
|
||||
"with prefix (go/v for a Go package in the go directory, v for one at the root), sorted. Read-only; read through the seat " +
|
||||
"git's verb tags, at most " + fmt.Sprint(bridge.TagsMaxPages) + " pages (more says so).",
|
||||
Input: map[string]any{"type": "object", "required": []string{"repository"}, "properties": map[string]any{
|
||||
"repository": map[string]any{"type": "string", "description": "<owner>/<name>"},
|
||||
"prefix": map[string]any{"type": "string", "description": "only tags starting with it"}}},
|
||||
Run: func(args map[string]any) (any, error) {
|
||||
return bridge.Tags(api, str(args, "repository"), str(args, "prefix"))
|
||||
return bridge.Tags(context.Background(), forge, str(args, "repository"), str(args, "prefix"))
|
||||
}},
|
||||
{Name: MergedTool,
|
||||
Description: "The merged message of one merge, asked again (issue 1585): the message the bridge wrote on its stream FORGE when the " +
|
||||
@@ -135,10 +135,10 @@ func Tools(ask asker, now func() time.Time, groupsRead func() ([]groups.Row, []s
|
||||
// (bridge.MergedMessage), and, for a member of a delivery group, the group and its members as the process sets them.
|
||||
//
|
||||
// When the gate holds no merge with that commit, the merge was made on the forge (a recovery merge, the operator's own):
|
||||
// the pull request is read from the forge through api (by number when given, else by the merge commit), the message is
|
||||
// the pull request is read through the seat git's verb pull (by number when given, else by the merge commit), the message is
|
||||
// built the same way, and madeOnForge is true, for the answer and its log line to say. It is a lone pull request: no
|
||||
// group is set.
|
||||
func MergedOf(raws map[string][]byte, repository, merge string, number int, trees bridge.Trees, files bridge.Files, api bridge.API) (out json.RawMessage, madeOnForge bool, err error) {
|
||||
func MergedOf(raws map[string][]byte, repository, merge string, number int, trees bridge.Trees, files bridge.Files, forge bridge.Caller) (out json.RawMessage, madeOnForge bool, err error) {
|
||||
owner, repo, ok := strings.Cut(repository, "/")
|
||||
if !ok || owner == "" || repo == "" {
|
||||
return nil, false, fmt.Errorf("%q is not owner/name", repository)
|
||||
@@ -169,10 +169,10 @@ func MergedOf(raws map[string][]byte, repository, merge string, number int, tree
|
||||
out, err = forgemsg.EncodeForge(f)
|
||||
return out, false, err
|
||||
}
|
||||
if api == nil {
|
||||
if forge == nil {
|
||||
return nil, false, fmt.Errorf("the gate's merges hold no merge of %s with the commit %s, so its pull request is not known", repository, merge)
|
||||
}
|
||||
fp, err := bridge.ForgeMade(api, owner, repo, number, merge)
|
||||
fp, err := bridge.ForgeMade(context.Background(), forge, owner, repo, number, merge)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("the gate's merges hold no merge of %s with the commit %s, and the forge does not give it as a merged pull request either: %w", repository, merge, err)
|
||||
}
|
||||
@@ -282,11 +282,6 @@ func askCaller(_ context.Context, key string, body any) (json.RawMessage, error)
|
||||
return stdio.Ask(key, body)
|
||||
}
|
||||
|
||||
// forgeAPI is one GET of the forge's API through the forge module's gitea_api, the path the bridge reads the forge by.
|
||||
func forgeAPI(path string) (json.RawMessage, error) {
|
||||
return stdio.Ask("gitea.gitea_api", map[string]any{"path": path, "method": "GET"})
|
||||
}
|
||||
|
||||
func main() {
|
||||
merges, performed, asks := stdio.State(gateMergesState), stdio.State(groups.PerformedState), stdio.State(groups.EndAsksState)
|
||||
tools := Tools(askEnd(asks, 25*time.Second), time.Now,
|
||||
@@ -303,8 +298,8 @@ func main() {
|
||||
}
|
||||
}
|
||||
ctx := context.Background()
|
||||
return MergedOf(raws, repository, merge, number, bridge.TreesVia(ctx, askCaller), bridge.FilesVia(ctx, askCaller), forgeAPI)
|
||||
}, forgeAPI)
|
||||
return MergedOf(raws, repository, merge, number, bridge.TreesVia(ctx, askCaller), bridge.FilesVia(ctx, askCaller), askCaller)
|
||||
}, askCaller)
|
||||
if err := stdio.Serve("", tools); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[mesh-delivery-forge-bridge-tools] %v\n", err)
|
||||
os.Exit(1)
|
||||
|
||||
+37
-25
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
@@ -9,11 +10,13 @@ import (
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/contracts/go/clients/meshdeliveryclient"
|
||||
git "git.novox.be/novox/mesh-delivery/contracts/go/seats/git"
|
||||
seat "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdelivery"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||
|
||||
"mesh-delivery-forge-bridge/internal/bridge"
|
||||
|
||||
"mesh-delivery-forge-bridge/internal/gitfixture"
|
||||
"mesh-delivery-forge-bridge/internal/groups"
|
||||
)
|
||||
|
||||
@@ -136,7 +139,7 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) {
|
||||
return nil, bridge.ErrNotFound
|
||||
}
|
||||
published := func(repo string, number int, head, branch, merge string) forgemsg.Forge {
|
||||
body, _ := json.Marshal(map[string]any{"owner": "novox", "repo": repo, "number": number, "head": branch, "head_sha": head, "base": "main", "merge_commit_sha": merge})
|
||||
body, _ := json.Marshal(git.PullMergedEvent{Repository: "novox/" + repo, Number: int64(number), Branch: branch, Head: head, Base: "main", MergeCommit: merge})
|
||||
msg, err := bridge.Convert(bridge.MergedSubject, body, trees, files)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -158,7 +161,7 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) {
|
||||
repository string
|
||||
merge string
|
||||
number int
|
||||
api bridge.API
|
||||
api bridge.Caller
|
||||
onForge bool
|
||||
want forgemsg.Forge
|
||||
fail string
|
||||
@@ -173,23 +176,25 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) {
|
||||
// Issue 1966: a merge made on the forge (a recovery merge) has no merge in the gate; the bridge reads the pull request
|
||||
// from the forge, by number or by the merge commit, and checks its merge commit.
|
||||
{name: "a merge made on the forge, by number", raws: map[string][]byte{"solo": lone}, repository: "novox/a", merge: c2, number: 9, onForge: true,
|
||||
api: forgeAnswers(t, "/repos/novox/a/pulls/9", true, "fix/x", h2, c2, "main"), want: published("a", 9, h2, "fix/x", c2)},
|
||||
api: forgeAnswers(t, 9, "", "merged", "fix/x", h2, c2, "main"), want: published("a", 9, h2, "fix/x", c2)},
|
||||
{name: "a merge made on the forge, by the merge commit", raws: map[string][]byte{}, repository: "novox/a", merge: c2, onForge: true,
|
||||
api: forgeAnswers(t, "/repos/novox/a/commits/"+c2+"/pull", true, "fix/x", h2, c2, "main"), want: published("a", 9, h2, "fix/x", c2)},
|
||||
api: forgeAnswers(t, 0, c2, "merged", "fix/x", h2, c2, "main"), want: published("a", 9, h2, "fix/x", c2)},
|
||||
{name: "a forge pull request merged at another commit", raws: map[string][]byte{}, repository: "novox/a", merge: c2, number: 9,
|
||||
api: forgeAnswers(t, "/repos/novox/a/pulls/9", true, "fix/x", h2, c1, "main"), fail: "not at " + c2},
|
||||
api: forgeAnswers(t, 9, "", "merged", "fix/x", h2, c1, "main"), fail: "not at " + c2},
|
||||
{name: "a forge pull request not merged", raws: map[string][]byte{}, repository: "novox/a", merge: c2, number: 9,
|
||||
api: forgeAnswers(t, "/repos/novox/a/pulls/9", false, "fix/x", h2, "", "main"), fail: "not merged"},
|
||||
api: forgeAnswers(t, 9, "", "open", "fix/x", h2, "", "main"), fail: "not merged"},
|
||||
{name: "a forge pull request merged into another branch", raws: map[string][]byte{}, repository: "novox/a", merge: c2, number: 9,
|
||||
api: forgeAnswers(t, "/repos/novox/a/pulls/9", true, "fix/x", h2, c2, "dev"), fail: "not the trunk"},
|
||||
api: forgeAnswers(t, 9, "", "merged", "fix/x", h2, c2, "dev"), fail: "not the trunk"},
|
||||
{name: "a forge pull request with no head branch", raws: map[string][]byte{}, repository: "novox/a", merge: c2, number: 9,
|
||||
api: forgeAnswers(t, "/repos/novox/a/pulls/9", true, "refs/pull/9/head", h2, c2, "main"), fail: "no head branch"},
|
||||
{name: "a forge answer that is the bare body, no wrapper", raws: map[string][]byte{}, repository: "novox/a", merge: c2, number: 9,
|
||||
api: func(string) (json.RawMessage, error) { return json.RawMessage(`{"number":9,"merged":true}`), nil }, fail: `{"result"`},
|
||||
{name: "a forge answer with no pull request number", raws: map[string][]byte{}, repository: "novox/a", merge: c2, number: 9,
|
||||
api: func(string) (json.RawMessage, error) { return json.RawMessage(`{"result":{"merged":true}}`), nil }, fail: "no number"},
|
||||
api: forgeAnswers(t, 9, "", "merged", "", h2, c2, "main"), fail: "no head branch"},
|
||||
{name: "a seat answer that says an error", raws: map[string][]byte{}, repository: "novox/a", merge: c2, number: 9,
|
||||
api: answering(gitfixture.Given(t, "pull answers an error, never none", "verb:pull.answer")), fail: "holds no pull request"},
|
||||
{name: "a seat answer with no pull request", raws: map[string][]byte{}, repository: "novox/a", merge: c2, number: 9,
|
||||
api: answering(json.RawMessage(`{}`)), fail: "carries nothing"},
|
||||
{name: "a forge that does not answer", raws: map[string][]byte{}, repository: "novox/a", merge: c2,
|
||||
api: func(string) (json.RawMessage, error) { return nil, errors.New("the forge is down") }, fail: "the forge is down"},
|
||||
api: func(context.Context, string, any) (json.RawMessage, error) {
|
||||
return nil, errors.New("the forge is down")
|
||||
}, fail: "the forge is down"},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
raw, onForge, err := MergedOf(c.raws, c.repository, c.merge, c.number, trees, files, c.api)
|
||||
@@ -216,21 +221,28 @@ func TestTheBridgeToolsReadTheGroupsAndAskTheEndAgain(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// forgeAnswers is the forge's gitea_api for one GET path: that pull request as the forge says it; any other path is not found.
|
||||
func forgeAnswers(t *testing.T, path string, merged bool, branch, head, merge, base string) bridge.API {
|
||||
// forgeAnswers is the seat git's verb pull for one pull request, asked by number (when number is given) or by the merge
|
||||
// commit asked: the seat's own fixture with that pull request's state, branch, head, merge commit and base; any other
|
||||
// ask is refused as the seat refuses it.
|
||||
func forgeAnswers(t *testing.T, number int, byMerge, state, branch, head, merge, base string) bridge.Caller {
|
||||
t.Helper()
|
||||
body, _ := json.Marshal(map[string]any{"number": 9, "merged": merged, "merge_commit_sha": merge,
|
||||
"head": map[string]any{"ref": "refs/pull/9/head", "label": branch, "sha": head}, "base": map[string]any{"ref": base}})
|
||||
return func(p string) (json.RawMessage, error) {
|
||||
if p != path {
|
||||
return nil, errors.New("the forge has no " + p)
|
||||
var a git.PullAnswer
|
||||
if err := json.Unmarshal(gitfixture.Given(t, "pull answers", "verb:pull.answer"), &a); err != nil || a.Pull == nil {
|
||||
t.Fatalf("the seat's pull fixture: %v", err)
|
||||
}
|
||||
p := *a.Pull
|
||||
p.Repository, p.Number, p.State, p.Branch, p.Head, p.MergeCommit, p.Base = "novox/a", 9, state, branch, head, merge, base
|
||||
answer, _ := json.Marshal(git.PullAnswer{Pull: &p})
|
||||
return func(_ context.Context, key string, body any) (json.RawMessage, error) {
|
||||
in, ok := body.(git.PullInput)
|
||||
if key != "seat:git.pull" || !ok || in.Repository != "novox/a" || int(in.Number) != number || in.MergeCommit != byMerge {
|
||||
return json.RawMessage(`{"error":"the forge holds no such pull request"}`), nil
|
||||
}
|
||||
return wrapped(body), nil
|
||||
return answer, nil
|
||||
}
|
||||
}
|
||||
|
||||
// wrapped is gitea_api's answer for a body: {"result": <body>} (mesh-catalog modules/gitea/tools/index.ts).
|
||||
func wrapped(body []byte) json.RawMessage {
|
||||
b, _ := json.Marshal(map[string]json.RawMessage{"result": body})
|
||||
return b
|
||||
// answering is a seat that answers raw to every ask.
|
||||
func answering(raw json.RawMessage) bridge.Caller {
|
||||
return func(context.Context, string, any) (json.RawMessage, error) { return raw, nil }
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ const callTimeout = 30 * time.Second
|
||||
// caller calls a module's tool by its manifest key (`<module>.<tool>`) with a JSON body and returns the tool's result.
|
||||
type caller func(ctx context.Context, key string, body any) (json.RawMessage, error)
|
||||
|
||||
// callerOn calls tools as the bridge's own principal. It copies mesh-sdk go/process/call.go (Call, callSubjects,
|
||||
// callerOn calls tools and seat verbs as the bridge's own principal. It copies mesh-sdk go/process/call.go (Call, callSubjects,
|
||||
// request; hand-written here until the SDK's helper is used, issue 1242): the request is the tool's arguments as
|
||||
// JSON, published on the caller-named subject `mesh.mod.<module>.call.<tool>.<user, dots as ~>`, or on the subject that
|
||||
// names no caller, `mesh.mod.<module>.tool.<tool>`, when nothing answers the first; the reply is
|
||||
@@ -49,22 +49,40 @@ func callerOn(nc *nats.Conn, user string) caller {
|
||||
return r.Result, nil
|
||||
}
|
||||
return func(ctx context.Context, key string, body any) (json.RawMessage, error) {
|
||||
module, tool, ok := strings.Cut(key, ".")
|
||||
if !ok || module == "" || tool == "" {
|
||||
return nil, fmt.Errorf("%q is not <module>.<tool>", key)
|
||||
called, fallback, err := callSubjects(key, token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result, err := request(ctx, "mesh.mod."+module+".call."+tool+"."+token, data)
|
||||
result, err := request(ctx, called, data)
|
||||
if errors.Is(err, nats.ErrNoResponders) {
|
||||
result, err = request(ctx, "mesh.mod."+module+".tool."+tool, data)
|
||||
result, err = request(ctx, fallback, data)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
|
||||
// callSubjects is the caller-named subject a key is called on and its plain fallback: a seat's verb
|
||||
// (`seat:<seat>.<verb>`) on mesh.seat.<seat>.call.<verb>.<token>, else a module's tool (`<module>.<tool>`) on
|
||||
// mesh.mod.<module>.call.<tool>.<token> (mesh-sdk go/process/call.go callSubjectsOn).
|
||||
func callSubjects(key, token string) (called, fallback string, err error) {
|
||||
if rest, isSeat := strings.CutPrefix(key, "seat:"); isSeat {
|
||||
seat, verb, ok := strings.Cut(rest, ".")
|
||||
if !ok || seat == "" || verb == "" {
|
||||
return "", "", fmt.Errorf("%q is not seat:<seat>.<verb>", key)
|
||||
}
|
||||
return "mesh.seat." + seat + ".call." + verb + "." + token, "mesh.seat." + seat + ".tool." + verb, nil
|
||||
}
|
||||
module, tool, ok := strings.Cut(key, ".")
|
||||
if !ok || module == "" || tool == "" {
|
||||
return "", "", fmt.Errorf("%q is not <module>.<tool>", key)
|
||||
}
|
||||
return "mesh.mod." + module + ".call." + tool + "." + token, "mesh.mod." + module + ".tool." + tool, nil
|
||||
}
|
||||
|
||||
// treesVia asks the forge for a commit's trees through call.
|
||||
func treesVia(ctx context.Context, call caller) bridge.Trees {
|
||||
return bridge.TreesVia(ctx, bridge.Caller(call))
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
git "git.novox.be/novox/mesh-delivery/contracts/go/seats/git"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||
|
||||
"mesh-delivery-forge-bridge/internal/bridge"
|
||||
@@ -197,7 +198,8 @@ func run(ctx context.Context, getenv func(string) string, logOut io.Writer) erro
|
||||
// The module's one pull of its durable consumer on the shared events stream is its event hub; the bridge has the one
|
||||
// handler, for every event its consumes filter lets through. Essential: the forge's events are what the bridge carries.
|
||||
hub := process.NewEventHub(logger.Printf)
|
||||
if err := hub.Handle("mesh.mod.*.event.>", func(m jetstream.Msg) {
|
||||
// The forge's events are the seat git's (novox/hq design 47 *The seat `git`*), whichever module holds it.
|
||||
if err := hub.Handle("mesh.seat."+git.Seat+".event.>", func(m jetstream.Msg) {
|
||||
hear(&turn, func() {
|
||||
// Bounded: the turn is held, and no sync or other event goes on without it.
|
||||
hctx, cancel := context.WithTimeout(ctx, syncDeadline)
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
git "git.novox.be/novox/mesh-delivery/contracts/go/seats/git"
|
||||
"git.novox.be/novox/mesh-sdk/go/process"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
@@ -21,6 +22,7 @@ import (
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||
|
||||
"mesh-delivery-forge-bridge/internal/bridge"
|
||||
"mesh-delivery-forge-bridge/internal/gitfixture"
|
||||
)
|
||||
|
||||
const head = "0123456789abcdef0123456789abcdef01234567"
|
||||
@@ -53,18 +55,14 @@ func (p *fakePub) Publish(_ context.Context, subject string, payload []byte, _ .
|
||||
return &jetstream.PubAck{}, nil
|
||||
}
|
||||
|
||||
// body is gitea.pull.updated as mesh-catalog modules/gitea/trunk.ts:233-250 writes it (the fields the bridge reads).
|
||||
// body is the seat git's pull.updated (contracts/seats/git), the head head_sha laid over it when given.
|
||||
func body(m map[string]any) []byte {
|
||||
base := map[string]any{"owner": "novox", "repo": "mesh-delivery", "number": 12, "head": "feat/x", "head_sha": head,
|
||||
"module_dirs": []string{"modules/a"}, "module_dirs_said": true}
|
||||
for k, v := range m {
|
||||
if v == nil {
|
||||
delete(base, k)
|
||||
} else {
|
||||
base[k] = v
|
||||
}
|
||||
e := map[string]any{"repository": "novox/mesh-delivery", "number": 12, "branch": "feat/x", "head": head, "base": "main",
|
||||
"change": map[string]any{"paths": []string{"modules/a/x.go"}, "module-dirs": []string{"modules/a"}}}
|
||||
if h, ok := m["head_sha"]; ok {
|
||||
e["head"] = h
|
||||
}
|
||||
b, _ := json.Marshal(base)
|
||||
b, _ := json.Marshal(e)
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -80,7 +78,7 @@ func TestReadableEventIsWrittenOnForgeAndAcked(t *testing.T) { // 1235/AC2 (unit
|
||||
}
|
||||
})
|
||||
var logs bytes.Buffer
|
||||
m, pub := &fakeMsg{subject: "mesh.mod.gitea.event.pull.updated", data: body(nil)}, &fakePub{}
|
||||
m, pub := &fakeMsg{subject: git.SubjectPullUpdated, data: body(nil)}, &fakePub{}
|
||||
handle(context.Background(), m, pub, nil, someFiles, nil, log.New(&logs, "", 0))
|
||||
if len(pub.subjects) != 1 || pub.subjects[0] != "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox.mesh-delivery.pull" {
|
||||
t.Fatalf("published on %v", pub.subjects)
|
||||
@@ -96,7 +94,7 @@ func TestReadableEventIsWrittenOnForgeAndAcked(t *testing.T) { // 1235/AC2 (unit
|
||||
|
||||
func TestUnreadableEventIsTerminatedAndLogged(t *testing.T) { // 1235/AC5
|
||||
var logs bytes.Buffer
|
||||
m, pub := &fakeMsg{subject: "mesh.mod.gitea.event.pull.updated", data: body(map[string]any{"head_sha": "short"})}, &fakePub{}
|
||||
m, pub := &fakeMsg{subject: git.SubjectPullUpdated, data: body(map[string]any{"head_sha": "short"})}, &fakePub{}
|
||||
handle(context.Background(), m, pub, nil, someFiles, nil, log.New(&logs, "", 0))
|
||||
if len(pub.subjects) != 0 {
|
||||
t.Errorf("a message was written: %v", pub.subjects)
|
||||
@@ -104,13 +102,13 @@ func TestUnreadableEventIsTerminatedAndLogged(t *testing.T) { // 1235/AC5
|
||||
if m.termd != 1 || m.acked != 0 {
|
||||
t.Errorf("terminated %d acked %d, want terminated once", m.termd, m.acked)
|
||||
}
|
||||
if !strings.Contains(logs.String(), "mesh.mod.gitea.event.pull.updated") || !strings.Contains(logs.String(), "head") {
|
||||
if !strings.Contains(logs.String(), git.SubjectPullUpdated) || !strings.Contains(logs.String(), "head") {
|
||||
t.Errorf("log %q names neither the subject nor why", logs.String())
|
||||
}
|
||||
// 1235: an unwritten message is not acked (a failed write is redelivered by the server, not retried here); merged here
|
||||
// from a test of its own, for the test budget.
|
||||
logs.Reset()
|
||||
m, pub = &fakeMsg{subject: "mesh.mod.gitea.event.pull.updated", data: body(nil)}, &fakePub{err: errors.New("no stream")}
|
||||
m, pub = &fakeMsg{subject: git.SubjectPullUpdated, data: body(nil)}, &fakePub{err: errors.New("no stream")}
|
||||
handle(context.Background(), m, pub, nil, someFiles, nil, log.New(&logs, "", 0))
|
||||
if m.acked != 0 || m.termd != 0 {
|
||||
t.Errorf("acked %d terminated %d, want neither", m.acked, m.termd)
|
||||
@@ -120,16 +118,16 @@ func TestUnreadableEventIsTerminatedAndLogged(t *testing.T) { // 1235/AC5
|
||||
}
|
||||
}
|
||||
|
||||
// mergedEvent is gitea.pull.merged as mesh-catalog modules/gitea/index.ts:186-207 writes it (the fields the bridge reads).
|
||||
// mergedEvent is the seat git's pull.merged.
|
||||
func mergedEvent() []byte {
|
||||
b, _ := json.Marshal(map[string]any{"owner": "novox", "repo": "mesh-delivery", "number": 12, "head": "feat/x", "head_sha": head,
|
||||
"base": "main", "merge_commit_sha": "89abcdef0123456789abcdef0123456789abcdef"})
|
||||
b, _ := json.Marshal(map[string]any{"repository": "novox/mesh-delivery", "number": 12, "branch": "feat/x", "head": head,
|
||||
"base": "main", "merge-commit": "89abcdef0123456789abcdef0123456789abcdef"})
|
||||
return b
|
||||
}
|
||||
|
||||
func TestTreesRefusedLeavesTheEventForTenSecondsAndLogs(t *testing.T) { // 1236/AC3
|
||||
var logs bytes.Buffer
|
||||
m, pub := &fakeMsg{subject: "mesh.mod.gitea.event.pull.merged", data: mergedEvent()}, &fakePub{}
|
||||
m, pub := &fakeMsg{subject: git.SubjectPullMerged, data: mergedEvent()}, &fakePub{}
|
||||
call := func(context.Context, string, any) (json.RawMessage, error) {
|
||||
return nil, errors.New("the tree listing of novox/mesh-delivery was cut")
|
||||
}
|
||||
@@ -147,33 +145,36 @@ func TestTreesRefusedLeavesTheEventForTenSecondsAndLogs(t *testing.T) { // 1236/
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeCallsTheToolOnceAndWritesTheMergedMessage(t *testing.T) { // 1236/AC1 (through the tool's request and answer)
|
||||
func TestMergeCallsTheToolOnceAndWritesTheMergedMessage(t *testing.T) { // 1236/AC1; design 47 *The seat git* (verbs trees, file)
|
||||
var logs bytes.Buffer
|
||||
m, pub := &fakeMsg{subject: "mesh.mod.gitea.event.pull.merged", data: mergedEvent()}, &fakePub{}
|
||||
m, pub := &fakeMsg{subject: git.SubjectPullMerged, data: mergedEvent()}, &fakePub{}
|
||||
var keys []string
|
||||
var args []any
|
||||
var mu sync.Mutex // the files are asked for concurrently (issue 1458)
|
||||
var mu sync.Mutex // the files are asked for concurrently (issue 1458)
|
||||
trees := gitfixture.Given(t, "trees answers", "verb:trees.answer") // root ddd…, "" ddd…, contracts eee…
|
||||
file := gitfixture.Given(t, "file answers", "verb:file.answer")
|
||||
call := func(_ context.Context, key string, body any) (json.RawMessage, error) {
|
||||
mu.Lock()
|
||||
keys, args = append(keys, key), append(args, body)
|
||||
mu.Unlock()
|
||||
if key == "gitea.gitea_get_file" { // the manifests, 1448; the trees are what this test is about
|
||||
return json.RawMessage(`{"file":{"path":"p","sha":"0123","size":2,"content":"{}"}}`), nil
|
||||
if key == "seat:git.file" { // the manifests, 1448; the trees are what this test is about
|
||||
return file, nil
|
||||
}
|
||||
return json.RawMessage(`{"root":"r0","trees":{"":"t0","modules/a":"t1"}}`), nil
|
||||
return trees, nil
|
||||
}
|
||||
handle(context.Background(), m, pub, treesVia(context.Background(), call), filesVia(context.Background(), call), nil, log.New(&logs, "", 0))
|
||||
if len(keys) == 0 || keys[0] != "gitea.gitea_commit_trees" || slices.Index(keys[1:], "gitea.gitea_commit_trees") >= 0 {
|
||||
if len(keys) == 0 || keys[0] != "seat:git.trees" || slices.Index(keys[1:], "seat:git.trees") >= 0 {
|
||||
t.Fatalf("calls %v, want the trees once and first", keys)
|
||||
}
|
||||
if got, _ := json.Marshal(args[0]); string(got) != `{"owner":"novox","repo":"mesh-delivery","sha":"89abcdef0123456789abcdef0123456789abcdef"}` {
|
||||
if got, _ := json.Marshal(args[0]); !sameJSON(got, []byte(`{"repository":"novox/mesh-delivery","commit":"89abcdef0123456789abcdef0123456789abcdef"}`)) {
|
||||
t.Errorf("arguments %s", got)
|
||||
}
|
||||
if len(pub.subjects) != 1 || pub.subjects[0] != "mesh.stream.mesh-delivery-forge-bridge.event.forge.novox.mesh-delivery.merged" {
|
||||
t.Fatalf("published on %v", pub.subjects)
|
||||
}
|
||||
f, err := forgemsg.DecodeForge(pub.data[0])
|
||||
if err != nil || f.Root != "r0" || f.Trees[""] != "t0" || f.Trees["modules/a"] != "t1" || f.Merge == "" {
|
||||
d, e := strings.Repeat("d", 40), strings.Repeat("e", 40)
|
||||
if err != nil || f.Root != d || f.Trees[""] != d || f.Trees["contracts"] != e || f.Merge == "" {
|
||||
t.Errorf("message %+v err %v", f, err)
|
||||
}
|
||||
if m.acked != 1 {
|
||||
@@ -181,60 +182,65 @@ func TestMergeCallsTheToolOnceAndWritesTheMergedMessage(t *testing.T) { // 1236/
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnAnswerWithoutARootIsNotUsed(t *testing.T) { // 1236 (a shape the tool does not answer)
|
||||
call := func(context.Context, string, any) (json.RawMessage, error) {
|
||||
return json.RawMessage(`{"trees":{"a":"t"}}`), nil
|
||||
}
|
||||
if _, _, err := treesVia(context.Background(), call)("o", "r", "s"); err == nil {
|
||||
t.Error("an answer with no root was taken")
|
||||
}
|
||||
call = func(context.Context, string, any) (json.RawMessage, error) { return json.RawMessage(`[1]`), nil }
|
||||
if _, _, err := treesVia(context.Background(), call)("o", "r", "s"); err == nil {
|
||||
t.Error("an answer that is not an object was taken")
|
||||
func TestAnAnswerWithoutARootIsNotUsed(t *testing.T) { // 1236 (a shape the seat does not answer); design 47 *A failed read answers an error*
|
||||
for _, raw := range []json.RawMessage{json.RawMessage(`{"trees":{"a":"t"}}`), json.RawMessage(`[1]`),
|
||||
gitfixture.Given(t, "trees answers an error, never none", "verb:trees.answer")} {
|
||||
call := func(context.Context, string, any) (json.RawMessage, error) { return raw, nil }
|
||||
if _, _, err := treesVia(context.Background(), call)("o", "r", "s"); err == nil {
|
||||
t.Errorf("the answer %s was taken", raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The answer of gitea_get_file as mesh-catalog modules/gitea tools/index.ts and client.ts getFile write it, and the
|
||||
// error of a path the forge does not have as client.ts request writes it (`Gitea API ${path}: ${status} ${text}`).
|
||||
const getFileAnswer = `{"file":{"path":"modules/a/go.sum","ref":"89abcdef0123456789abcdef0123456789abcdef","sha":"0123","size":4,"content":"x y\n"}}`
|
||||
const getFile404 = "Gitea API /repos/novox/mesh-delivery/contents/modules/a/go.sum?ref=89abcdef: 404 {\"message\":\"GetContentsOrList\",\"url\":\"https://forge.example/api/swagger\"}"
|
||||
|
||||
func TestFilesViaAsksByOwnerRepoPathRefAndTakesTheContent(t *testing.T) { // 1448/AC4
|
||||
func TestFilesViaAsksByOwnerRepoPathRefAndTakesTheContent(t *testing.T) { // 1448/AC4; design 47 *The seat git* (verb file)
|
||||
var keys []string
|
||||
var args []any
|
||||
call := func(_ context.Context, key string, body any) (json.RawMessage, error) {
|
||||
keys, args = append(keys, key), append(args, body)
|
||||
return json.RawMessage(getFileAnswer), nil
|
||||
return gitfixture.Given(t, "file answers", "verb:file.answer"), nil
|
||||
}
|
||||
got, err := filesVia(context.Background(), call)("novox", "mesh-delivery", "modules/a/go.sum", "89abcdef0123456789abcdef0123456789abcdef")
|
||||
if err != nil || string(got) != "x y\n" {
|
||||
got, err := filesVia(context.Background(), call)("novox", "mesh-contracts", "seats/index.json", strings.Repeat("a", 40))
|
||||
if err != nil || string(got) != `{"format":1,"seats":["mesh-broker"]}` {
|
||||
t.Fatalf("got %q err %v", got, err)
|
||||
}
|
||||
if len(keys) != 1 || keys[0] != "gitea.gitea_get_file" {
|
||||
if len(keys) != 1 || keys[0] != "seat:git.file" {
|
||||
t.Fatalf("calls %v", keys)
|
||||
}
|
||||
if b, _ := json.Marshal(args[0]); string(b) != `{"owner":"novox","path":"modules/a/go.sum","ref":"89abcdef0123456789abcdef0123456789abcdef","repo":"mesh-delivery"}` {
|
||||
t.Errorf("arguments %s", b)
|
||||
want := gitfixture.Given(t, "file asks", "verb:file.input")
|
||||
if b, _ := json.Marshal(args[0]); !sameJSON(b, want) {
|
||||
t.Errorf("arguments %s, want the seat's %s", b, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilesViaTellsNotFoundFromOtherErrorsAndOtherShapes(t *testing.T) { // 1448/AC3
|
||||
call := func(err error, raw string) caller {
|
||||
return func(context.Context, string, any) (json.RawMessage, error) { return json.RawMessage(raw), err }
|
||||
func TestFilesViaTellsNotFoundFromOtherErrorsAndOtherShapes(t *testing.T) { // 1448/AC3; design 47 *A failed read answers an error*
|
||||
call := func(err error, raw json.RawMessage) caller {
|
||||
return func(context.Context, string, any) (json.RawMessage, error) { return raw, err }
|
||||
}
|
||||
if _, err := filesVia(context.Background(), call(errors.New(getFile404), ""))("o", "r", "p", "s"); !errors.Is(err, bridge.ErrNotFound) {
|
||||
t.Errorf("a 404 is %v, want ErrNotFound", err)
|
||||
absent := gitfixture.All(t, "file answers", "verb:file.answer")[1]
|
||||
if _, err := filesVia(context.Background(), call(nil, absent))("o", "r", "p", "s"); !errors.Is(err, bridge.ErrNotFound) {
|
||||
t.Errorf("an absent file is %v, want ErrNotFound", err)
|
||||
}
|
||||
if _, err := filesVia(context.Background(), call(errors.New("Gitea API /repos/o/r/contents/p: 502 bad gateway"), ""))("o", "r", "p", "s"); err == nil || errors.Is(err, bridge.ErrNotFound) {
|
||||
t.Errorf("a 502 is %v, want an error that is not ErrNotFound", err)
|
||||
if _, err := filesVia(context.Background(), call(errors.New("nats: timeout"), nil))("o", "r", "p", "s"); err == nil || errors.Is(err, bridge.ErrNotFound) {
|
||||
t.Errorf("a seat that does not answer is %v, want an error that is not ErrNotFound", err)
|
||||
}
|
||||
for _, raw := range []string{`{"file":null}`, `{}`, `[1]`, `{"file":{"path":"p"}}`} {
|
||||
for _, raw := range []json.RawMessage{gitfixture.Given(t, "file answers an error, never none", "verb:file.answer"), json.RawMessage(`{}`), json.RawMessage(`[1]`), json.RawMessage(`{"path":"p"}`)} {
|
||||
if _, err := filesVia(context.Background(), call(nil, raw))("o", "r", "p", "s"); err == nil || errors.Is(err, bridge.ErrNotFound) {
|
||||
t.Errorf("the answer %s gave %v, want an error", raw, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sameJSON is whether two JSON texts say the same value.
|
||||
func sameJSON(a, b []byte) bool {
|
||||
var x, y any
|
||||
if json.Unmarshal(a, &x) != nil || json.Unmarshal(b, &y) != nil {
|
||||
return false
|
||||
}
|
||||
ax, _ := json.Marshal(x)
|
||||
by, _ := json.Marshal(y)
|
||||
return bytes.Equal(ax, by)
|
||||
}
|
||||
|
||||
// someFiles is a forge that has a module.json at every path and no other file, for the tests that are not about the files.
|
||||
func someFiles(_, _, path, _ string) ([]byte, error) {
|
||||
if strings.HasSuffix(path, "module.json") {
|
||||
|
||||
@@ -10,14 +10,15 @@ import (
|
||||
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
git "git.novox.be/novox/mesh-delivery/contracts/go/seats/git"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||
|
||||
"mesh-delivery-forge-bridge/internal/bridge"
|
||||
)
|
||||
|
||||
// openPullsTool is the forge's tool for every open pull request (mesh-catalog modules/gitea/tools/index.ts
|
||||
// gitea_open_pulls: no arguments, answers {pulls: [...]}).
|
||||
const openPullsTool = "gitea.gitea_open_pulls"
|
||||
// openPullsTool is the seat git's verb for every open pull request (novox/hq design 47 *The seat `git`*): no arguments,
|
||||
// answers {pulls: [...]} or an error.
|
||||
var openPullsTool = bridge.GitKey(git.VerbOpenPulls)
|
||||
|
||||
// headStore is the bucket heads: the head last written on FORGE for each pull request, by bridge.Track's key
|
||||
// (novox/hq ADR 0443 §5).
|
||||
@@ -84,7 +85,7 @@ func syncOpen(ctx context.Context, call caller, pub publisher, heads headStore,
|
||||
defer mu.Unlock()
|
||||
ctx, cancel := context.WithTimeout(ctx, syncDeadline)
|
||||
defer cancel()
|
||||
raw, err := call(ctx, openPullsTool, struct{}{})
|
||||
raw, err := call(ctx, openPullsTool, git.OpenPullsInput{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", openPullsTool, err)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -16,12 +17,14 @@ import (
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||
)
|
||||
|
||||
// openAnswer is gitea_open_pulls's answer as mesh-catalog modules/gitea/client.ts:369-381 builds its entries inside {pulls}.
|
||||
// openAnswer is the seat git's answer of open-pulls: two into the trunk, one into another branch.
|
||||
func openAnswer() json.RawMessage {
|
||||
return json.RawMessage(`{"pulls":[
|
||||
{"owner":"novox","repo":"mesh-delivery","number":12,"base":"main","head":"feat/x","head_sha":"` + head + `","html_url":"h","required":[],"statuses":[],"checks":{},"labels":[]},
|
||||
{"owner":"novox","repo":"mesh-catalog","number":300,"base":"main","head":"feat/x","head_sha":"` + strings.Repeat("a", 40) + `","html_url":"h","required":[],"statuses":[],"checks":{},"labels":[]},
|
||||
{"owner":"novox","repo":"mesh-catalog","number":301,"base":"feat/y","head":"feat/z","head_sha":"` + strings.Repeat("b", 40) + `","html_url":"h","required":[],"statuses":[],"checks":{},"labels":[]}]}`)
|
||||
entry := func(repo string, number int, base, branch, sha string) string {
|
||||
return `{"pull":{"repository":"novox/` + repo + `","number":` + strconv.Itoa(number) + `,"base":"` + base + `","branch":"` + branch +
|
||||
`","head":"` + sha + `","state":"open","url":"h"},"required":[],"checks":{}}`
|
||||
}
|
||||
return json.RawMessage(`{"pulls":[` + entry("mesh-delivery", 12, "main", "feat/x", head) + `,` +
|
||||
entry("mesh-catalog", 300, "main", "feat/x", strings.Repeat("a", 40)) + `,` + entry("mesh-catalog", 301, "feat/y", "feat/z", strings.Repeat("b", 40)) + `]}`)
|
||||
}
|
||||
|
||||
func TestSyncOpenWritesOneUpdatedMessagePerOpenPull(t *testing.T) { // 1237/AC1, AC4
|
||||
@@ -35,7 +38,7 @@ func TestSyncOpenWritesOneUpdatedMessagePerOpenPull(t *testing.T) { // 1237/AC1,
|
||||
if err := runSync(call, pub, newHeads(), &sync.Mutex{}, log.New(&logs, "", 0)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(keys) != 1 || keys[0] != "gitea.gitea_open_pulls" {
|
||||
if len(keys) != 1 || keys[0] != "seat:git.open-pulls" {
|
||||
t.Errorf("calls %v", keys)
|
||||
}
|
||||
if len(pub.subjects) != 3 {
|
||||
@@ -232,7 +235,7 @@ func TestSyncOpenFailureNamesTheCallAndTheForgesWords(t *testing.T) { // 1237/AC
|
||||
return nil, errors.New("the forge said 502")
|
||||
}
|
||||
err := runSync(call, pub, newHeads(), &sync.Mutex{}, log.New(&bytes.Buffer{}, "", 0))
|
||||
if err == nil || !strings.Contains(err.Error(), "gitea.gitea_open_pulls") || !strings.Contains(err.Error(), "the forge said 502") {
|
||||
if err == nil || !strings.Contains(err.Error(), "seat:git.open-pulls") || !strings.Contains(err.Error(), "the forge said 502") {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
if len(pub.subjects) != 0 {
|
||||
|
||||
@@ -4,6 +4,7 @@ go 1.26.0
|
||||
|
||||
require (
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/clients/meshdeliveryclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/seats/git v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/seats/meshdelivery v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/sdk v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Package bridge turns the forge module's pull request events into the messages the forge bridge writes on FORGE.
|
||||
// Package bridge turns the seat git's pull request events into the messages the forge bridge writes on FORGE.
|
||||
package bridge
|
||||
|
||||
import (
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
git "git.novox.be/novox/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/catalogue"
|
||||
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||
@@ -35,13 +36,13 @@ var ErrRetry = errors.New("the forge could not say")
|
||||
const Trunk = "main"
|
||||
|
||||
// Trees asks the forge for the trees of a commit: the root tree and the tree of every directory holding a module.json
|
||||
// (the root directory spelled ""), as the forge's gitea_commit_trees answers (issue 1234/AC4).
|
||||
// (the root directory spelled ""), as the seat git's verb trees answers (issue 1234/AC4).
|
||||
type Trees func(owner, repo, sha string) (root string, trees map[string]string, err error)
|
||||
|
||||
// ErrNotFound says the forge has no file at that path and ref (the forge's "404", issue 1448).
|
||||
var ErrNotFound = errors.New("the forge has no such file")
|
||||
|
||||
// Files asks the forge for one file's bytes at a ref (a commit), as the forge's gitea_get_file answers; a file the forge
|
||||
// Files asks the forge for one file's bytes at a ref (a commit), as the seat git's verb file answers; a file the forge
|
||||
// says is not there is ErrNotFound, any other failure any other error (issue 1448).
|
||||
type Files func(owner, repo, path, ref string) ([]byte, error)
|
||||
|
||||
@@ -64,16 +65,16 @@ type Message struct {
|
||||
Said string
|
||||
}
|
||||
|
||||
// The subjects of the forge module's events the bridge writes pull messages for (mesh-catalog modules/gitea,
|
||||
// published by the module as mesh.mod.<module>.event.<event>).
|
||||
// The subjects of the seat git's events the bridge writes pull messages for (novox/hq design 47 *The seat `git`*): the
|
||||
// seat's holder says them on mesh.seat.git.event.<event>, whichever module holds it; the bridge names none.
|
||||
const (
|
||||
UpdatedSubject = "mesh.mod.gitea.event.pull.updated"
|
||||
ClosedSubject = "mesh.mod.gitea.event.pull.closed"
|
||||
MergedSubject = "mesh.mod.gitea.event.pull.merged"
|
||||
// DeletedSubject is the forge module's forge.delete: a branch or a tag deleted (mesh-catalog modules/gitea
|
||||
// webhook.ts: owner, repo, ref, ref_type, sha). Only the forge module's account may publish it, the subject being
|
||||
// in its own namespace (design 32 § 2), so no other module can end a branch's tagged versions.
|
||||
DeletedSubject = "mesh.mod.gitea.event.forge.delete"
|
||||
UpdatedSubject = git.SubjectPullUpdated
|
||||
ClosedSubject = git.SubjectPullClosed
|
||||
MergedSubject = git.SubjectPullMerged
|
||||
// DeletedSubject is the seat's forge.delete: a branch or a tag deleted (repository, ref, ref-type). Only the seat's
|
||||
// holder may publish on the seat's subjects (novox/hq ADR 0363 §2), so no other module can end a branch's tagged
|
||||
// versions.
|
||||
DeletedSubject = git.SubjectForgeDelete
|
||||
)
|
||||
|
||||
// ForgeBranchDeleted is the kind of a branch-deleted message on FORGE (novox/hq design 51 *The forge bridge says a
|
||||
@@ -91,21 +92,14 @@ type BranchDeleted struct {
|
||||
Head string `json:"head,omitempty"`
|
||||
}
|
||||
|
||||
// deleted is the part of forge.delete the bridge reads.
|
||||
type deleted struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Ref string `json:"ref"`
|
||||
RefType string `json:"ref_type"`
|
||||
SHA string `json:"sha"`
|
||||
}
|
||||
|
||||
// branchDeleted turns forge.delete into a branch-deleted message; a deleted tag is not handled.
|
||||
func branchDeleted(subject string, data []byte) (Message, error) {
|
||||
var d deleted
|
||||
if err := json.Unmarshal(data, &d); err != nil {
|
||||
var e git.ForgeDeleteEvent
|
||||
if err := json.Unmarshal(data, &e); err != nil {
|
||||
return Message{}, unreadable(subject, "the body is not json: %v", err)
|
||||
}
|
||||
d := struct{ Owner, Repo, Ref, RefType string }{Ref: e.Ref, RefType: e.RefType}
|
||||
d.Owner, d.Repo, _ = strings.Cut(e.Repository, "/")
|
||||
if d.RefType != "branch" {
|
||||
return Message{}, fmt.Errorf("%w: %s of a %s (%s)", ErrNotHandled, subject, d.RefType, d.Ref)
|
||||
}
|
||||
@@ -118,7 +112,7 @@ func branchDeleted(subject string, data []byte) (Message, error) {
|
||||
case branch == Trunk:
|
||||
return Message{}, fmt.Errorf("%w: %s of the trunk %s", ErrNotHandled, subject, Trunk)
|
||||
}
|
||||
body, err := json.Marshal(BranchDeleted{V: keys.Version, Kind: ForgeBranchDeleted, Repository: d.Owner + "/" + d.Repo, Branch: branch, Head: d.SHA})
|
||||
body, err := json.Marshal(BranchDeleted{V: keys.Version, Kind: ForgeBranchDeleted, Repository: d.Owner + "/" + d.Repo, Branch: branch})
|
||||
if err != nil {
|
||||
return Message{}, unreadable(subject, "%v", err)
|
||||
}
|
||||
@@ -128,22 +122,48 @@ func branchDeleted(subject string, data []byte) (Message, error) {
|
||||
|
||||
var sha40 = regexp.MustCompile(`^[0-9a-f]{40}$`)
|
||||
|
||||
// pull is the part of gitea.pull.merged (index.ts:186-207), gitea.pull.updated (mesh-catalog modules/gitea/trunk.ts:233-250) and gitea.pull.closed
|
||||
// (index.ts:127-128) the bridge reads; head is the branch and head_sha the commit.
|
||||
// pull is the part of the seat git's pull.updated, pull.merged and pull.closed the bridge reads: Head is the branch and
|
||||
// HeadSHA the commit, Owner and Repo the repository's two halves.
|
||||
type pull struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Number int `json:"number"`
|
||||
Head string `json:"head"`
|
||||
HeadSHA string `json:"head_sha"`
|
||||
ModuleDirs []string `json:"module_dirs"`
|
||||
ModuleDirsSaid bool `json:"module_dirs_said"`
|
||||
Base string `json:"base"`
|
||||
MergeSHA string `json:"merge_commit_sha"`
|
||||
// Paths are the files the pull request changes (a removed or renamed one included), PathsTruncated says the forge
|
||||
// cut the list; nil when the forge did not say them.
|
||||
Paths *[]string `json:"paths"`
|
||||
PathsTruncated bool `json:"paths_truncated"`
|
||||
Owner, Repo string
|
||||
Number int
|
||||
Head, HeadSHA string
|
||||
ModuleDirs []string
|
||||
ModuleDirsSaid bool
|
||||
Base string
|
||||
MergeSHA string
|
||||
}
|
||||
|
||||
// pullOf reads one of the seat's pull events. The directories are the seat's word only when its change says them: an
|
||||
// absent `module-dirs` is "could not be read", never "none".
|
||||
func pullOf(data []byte) (pull, error) {
|
||||
var e struct {
|
||||
Repository string `json:"repository"`
|
||||
Number int64 `json:"number"`
|
||||
Branch string `json:"branch"`
|
||||
Head string `json:"head"`
|
||||
Base string `json:"base"`
|
||||
MergeCommit string `json:"merge-commit"`
|
||||
Change json.RawMessage `json:"change"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &e); err != nil {
|
||||
return pull{}, err
|
||||
}
|
||||
p := pull{Number: int(e.Number), Head: e.Branch, HeadSHA: e.Head, Base: e.Base, MergeSHA: e.MergeCommit}
|
||||
p.Owner, p.Repo, _ = strings.Cut(e.Repository, "/")
|
||||
if len(e.Change) > 0 && string(e.Change) != "null" {
|
||||
var c map[string]json.RawMessage
|
||||
if err := json.Unmarshal(e.Change, &c); err != nil {
|
||||
return pull{}, fmt.Errorf("its change: %w", err)
|
||||
}
|
||||
if raw, said := c["module-dirs"]; said {
|
||||
if err := json.Unmarshal(raw, &p.ModuleDirs); err != nil {
|
||||
return pull{}, fmt.Errorf("its change's module-dirs: %w", err)
|
||||
}
|
||||
p.ModuleDirsSaid = true
|
||||
}
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func unreadable(subject, why string, args ...any) error {
|
||||
@@ -176,12 +196,12 @@ func ConvertKept(subject string, data []byte, trees Trees, files Files, kept Kep
|
||||
default:
|
||||
return Message{}, fmt.Errorf("%w: %s", ErrNotHandled, subject)
|
||||
}
|
||||
var p pull
|
||||
if err := json.Unmarshal(data, &p); err != nil {
|
||||
return Message{}, unreadable(subject, "the body is not json: %v", err)
|
||||
p, err := pullOf(data)
|
||||
if err != nil {
|
||||
return Message{}, unreadable(subject, "the body is not the seat's json: %v", err)
|
||||
}
|
||||
switch {
|
||||
case p.Owner == "" || p.Repo == "":
|
||||
case p.Owner == "" || p.Repo == "" || strings.Contains(p.Repo, "/"):
|
||||
return Message{}, unreadable(subject, "no owner or repo")
|
||||
case p.Number <= 0:
|
||||
return Message{}, unreadable(subject, "no pull request number")
|
||||
@@ -462,29 +482,39 @@ func merged(subject string, p pull, trees Trees, files Files, kept Kept) (Messag
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
// openPull is an entry of gitea_open_pulls's answer (mesh-catalog modules/gitea/client.ts:369-381 openPulls, with the
|
||||
// branch as `head`, issue 1234/AC5), the part the bridge reads.
|
||||
type openPull struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Number int `json:"number"`
|
||||
Base string `json:"base"`
|
||||
Head string `json:"head"`
|
||||
HeadSHA string `json:"head_sha"`
|
||||
// openPulls reads the seat git's answer of open-pulls: an answer saying an error is the error, never none open.
|
||||
func openPulls(answer []byte) ([]pull, error) {
|
||||
var a git.OpenPullsAnswer
|
||||
if err := json.Unmarshal(answer, &a); err != nil {
|
||||
return nil, fmt.Errorf("the seat git's answer of open-pulls is not its contract's: %v", err)
|
||||
}
|
||||
if a.Error != "" {
|
||||
return nil, fmt.Errorf("the seat git refused open-pulls: %s", a.Error)
|
||||
}
|
||||
var said map[string]json.RawMessage
|
||||
if _ = json.Unmarshal(answer, &said); said["pulls"] == nil || string(said["pulls"]) == "null" {
|
||||
// None open is `pulls: []`; an answer naming no list would drop every candidate on a holder's fault.
|
||||
return nil, fmt.Errorf("open-pulls: %w", errNoAnswer)
|
||||
}
|
||||
out := make([]pull, 0, len(a.Pulls))
|
||||
for _, o := range a.Pulls {
|
||||
p := pull{Number: int(o.Pull.Number), Head: o.Pull.Branch, HeadSHA: o.Pull.Head, Base: o.Pull.Base}
|
||||
p.Owner, p.Repo, _ = strings.Cut(o.Pull.Repository, "/")
|
||||
out = append(out, p)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// OpenPulls turns the answer of gitea_open_pulls, {pulls: [...]}, into one pull message per open pull request into
|
||||
// OpenPulls turns the seat git's answer of open-pulls into one pull message per open pull request into
|
||||
// the trunk, action updated and its directories not known (the forge's answer does not say them). A pull request into
|
||||
// another branch gets none. An entry that cannot be read is named in skipped and gets none; an answer that is not
|
||||
// {pulls} is an error.
|
||||
// the contract's, or that says an error, is an error.
|
||||
func OpenPulls(answer []byte) (msgs []Message, skipped []string, err error) {
|
||||
var a struct {
|
||||
Pulls *[]openPull `json:"pulls"`
|
||||
pulls, err := openPulls(answer)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err := json.Unmarshal(answer, &a); err != nil || a.Pulls == nil {
|
||||
return nil, nil, fmt.Errorf("the answer of gitea_open_pulls is not {pulls: [...]}: %v", err)
|
||||
}
|
||||
for _, p := range *a.Pulls {
|
||||
for _, p := range pulls {
|
||||
if p.Base != Trunk {
|
||||
continue
|
||||
}
|
||||
@@ -523,18 +553,16 @@ func Track(f forgemsg.Forge) (key, head string, ok bool) {
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
// OpenMessage turns the answer of gitea_open_pulls into the open message: every open pull request into the trunk, by
|
||||
// OpenMessage turns the seat git's answer of open-pulls into the open message: every open pull request into the trunk, by
|
||||
// repository and number, whether its head reads or not and whether FORGE holds it or not, in repository and number
|
||||
// order. A pull request into another branch is not named. An answer that names none is a message too: nothing is open.
|
||||
func OpenMessage(answer []byte) (Message, error) {
|
||||
var a struct {
|
||||
Pulls *[]openPull `json:"pulls"`
|
||||
}
|
||||
if err := json.Unmarshal(answer, &a); err != nil || a.Pulls == nil {
|
||||
return Message{}, fmt.Errorf("the answer of gitea_open_pulls is not {pulls: [...]}: %v", err)
|
||||
pulls, err := openPulls(answer)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
open := []forgemsg.OpenPull{}
|
||||
for _, p := range *a.Pulls {
|
||||
for _, p := range pulls {
|
||||
if p.Base != Trunk || p.Owner == "" || p.Repo == "" || p.Number <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package bridge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
@@ -8,44 +9,63 @@ import (
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-forge-bridge/forgemsg"
|
||||
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||
|
||||
"mesh-delivery-forge-bridge/internal/gitfixture"
|
||||
)
|
||||
|
||||
const head = "0123456789abcdef0123456789abcdef01234567"
|
||||
|
||||
// updated is the body of gitea.pull.updated as mesh-catalog modules/gitea/trunk.ts:233-250 (announcementOf) writes
|
||||
// it: owner, repo, number, base_sha, title, body, base, head (the branch), head_sha, clone_url, html_url, paths,
|
||||
// paths_truncated, removed, module_dirs, module_dirs_said, merge_check, merge_check_said.
|
||||
// updated is the body of the seat git's pull.updated (contracts/seats/git): the pull request, its base's head and its
|
||||
// change. extra names the parts a test changes by their short names (owner, repo, number, head for the branch,
|
||||
// head_sha for the commit, base, module_dirs, module_dirs_said), a nil value leaving the part out (seatPull).
|
||||
func updated(extra map[string]any) []byte {
|
||||
m := map[string]any{
|
||||
"owner": "novox", "repo": "mesh-delivery", "number": 12,
|
||||
"base_sha": "ffffffffffffffffffffffffffffffffffffffff", "title": "A change", "body": "text", "base": "main",
|
||||
"head": "feat/x", "head_sha": head, "clone_url": "https://forge.example/novox/mesh-delivery.git",
|
||||
"html_url": "https://forge.example/novox/mesh-delivery/pulls/12",
|
||||
"paths": []string{"modules/a/x.go"}, "paths_truncated": false, "removed": []string{},
|
||||
"module_dirs": []string{"modules/a"}, "module_dirs_said": true,
|
||||
"merge_check": true, "merge_check_said": true,
|
||||
}
|
||||
for k, v := range extra {
|
||||
if v == nil {
|
||||
delete(m, k)
|
||||
} else {
|
||||
m[k] = v
|
||||
return seatPull(map[string]any{"base_sha": "ffffffffffffffffffffffffffffffffffffffff",
|
||||
"paths": []string{"modules/a/x.go"}, "module_dirs": []string{"modules/a"}, "module_dirs_said": true}, extra)
|
||||
}
|
||||
|
||||
// closed is the body of the seat git's pull.closed.
|
||||
func closed() []byte { return seatPull(nil, nil) }
|
||||
|
||||
// seatPull is a pull event of the seat git: the pull request novox/mesh-delivery#12 of feat/x at head into main, with
|
||||
// the parts of base and extra laid over it by their short names, a nil value leaving the part out. module_dirs is said
|
||||
// in the change only when module_dirs_said is true: its absence is "could not be read".
|
||||
func seatPull(base, extra map[string]any) []byte {
|
||||
m := map[string]any{"owner": "novox", "repo": "mesh-delivery", "number": 12, "title": "A change", "head": "feat/x",
|
||||
"head_sha": head, "base": "main", "html_url": "https://forge.example/novox/mesh-delivery/pulls/12"}
|
||||
for _, over := range []map[string]any{base, extra} {
|
||||
for k, v := range over {
|
||||
if v == nil {
|
||||
delete(m, k)
|
||||
} else {
|
||||
m[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
b, _ := json.Marshal(m)
|
||||
e := map[string]any{}
|
||||
owner, _ := m["owner"].(string)
|
||||
repo, _ := m["repo"].(string)
|
||||
if owner != "" || repo != "" {
|
||||
e["repository"] = owner + "/" + repo
|
||||
}
|
||||
for short, name := range map[string]string{"number": "number", "title": "title", "head": "branch", "head_sha": "head",
|
||||
"base": "base", "html_url": "url", "merge_commit_sha": "merge-commit", "base_sha": "base-head", "merged_at": "merged-at"} {
|
||||
if v, ok := m[short]; ok {
|
||||
e[name] = v
|
||||
}
|
||||
}
|
||||
if paths, ok := m["paths"]; ok {
|
||||
c := map[string]any{"paths": paths}
|
||||
if m["module_dirs_said"] == true && m["module_dirs"] != nil {
|
||||
c["module-dirs"] = m["module_dirs"]
|
||||
}
|
||||
e["change"] = c
|
||||
}
|
||||
b, _ := json.Marshal(e)
|
||||
return b
|
||||
}
|
||||
|
||||
// closed is the body of gitea.pull.closed as mesh-catalog modules/gitea/index.ts:127-128 writes it: owner, repo,
|
||||
// number, title, head, head_sha, base, html_url.
|
||||
func closed() []byte {
|
||||
b, _ := json.Marshal(map[string]any{"owner": "novox", "repo": "mesh-delivery", "number": 12, "title": "A change",
|
||||
"head": "feat/x", "head_sha": head, "base": "main", "html_url": "https://forge.example/novox/mesh-delivery/pulls/12"})
|
||||
return b
|
||||
}
|
||||
|
||||
const updatedSubject = "mesh.mod.gitea.event.pull.updated"
|
||||
const closedSubject = "mesh.mod.gitea.event.pull.closed"
|
||||
const updatedSubject = "mesh.seat.git.event.pull-updated"
|
||||
const closedSubject = "mesh.seat.git.event.pull-closed"
|
||||
|
||||
func TestUpdatedBecomesAPullMessage(t *testing.T) { // 1235/AC2 (the conversion; the bus is the integration test)
|
||||
msg, err := Convert(updatedSubject, updated(nil), nil, anyFiles)
|
||||
@@ -142,8 +162,8 @@ func TestUnreadableEventsWriteNothing(t *testing.T) { // 1235/AC5
|
||||
"upper-case head": {updatedSubject, updated(map[string]any{"head_sha": strings.ToUpper(head)}), "head"},
|
||||
"no branch": {updatedSubject, updated(map[string]any{"head": nil}), "branch"},
|
||||
"not json": {updatedSubject, []byte("{"), "json"},
|
||||
"closed no head": {closedSubject, mustDrop(closed(), "head_sha"), "head"},
|
||||
"closed no repo": {closedSubject, mustDrop(closed(), "repo"), "repo"},
|
||||
"closed no head": {closedSubject, mustDrop(closed(), "head"), "head"},
|
||||
"closed no repo": {closedSubject, mustDrop(closed(), "repository"), "repo"},
|
||||
} {
|
||||
msg, err := Convert(c.subject, c.data, nil, anyFiles)
|
||||
if err == nil {
|
||||
@@ -171,21 +191,21 @@ func mustDrop(body []byte, key string) []byte {
|
||||
}
|
||||
|
||||
func TestOtherSubjectsAreNotHandled(t *testing.T) { // 1235, 1236
|
||||
for _, s := range []string{"mesh.mod.gitea.event.something.else", "mesh.mod.other.event.pull.updated", "mesh.mod.other.event.forge.delete"} {
|
||||
for _, s := range []string{"mesh.seat.git.event.something.else", "mesh.mod.gitea.event.pull.updated", "mesh.mod.other.event.forge.delete"} {
|
||||
if _, err := Convert(s, updated(nil), nil, anyFiles); !errors.Is(err, ErrNotHandled) {
|
||||
t.Errorf("%s: err = %v, want ErrNotHandled", s, err)
|
||||
}
|
||||
}
|
||||
// The forge module's forge.delete of a branch is a branch-deleted message (DLV-FB1, design 51); of a tag or the
|
||||
// The seat's forge.delete of a branch is a branch-deleted message (DLV-FB1, design 51); of a tag or the
|
||||
// trunk it is not handled, and a body naming no repository is unreadable.
|
||||
del := func(refType, ref string) []byte {
|
||||
b, _ := json.Marshal(map[string]any{"owner": "novox", "repo": "mesh-sdk", "full_name": "novox/mesh-sdk", "ref": ref, "ref_type": refType, "sha": head})
|
||||
b, _ := json.Marshal(map[string]any{"repository": "novox/mesh-sdk", "ref": ref, "ref-type": refType, "sender": "jochen"})
|
||||
return b
|
||||
}
|
||||
msg, err := Convert(DeletedSubject, del("branch", "feat/x"), nil, anyFiles)
|
||||
var bd BranchDeleted
|
||||
if err != nil || msg.Event != "forge.novox.mesh-sdk.branch-deleted" || json.Unmarshal(msg.Data, &bd) != nil ||
|
||||
bd.Kind != ForgeBranchDeleted || bd.Repository != "novox/mesh-sdk" || bd.Branch != "feat/x" || bd.Head != head || msg.Said == "" {
|
||||
bd.Kind != ForgeBranchDeleted || bd.Repository != "novox/mesh-sdk" || bd.Branch != "feat/x" || bd.Head != "" || msg.Said == "" {
|
||||
t.Errorf("forge.delete of a branch = %+v (%s), %v", msg, msg.Data, err)
|
||||
}
|
||||
for _, c := range []struct{ refType, ref string }{{"tag", "go/v0.1.13"}, {"branch", "main"}} {
|
||||
@@ -193,7 +213,7 @@ func TestOtherSubjectsAreNotHandled(t *testing.T) { // 1235, 1236
|
||||
t.Errorf("forge.delete of %s %s: err = %v, want ErrNotHandled", c.refType, c.ref, err)
|
||||
}
|
||||
}
|
||||
if _, err := Convert(DeletedSubject, []byte(`{"ref":"x","ref_type":"branch"}`), nil, anyFiles); !errors.Is(err, ErrUnreadable) {
|
||||
if _, err := Convert(DeletedSubject, []byte(`{"ref":"x","ref-type":"branch"}`), nil, anyFiles); !errors.Is(err, ErrUnreadable) {
|
||||
t.Errorf("forge.delete naming no repository: err = %v, want ErrUnreadable", err)
|
||||
}
|
||||
// 1236: a merged event without a full merge commit is unreadable, and the forge is not asked.
|
||||
@@ -206,27 +226,13 @@ func TestOtherSubjectsAreNotHandled(t *testing.T) { // 1235, 1236
|
||||
|
||||
const merge = "89abcdef0123456789abcdef0123456789abcdef"
|
||||
|
||||
// mergedBody is gitea.pull.merged as mesh-catalog modules/gitea/index.ts:186-207 writes it.
|
||||
// mergedBody is the seat git's pull.merged, extra by the short names seatPull reads.
|
||||
func mergedBody(extra map[string]any) []byte {
|
||||
m := map[string]any{
|
||||
"owner": "novox", "repo": "mesh-delivery", "number": 12, "title": "A change", "body": "text",
|
||||
"head_sha": head, "head_checks": map[string]string{"mesh/merge-gate": "success"}, "head": "feat/x", "base": "main",
|
||||
"merge_commit_sha": merge, "merged_at": "2026-10-19T12:00:00Z", "clone_url": "https://forge.example/novox/mesh-delivery.git",
|
||||
"html_url": "https://forge.example/novox/mesh-delivery/pulls/12", "paths": []string{"modules/a/x.go"}, "paths_truncated": false,
|
||||
"removed": []string{}, "module_dirs": []string{"modules/a"}, "module_dirs_said": true,
|
||||
}
|
||||
for k, v := range extra {
|
||||
if v == nil {
|
||||
delete(m, k)
|
||||
} else {
|
||||
m[k] = v
|
||||
}
|
||||
}
|
||||
b, _ := json.Marshal(m)
|
||||
return b
|
||||
return seatPull(map[string]any{"merge_commit_sha": merge, "merged_at": "2026-10-19T12:00:00Z",
|
||||
"paths": []string{"modules/a/x.go"}, "module_dirs": []string{"modules/a"}, "module_dirs_said": true}, extra)
|
||||
}
|
||||
|
||||
const mergedSubject = "mesh.mod.gitea.event.pull.merged"
|
||||
const mergedSubject = "mesh.seat.git.event.pull-merged"
|
||||
|
||||
type call struct{ owner, repo, sha string }
|
||||
|
||||
@@ -311,16 +317,15 @@ func TestNoModuleJSONAnywhereWritesAMergedMessageWithNoTrees(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// openAnswer is gitea_open_pulls's answer as mesh-catalog modules/gitea/client.ts:369-381 builds each entry
|
||||
// (owner, repo, number, base, head, head_sha, html_url, required, statuses, checks, labels) inside {pulls}.
|
||||
// openAnswer is the seat git's answer of open-pulls.
|
||||
func openAnswer(pulls ...map[string]any) []byte {
|
||||
b, _ := json.Marshal(map[string]any{"pulls": pulls})
|
||||
return b
|
||||
}
|
||||
|
||||
func openEntry(repo string, number int, base, branch, sha string) map[string]any {
|
||||
return map[string]any{"owner": "novox", "repo": repo, "number": number, "base": base, "head": branch, "head_sha": sha,
|
||||
"html_url": "https://forge.example/x", "required": []string{"mesh/merge-gate"}, "statuses": []string{}, "checks": map[string]string{}, "labels": []string{}}
|
||||
return map[string]any{"pull": map[string]any{"repository": "novox/" + repo, "number": number, "base": base, "branch": branch, "head": sha,
|
||||
"state": "open", "url": "https://forge.example/x"}, "required": []string{"mesh/check"}, "checks": map[string]any{}}
|
||||
}
|
||||
|
||||
func TestOpenPullsAreUpdatedPullMessagesWithUnknownDirs(t *testing.T) { // 1237/AC1
|
||||
@@ -355,7 +360,7 @@ func TestOpenPullsUnreadableEntryIsNamedAndTheRestGo(t *testing.T) { // 1237, 12
|
||||
if err != nil || len(msgs) != 1 || len(skipped) != 1 || !strings.Contains(skipped[0], "novox/a#1") {
|
||||
t.Errorf("msgs %d skipped %v err %v", len(msgs), skipped, err)
|
||||
}
|
||||
for _, bad := range []string{`{"pulls":null}`, `{}`, `[]`, `x`} {
|
||||
for _, bad := range []string{`{"pulls":null}`, `{}`, `[]`, `x`, string(gitFixture(t, "open-pulls answers an error, never none", "verb:open-pulls.answer"))} {
|
||||
if _, _, err := OpenPulls([]byte(bad)); err == nil {
|
||||
t.Errorf("%s was taken as an answer", bad)
|
||||
}
|
||||
@@ -381,7 +386,7 @@ func TestOpenMessageNamesEveryOpenPullIntoTheTrunk(t *testing.T) { // 1279/AC6
|
||||
if f, derr := forgemsg.DecodeForge(m.Data); err != nil || derr != nil || f.Kind != forgemsg.ForgeOpen || len(f.Open) != 0 {
|
||||
t.Errorf("empty answer: %+v %v %v", f, err, derr)
|
||||
}
|
||||
for _, bad := range []string{`{"pulls":null}`, `{}`, `x`} {
|
||||
for _, bad := range []string{`{"pulls":null}`, `{}`, `x`, `{"error":"the forge answered 502","pulls":[]}`} {
|
||||
if _, err := OpenMessage([]byte(bad)); err == nil {
|
||||
t.Errorf("%s was taken as an answer", bad)
|
||||
}
|
||||
@@ -411,41 +416,66 @@ func TestTrackSaysWhichHeadOfWhichPullRequestWasWritten(t *testing.T) { // 1279/
|
||||
}
|
||||
}
|
||||
|
||||
func TestBridgeTagsAnswersAPackagesTagsByPrefixPageByPage(t *testing.T) { // DLV-FB1, design 51 *The floor*
|
||||
var asked []string
|
||||
page := func(n int) json.RawMessage {
|
||||
var tags []map[string]string
|
||||
for i := 0; i < n; i++ {
|
||||
tags = append(tags, map[string]string{"name": "x" + strings.Repeat("y", i)})
|
||||
// gitFixture is one answer of the seat git's fixtures, by its name and what it is of.
|
||||
func gitFixture(t *testing.T, name, of string) json.RawMessage {
|
||||
t.Helper()
|
||||
return gitfixture.Given(t, name, of)
|
||||
}
|
||||
|
||||
func TestBridgeTagsAnswersAPackagesTagsByPrefixPageByPage(t *testing.T) { // DLV-FB1, design 51 *The floor*; design 47 *The seat git*
|
||||
more := gitFixture(t, "tags answers", "verb:tags.answer") // go/v0.1.15, go/v0.1.14, more
|
||||
failed := gitFixture(t, "tags answers an error, never none", "verb:tags.answer")
|
||||
last := json.RawMessage(`{"repository":"novox/mesh-sdk","tags":["go/v0.1.2","v0.0.1"],"page":2,"bound":50}`)
|
||||
type asked struct {
|
||||
key string
|
||||
in string
|
||||
}
|
||||
for _, row := range []struct {
|
||||
name string
|
||||
answer func(page int) (json.RawMessage, error)
|
||||
repo string
|
||||
tags []string
|
||||
more bool
|
||||
pages int
|
||||
failing string
|
||||
}{
|
||||
{name: "two pages, the second says no more", repo: "novox/mesh-sdk", tags: []string{"go/v0.1.14", "go/v0.1.15", "go/v0.1.2"}, pages: 2,
|
||||
answer: func(p int) (json.RawMessage, error) {
|
||||
if p == 1 {
|
||||
return more, nil
|
||||
}
|
||||
return last, nil
|
||||
}},
|
||||
{name: "every page says more: stopped at the bound", repo: "novox/mesh-sdk", more: true, pages: TagsMaxPages,
|
||||
answer: func(int) (json.RawMessage, error) { return json.RawMessage(`{"tags":[],"more":true}`), nil }},
|
||||
{name: "the seat does not answer", repo: "novox/mesh-sdk", failing: "page 1", pages: 1,
|
||||
answer: func(int) (json.RawMessage, error) { return nil, errors.New("down") }},
|
||||
{name: "an error answer is an error, never none", repo: "novox/mesh-sdk", failing: "404", pages: 1,
|
||||
answer: func(int) (json.RawMessage, error) { return failed, nil }},
|
||||
{name: "a repository with no owner", repo: "mesh-sdk", failing: "<owner>/<name>"},
|
||||
} {
|
||||
var calls []asked
|
||||
call := func(_ context.Context, key string, body any) (json.RawMessage, error) {
|
||||
in, _ := json.Marshal(body)
|
||||
calls = append(calls, asked{key, string(in)})
|
||||
var p struct{ Page int }
|
||||
_ = json.Unmarshal(in, &p)
|
||||
return row.answer(p.Page)
|
||||
}
|
||||
b, _ := json.Marshal(tags)
|
||||
return b
|
||||
}
|
||||
api := func(path string) (json.RawMessage, error) {
|
||||
asked = append(asked, path)
|
||||
if strings.Contains(path, "page=1&") {
|
||||
full := page(TagsPageSize - 2)
|
||||
var tags []map[string]string
|
||||
_ = json.Unmarshal(full, &tags)
|
||||
tags = append(tags, map[string]string{"name": "go/v0.1.13"}, map[string]string{"name": "v0.0.1"})
|
||||
b, _ := json.Marshal(map[string]any{"result": tags})
|
||||
return b, nil
|
||||
a, err := Tags(context.Background(), call, row.repo, "go/v")
|
||||
if row.failing != "" {
|
||||
if err == nil || !strings.Contains(err.Error(), row.failing) {
|
||||
t.Errorf("%s: %v, want an error naming %q", row.name, err, row.failing)
|
||||
}
|
||||
} else if err != nil || !slicesEqual(a.Tags, row.tags) || a.More != row.more {
|
||||
t.Errorf("%s: Tags = %+v, %v", row.name, a, err)
|
||||
}
|
||||
if len(calls) != row.pages {
|
||||
t.Errorf("%s: %d calls, want %d", row.name, len(calls), row.pages)
|
||||
}
|
||||
if len(calls) > 0 && (calls[0].key != "seat:git.tags" || calls[0].in != `{"page":1,"prefix":"go/v","repository":"novox/mesh-sdk"}`) {
|
||||
t.Errorf("%s: first call %+v", row.name, calls[0])
|
||||
}
|
||||
return json.RawMessage(`{"result":[{"name":"go/v0.1.2"}]}`), nil
|
||||
}
|
||||
a, err := Tags(api, "novox/mesh-sdk", "go/v")
|
||||
if err != nil || !slicesEqual(a.Tags, []string{"go/v0.1.13", "go/v0.1.2"}) || a.More || len(asked) != 2 ||
|
||||
asked[0] != "/repos/novox/mesh-sdk/tags?page=1&limit=50" {
|
||||
t.Errorf("Tags = %+v, %v; asked %v", a, err, asked)
|
||||
}
|
||||
if _, err := Tags(func(string) (json.RawMessage, error) { return nil, errors.New("down") }, "novox/mesh-sdk", ""); err == nil || !strings.Contains(err.Error(), "page 1") {
|
||||
t.Errorf("a forge not answering = %v, want an error naming the page", err)
|
||||
}
|
||||
if _, err := Tags(func(string) (json.RawMessage, error) { return json.RawMessage(`[{"name":"go/v0.1.2"}]`), nil }, "novox/mesh-sdk", ""); err == nil || !strings.Contains(err.Error(), `{"result"`) {
|
||||
t.Errorf("a bare list with no wrapper = %v, want the shape named", err)
|
||||
}
|
||||
if _, err := Tags(api, "mesh-sdk", ""); err == nil {
|
||||
t.Error("a repository with no owner answered")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,77 +3,106 @@ package bridge
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
git "git.novox.be/novox/mesh-delivery/contracts/go/seats/git"
|
||||
)
|
||||
|
||||
// Caller calls a module's tool by its manifest key (`<module>.<tool>`) with a JSON body and returns the tool's result:
|
||||
// the bridge's process calls through the bus as its own principal, its tools bundle through the runtime (stdio.Ask).
|
||||
// Caller calls a seat's verb by its manifest key (`seat:<seat>.<verb>`) with a JSON body and returns its answer: the
|
||||
// bridge's process calls through the bus as its own principal, its tools bundle through the runtime (stdio.Ask).
|
||||
type Caller func(ctx context.Context, key string, body any) (json.RawMessage, error)
|
||||
|
||||
// CommitTreesTool is the forge's tool for a commit's trees (mesh-catalog modules/gitea, issue 1234): arguments owner,
|
||||
// repo and sha, answer {root, trees}, the root directory spelled "" (1234/AC4).
|
||||
const CommitTreesTool = "gitea.gitea_commit_trees"
|
||||
// GitKey is the manifest key of a verb of the seat `git` (novox/hq design 47 *The seat `git`*): the bridge reaches the
|
||||
// forge through the seat alone and names no module that holds it.
|
||||
func GitKey(verb string) string { return "seat:" + git.Seat + "." + verb }
|
||||
|
||||
// TreesVia asks the forge for a commit's trees through call.
|
||||
// askGit calls one verb of the seat `git` and decodes its answer into A. An answer the contract cannot type, or one
|
||||
// that says an error, is the call's error: a failed read is never read as none (design 47 *A failed read answers an
|
||||
// error*).
|
||||
func askGit[A any](ctx context.Context, call Caller, verb string, in any) (A, error) {
|
||||
var a A
|
||||
raw, err := call(ctx, GitKey(verb), in)
|
||||
if err != nil {
|
||||
return a, fmt.Errorf("the seat git did not answer %s: %w", verb, err)
|
||||
}
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return a, fmt.Errorf("the seat git's answer of %s is not its contract's: %w", verb, err)
|
||||
}
|
||||
var said struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
_ = json.Unmarshal(raw, &said)
|
||||
if said.Error != "" {
|
||||
return a, fmt.Errorf("the seat git refused %s: %s", verb, said.Error)
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
// repository is `<owner>/<name>`, as the seat names a repository.
|
||||
func repository(owner, repo string) string { return owner + "/" + repo }
|
||||
|
||||
// TreesVia asks the seat `git` for a commit's trees (verb trees).
|
||||
func TreesVia(ctx context.Context, call Caller) Trees {
|
||||
return func(owner, repo, sha string) (string, map[string]string, error) {
|
||||
raw, err := call(ctx, CommitTreesTool, map[string]string{"owner": owner, "repo": repo, "sha": sha})
|
||||
a, err := askGit[git.TreesAnswer](ctx, call, git.VerbTrees, git.TreesInput{Repository: repository(owner, repo), Commit: sha})
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
var a struct {
|
||||
Root string `json:"root"`
|
||||
Trees map[string]string `json:"trees"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return "", nil, fmt.Errorf("the answer of %s is not {root, trees}: %w", CommitTreesTool, err)
|
||||
}
|
||||
if a.Root == "" {
|
||||
return "", nil, fmt.Errorf("the answer of %s names no root tree", CommitTreesTool)
|
||||
return "", nil, fmt.Errorf("the seat git's trees of %s at %s name no root tree", repository(owner, repo), sha)
|
||||
}
|
||||
return a.Root, a.Trees, nil
|
||||
}
|
||||
}
|
||||
|
||||
// GetFileTool is the forge's tool for one file at a ref (mesh-catalog modules/gitea tools/index.ts gitea_get_file):
|
||||
// arguments owner, repo, path and ref, answer {file: {path, ref, sha, size, content}}, the content decoded to text
|
||||
// (client.ts getFile). A path the forge does not have fails with `Gitea API <path>: 404 <body>` (client.ts request).
|
||||
const GetFileTool = "gitea.gitea_get_file"
|
||||
|
||||
// notFound is how the forge's error says 404 (client.ts request: `Gitea API ${path}: ${status} ${text}`; client.ts:670
|
||||
// recognises it as /: 404 /).
|
||||
var notFound = regexp.MustCompile(`: 404 `)
|
||||
|
||||
// FilesVia asks the forge for a file through call. A file the forge says is not there is ErrNotFound.
|
||||
// FilesVia asks the seat `git` for one file at a ref (verb file). A file the seat says is absent is ErrNotFound; an
|
||||
// answer that says neither absent nor content is an error, never an empty file.
|
||||
func FilesVia(ctx context.Context, call Caller) Files {
|
||||
return func(owner, repo, path, ref string) ([]byte, error) {
|
||||
raw, err := call(ctx, GetFileTool, map[string]string{"owner": owner, "repo": repo, "path": path, "ref": ref})
|
||||
raw, err := call(ctx, GitKey(git.VerbFile), git.FileInput{Repository: repository(owner, repo), Path: path, Ref: ref})
|
||||
if err != nil {
|
||||
if notFound.MatchString(err.Error()) {
|
||||
return nil, fmt.Errorf("%w: %v", ErrNotFound, err)
|
||||
}
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("the seat git did not answer file: %w", err)
|
||||
}
|
||||
var a struct {
|
||||
File *struct {
|
||||
Content *string `json:"content"`
|
||||
} `json:"file"`
|
||||
git.FileAnswer
|
||||
Content *string `json:"content"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &a); err != nil || a.File == nil || a.File.Content == nil {
|
||||
return nil, fmt.Errorf("the answer of %s is not {file: {content}}: %v", GetFileTool, err)
|
||||
if err := json.Unmarshal(raw, &a); err != nil {
|
||||
return nil, fmt.Errorf("the seat git's answer of file is not its contract's: %w", err)
|
||||
}
|
||||
return []byte(*a.File.Content), nil
|
||||
switch {
|
||||
case a.Error != "":
|
||||
return nil, fmt.Errorf("the seat git refused file %s at %s: %s", path, ref, a.Error)
|
||||
case a.Absent:
|
||||
return nil, fmt.Errorf("%w: %s at %s in %s", ErrNotFound, path, ref, repository(owner, repo))
|
||||
case a.Content == nil:
|
||||
return nil, fmt.Errorf("the seat git's file %s at %s says neither its content nor that it is absent", path, ref)
|
||||
}
|
||||
return []byte(*a.Content), nil
|
||||
}
|
||||
}
|
||||
|
||||
// errNoAnswer is a read whose answer carries nothing the contract says it must.
|
||||
var errNoAnswer = errors.New("the seat git's answer carries nothing")
|
||||
|
||||
// MergedMessage is the merged message of a merge into the trunk, asked again: built as the bridge builds the one it writes
|
||||
// on FORGE (it is that event's body through ConvertKept), from the pull request's owner, repository, number, branch, head
|
||||
// and merge commit, which the gate's merges state holds. It reads the forge for every module directory (no kept inputs).
|
||||
func MergedMessage(owner, repo string, number int, branch, head, merge string, trees Trees, files Files) (Message, error) {
|
||||
body, err := json.Marshal(pull{Owner: owner, Repo: repo, Number: number, Head: branch, HeadSHA: head, Base: Trunk, MergeSHA: merge})
|
||||
body, err := json.Marshal(git.PullMergedEvent{Repository: repository(owner, repo), Number: int64(number), Branch: branch, Head: head, Base: Trunk, MergeCommit: merge})
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
return ConvertKept(MergedSubject, body, trees, files, nil)
|
||||
}
|
||||
|
||||
// splitRepository is a `<owner>/<name>` cut in two, refusing anything else.
|
||||
func splitRepository(repository string) (string, string, error) {
|
||||
owner, name, ok := strings.Cut(strings.TrimSpace(repository), "/")
|
||||
if !ok || owner == "" || name == "" || strings.Contains(name, "/") {
|
||||
return "", "", fmt.Errorf("repository %q is not <owner>/<name>", repository)
|
||||
}
|
||||
return owner, name, nil
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package bridge
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
git "git.novox.be/novox/mesh-delivery/contracts/go/seats/git"
|
||||
)
|
||||
|
||||
// ForgePull is a pull request as the forge itself says it: what the bridge needs of a merge made on the forge, not
|
||||
@@ -16,51 +16,30 @@ type ForgePull struct {
|
||||
Merge string // the merge commit into the trunk
|
||||
}
|
||||
|
||||
// ForgeMade reads, through the forge module's gitea_api (the path the bridge reads the forge by), the merged pull
|
||||
// request whose merge commit is merge: by number when number is given (the pull request's merge commit must then equal
|
||||
// merge), else by the merge commit (the forge's commit-to-pull-request lookup). A pull request not merged, merged into
|
||||
// another branch than the trunk, or merged at another commit is refused.
|
||||
func ForgeMade(api API, owner, repo string, number int, merge string) (ForgePull, error) {
|
||||
path := fmt.Sprintf("/repos/%s/%s/commits/%s/pull", url.PathEscape(owner), url.PathEscape(repo), url.PathEscape(merge))
|
||||
// ForgeMade reads, through the seat `git`'s verb pull, the merged pull request whose merge commit is merge: by number
|
||||
// when number is given (its merge commit must then equal merge), else by the merge commit. A pull request not merged,
|
||||
// merged into another branch than the trunk, or merged at another commit is refused.
|
||||
func ForgeMade(ctx context.Context, call Caller, owner, repo string, number int, merge string) (ForgePull, error) {
|
||||
in := git.PullInput{Repository: repository(owner, repo), MergeCommit: merge}
|
||||
if number > 0 {
|
||||
path = fmt.Sprintf("/repos/%s/%s/pulls/%d", url.PathEscape(owner), url.PathEscape(repo), number)
|
||||
in = git.PullInput{Repository: repository(owner, repo), Number: int64(number)}
|
||||
}
|
||||
raw, err := api(path)
|
||||
a, err := askGit[git.PullAnswer](ctx, call, git.VerbPull, in)
|
||||
if err != nil {
|
||||
return ForgePull{}, fmt.Errorf("the forge did not answer %s: %w", path, err)
|
||||
}
|
||||
body, err := forgeBody(raw)
|
||||
if err != nil {
|
||||
return ForgePull{}, fmt.Errorf("the forge's answer for %s: %w", path, err)
|
||||
}
|
||||
var p struct {
|
||||
Number int `json:"number"`
|
||||
Merged bool `json:"merged"`
|
||||
MergeCommitSHA string `json:"merge_commit_sha"`
|
||||
Head struct {
|
||||
Label string `json:"label"`
|
||||
SHA string `json:"sha"`
|
||||
} `json:"head"`
|
||||
Base struct {
|
||||
Ref string `json:"ref"`
|
||||
} `json:"base"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &p); err != nil {
|
||||
return ForgePull{}, fmt.Errorf("the forge's answer for %s is no pull request: %w", path, err)
|
||||
return ForgePull{}, err
|
||||
}
|
||||
p := a.Pull
|
||||
switch {
|
||||
case p.Number == 0:
|
||||
return ForgePull{}, fmt.Errorf("the forge's answer for %s is no pull request (no number)", path)
|
||||
case !p.Merged:
|
||||
return ForgePull{}, fmt.Errorf("the forge says the pull request %s/%s#%d is not merged", owner, repo, p.Number)
|
||||
case p.MergeCommitSHA != merge:
|
||||
return ForgePull{}, fmt.Errorf("the forge says %s/%s#%d was merged at %s, not at %s", owner, repo, p.Number, p.MergeCommitSHA, merge)
|
||||
case p.Base.Ref != Trunk:
|
||||
return ForgePull{}, fmt.Errorf("the forge says %s/%s#%d was merged into %s, not the trunk %s", owner, repo, p.Number, p.Base.Ref, Trunk)
|
||||
case p == nil || p.Number == 0:
|
||||
return ForgePull{}, fmt.Errorf("the pull request of %s at %s: %w", in.Repository, merge, errNoAnswer)
|
||||
case p.State != git.PullStateMerged:
|
||||
return ForgePull{}, fmt.Errorf("the forge says the pull request %s#%d is %s, not merged", in.Repository, p.Number, p.State)
|
||||
case p.MergeCommit != merge:
|
||||
return ForgePull{}, fmt.Errorf("the forge says %s#%d was merged at %s, not at %s", in.Repository, p.Number, p.MergeCommit, merge)
|
||||
case p.Base != Trunk:
|
||||
return ForgePull{}, fmt.Errorf("the forge says %s#%d was merged into %s, not the trunk %s", in.Repository, p.Number, p.Base, Trunk)
|
||||
case p.Branch == "":
|
||||
return ForgePull{}, fmt.Errorf("the forge gives no head branch for %s#%d", in.Repository, p.Number)
|
||||
}
|
||||
// For a merged pull request the forge's head.ref is "refs/pull/N/head"; the branch is in head.label (same-repository pull requests).
|
||||
if p.Head.Label == "" || strings.HasPrefix(p.Head.Label, "refs/") {
|
||||
return ForgePull{}, fmt.Errorf("the forge gives no head branch for %s/%s#%d (label %q)", owner, repo, p.Number, p.Head.Label)
|
||||
}
|
||||
return ForgePull{Number: p.Number, Branch: p.Head.Label, Head: p.Head.SHA, Merge: p.MergeCommitSHA}, nil
|
||||
return ForgePull{Number: int(p.Number), Branch: p.Branch, Head: p.Head, Merge: p.MergeCommit}, nil
|
||||
}
|
||||
|
||||
@@ -1,39 +1,20 @@
|
||||
package bridge
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
git "git.novox.be/novox/mesh-delivery/contracts/go/seats/git"
|
||||
)
|
||||
|
||||
// TagsTool is the forge bridge's tool bridge_tags (novox/hq design 51 *The floor*): a package's git tags, which the
|
||||
// builder's floor reads as one of its three sources.
|
||||
const TagsTool = "bridge_tags"
|
||||
|
||||
// TagsPageSize is how many tags one page of the forge's tags answers; TagsMaxPages bounds the read.
|
||||
const (
|
||||
TagsPageSize = 50
|
||||
TagsMaxPages = 40
|
||||
)
|
||||
|
||||
// API asks the forge one GET of its API (the forge module's gitea_api), answering what the tool answers: the body
|
||||
// wrapped as {"result": <body>}. Every reader of an API answer takes the body through forgeBody, the one place that
|
||||
// knows that shape (issue 1966: two readers took the wrapper for the body).
|
||||
type API func(path string) (json.RawMessage, error)
|
||||
|
||||
// forgeBody is the body of the forge's answer: the tool's "result". An answer without one is refused, naming the shape,
|
||||
// never read as an empty body.
|
||||
func forgeBody(raw json.RawMessage) (json.RawMessage, error) {
|
||||
var w struct {
|
||||
Result json.RawMessage `json:"result"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &w); err != nil || len(w.Result) == 0 || string(w.Result) == "null" {
|
||||
return nil, fmt.Errorf(`the forge module's answer is not {"result": <body>}`)
|
||||
}
|
||||
return w.Result, nil
|
||||
}
|
||||
// TagsMaxPages bounds the read: at most this many pages of the seat `git`'s verb tags.
|
||||
const TagsMaxPages = 40
|
||||
|
||||
// TagsAnswer is what bridge_tags answers.
|
||||
type TagsAnswer struct {
|
||||
@@ -45,39 +26,30 @@ type TagsAnswer struct {
|
||||
}
|
||||
|
||||
// Tags is bridge_tags: every tag of repository (`<owner>/<name>`) whose name starts with prefix (`go/v` for the Go
|
||||
// package of mesh-sdk's `go` directory, `v` or empty for a package at the root), sorted. A forge that does not answer
|
||||
// is an error naming the page: the builder then counts this source silent.
|
||||
func Tags(api API, repository, prefix string) (TagsAnswer, error) {
|
||||
owner, name, ok := strings.Cut(strings.TrimSpace(repository), "/")
|
||||
if !ok || owner == "" || name == "" || strings.Contains(name, "/") {
|
||||
return TagsAnswer{}, fmt.Errorf("repository %q is not <owner>/<name>", repository)
|
||||
// package of mesh-sdk's `go` directory, `v` or empty for a package at the root), sorted, read page by page through the
|
||||
// seat `git`'s verb tags until a page says no more. A page the seat does not answer is an error naming it: the builder
|
||||
// then counts this source silent.
|
||||
func Tags(ctx context.Context, call Caller, repository, prefix string) (TagsAnswer, error) {
|
||||
owner, name, err := splitRepository(repository)
|
||||
if err != nil {
|
||||
return TagsAnswer{}, err
|
||||
}
|
||||
out := TagsAnswer{Repository: owner + "/" + name, Prefix: prefix, Tags: []string{}}
|
||||
for page := 1; ; page++ {
|
||||
for page := int64(1); ; page++ {
|
||||
if page > TagsMaxPages {
|
||||
out.More = true
|
||||
break
|
||||
}
|
||||
raw, err := api(fmt.Sprintf("/repos/%s/%s/tags?page=%d&limit=%d", url.PathEscape(owner), url.PathEscape(name), page, TagsPageSize))
|
||||
a, err := askGit[git.TagsAnswer](ctx, call, git.VerbTags, git.TagsInput{Repository: out.Repository, Prefix: prefix, Page: page})
|
||||
if err != nil {
|
||||
return TagsAnswer{}, fmt.Errorf("the forge did not answer the tags of %s (page %d): %w", out.Repository, page, err)
|
||||
return TagsAnswer{}, fmt.Errorf("the tags of %s (page %d): %w", out.Repository, page, err)
|
||||
}
|
||||
body, err := forgeBody(raw)
|
||||
if err != nil {
|
||||
return TagsAnswer{}, fmt.Errorf("the forge's tags of %s (page %d): %w", out.Repository, page, err)
|
||||
}
|
||||
var tags []struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &tags); err != nil {
|
||||
return TagsAnswer{}, fmt.Errorf("the forge's tags of %s (page %d) are no list of tags: %w", out.Repository, page, err)
|
||||
}
|
||||
for _, t := range tags {
|
||||
if strings.HasPrefix(t.Name, prefix) {
|
||||
out.Tags = append(out.Tags, t.Name)
|
||||
for _, t := range a.Tags {
|
||||
if strings.HasPrefix(t, prefix) {
|
||||
out.Tags = append(out.Tags, t)
|
||||
}
|
||||
}
|
||||
if len(tags) < TagsPageSize {
|
||||
if !a.More {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// Package gitfixture reads the seat git's own fixtures (contracts/seats/git/fixtures/verbs.json), so the bridge's unit
|
||||
// fakes answer what the seat's holder answers and nothing the contract does not say (design 47 *The seat `git`*).
|
||||
package gitfixture
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// All is every fixture's body named name of of (such as "file answers", "verb:file.answer"), in the file's order; a
|
||||
// name with none fails the test.
|
||||
func All(t testing.TB, name, of string) []json.RawMessage {
|
||||
t.Helper()
|
||||
_, here, _, _ := runtime.Caller(0)
|
||||
raw, err := os.ReadFile(filepath.Join(filepath.Dir(here), "../../../../contracts/seats/git/fixtures/verbs.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var all []struct {
|
||||
Name, Of string
|
||||
Given json.RawMessage
|
||||
}
|
||||
if err := json.Unmarshal(raw, &all); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out []json.RawMessage
|
||||
for _, f := range all {
|
||||
if f.Name == name && f.Of == of {
|
||||
out = append(out, f.Given)
|
||||
}
|
||||
}
|
||||
if len(out) == 0 {
|
||||
t.Fatalf("no fixture %q of %s", name, of)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Given is the first fixture's body named name of of.
|
||||
func Given(t testing.TB, name, of string) json.RawMessage { t.Helper(); return All(t, name, of)[0] }
|
||||
@@ -91,15 +91,16 @@ func TestManifestDeclarations(t *testing.T) { // 1235/AC1
|
||||
t.Errorf("streams = %+v", m.Streams)
|
||||
}
|
||||
// The checker's owed is the forge module's to write (ADR 0521, R4e finding R2-1): the bridge neither hears nor reads it.
|
||||
// gitea.forge.delete: a branch's end, said as a branch-deleted kind (design 51, DLV-FB1).
|
||||
if want := []string{"gitea.forge.delete", "gitea.pull.closed", "gitea.pull.merged", "gitea.pull.updated"}; !reflect.DeepEqual(sorted(m.Consumes), want) {
|
||||
// The forge's events are the seat git's (design 47 *The seat git*); forge.delete: a branch's end, said as a
|
||||
// branch-deleted kind (design 51, DLV-FB1).
|
||||
if want := []string{"git.forge-delete", "git.pull-closed", "git.pull-merged", "git.pull-updated"}; !reflect.DeepEqual(sorted(m.Consumes), want) {
|
||||
t.Errorf("consumes = %v, want %v", m.Consumes, want)
|
||||
}
|
||||
if want := []string{"mesh-delivery-gate.merges", "mesh-delivery-gate.owed"}; !reflect.DeepEqual(sorted(m.Reads), want) {
|
||||
t.Errorf("reads = %v, want %v", m.Reads, want)
|
||||
}
|
||||
if want := []string{"gitea.gitea_add_comment", "gitea.gitea_api", "gitea.gitea_commit_status", "gitea.gitea_commit_trees", "gitea.gitea_delivery_view",
|
||||
"gitea.gitea_get_file", "gitea.gitea_merge_pull_request", "gitea.gitea_note_append", "gitea.gitea_open_pulls"}; !reflect.DeepEqual(sorted(m.Invokes), want) {
|
||||
if want := []string{"gitea.gitea_add_comment", "gitea.gitea_commit_status", "gitea.gitea_delivery_view", "gitea.gitea_merge_pull_request",
|
||||
"gitea.gitea_note_append", "seat:git.file", "seat:git.open-pulls", "seat:git.pull", "seat:git.tags", "seat:git.trees"}; !reflect.DeepEqual(sorted(m.Invokes), want) {
|
||||
t.Errorf("invokes = %v, want %v", m.Invokes, want)
|
||||
}
|
||||
if b, ok := m.OwnSecrets["broker"]; !ok || len(m.OwnSecrets) != 1 || b != "${dir:state}/broker" {
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
"version": "1",
|
||||
"slug": "forge-bridge",
|
||||
"consumes": [
|
||||
"gitea.pull.updated",
|
||||
"gitea.pull.merged",
|
||||
"gitea.pull.closed",
|
||||
"gitea.forge.delete"
|
||||
"git.pull-updated",
|
||||
"git.pull-merged",
|
||||
"git.pull-closed",
|
||||
"git.forge-delete"
|
||||
],
|
||||
"streams": [
|
||||
{ "name": "forge", "events": ["forge.**"] }
|
||||
@@ -33,10 +33,11 @@
|
||||
"gitea.gitea_delivery_view",
|
||||
"gitea.gitea_note_append",
|
||||
"gitea.gitea_add_comment",
|
||||
"gitea.gitea_open_pulls",
|
||||
"gitea.gitea_commit_trees",
|
||||
"gitea.gitea_get_file",
|
||||
"gitea.gitea_api"
|
||||
"seat:git.open-pulls",
|
||||
"seat:git.trees",
|
||||
"seat:git.file",
|
||||
"seat:git.pull",
|
||||
"seat:git.tags"
|
||||
],
|
||||
"own-secrets": {
|
||||
"broker": "${dir:state}/broker"
|
||||
|
||||
Reference in New Issue
Block a user