Files
archie/internal/domain/workintake/dispatch.go
T
sam 508a0ad48c feat(forge): GitHub webhook receiver for immediate issue dispatch
Add [forge] intake = "poll" (default) | "webhook" | "both", and a forge
webhook receiver (internal/forge/webhook) that verifies a GitHub webhook's
HMAC signature, decodes the "issues" event, and publishes a matched issue
through the same path the poller uses -- so a labelled or assigned issue
starts a task the moment it happens instead of waiting up to poll_interval
(bead archie-core-7d5u.4).

The dispatch predicate (workintake.MatchesDispatch/RequiresLabel) is now a
single shared definition consumed by both the poller and the webhook
receiver, so the two cannot drift -- including the empty-label guard from
63d0446 (an empty label must never match every open issue). Assignee
matching is case-insensitive to agree with AssignedIssues' server-side
GitHub filter; label matching stays case-sensitive, since GitHub labels
are case-distinct.

daemon.publishTask is exported as PublishTask, the single enqueue path
now shared by the poller and the webhook intake.

Two failure modes fixed during adversarial self-review before this landed:
a webhook secret that fails to resolve at runtime now degrades (logs and
skips the receiver) instead of aborting the whole daemon -- under
intake="both" the poll exists specifically as a backstop for this exact
misconfiguration, so crashing boot over it defeated its own purpose;
multi-identity deployments ([[identities]]) are explicitly unsupported for
now (webhook intake refuses to start, logs why, and leaves each identity's
own poll loop unaffected) rather than silently matching issues against the
wrong identity's dispatch rules.
2026-08-22 19:21:09 +10:00

53 lines
2.2 KiB
Go

package workintake
// This file is the single definition of what a dispatch trigger means: which
// triggers match on a label, and whether a concrete issue (its labels and
// assignees) is eligible work under a trigger. The daemon's poller and the
// forge webhook intake both consult it, so the two cannot drift -- the
// empty-label guard here is the one that previously regressed (3209a1f: an
// empty label matches every open issue via the forge API).
import (
"slices"
"strings"
)
// RequiresLabel reports whether the dispatch trigger matches on a label and
// therefore needs a non-empty label to be meaningful.
func RequiresLabel(trigger string) bool {
return trigger == "label" || trigger == "either"
}
// MatchesDispatch reports whether an issue is eligible work under the dispatch
// trigger. labels and assignees are the issue's current labels and assignee
// logins. An empty label never matches, even when the trigger is "label":
// a missing label must not be mistaken for "every issue" (3209a1f).
func MatchesDispatch(trigger, label, botUser string, labels, assignees []string) bool {
switch trigger {
case "label":
return label != "" && containsLabel(labels, label)
case "either":
return containsAssignee(assignees, botUser) || (label != "" && containsLabel(labels, label))
default: // "assignee"
return containsAssignee(assignees, botUser)
}
}
// containsLabel matches label names exactly: GitHub label names are
// case-sensitive and "Bug" and "bug" can coexist as distinct labels, matching
// IssuesWithLabel's exact-match server-side filter.
func containsLabel(labels []string, needle string) bool {
return slices.Contains(labels, needle)
}
// containsAssignee matches logins case-insensitively: GitHub usernames are
// unique case-insensitively, and AssignedIssues' server-side assignee filter
// matches the same way -- an exact-case comparison here would silently miss
// what the poller finds whenever the configured bot_user's case differs from
// the login GitHub reports on the event.
func containsAssignee(assignees []string, botUser string) bool {
return slices.ContainsFunc(assignees, func(a string) bool {
return strings.EqualFold(a, botUser)
})
}