diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 7f82ea733..a7490f2ca 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -357,6 +357,10 @@ 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 == "" { + 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 + } issues, err := fg.IssuesWithLabel(ctx, repo.Owner, repo.Name, cfg.Label) if err != nil { d.Log.Error("label poll failed", "repo", repo.FullName(), "err", err) @@ -388,6 +392,10 @@ func (d *Daemon) pollEitherWithConfig(ctx context.Context, fg forge.Forge, cfg c out = append(out, is) } } + if 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 + } labelled, err := fg.IssuesWithLabel(ctx, repo.Owner, repo.Name, cfg.Label) if err != nil { d.Log.Error("label poll failed", "repo", repo.FullName(), "err", err) diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go index 36f607d65..9872ee3b5 100644 --- a/internal/daemon/daemon_test.go +++ b/internal/daemon/daemon_test.go @@ -1059,16 +1059,73 @@ func TestPollIssuesWithConfigUsesIdentityScopedDispatch(t *testing.T) { } } +// An empty cfg.Label with trigger "label" or "either" must never reach +// IssuesWithLabel: GitHub's issues-list API treats an empty label filter as +// "no filter" and returns every open issue in the repo. A live incident +// (GH#445) queued 124 unrelated issues in one poll cycle this way, after +// [dispatch.labels] (a different, unrelated field for task-state labels) was +// configured while the actual trigger-match label was left unset. +func TestPollIssuesWithConfigRefusesEmptyLabel(t *testing.T) { + tests := []struct { + name string + trigger string + }{ + {name: "label trigger", trigger: "label"}, + {name: "either trigger", trigger: "either"}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + fg := &recordingForge{} + d := &Daemon{Cfg: config.NewHolder(config.Config{}), Log: slog.New(slog.DiscardHandler)} + cfg := config.Config{Dispatch: config.Dispatch{Trigger: tc.trigger}} // Label left blank. + + issues := d.pollIssuesWithConfig(context.Background(), fg, cfg, config.Repo{Owner: "acme", Name: "shared"}) + + if issues != nil { + t.Fatalf("issues = %v, want nil when label is unset", issues) + } + if len(fg.labelled) != 0 { + t.Fatalf("IssuesWithLabel called with %v, want no call at all (empty label matches every open issue)", fg.labelled) + } + }) + } +} + +// The "either" trigger must keep polling by assignee when the label is +// empty -- only the label side is unsafe, not the assignee side. A prior +// version of this guard passed even when the empty-label branch dropped +// assignee matches too (return nil instead of return out), because the +// fixture always returned nil from AssignedIssues regardless. +func TestPollIssuesWithConfigEitherTriggerKeepsAssigneeMatchesWhenLabelEmpty(t *testing.T) { + fg := &recordingForge{assignedIssues: []forge.Issue{{Number: 7}}} + d := &Daemon{Cfg: config.NewHolder(config.Config{}), Log: slog.New(slog.DiscardHandler)} + cfg := config.Config{BotUser: "archie", Dispatch: config.Dispatch{Trigger: "either"}} // Label left blank. + + issues := d.pollIssuesWithConfig(context.Background(), fg, cfg, config.Repo{Owner: "acme", Name: "shared"}) + + if len(issues) != 1 || issues[0].Number != 7 { + t.Fatalf("issues = %v, want the one assignee-matched issue preserved", issues) + } + if len(fg.labelled) != 0 { + t.Fatalf("IssuesWithLabel called with %v, want no call at all (empty label matches every open issue)", fg.labelled) + } +} + // recordingForge records the bot user/label passed to the discovery calls // so tests can assert identity-scoped polling. Everything else panics. type recordingForge struct { assigned []string labelled []string + + // assignedIssues is returned by AssignedIssues, letting tests confirm + // assignee matches survive alongside (or despite) the label poll. + assignedIssues []forge.Issue } func (f *recordingForge) AssignedIssues(_ context.Context, _, _, botUser string) ([]forge.Issue, error) { f.assigned = append(f.assigned, botUser) - return nil, nil + return f.assignedIssues, nil } func (f *recordingForge) IssuesWithLabel(_ context.Context, _, _, label string) ([]forge.Issue, error) { diff --git a/internal/infrastructure/configuration/validate.go b/internal/infrastructure/configuration/validate.go index 0933b4846..033bd5726 100644 --- a/internal/infrastructure/configuration/validate.go +++ b/internal/infrastructure/configuration/validate.go @@ -106,10 +106,20 @@ func validateAgent(cfg *config.Config) error { return nil } +// validateDispatch rejects a "label" or "either" trigger left with no label +// to match. GitHub's issues-list API treats an empty label filter as no +// filter at all and returns every open issue in the repo -- a live incident +// (GH#445) queued 124 unrelated issues in one poll cycle this way, after +// [dispatch.labels] (a different field, mapping task states to their own +// labels) was configured while the actual trigger-match label was left +// blank. func validateDispatch(cfg *config.Config) error { if !oneOf(cfg.Dispatch.Trigger, dispatchTriggers) { return fmt.Errorf("%w: dispatch.trigger %q (want %s)", ErrInvalidInput, cfg.Dispatch.Trigger, list(dispatchTriggers)) } + if (cfg.Dispatch.Trigger == dispatchTriggerLabel || cfg.Dispatch.Trigger == dispatchTriggerEither) && cfg.Label == "" { + return fmt.Errorf("%w: label is required when dispatch.trigger is %q (an empty label matches every open issue)", ErrInvalidInput, cfg.Dispatch.Trigger) + } return nil } diff --git a/internal/infrastructure/configuration/validate_test.go b/internal/infrastructure/configuration/validate_test.go index 62f73eae0..66549c5e4 100644 --- a/internal/infrastructure/configuration/validate_test.go +++ b/internal/infrastructure/configuration/validate_test.go @@ -88,6 +88,33 @@ func TestValidate_RejectsTheSameProblemsAsLoaderLoad(t *testing.T) { mutate: func(cfg *config.Config) { cfg.Dispatch.Trigger = "not-a-real-trigger" }, wantErr: true, }, + { + // GH#445: an empty label matches every open issue via the forge + // API's issues-list filter, so a "label" trigger with no label + // set must fail validation rather than queue the whole repo. + name: "label trigger with empty label", + mutate: func(cfg *config.Config) { + cfg.Dispatch.Trigger = "label" + cfg.Label = "" + }, + wantErr: true, + }, + { + name: "either trigger with empty label", + mutate: func(cfg *config.Config) { + cfg.Dispatch.Trigger = "either" + cfg.Label = "" + }, + wantErr: true, + }, + { + name: "label trigger with a label set", + mutate: func(cfg *config.Config) { + cfg.Dispatch.Trigger = "label" + cfg.Label = "archie" + }, + wantErr: false, + }, { name: "unrecognised forge.type", mutate: func(cfg *config.Config) { cfg.Forge.Type = "not-a-real-forge" },