From 508a0ad48caee289bc07806790e0d460321c19f3 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 22 Aug 2026 19:21:09 +1000 Subject: [PATCH] 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. --- cmd/archied/bootstrap.go | 1 + config.example.toml | 12 + internal/config/config.go | 19 ++ internal/daemon/daemon.go | 19 +- internal/domain/workintake/dispatch.go | 52 ++++ internal/domain/workintake/dispatch_test.go | 142 ++++++++++ internal/forge/webhook/receiver.go | 191 ++++++++++++++ internal/forge/webhook/receiver_test.go | 249 ++++++++++++++++++ .../infrastructure/configuration/defaults.go | 3 + .../configuration/defaults_test.go | 14 + .../infrastructure/configuration/validate.go | 32 +++ .../configuration/validate_test.go | 42 +++ 12 files changed, 770 insertions(+), 6 deletions(-) create mode 100644 internal/domain/workintake/dispatch.go create mode 100644 internal/domain/workintake/dispatch_test.go create mode 100644 internal/forge/webhook/receiver.go create mode 100644 internal/forge/webhook/receiver_test.go diff --git a/cmd/archied/bootstrap.go b/cmd/archied/bootstrap.go index 76dfdb3c4..e487c3e35 100644 --- a/cmd/archied/bootstrap.go +++ b/cmd/archied/bootstrap.go @@ -997,6 +997,7 @@ func (b *boot) buildDaemon() { // polling too. Config validation already requires webhook_secret to be a // configured reference before intake="webhook"/"both" is accepted; this // handles the reference resolving to nothing at runtime. +// // Single-identity only: the receiver has one dispatch config (root // Dispatch.Trigger/Label/BotUser), so a multi-identity deployment's // per-identity trigger/label/bot user cannot be matched correctly. Rather diff --git a/config.example.toml b/config.example.toml index f1a9d3753..b4a569f30 100644 --- a/config.example.toml +++ b/config.example.toml @@ -29,6 +29,18 @@ diff_cap_lines = 400 type = "github" host = "https://github.com" token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN" } +# How forge issues become work: +# "poll" – re-discover issues each poll_interval (default) +# "webhook" – react to GitHub webhook events the moment they arrive +# "both" – webhook plus the poll as a backstop +# intake = "poll" +# Required when intake is "webhook" or "both": the shared secret configured +# on the GitHub webhook, and the host:port the receiver listens on. On +# GitHub, set the webhook's Content type to application/json (the UI +# default) -- the alternate form-encoded delivery is not supported. +# Not supported alongside [[identities]] (each identity keeps polling). +# webhook_secret = { engine = "env", key = "ARCHIE_WEBHOOK_SECRET" } +# webhook_addr = "0.0.0.0:8645" [dispatch] # Can be of "assignee", "label", or "either" diff --git a/internal/config/config.go b/internal/config/config.go index 510494e7e..482e70041 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -188,12 +188,31 @@ type Agent struct { Env []string `toml:"env" yaml:"env"` } +// Forge intake modes for Forge.Intake. Poll is the default; webhook reacts to +// forge events as they arrive instead of (or in addition to) polling. +const ( + ForgeIntakePoll = "poll" + ForgeIntakeWebhook = "webhook" + ForgeIntakeBoth = "both" +) + // Forge configures the code forge integration. type Forge struct { Type string `toml:"type" yaml:"type"` Host string `toml:"host" yaml:"host"` Token secret.SecretRef `toml:"token" yaml:"token"` TokenEnv string `toml:"token_env" yaml:"token_env"` + // Intake selects how forge issues become work: "poll" (default), + // "webhook", or "both". Webhook intake reacts to forge events the moment + // they arrive instead of up to poll_interval later. + Intake string `toml:"intake" yaml:"intake"` + // WebhookSecret is the shared secret used to verify GitHub webhook + // signatures (X-Hub-Signature-256). Required when Intake is "webhook" or + // "both". + WebhookSecret secret.SecretRef `toml:"webhook_secret" yaml:"webhook_secret"` + // WebhookAddr is the host:port the webhook receiver listens on. Required + // when Intake is "webhook" or "both". + WebhookAddr string `toml:"webhook_addr" yaml:"webhook_addr"` } // Dispatch configures how archied discovers work and reflects task state diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index a7490f2ca..4ebfb721a 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -357,7 +357,9 @@ func (d *Daemon) maintainAndDrain(ctx context.Context) { func (d *Daemon) pollIssuesWithConfig(ctx context.Context, fg forge.Forge, cfg config.Config, repo config.Repo) []forge.Issue { switch cfg.Dispatch.Trigger { case "label": - if cfg.Label == "" { + // Empty-label rule shared with workintake.MatchesDispatch: a label + // trigger needs a non-empty label or it would match every open issue. + if workintake.RequiresLabel(cfg.Dispatch.Trigger) && cfg.Label == "" { d.Log.Error("label trigger configured with an empty label; refusing to poll (an empty label matches every open issue)", "repo", repo.FullName()) return nil } @@ -392,7 +394,7 @@ func (d *Daemon) pollEitherWithConfig(ctx context.Context, fg forge.Forge, cfg c out = append(out, is) } } - if cfg.Label == "" { + if workintake.RequiresLabel(cfg.Dispatch.Trigger) && cfg.Label == "" { d.Log.Error("either trigger configured with an empty label; skipping the label poll (an empty label matches every open issue)", "repo", repo.FullName()) return out } @@ -635,17 +637,22 @@ func (d *Daemon) pollNATS(ctx context.Context, fg forge.Forge, cfg config.Config Identity: identity, Kind: workintake.KindForLabels(parsed), } - if err := d.publishTask(ctx, task); err != nil { + if err := d.PublishTask(ctx, task); err != nil { d.Log.Error("task publish failed", "repo", repo.FullName(), "issue", is.Number, "err", err) return } d.acknowledge(ctx, fg, cfg, repo, is) } -// publishTask validates, encodes and publishes a task envelope. The +// PublishTask validates, encodes and publishes a task envelope. The // idempotency key means rediscovering the same issue on a later poll does not -// enqueue the work twice. -func (d *Daemon) publishTask(ctx context.Context, task workintake.TaskEnvelope) error { +// enqueue the work twice. It is the single enqueue path shared by the poller +// and the forge webhook intake, so neither can drift in how a discovered issue +// becomes a task. +func (d *Daemon) PublishTask(ctx context.Context, task workintake.TaskEnvelope) error { + if d.Tasks == nil { + return fmt.Errorf("publish task %s: no task bus configured (nats.mode is off)", task.Ref()) + } if err := task.Kind.Validate(); err != nil { return fmt.Errorf("publish task %s: %w", task.Ref(), err) } diff --git a/internal/domain/workintake/dispatch.go b/internal/domain/workintake/dispatch.go new file mode 100644 index 000000000..c4b1c992a --- /dev/null +++ b/internal/domain/workintake/dispatch.go @@ -0,0 +1,52 @@ +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) + }) +} diff --git a/internal/domain/workintake/dispatch_test.go b/internal/domain/workintake/dispatch_test.go new file mode 100644 index 000000000..92355bf9f --- /dev/null +++ b/internal/domain/workintake/dispatch_test.go @@ -0,0 +1,142 @@ +package workintake + +import ( + "strings" + "testing" +) + +func TestRequiresLabel(t *testing.T) { + tests := []struct { + trigger string + want bool + }{ + {trigger: "label", want: true}, + {trigger: "either", want: true}, + {trigger: "assignee", want: false}, + {trigger: "", want: false}, + {trigger: "unknown", want: false}, + } + for _, tc := range tests { + if got := RequiresLabel(tc.trigger); got != tc.want { + t.Errorf("RequiresLabel(%q) = %v, want %v", tc.trigger, got, tc.want) + } + } +} + +func TestMatchesDispatch(t *testing.T) { + const ( + label = "archie" + botUser = "archie-bot" + ) + tests := []struct { + name string + trigger string + cfgLabel string + labels []string + assignees []string + want bool + }{ + { + name: "label trigger matches the configured label", + trigger: "label", + cfgLabel: label, + labels: []string{"bug", label}, + want: true, + }, + { + name: "label trigger misses without the label", + trigger: "label", + cfgLabel: label, + labels: []string{"bug", "feature"}, + want: false, + }, + { + // 3209a1f: an empty configured label must match nothing, never + // every issue. + name: "label trigger with empty label never matches", + trigger: "label", + cfgLabel: "", + labels: []string{"bug"}, + want: false, + }, + { + name: "assignee trigger matches the bot", + trigger: "assignee", + assignees: []string{"someone", botUser}, + want: true, + }, + { + name: "assignee trigger misses without the bot", + trigger: "assignee", + assignees: []string{"someone-else"}, + want: false, + }, + { + name: "either matches on label alone", + trigger: "either", + cfgLabel: label, + labels: []string{label}, + assignees: []string{"someone-else"}, + want: true, + }, + { + name: "either matches on assignee alone", + trigger: "either", + cfgLabel: label, + labels: []string{"bug"}, + assignees: []string{botUser}, + want: true, + }, + { + name: "either misses on neither", + trigger: "either", + cfgLabel: label, + labels: []string{"bug"}, + assignees: []string{"someone-else"}, + want: false, + }, + { + name: "either with empty label falls back to assignee only", + trigger: "either", + cfgLabel: "", + labels: []string{"anything"}, + assignees: []string{botUser}, + want: true, + }, + { + name: "unknown trigger behaves as assignee", + trigger: "", + assignees: []string{botUser}, + want: true, + }, + { + // GitHub's AssignedIssues server-side filter matches assignee + // case-insensitively (usernames are unique regardless of case), so + // the webhook path's client-side comparison must agree, or a + // bot_user configured with different case than the login GitHub + // reports on the event silently never matches. + name: "assignee match is case-insensitive", + trigger: "assignee", + assignees: []string{"Archie-Bot"}, + want: true, + }, + { + // Labels, unlike usernames, are case-sensitive on GitHub: "Bug" + // and "bug" can coexist as distinct labels. + name: "label match is case-sensitive", + trigger: "label", + cfgLabel: label, + labels: []string{strings.ToUpper(label)}, + want: false, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + got := MatchesDispatch(tc.trigger, tc.cfgLabel, botUser, tc.labels, tc.assignees) + if got != tc.want { + t.Errorf("MatchesDispatch(%q, %q, %q, %v, %v) = %v, want %v", + tc.trigger, tc.cfgLabel, botUser, tc.labels, tc.assignees, got, tc.want) + } + }) + } +} diff --git a/internal/forge/webhook/receiver.go b/internal/forge/webhook/receiver.go new file mode 100644 index 000000000..02df1362e --- /dev/null +++ b/internal/forge/webhook/receiver.go @@ -0,0 +1,191 @@ +// Package webhook implements the GitHub forge webhook receiver: an HTTP +// endpoint that verifies a GitHub webhook's HMAC signature, decodes the +// "issues" event, and publishes a matched issue as a workintake.TaskEnvelope. +// +// It is forge intake, distinct from internal/channels/webhook (the chat +// channel): that package routes inbound text through the gateway for LLM +// processing; this one decodes a GitHub payload into the same TaskEnvelope +// the poller produces and hands it to the same publish path, so a labelled or +// assigned issue becomes work the moment it happens rather than up to +// poll_interval later (see docs/prds/event-sources-and-reactions.md). +// +// GitHub's webhook must be configured with "Content type: application/json" +// (the UI default). The alternate application/x-www-form-urlencoded delivery +// is not decoded here and is rejected as a bad payload. +package webhook + +import ( + "context" + "io" + "log/slog" + "net/http" + + "github.com/google/go-github/v78/github" + + "github.com/samcharles93/archie-core/internal/domain/workintake" + "github.com/samcharles93/archie-core/internal/webhookguard" +) + +// maxBodyBytes bounds a webhook payload before it is read. GitHub webhook +// bodies are small; the cap exists so a hostile sender cannot exhaust memory. +const maxBodyBytes = 1 << 20 // 1 MiB + +// PublishFunc delivers a decoded, dispatch-matched task envelope to the +// daemon's publish path. The composition root wires it to +// (*daemon.Daemon).PublishTask, the same enqueue path the poller uses. +type PublishFunc func(ctx context.Context, task workintake.TaskEnvelope) error + +// Receiver is the HTTP handler for forge webhooks. It verifies the signature, +// decodes the event, applies the shared dispatch predicate, and publishes +// matched issues. +type Receiver struct { + secret string + trigger string + label string + botUser string + publish PublishFunc + log *slog.Logger +} + +// New returns an unstarted Receiver. +func New(secret, trigger, label, botUser string, publish PublishFunc, log *slog.Logger) *Receiver { + if log == nil { + log = slog.New(slog.DiscardHandler) + } + return &Receiver{ + secret: secret, + trigger: trigger, + label: label, + botUser: botUser, + publish: publish, + log: log.With("component", "forge-webhook"), + } +} + +// ServeHTTP handles one forge webhook delivery. +func (r *Receiver) ServeHTTP(w http.ResponseWriter, req *http.Request) { + if req.Method != http.MethodPost { + http.Error(w, "method not allowed", http.StatusMethodNotAllowed) + return + } + + body, err := io.ReadAll(io.LimitReader(req.Body, maxBodyBytes)) + if err != nil { + r.log.Error("read body", "err", err) + http.Error(w, "read error", http.StatusBadRequest) + return + } + + // HMAC verification. An invalid or missing signature is rejected and + // logged, never silently accepted. + sig := req.Header.Get("X-Hub-Signature-256") + if !webhookguard.VerifyHMAC(body, sig, r.secret) { + r.log.Warn("invalid signature", "remote", req.RemoteAddr) + http.Error(w, "invalid signature", http.StatusUnauthorized) + return + } + + event, err := github.ParseWebHook(req.Header.Get("X-GitHub-Event"), body) + if err != nil { + r.log.Warn("parse webhook", "err", err) + http.Error(w, "bad payload", http.StatusBadRequest) + return + } + + issueEvent, ok := event.(*github.IssuesEvent) + if !ok { + // Not an issue event (ping, pull_request, etc.). Acknowledge and + // ignore -- this receiver only turns issue activity into work. + w.WriteHeader(http.StatusAccepted) + return + } + + task, ok := r.taskFromEvent(issueEvent) + if !ok { + // Delivered but not eligible work (PR, closed, unrelated action, + // dispatch mismatch). Acknowledge without publishing. + w.WriteHeader(http.StatusAccepted) + return + } + + if err := r.publish(req.Context(), task); err != nil { + r.log.Error("publish task", "task", task.Ref(), "err", err) + http.Error(w, "publish failed", http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusAccepted) +} + +// taskFromEvent decodes a GitHub issues event into a task envelope, or +// reports (zero, false) when the event is not eligible work. +func (r *Receiver) taskFromEvent(event *github.IssuesEvent) (workintake.TaskEnvelope, bool) { + issue := event.GetIssue() + if issue == nil || issue.IsPullRequest() { + return workintake.TaskEnvelope{}, false + } + // Only events that could newly make an issue eligible. unlabeled, + // unassigned, closed, and edited never queue work. + switch event.GetAction() { + case "opened", "reopened", "labeled", "assigned": + default: + return workintake.TaskEnvelope{}, false + } + if issue.GetState() != "open" { + return workintake.TaskEnvelope{}, false + } + + labels := labelNames(issue.Labels) + assignees := assigneeLogins(issue.Assignees, issue.Assignee, event.GetAssignee()) + if !workintake.MatchesDispatch(r.trigger, r.label, r.botUser, labels, assignees) { + return workintake.TaskEnvelope{}, false + } + + repo := event.GetRepo() + if repo == nil || repo.GetOwner().GetLogin() == "" || repo.GetName() == "" { + r.log.Warn("event missing repository", "action", event.GetAction()) + return workintake.TaskEnvelope{}, false + } + + return workintake.TaskEnvelope{ + Owner: repo.GetOwner().GetLogin(), + Repo: repo.GetName(), + Number: issue.GetNumber(), + Title: issue.GetTitle(), + Body: issue.GetBody(), + Labels: labels, + Kind: workintake.KindForLabels(labels), + }, true +} + +func labelNames(labels []*github.Label) []string { + var out []string + for _, l := range labels { + if name := l.GetName(); name != "" { + out = append(out, name) + } + } + return out +} + +func assigneeLogins(assignees []*github.User, extra ...*github.User) []string { + seen := map[string]bool{} + var out []string + add := func(u *github.User) { + if u == nil { + return + } + login := u.GetLogin() + if login == "" || seen[login] { + return + } + seen[login] = true + out = append(out, login) + } + for _, a := range assignees { + add(a) + } + for _, a := range extra { + add(a) + } + return out +} diff --git a/internal/forge/webhook/receiver_test.go b/internal/forge/webhook/receiver_test.go new file mode 100644 index 000000000..8a68863f6 --- /dev/null +++ b/internal/forge/webhook/receiver_test.go @@ -0,0 +1,249 @@ +package webhook + +import ( + "context" + "crypto/hmac" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "net/http" + "net/http/httptest" + "slices" + "strings" + "testing" + + "github.com/google/go-github/v78/github" + + "github.com/samcharles93/archie-core/internal/domain/workintake" +) + +const ( + testSecret = "test-secret" + testBot = "archie-bot" + testLabel = "archie" +) + +func issueEvent(t *testing.T, action string, mutate func(*github.IssuesEvent)) *github.IssuesEvent { + t.Helper() + ev := &github.IssuesEvent{ + Action: new(action), + Issue: &github.Issue{ + Number: new(42), + Title: new("Do the thing"), + Body: new("please"), + State: new("open"), + }, + Repo: &github.Repository{ + Name: new("repo"), + Owner: &github.User{Login: new("owner")}, + }, + } + if mutate != nil { + mutate(ev) + } + return ev +} + +func signedRequest(t *testing.T, secret, eventType string, payload []byte) *http.Request { + t.Helper() + req := httptest.NewRequestWithContext(t.Context(), http.MethodPost, "/webhook", strings.NewReader(string(payload))) + req.Header.Set("X-GitHub-Event", eventType) + if secret != "" { + mac := hmac.New(sha256.New, []byte(secret)) + mac.Write(payload) + req.Header.Set("X-Hub-Signature-256", "sha256="+hex.EncodeToString(mac.Sum(nil))) + } + return req +} + +func serve(t *testing.T, r *Receiver, req *http.Request) (*httptest.ResponseRecorder, []workintake.TaskEnvelope) { + t.Helper() + var published []workintake.TaskEnvelope + r.publish = func(_ context.Context, task workintake.TaskEnvelope) error { + published = append(published, task) + return nil + } + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + return rec, published +} + +func TestReceiverPublishesMatchingIssue(t *testing.T) { + tests := []struct { + name string + trigger string + cfgLabel string + eventType string + action string + secret string + mutate func(*github.IssuesEvent) + wantStatus int + wantPubl int + }{ + { + name: "labelled issue matches label trigger", + trigger: "label", + cfgLabel: testLabel, + eventType: "issues", + action: "labeled", + secret: testSecret, + mutate: func(ev *github.IssuesEvent) { ev.Issue.Labels = []*github.Label{{Name: new(testLabel)}} }, + wantStatus: http.StatusAccepted, + wantPubl: 1, + }, + { + name: "assigned issue matches assignee trigger", + trigger: "assignee", + eventType: "issues", + action: "assigned", + secret: testSecret, + mutate: func(ev *github.IssuesEvent) { ev.Issue.Assignees = []*github.User{{Login: new(testBot)}} }, + wantStatus: http.StatusAccepted, + wantPubl: 1, + }, + { + name: "opened issue with label matches label trigger", + trigger: "label", + cfgLabel: testLabel, + eventType: "issues", + action: "opened", + secret: testSecret, + mutate: func(ev *github.IssuesEvent) { ev.Issue.Labels = []*github.Label{{Name: new(testLabel)}} }, + wantStatus: http.StatusAccepted, + wantPubl: 1, + }, + { + name: "wrong label does not match", + trigger: "label", + cfgLabel: testLabel, + eventType: "issues", + action: "labeled", + secret: testSecret, + mutate: func(ev *github.IssuesEvent) { ev.Issue.Labels = []*github.Label{{Name: new("other")}} }, + wantStatus: http.StatusAccepted, + wantPubl: 0, + }, + { + name: "invalid signature is rejected", + trigger: "label", + cfgLabel: testLabel, + eventType: "issues", + action: "labeled", + secret: "wrong-secret", + mutate: func(ev *github.IssuesEvent) { ev.Issue.Labels = []*github.Label{{Name: new(testLabel)}} }, + wantStatus: http.StatusUnauthorized, + wantPubl: 0, + }, + { + name: "missing signature is rejected", + trigger: "label", + cfgLabel: testLabel, + eventType: "issues", + action: "labeled", + secret: "", + mutate: func(ev *github.IssuesEvent) { ev.Issue.Labels = []*github.Label{{Name: new(testLabel)}} }, + wantStatus: http.StatusUnauthorized, + wantPubl: 0, + }, + { + name: "non-issue event is acknowledged and ignored", + trigger: "label", + cfgLabel: testLabel, + eventType: "ping", + action: "", + secret: testSecret, + wantStatus: http.StatusAccepted, + wantPubl: 0, + }, + { + name: "pull request is ignored", + trigger: "label", + cfgLabel: testLabel, + eventType: "issues", + action: "labeled", + secret: testSecret, + mutate: func(ev *github.IssuesEvent) { + ev.Issue.Labels = []*github.Label{{Name: new(testLabel)}} + ev.Issue.PullRequestLinks = &github.PullRequestLinks{URL: new("https://api.github.com/repos/o/r/pulls/1")} + }, + wantStatus: http.StatusAccepted, + wantPubl: 0, + }, + { + name: "closed issue is ignored", + trigger: "label", + cfgLabel: testLabel, + eventType: "issues", + action: "labeled", + secret: testSecret, + mutate: func(ev *github.IssuesEvent) { + ev.Issue.State = new("closed") + ev.Issue.Labels = []*github.Label{{Name: new(testLabel)}} + }, + wantStatus: http.StatusAccepted, + wantPubl: 0, + }, + { + name: "unlabeled action never queues", + trigger: "label", + cfgLabel: testLabel, + eventType: "issues", + action: "unlabeled", + secret: testSecret, + mutate: func(ev *github.IssuesEvent) { ev.Issue.Labels = []*github.Label{{Name: new(testLabel)}} }, + wantStatus: http.StatusAccepted, + wantPubl: 0, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + ev := issueEvent(t, tc.action, tc.mutate) + payload, err := json.Marshal(ev) + if err != nil { + t.Fatalf("marshal event: %v", err) + } + // The receiver always trusts testSecret; tc.secret is what the + // request is signed with, so a case can sign with the wrong (or no) + // secret to exercise rejection. + r := New(testSecret, tc.trigger, tc.cfgLabel, testBot, nil, nil) + rec, published := serve(t, r, signedRequest(t, tc.secret, tc.eventType, payload)) + + if rec.Code != tc.wantStatus { + t.Errorf("status = %d, want %d", rec.Code, tc.wantStatus) + } + if len(published) != tc.wantPubl { + t.Errorf("published %d envelopes, want %d", len(published), tc.wantPubl) + } + }) + } +} + +func TestPublishedEnvelopeCarriesIssueIdentityAndKind(t *testing.T) { + ev := issueEvent(t, "labeled", func(ev *github.IssuesEvent) { + ev.Issue.Labels = []*github.Label{{Name: new(testLabel)}, {Name: new("bug")}} + }) + payload, err := json.Marshal(ev) + if err != nil { + t.Fatalf("marshal event: %v", err) + } + r := New(testSecret, "label", testLabel, testBot, nil, nil) + _, published := serve(t, r, signedRequest(t, testSecret, "issues", payload)) + + if len(published) != 1 { + t.Fatalf("published = %d, want 1", len(published)) + } + task := published[0] + if task.Owner != "owner" || task.Repo != "repo" || task.Number != 42 { + t.Errorf("envelope identity = %s/%s#%d, want owner/repo#42", task.Owner, task.Repo, task.Number) + } + if task.Kind != workintake.KindBug { + t.Errorf("envelope kind = %q, want %q", task.Kind, workintake.KindBug) + } + if !slices.Contains(task.Labels, testLabel) || !slices.Contains(task.Labels, "bug") { + t.Errorf("envelope labels = %v, want to contain %q and %q", task.Labels, testLabel, "bug") + } + if task.IdempotencyKey() != "archie:owner/repo/42" { + t.Errorf("idempotency key = %q, want archie:owner/repo/42", task.IdempotencyKey()) + } +} diff --git a/internal/infrastructure/configuration/defaults.go b/internal/infrastructure/configuration/defaults.go index 71f9841b3..61b131a7e 100644 --- a/internal/infrastructure/configuration/defaults.go +++ b/internal/infrastructure/configuration/defaults.go @@ -190,6 +190,9 @@ func applyForgeDefaults(cfg *config.Config) { if cfg.Forge.Token.Engine == "" && cfg.Forge.Token.Key == "" && cfg.Forge.TokenEnv != "" { cfg.Forge.Token = secret.SecretRef{Engine: "env", Key: cfg.Forge.TokenEnv} } + if cfg.Forge.Intake == "" { + cfg.Forge.Intake = config.ForgeIntakePoll + } } // applyNATSDefaults resolves the nats mode from URL when the operator left it diff --git a/internal/infrastructure/configuration/defaults_test.go b/internal/infrastructure/configuration/defaults_test.go index 76fd07a8e..c45252498 100644 --- a/internal/infrastructure/configuration/defaults_test.go +++ b/internal/infrastructure/configuration/defaults_test.go @@ -31,6 +31,20 @@ func TestApplyNATSDefaultsResolvesMode(t *testing.T) { } } +func TestApplyForgeDefaultsIntake(t *testing.T) { + cfg := &config.Config{} + applyForgeDefaults(cfg) + if cfg.Forge.Intake != config.ForgeIntakePoll { + t.Errorf("applyForgeDefaults() intake = %q, want %q", cfg.Forge.Intake, config.ForgeIntakePoll) + } + + explicit := &config.Config{Forge: config.Forge{Intake: config.ForgeIntakeWebhook}} + applyForgeDefaults(explicit) + if explicit.Forge.Intake != config.ForgeIntakeWebhook { + t.Errorf("applyForgeDefaults() intake = %q, want unchanged %q", explicit.Forge.Intake, config.ForgeIntakeWebhook) + } +} + func TestApplyCaptureDefaultsFillsZeroValues(t *testing.T) { cfg := &config.Config{} applyCaptureDefaults(cfg) diff --git a/internal/infrastructure/configuration/validate.go b/internal/infrastructure/configuration/validate.go index 09ef922cf..58d2ca431 100644 --- a/internal/infrastructure/configuration/validate.go +++ b/internal/infrastructure/configuration/validate.go @@ -34,6 +34,7 @@ var ( forgeTypes = []string{forgeTypeGitHub, forgeTypeGitea, forgeTypeNone, forgeTypeOff, forgeTypeDisabled} dispatchTriggers = []string{dispatchTriggerAssignee, dispatchTriggerLabel, dispatchTriggerEither} natsModes = []string{config.NATSModeEmbedded, config.NATSModeExternal, config.NATSModeOff} + forgeIntakes = []string{config.ForgeIntakePoll, config.ForgeIntakeWebhook, config.ForgeIntakeBoth} ) // Validate runs the same checks Loader applies before accepting a config, @@ -63,6 +64,9 @@ func validate(cfg *config.Config) error { if err := validateDispatch(cfg); err != nil { return err } + if err := validateForgeIntake(cfg); err != nil { + return err + } if err := validateProviders(cfg.Providers); err != nil { return err } @@ -155,6 +159,34 @@ func validateDispatch(cfg *config.Config) error { return nil } +// validateForgeIntake checks the forge intake mode and that webhook intake has +// the secret and listen address it needs. An unset intake resolves to "poll" +// without mutating cfg, so Validate (which does not apply defaults) accepts a +// hand-built config the same way Loader.File accepts its on-disk form. +func validateForgeIntake(cfg *config.Config) error { + intake := cfg.Forge.Intake + if intake == "" { + intake = config.ForgeIntakePoll + } + if !oneOf(intake, forgeIntakes) { + return fmt.Errorf("%w: forge.intake %q (want %s)", ErrInvalidInput, cfg.Forge.Intake, list(forgeIntakes)) + } + if intake == config.ForgeIntakeWebhook || intake == config.ForgeIntakeBoth { + if cfg.Forge.WebhookSecret == (secret.SecretRef{}) { + return fmt.Errorf("%w: forge.webhook_secret is required when forge.intake is %q", ErrInvalidInput, intake) + } + if cfg.Forge.WebhookAddr == "" { + return fmt.Errorf("%w: forge.webhook_addr is required when forge.intake is %q", ErrInvalidInput, intake) + } + // Webhook intake publishes to the task queue, which is NATS (embedded + // or external). nats.mode="off" leaves no queue to publish to. + if effectiveNATSMode(cfg) == config.NATSModeOff { + return fmt.Errorf("%w: forge.intake %q requires NATS (nats.mode must not be %q)", ErrInvalidInput, intake, config.NATSModeOff) + } + } + return nil +} + // validateProviders rejects base URLs carrying credentials or query state. // A provider URL is used to build API requests, so userinfo or a stray query // string would be silently attached to every call. diff --git a/internal/infrastructure/configuration/validate_test.go b/internal/infrastructure/configuration/validate_test.go index ecae72caa..5b0d413e2 100644 --- a/internal/infrastructure/configuration/validate_test.go +++ b/internal/infrastructure/configuration/validate_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/secret" ) // A minimal config that satisfies every check validate applies without @@ -260,6 +261,47 @@ func TestValidateContainersResolvesNATSMode(t *testing.T) { }) } +// TestValidateForgeIntake pins the forge.intake contract: an unset intake +// resolves to poll without mutating cfg, and webhook/both require a webhook +// secret and listen address. +func TestValidateForgeIntake(t *testing.T) { + tests := []struct { + name string + intake string + secret secret.SecretRef + addr string + natsMode string + wantErr bool + }{ + {name: "unset resolves poll"}, + {name: "poll is valid", intake: config.ForgeIntakePoll}, + {name: "webhook requires secret", intake: config.ForgeIntakeWebhook, addr: "0.0.0.0:8645", wantErr: true}, + {name: "webhook requires addr", intake: config.ForgeIntakeWebhook, secret: secret.SecretRef{Engine: "env", Key: "SECRET"}, wantErr: true}, + {name: "webhook with secret and addr is valid", intake: config.ForgeIntakeWebhook, secret: secret.SecretRef{Engine: "env", Key: "SECRET"}, addr: "0.0.0.0:8645"}, + {name: "both with secret and addr is valid", intake: config.ForgeIntakeBoth, secret: secret.SecretRef{Engine: "env", Key: "SECRET"}, addr: "0.0.0.0:8645"}, + {name: "webhook with nats off is rejected", intake: config.ForgeIntakeWebhook, secret: secret.SecretRef{Engine: "env", Key: "SECRET"}, addr: "0.0.0.0:8645", natsMode: config.NATSModeOff, wantErr: true}, + {name: "unknown intake", intake: "not-a-mode", wantErr: true}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + cfg := minimalValidConfig() + cfg.Forge.Intake = tc.intake + cfg.Forge.WebhookSecret = tc.secret + cfg.Forge.WebhookAddr = tc.addr + if tc.natsMode != "" { + cfg.NATS.Mode = tc.natsMode + } + err := validateForgeIntake(&cfg) + if tc.wantErr && err == nil { + t.Fatal("validateForgeIntake() = nil, want an error") + } + if !tc.wantErr && err != nil { + t.Fatalf("validateForgeIntake() = %v, want nil", err) + } + }) + } +} + // Validate must run the exact same checks Loader.File applies internally -- // this pins that a config Validate accepts on its own, once the same // defaults Loader.File would apply are filled in by hand, is also accepted