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.
This commit is contained in:
sam committed 2026-08-22 19:21:09 +10:00
1 parent aba035a396
commit 508a0ad48c
12 files changed
+770 -6

No files matched your search

@@ -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
@@ -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)
@@ -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.
@@ -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