62 lines
2.5 KiB
Go
62 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
// Issue 1107: a pull request closed unmerged in a quiet repository is heard from the forge's own event
|
|
// (gitea.forge.pull-request.closed), not only from the poll, which skips a repository whose update time did not move.
|
|
// Its delivery leaves its group and stops; a merged one is ignored (the webhook says merged for it); a close heard
|
|
// twice, from the event and the poll, changes nothing the second time.
|
|
func forgeClosed(owner, repo string, number int, merged bool) map[string]any {
|
|
return map[string]any{"owner": owner, "repo": repo, "number": number, "merged": merged}
|
|
}
|
|
|
|
func TestAForgeClosedEventTakesAGroupMembersDeliveryOutOfItsGroupAndStopsIt(t *testing.T) {
|
|
w := newWorld(t)
|
|
w.h.PullUpdated(pr(gCtl, 1, gHc, "feat/g", "internal/a.go"))
|
|
w.h.PullUpdated(pr(gCat, 2, gHk, "feat/g", "modules/app/module.json"))
|
|
id := IDOf(gCtl, gHc)
|
|
hear(t, w, "gitea.forge.pull-request.closed", forgeClosed("novox", "mesh-controller", 1, false))
|
|
d := w.delivery(id)
|
|
if !d.ClosedUnmerged || !d.State.Final() || inGroup(w, "feat/g", id) {
|
|
t.Fatalf("closed %v, state %s, in its group %v; wanted closed, stopped, out of its group", d.ClosedUnmerged, d.State,
|
|
inGroup(w, "feat/g", id))
|
|
}
|
|
// Heard again, from the event and from the poll: nothing changes.
|
|
before, _ := json.Marshal(d)
|
|
hear(t, w, "gitea.forge.pull-request.closed", forgeClosed("novox", "mesh-controller", 1, false))
|
|
hear(t, w, "gitea.pull.closed", map[string]any{"owner": "novox", "repo": "mesh-controller", "number": 1})
|
|
if after, _ := json.Marshal(w.delivery(id)); string(after) != string(before) {
|
|
t.Fatalf("a second close changed it:\n%s\n%s", before, after)
|
|
}
|
|
}
|
|
|
|
func TestAForgeClosedEventOfAMergedPullRequestIsIgnored(t *testing.T) {
|
|
w := newWorld(t)
|
|
w.h.PullUpdated(pr(gCtl, 1, gHc, "feat/g", "internal/a.go"))
|
|
w.h.PullUpdated(pr(gCat, 2, gHk, "feat/g", "modules/app/module.json"))
|
|
id := IDOf(gCtl, gHc)
|
|
hear(t, w, "gitea.forge.pull-request.closed", forgeClosed("novox", "mesh-controller", 1, true))
|
|
if d := w.delivery(id); d.ClosedUnmerged || !inGroup(w, "feat/g", id) || d.State.Final() {
|
|
t.Fatalf("a merged pull request's close stopped it: closed %v, %s", d.ClosedUnmerged, d.State)
|
|
}
|
|
}
|
|
|
|
// inGroup is whether the group still lists the delivery as a member.
|
|
func inGroup(w *world, group, id string) bool {
|
|
w.h.mu.Lock()
|
|
defer w.h.mu.Unlock()
|
|
g := w.h.groups[group]
|
|
if g == nil {
|
|
return false
|
|
}
|
|
for _, m := range g.Members {
|
|
if m == id {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|