diff --git a/internal/daemon/binding_target.go b/internal/daemon/binding_target.go index 9f7fe117f..7d26bfc3b 100644 --- a/internal/daemon/binding_target.go +++ b/internal/daemon/binding_target.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/samcharles93/archie-core/internal/domain/binding" + "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/workflow" workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task" ) @@ -38,6 +39,38 @@ func (d *Daemon) workflowEnablement(ctx context.Context) (workflowtask.WorkflowE return d.WorkflowEnablement.WorkflowEnablement(ctx) } +// disabledRoutedWorkflow reports the workflow an issue's labels route to and +// whether the identity's org has that workflow disabled, matching the +// enablement filter binding dispatch applies. A read failure (org, +// definitions, enablement) is an error so the caller falls through to +// publishing rather than dropping the issue; no routable definition is not an +// error, it is reported as not disabled and the pin path parks the task. +func (d *Daemon) disabledRoutedWorkflow(ctx context.Context, labels, identityID string) (routed string, disabled bool, err error) { + orgID, err := d.identityOrg(ctx, identity.IdentityID(identityID)) + if err != nil { + return "", false, err + } + definitions, err := d.activeWorkflows(ctx) + if err != nil { + return "", false, err + } + available := make(map[string]struct{}, len(definitions.Definitions)) + for _, definition := range definitions.Definitions { + available[definition.ID] = struct{}{} + } + routed, resolveErr := workflow.ResolveWorkflowID(&workflow.Task{Labels: labels}, available) + if resolveErr == nil { + enablement, err := d.workflowEnablement(ctx) + if err != nil { + return "", false, err + } + return routed, !enablement.Enabled(orgID, routed), nil + } + // No routable definition is a normal outcome here, not a read failure: + // keep the publish behaviour and let the pin path park the task. + return "", false, nil +} + // resolveBindingTarget checks a matched binding against the workflow it // targets and resolves the task's repository and inputs. A non-empty reason // means the binding does not dispatch and the reason is recorded on it. diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 159275cef..12c51fefb 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -1409,6 +1409,15 @@ func (d *Daemon) pollNATS(ctx context.Context, fg forge.Forge, cfg config.Config if existing != nil { return false } + // An issue whose label-routed workflow the identity's org disabled must + // not queue a task at all. Skip without acknowledging, so a later poll + // picks the issue up again if the workflow is re-enabled. + if routed, disabled, err := d.disabledRoutedWorkflow(ctx, labels, identity); err != nil { + d.Log.WarnContext(ctx, "routed workflow check failed; publishing anyway", "repo", repo.FullName(), "issue", is.Number, "err", err) + } else if disabled { + d.Log.InfoContext(ctx, "issue skipped: its routed workflow is disabled for this org", "repo", repo.FullName(), "issue", is.Number, "workflow", routed) + return false + } // Classify and encode here: the envelope, its routing kind and its // subject belong to the work-intake domain, not to the transport. parsed := workintake.SplitLabels(labels) diff --git a/internal/daemon/intake_disabled_test.go b/internal/daemon/intake_disabled_test.go new file mode 100644 index 000000000..72f739c78 --- /dev/null +++ b/internal/daemon/intake_disabled_test.go @@ -0,0 +1,113 @@ +package daemon + +import ( + "bytes" + "context" + "errors" + "log/slog" + "strings" + "testing" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/access" + "github.com/samcharles93/archie-core/internal/domain/org" + "github.com/samcharles93/archie-core/internal/domain/workflow" + workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task" + "github.com/samcharles93/archie-core/internal/forge" +) + +// reactCounter is a forge that counts pickup reactions, so a test can tell +// whether the daemon acknowledged an issue. +type reactCounter struct { + forge.Forge + reactions int +} + +func (f *reactCounter) React(context.Context, string, string, int, string) error { + f.reactions++ + return nil +} + +// staticDefinitions serves one definition collection, or an error. +type staticDefinitions struct { + collection workflow.WorkflowDefinitionCollection + err error +} + +func (s staticDefinitions) WorkflowDefinitions(context.Context) (workflow.WorkflowDefinitionCollection, int64, error) { + return s.collection, 1, s.err +} + +// enablementSource serves one enablement value, or an error. +type enablementSource struct { + value workflowtask.WorkflowEnablement + err error +} + +func (e enablementSource) WorkflowEnablement(context.Context) (workflowtask.WorkflowEnablement, error) { + return e.value, e.err +} + +// An issue whose label-routed workflow is disabled for the polling identity's +// org is skipped at intake: no task is published and the issue is not +// acknowledged, so a later poll rediscovers it if the workflow is re-enabled. +// A failed read of org, definitions or enablement falls through to publishing +// rather than dropping the issue. +func TestPollNATSSkipsDisabledRoutedWorkflow(t *testing.T) { + repo := config.Repo{Owner: "acme", Name: "widget"} + cfg := config.Config{Dispatch: config.Dispatch{AckReaction: "eyes"}} + shipped := staticDefinitions{collection: workflow.ShippedDefinitions()} + principals := workflowPrincipals{"acme-bot": "acme"} + disabled := func(owner org.OrgID, workflowID string) enablementSource { + return enablementSource{value: workflowtask.WorkflowEnablement{}.SetEnabled(owner, workflowID, false)} + } + tests := []struct { + name string + labels string + definitions staticDefinitions + enablement enablementSource + principals access.PrincipalSource + wantCreated bool + wantPublished int + wantAck bool + wantSkipLog bool + }{ + {"bug routes to tdd, disabled for the org, skipped", "bug", shipped, disabled("acme", "tdd"), principals, false, 0, false, true}, + {"feature routes to feasibility, disabled for the org, skipped", "feature", shipped, disabled("acme", "feasibility"), principals, false, 0, false, true}, + {"no recognised label routes to triage, disabled, skipped", "", shipped, disabled("acme", "triage"), principals, false, 0, false, true}, + {"unrecognised labels route to triage, disabled, skipped", "archie,priority::high", shipped, disabled("acme", "triage"), principals, false, 0, false, true}, + {"same workflow enabled publishes and acknowledges", "bug", shipped, enablementSource{}, principals, true, 1, true, false}, + {"disabled for a different org still publishes", "bug", shipped, disabled("other", "tdd"), principals, true, 1, true, false}, + {"enablement read error falls through to publish", "bug", shipped, enablementSource{err: errors.New("store down")}, principals, true, 1, true, false}, + {"definitions read error falls through to publish", "bug", staticDefinitions{err: errors.New("store down")}, enablementSource{}, principals, true, 1, true, false}, + // The org read fails again inside PublishTask, so nothing is queued, + // but the issue is not skipped or acknowledged and is re-polled. + {"org lookup error falls through, not skipped", "bug", shipped, enablementSource{}, workflowPrincipals{}, false, 0, false, false}, + {"no routable definition publishes; the pin path parks", "", staticDefinitions{collection: workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "pr-review"}}}}, enablementSource{}, principals, true, 1, true, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + bus := &publishCounter{} + fg := &reactCounter{} + var logs bytes.Buffer + d := &Daemon{ + Log: slog.New(slog.NewTextHandler(&logs, nil)), Store: taskLookup{}, Tasks: bus, + WorkflowDefinitions: tt.definitions, WorkflowEnablement: tt.enablement, + Principals: tt.principals, + } + is := forge.Issue{Number: 42, Title: "crash on startup"} + if created := d.pollNATS(t.Context(), fg, cfg, repo, is, tt.labels, "acme-bot"); created != tt.wantCreated { + t.Errorf("pollNATS created %v, want %v", created, tt.wantCreated) + } + if bus.published != tt.wantPublished { + t.Errorf("published %d tasks, want %d", bus.published, tt.wantPublished) + } + if acked := fg.reactions > 0; acked != tt.wantAck { + t.Errorf("acknowledged %v, want %v", acked, tt.wantAck) + } + if skipped := strings.Contains(logs.String(), "issue skipped"); skipped != tt.wantSkipLog { + t.Errorf("skip logged %v, want %v; logs: %s", skipped, tt.wantSkipLog, logs.String()) + } + }) + } +}