feat(daemon): withdrawing an issue declines its queued or running work

This commit is contained in:
sam committed 2026-10-05 20:50:57 +11:00
1 parent 4295bf6e46
commit 34077cb4ca
7 files changed
+178 -48

No files matched your search

+32 -2
View File
@@ -28,6 +28,9 @@ const maxBodyBytes = 1 << 20 // 1 MiB
// (*daemon.Daemon).PublishTask, the same enqueue path the poller uses.
type PublishFunc func(ctx context.Context, task workintake.TaskEnvelope) error
// WithdrawFunc declines the work an issue no longer asks for.
type WithdrawFunc func(ctx context.Context, owner, repo string, number int, reason string) error
// ReactionPublishFunc publishes one review reaction.
type ReactionPublishFunc func(ctx context.Context, reaction workintake.ReviewCommentEnvelope) error
@@ -52,6 +55,7 @@ type Receiver struct {
botUser string
publish PublishFunc
reactionPublish ReactionPublishFunc
withdraw WithdrawFunc
log *slog.Logger
mu sync.Mutex
@@ -71,7 +75,7 @@ type reactionWindow struct {
}
// New returns an unstarted Receiver.
func New(secret string, parser forge.WebhookParser, trigger, label, botUser string, publish PublishFunc, reactionPublish ReactionPublishFunc, log *slog.Logger) *Receiver {
func New(secret string, parser forge.WebhookParser, trigger, label, botUser string, publish PublishFunc, reactionPublish ReactionPublishFunc, withdraw WithdrawFunc, log *slog.Logger) *Receiver {
if log == nil {
log = slog.New(slog.DiscardHandler)
}
@@ -83,6 +87,7 @@ func New(secret string, parser forge.WebhookParser, trigger, label, botUser stri
botUser: botUser,
publish: publish,
reactionPublish: reactionPublish,
withdraw: withdraw,
reactionLimiter: map[string]*reactionWindow{},
log: log.With("component", "forge-webhook"),
startedAt: time.Now(),
@@ -211,7 +216,15 @@ func (r *Receiver) serveIssueEvent(w http.ResponseWriter, req *http.Request, iss
task, ok := r.taskFromEvent(issueEvent)
if !ok {
// Delivered but not eligible work (PR, closed, unrelated action,
// dispatch mismatch). Acknowledge without publishing.
// dispatch mismatch). A withdrawal declines the work it queued;
// anything else is acknowledged without publishing.
if reason, withdrawn := r.withdrawal(issueEvent); withdrawn && r.withdraw != nil {
if err := r.withdraw(req.Context(), issueEvent.Owner, issueEvent.Repo, issueEvent.Number, reason); err != nil {
r.log.Error("withdraw task", "repo", issueEvent.Owner+"/"+issueEvent.Repo, "issue", issueEvent.Number, "err", err)
http.Error(w, "withdraw failed", http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusAccepted)
return
}
@@ -321,6 +334,23 @@ func (r *Receiver) allowReactionDelivery(remote string) bool {
return w.count <= reactionRateLimit
}
// withdrawal reports whether an issues event takes an issue out of dispatch:
// it was closed, or lost the label or assignee that made it work.
func (r *Receiver) withdrawal(event *forge.IssueEvent) (string, bool) {
if event.PullRequest || event.Owner == "" || event.Repo == "" {
return "", false
}
switch event.Action {
case "closed":
return "the issue was closed", true
case "unlabeled", "unassigned":
if !workintake.MatchesDispatch(r.trigger, r.label, r.botUser, event.Labels, event.Assignees) {
return "the issue was " + event.Action, true
}
}
return "", false
}
// taskFromEvent reports the task an issues event makes eligible, or
// (zero, false) when it is not eligible work.
func (r *Receiver) taskFromEvent(event *forge.IssueEvent) (workintake.TaskEnvelope, bool) {
+34
View File
@@ -0,0 +1,34 @@
package webhook
import (
"testing"
"github.com/samcharles93/archie-core/internal/forge"
)
// An issue leaves dispatch when it is closed or loses what made it work; an
// unlabel or unassign that still leaves it eligible withdraws nothing.
func TestWithdrawal(t *testing.T) {
r := New("", nil, "either", "archie", "bot", nil, nil, nil, nil)
tests := []struct {
name string
event forge.IssueEvent
want bool
}{
{"closed", forge.IssueEvent{Action: "closed", State: "closed"}, true},
{"label removed, still assigned", forge.IssueEvent{Action: "unlabeled", Assignees: []string{"bot"}}, false},
{"label removed, nothing left", forge.IssueEvent{Action: "unlabeled"}, true},
{"unassigned, still labelled", forge.IssueEvent{Action: "unassigned", Labels: []string{"archie"}}, false},
{"unassigned, nothing left", forge.IssueEvent{Action: "unassigned"}, true},
{"edited", forge.IssueEvent{Action: "edited"}, false},
{"a pull request closing", forge.IssueEvent{Action: "closed", PullRequest: true}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.event.Owner, tt.event.Repo = "acme", "widget"
if _, got := r.withdrawal(&tt.event); got != tt.want {
t.Fatalf("withdrawal = %v, want %v", got, tt.want)
}
})
}
}