diff --git a/changelog.d/added-18448-advisory-report-every-repo.md b/changelog.d/added-18448-advisory-report-every-repo.md new file mode 100644 index 0000000000..893c328534 --- /dev/null +++ b/changelog.d/added-18448-advisory-report-every-repo.md @@ -0,0 +1 @@ +- New owner setting **Advisory report in every repo** (Settings, Governor Config, Advisory; `governor.advisory.per_repo`, default off, `HIVE_ADVISORY_PER_REPO` sets the default when the key is absent). Off keeps one Hive Advisory Report in the primary repo covering all repos. On, each monitored repo also gets exactly one report, found by title plus the `hive/advisory` label or a hive marker so a restart never files a second; a report a person closed is left closed and no longer posted to; repos with Issues disabled or no write access are skipped with one warning. Turning it off stops updating those reports without closing them ([#18448](https://github.com/hivecommons/hive/issues/18448)). diff --git a/dashboard/openapi.json b/dashboard/openapi.json index 2243ea8b69..9afcc98cbf 100644 --- a/dashboard/openapi.json +++ b/dashboard/openapi.json @@ -11854,6 +11854,10 @@ }, "linear_issue": { "type": "string" + }, + "per_repo": { + "type": "boolean", + "description": "Advisory report in every repo (governor.advisory.per_repo, default false)" } } } @@ -11905,6 +11909,10 @@ "linear_issue": { "type": "string", "description": "Required when target is linear" + }, + "per_repo": { + "type": "boolean", + "description": "Off: one Hive Advisory Report in the primary repo covers all repos. On: each monitored repo also gets its own report." } } } diff --git a/src/cmd/hive/advisory_primary_only_test.go b/src/cmd/hive/advisory_primary_only_test.go index 96396692cc..010a79f429 100644 --- a/src/cmd/hive/advisory_primary_only_test.go +++ b/src/cmd/hive/advisory_primary_only_test.go @@ -173,9 +173,16 @@ func TestAdvisoryWrites_NeverIssuedInsideALoop(t *testing.T) { // seams exactly once each, and reads advisoryEnsureDepsForCycle.ensure only // where it assigns it. The #18568 regression wired a second copy of each into // a per-repo loop helper, which raised all of these counts to two. +// +// The opt-in per-repo report (#18448, governor.advisory.per_repo) adds exactly +// one more PostAdvisoryDigest wiring and one EnsureSecondaryAdvisoryIssue +// wiring, both handed to the secondary helpers, whose repo list is pinned to +// advisoryPerRepoSecondaryRepos (empty unless the owner opted in) by +// TestAdvisorySecondaryHelpers_OnlyFedThePerRepoGatedList. var advisoryWriterWiring = map[string]int{ "EnsureAdvisoryIssue": 1, - "PostAdvisoryDigest": 1, + "EnsureSecondaryAdvisoryIssue": 1, + "PostAdvisoryDigest": 2, "advisoryEnsureDepsForCycle.ensure": 1, } @@ -209,7 +216,7 @@ func TestAdvisoryWriters_WiredOnceForThePrimaryOnly(t *testing.T) { return true } switch sel.Sel.Name { - case "EnsureAdvisoryIssue", "PostAdvisoryDigest": + case "EnsureAdvisoryIssue", "EnsureSecondaryAdvisoryIssue", "PostAdvisoryDigest": if !called[sel] { got[sel.Sel.Name]++ } diff --git a/src/cmd/hive/advisory_repo_issues.go b/src/cmd/hive/advisory_repo_issues.go new file mode 100644 index 0000000000..efd9152d5d --- /dev/null +++ b/src/cmd/hive/advisory_repo_issues.go @@ -0,0 +1,394 @@ +package main + +import ( + "context" + "errors" + "fmt" + "log/slog" + "strings" + "sync" + "time" + + "github.com/hivecommons/hive/pkg/advisory" + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github" +) + +// Opt-in per-repo advisory reports (#18448). By default a hive keeps exactly +// one Hive Advisory Report, in the primary repo (#18568). When the owner turns +// on governor.advisory.per_repo, every other repo in project.repos also gets +// one report, re-ensured each eval cycle while unresolved so a repo added in +// Settings gets its report without a restart. +// +// Unlike the primary report, a secondary report a human closed is left +// closed: EnsureSecondaryAdvisoryIssue finds it (title plus label or marker, +// any state) and reports it closed instead of reopening or replacing it, and +// the post path stops writing to a report that is closed mid-run. +// +// When per_repo is switched off nothing here runs: the existing secondary +// reports are neither updated nor closed. + +const ( + // advisoryRepoIssueAlertPrefix namespaces the per-repo dashboard alerts + // raised when a managed repo cannot hold an advisory report. + advisoryRepoIssueAlertPrefix = "advisory-repo-issue:" + // advisoryRepoIssueAlertSeverity is the dashboard severity of those + // alerts: the rest of the hive keeps working, so it is a warning. + advisoryRepoIssueAlertSeverity = "warning" + // advisoryRepoIssueRefusedBackoff is how long a repo whose ensure was + // refused for a reason retrying cannot fix (no write access, Issues + // disabled, repo moved or archived) waits before the next attempt, so a + // refused repo costs at most one lookup an hour, never one per cycle. + advisoryRepoIssueRefusedBackoff = time.Hour + // advisorySecondaryOpenCheckInterval paces the "is this report still + // open?" check before a secondary digest post. + advisorySecondaryOpenCheckInterval = time.Hour +) + +// advisoryRepoIssueAlerts is the dashboard surface for per-repo ensure +// refusals; *dashboard.Server satisfies it. +type advisoryRepoIssueAlerts interface { + AddSystemAlert(id, severity, message string) + ClearSystemAlertsExcept(prefix string, keep map[string]struct{}) +} + +// advisoryRepoIssueRefusal is one repo's most recent non-transient ensure +// refusal: when the next attempt is allowed, and the operator-facing message. +type advisoryRepoIssueRefusal struct { + retryAt time.Time + message string +} + +// advisoryRepoIssueBackoff remembers, across eval cycles, which secondary +// repos were refused (and already logged), which reports a human closed, and +// when each report was last confirmed open. +type advisoryRepoIssueBackoff struct { + mu sync.Mutex + refused map[string]advisoryRepoIssueRefusal + logged map[string]string // repo -> refusal message already logged + closed map[string]int // repo -> human-closed report number + openChecked map[string]time.Time +} + +func newAdvisoryRepoIssueBackoff() *advisoryRepoIssueBackoff { + return &advisoryRepoIssueBackoff{ + refused: map[string]advisoryRepoIssueRefusal{}, + logged: map[string]string{}, + closed: map[string]int{}, + openChecked: map[string]time.Time{}, + } +} + +// advisoryRepoIssueGate is the process-wide state the eval cycle uses. +var advisoryRepoIssueGate = newAdvisoryRepoIssueBackoff() + +func (b *advisoryRepoIssueBackoff) get(repo string) (advisoryRepoIssueRefusal, bool) { + b.mu.Lock() + defer b.mu.Unlock() + r, ok := b.refused[repo] + return r, ok +} + +// refuse records a refusal and reports whether this message is new for repo, +// i.e. whether it should be logged at warning level. +func (b *advisoryRepoIssueBackoff) refuse(repo string, r advisoryRepoIssueRefusal) bool { + b.mu.Lock() + defer b.mu.Unlock() + b.refused[repo] = r + if b.logged[repo] == r.message { + return false + } + b.logged[repo] = r.message + return true +} + +func (b *advisoryRepoIssueBackoff) clear(repo string) { + b.mu.Lock() + defer b.mu.Unlock() + delete(b.refused, repo) + delete(b.logged, repo) +} + +func (b *advisoryRepoIssueBackoff) markClosed(repo string, num int) { + b.mu.Lock() + defer b.mu.Unlock() + b.closed[repo] = num + delete(b.refused, repo) + delete(b.logged, repo) +} + +func (b *advisoryRepoIssueBackoff) isClosed(repo string) bool { + b.mu.Lock() + defer b.mu.Unlock() + _, ok := b.closed[repo] + return ok +} + +// openCheckDue reports whether repo's report should be re-checked for a +// human close, and if so stamps now as the check time. +func (b *advisoryRepoIssueBackoff) openCheckDue(repo string, now time.Time) bool { + b.mu.Lock() + defer b.mu.Unlock() + if last, ok := b.openChecked[repo]; ok && now.Sub(last) < advisorySecondaryOpenCheckInterval { + return false + } + b.openChecked[repo] = now + return true +} + +// advisorySecondaryRepos returns the managed repos other than the primary, in +// configured order. An entry naming the primary or an earlier entry, bare +// ("talks") or org-qualified ("org/talks") and in any case, is dropped so one +// repository never gets two reports. The spelling kept is the first seen, +// because advisoryIssues and the post gate are keyed by that exact string. +func advisorySecondaryRepos(cfg *config.Config) []string { + if cfg == nil { + return nil + } + org := strings.TrimSpace(cfg.Project.Org) + key := func(repo string) string { + if !strings.Contains(repo, "/") && org != "" { + repo = org + "/" + repo + } + return strings.ToLower(repo) + } + primary := strings.TrimSpace(advisory.PrimaryRepo(cfg)) + seen := map[string]struct{}{} + if primary != "" { + seen[key(primary)] = struct{}{} + } + var out []string + for _, r := range cfg.Project.Repos { + r = strings.TrimSpace(r) + if r == "" { + continue + } + k := key(r) + if _, dup := seen[k]; dup { + continue + } + seen[k] = struct{}{} + out = append(out, r) + } + return out +} + +// advisoryPerRepoSecondaryRepos is the set the eval cycle ensures and posts +// to: the secondary repos when governor.advisory.per_repo is on, else none. +func advisoryPerRepoSecondaryRepos(cfg *config.Config) []string { + if cfg == nil || !cfg.Governor.Advisory.PerRepoEnabled() { + return nil + } + return advisorySecondaryRepos(cfg) +} + +// advisoryRepoIssueRefusalMessage reports whether err is a refusal retrying +// cannot fix and, if so, the operator-facing alert text naming repo and the +// remedy. A rate limit, 5xx or network error returns ok=false: the next cycle +// simply retries. +func advisoryRepoIssueRefusalMessage(repo string, err error) (string, bool) { + var disabled *github.IssuesDisabledError + if errors.As(err, &disabled) { + return fmt.Sprintf("No advisory report in %s: %s.", repo, disabled.Error()), true + } + var moved *github.RepoMovedError + if errors.As(err, &moved) { + return fmt.Sprintf("No advisory report in %s: %s.", repo, moved.Error()), true + } + switch classifyAdvisoryPostError(err) { + case advisoryPostWriteForbidden: + return fmt.Sprintf("No advisory report in %s: the hive's GitHub App cannot create issues there. Add the repository to the App installation and grant Issues read and write, or turn off \"Advisory report in every repo\".", repo), true + case advisoryPostRepoArchived: + return fmt.Sprintf("No advisory report in %s: the repository is archived and read-only.", repo), true + } + return "", false +} + +// ensureSecondaryAdvisoryIssues resolves (creating when missing) the advisory +// report of every repo in repos that has none yet, recording each number in +// advisoryIssues under the repo's configured spelling. repos is empty unless +// governor.advisory.per_repo is on; with it empty, only stale alerts are +// cleared. +// +// One repo's failure never stops the others. A report found closed is +// remembered and the repo is skipped for the rest of the run. A refusal +// retrying cannot fix is logged once, raised as a per-repo dashboard alert and +// backed off for advisoryRepoIssueRefusedBackoff; a transient failure is +// logged and retried next cycle. A paused repo is skipped quietly. +// +// The returned map holds this cycle's ensure error per repo, for tests and +// logging; callers need not act on it. +func ensureSecondaryAdvisoryIssues( + ctx context.Context, + advisoryIssues map[string]int, + repos []string, + ensure func(ctx context.Context, repo string) (int, error), + gate *advisoryRepoIssueBackoff, + alerts advisoryRepoIssueAlerts, + now time.Time, + logger *slog.Logger, +) map[string]error { + if logger == nil { + logger = slog.Default() + } + errs := map[string]error{} + keep := map[string]struct{}{} + defer func() { + if alerts != nil { + alerts.ClearSystemAlertsExcept(advisoryRepoIssueAlertPrefix, keep) + } + }() + if ensure == nil || advisoryIssues == nil || gate == nil { + return errs + } + for _, repo := range repos { + if gate.isClosed(repo) { + continue + } + if !advisoryIssueUnresolved(advisoryIssues, repo) { + gate.clear(repo) + continue + } + if r, ok := gate.get(repo); ok && now.Before(r.retryAt) { + if r.message != "" { + keep[advisoryRepoIssueAlertPrefix+repo] = struct{}{} + } + continue + } + num, err := ensure(ctx, repo) + if err == nil && num > 0 { + advisoryIssues[repo] = num + gate.clear(repo) + logger.Info("per-repo advisory report ready", "repo", repo, "number", num) + continue + } + if err == nil { + err = fmt.Errorf("advisory report for %s did not resolve to an issue number", repo) + } + errs[repo] = err + var closed *github.AdvisoryReportClosedError + if errors.As(err, &closed) { + gate.markClosed(repo, closed.Number) + logger.Info("per-repo advisory report was closed by a person; leaving it closed and not posting there", + "repo", repo, "number", closed.Number) + continue + } + var paused *github.RepoPausedError + if errors.As(err, &paused) { + gate.refuse(repo, advisoryRepoIssueRefusal{retryAt: now.Add(advisoryRepoIssueRefusedBackoff)}) + logger.Debug("per-repo advisory report not ensured: repo paused", "repo", repo) + continue + } + if msg, refused := advisoryRepoIssueRefusalMessage(repo, err); refused { + first := gate.refuse(repo, advisoryRepoIssueRefusal{retryAt: now.Add(advisoryRepoIssueRefusedBackoff), message: msg}) + keep[advisoryRepoIssueAlertPrefix+repo] = struct{}{} + if alerts != nil { + alerts.AddSystemAlert(advisoryRepoIssueAlertPrefix+repo, advisoryRepoIssueAlertSeverity, msg) + } + if first { + logger.Warn("per-repo advisory report skipped for repo", + "repo", repo, "recheck_after", advisoryRepoIssueRefusedBackoff.String(), "error", err) + } else { + logger.Debug("per-repo advisory report still refused for repo", "repo", repo, "error", err) + } + continue + } + // Transient: keep any alert a refusal raised until a real answer. + if r, ok := gate.get(repo); ok && r.message != "" { + keep[advisoryRepoIssueAlertPrefix+repo] = struct{}{} + } + logger.Warn("per-repo advisory report still unresolved; retrying next cycle", "repo", repo, "error", err) + } + return errs +} + +// advisorySecondaryPostDeps are the effects postAdvisoryDigestToSecondaryRepos +// performs; the eval cycle wires them to the GitHub client and post gate. +type advisorySecondaryPostDeps struct { + post func(ctx context.Context, repo string, issueNum int, md string) error + // isOpen reports whether the report is still open; nil skips the check. + isOpen func(ctx context.Context, repo string, issueNum int) (bool, error) + due func(repo string) bool + recordSuccess func(repo string) + // recordForbidden pauses further post attempts for repo after a + // non-transient 403. + recordForbidden func(repo string) +} + +// postAdvisoryDigestToSecondaryRepos writes the rendered digest to the +// advisory report of every repo in repos that has one, paced per repo by the +// same post gate as the primary. A report found closed is dropped from +// advisoryIssues and never posted to again this run. Failures are logged per +// repo and never touch the primary's hub-facing digest error or the App +// banner. It returns the repos posted to. +func postAdvisoryDigestToSecondaryRepos( + ctx context.Context, + md string, + advisoryIssues map[string]int, + repos []string, + gate *advisoryRepoIssueBackoff, + deps advisorySecondaryPostDeps, + now time.Time, + logger *slog.Logger, +) []string { + if md == "" || deps.post == nil { + return nil + } + if logger == nil { + logger = slog.Default() + } + var posted []string + for _, repo := range repos { + if gate != nil && gate.isClosed(repo) { + continue + } + num, ok := advisoryIssueNumber(advisoryIssues, repo) + if !ok { + continue + } + if _, archived := archivedAdvisoryRepos.Load(repo); archived { + continue + } + if deps.due != nil && !deps.due(repo) { + continue + } + if deps.isOpen != nil && gate != nil && gate.openCheckDue(repo, now) { + open, err := deps.isOpen(ctx, repo, num) + if err != nil { + logger.Debug("could not confirm per-repo advisory report is open; posting anyway", + "repo", repo, "issue", num, "error", err) + } else if !open { + gate.markClosed(repo, num) + delete(advisoryIssues, repo) + logger.Info("per-repo advisory report was closed by a person; leaving it closed and not posting there", + "repo", repo, "number", num) + continue + } + } + err := deps.post(ctx, repo, num, md) + var paused *github.RepoPausedError + switch { + case err == nil: + posted = append(posted, repo) + if deps.recordSuccess != nil { + deps.recordSuccess(repo) + } + logger.Info("posted advisory digest", "repo", repo, "issue", num, "via", "app") + case errors.As(err, &paused): + if deps.recordSuccess != nil { + deps.recordSuccess(repo) + } + case classifyAdvisoryPostError(err) == advisoryPostRepoArchived: + archivedAdvisoryRepos.Store(repo, struct{}{}) + logger.Warn("advisory repo is archived and read-only, skipping digest posts", "repo", repo) + case classifyAdvisoryPostError(err) == advisoryPostWriteForbidden: + if deps.recordForbidden != nil { + deps.recordForbidden(repo) + } + logger.Warn("advisory digest write forbidden for repo", "repo", repo, "issue", num, "error", err) + default: + logger.Warn("failed to post advisory digest to repo", "repo", repo, "issue", num, "error", err) + } + } + return posted +} diff --git a/src/cmd/hive/advisory_repo_issues_test.go b/src/cmd/hive/advisory_repo_issues_test.go new file mode 100644 index 0000000000..d6a6794543 --- /dev/null +++ b/src/cmd/hive/advisory_repo_issues_test.go @@ -0,0 +1,371 @@ +package main + +import ( + "bytes" + "context" + "errors" + "fmt" + "go/ast" + "go/parser" + "go/token" + "log/slog" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/config" + "github.com/hivecommons/hive/pkg/github" +) + +// Opt-in per-repo advisory reports (#18448): governor.advisory.per_repo, +// default off. Off keeps the #18568 rule (one report, in the primary repo). + +// fakeRepoIssueAlerts records the per-repo advisory-report alerts. +type fakeRepoIssueAlerts struct { + alerts map[string]string +} + +func newFakeRepoIssueAlerts() *fakeRepoIssueAlerts { + return &fakeRepoIssueAlerts{alerts: map[string]string{}} +} + +func (f *fakeRepoIssueAlerts) AddSystemAlert(id, severity, message string) { + f.alerts[id] = severity + ": " + message +} + +func (f *fakeRepoIssueAlerts) ClearSystemAlertsExcept(prefix string, keep map[string]struct{}) { + for id := range f.alerts { + if !strings.HasPrefix(id, prefix) { + continue + } + if _, ok := keep[id]; !ok { + delete(f.alerts, id) + } + } +} + +// fakeReportForge stands in for GitHub behind EnsureSecondaryAdvisoryIssue: +// one report per repo, found (open or closed) before anything is created. +type fakeReportForge struct { + open map[string]int // repo -> open report + closed map[string]int // repo -> report a human closed + refuse map[string]error // repo -> error every ensure returns + creates map[string]int + calls map[string]int + next int +} + +func newFakeReportForge() *fakeReportForge { + return &fakeReportForge{ + open: map[string]int{}, closed: map[string]int{}, refuse: map[string]error{}, + creates: map[string]int{}, calls: map[string]int{}, next: 100, + } +} + +func (f *fakeReportForge) ensure(_ context.Context, repo string) (int, error) { + f.calls[repo]++ + if err := f.refuse[repo]; err != nil { + return 0, err + } + if n, ok := f.open[repo]; ok { + return n, nil + } + if n, ok := f.closed[repo]; ok { + return 0, &github.AdvisoryReportClosedError{Repo: repo, Number: n} + } + f.next++ + f.open[repo] = f.next + f.creates[repo]++ + return f.next, nil +} + +func perRepoConfig(on *bool, primary string, repos ...string) *config.Config { + cfg := &config.Config{} + cfg.Project.Org = "hivecommons" + cfg.Project.PrimaryRepo = primary + cfg.Project.Repos = repos + cfg.Governor.Advisory.PerRepo = on + return cfg +} + +func TestAdvisorySecondaryRepos_AllButPrimaryDeduped(t *testing.T) { + cfg := perRepoConfig(nil, "", "hive", "crux", "hivecommons/Crux", "other/notes") + if got, want := fmt.Sprint(advisorySecondaryRepos(cfg)), fmt.Sprint([]string{"crux", "other/notes"}); got != want { + t.Fatalf("secondary repos = %s, want %s", got, want) + } + cfg.Project.PrimaryRepo = "crux" + if got, want := fmt.Sprint(advisorySecondaryRepos(cfg)), fmt.Sprint([]string{"hive", "other/notes"}); got != want { + t.Fatalf("explicit primary must be excluded: got %s, want %s", got, want) + } + if got := advisorySecondaryRepos(perRepoConfig(nil, "", "only")); len(got) != 0 { + t.Fatalf("single-repo hive has no secondary repos, got %v", got) + } +} + +// TestAdvisoryPerRepo_DefaultOffCreatesNothingOutsidePrimary: with per_repo +// absent (and the env override unset) or explicitly false, the eval cycle's +// secondary list is empty, so no repo but the primary is ever asked. +func TestAdvisoryPerRepo_DefaultOffCreatesNothingOutsidePrimary(t *testing.T) { + t.Setenv(config.AdvisoryPerRepoEnvVar, "") + for name, on := range map[string]*bool{"absent": nil, "explicit false": boolPtr(false)} { + t.Run(name, func(t *testing.T) { + cfg := perRepoConfig(on, "hive", "hive", "crux", "dibs", "docs") + if got := advisoryPerRepoSecondaryRepos(cfg); len(got) != 0 { + t.Fatalf("per_repo off: secondary repos = %v, want none", got) + } + forge := newFakeReportForge() + issues := map[string]int{"hive": 9} + for cycle := 0; cycle < 3; cycle++ { + ensureSecondaryAdvisoryIssues(context.Background(), issues, advisoryPerRepoSecondaryRepos(cfg), + forge.ensure, newAdvisoryRepoIssueBackoff(), newFakeRepoIssueAlerts(), time.Now(), nil) + } + if len(forge.calls) != 0 || len(forge.creates) != 0 { + t.Fatalf("per_repo off asked %v / created %v outside the primary", forge.calls, forge.creates) + } + if len(issues) != 1 { + t.Fatalf("advisory issues = %v, want only the primary", issues) + } + }) + } +} + +func TestAdvisoryPerRepo_EnvOverrideOnlyWhenUnset(t *testing.T) { + t.Setenv(config.AdvisoryPerRepoEnvVar, "true") + if got := advisoryPerRepoSecondaryRepos(perRepoConfig(nil, "hive", "hive", "crux")); fmt.Sprint(got) != "[crux]" { + t.Fatalf("env on, key absent: got %v, want [crux]", got) + } + if got := advisoryPerRepoSecondaryRepos(perRepoConfig(boolPtr(false), "hive", "hive", "crux")); len(got) != 0 { + t.Fatalf("explicit false must beat the env: got %v", got) + } +} + +// TestAdvisoryPerRepo_OnCreatesOnePerRepoOnce: switched on, each secondary +// repo gets exactly one report, and later cycles cost no further calls. +func TestAdvisoryPerRepo_OnCreatesOnePerRepoOnce(t *testing.T) { + cfg := perRepoConfig(boolPtr(true), "hive", "hive", "crux", "dibs") + forge := newFakeReportForge() + issues := map[string]int{"hive": 9} + gate := newAdvisoryRepoIssueBackoff() + const cycles = 4 + for cycle := 0; cycle < cycles; cycle++ { + if errs := ensureSecondaryAdvisoryIssues(context.Background(), issues, advisoryPerRepoSecondaryRepos(cfg), + forge.ensure, gate, newFakeRepoIssueAlerts(), time.Now(), nil); len(errs) != 0 { + t.Fatalf("cycle %d: %v", cycle, errs) + } + } + for _, repo := range []string{"crux", "dibs"} { + if forge.creates[repo] != 1 || forge.calls[repo] != 1 { + t.Fatalf("%s: %d creates over %d calls, want exactly one of each", repo, forge.creates[repo], forge.calls[repo]) + } + if issues[repo] <= 0 { + t.Fatalf("%s has no recorded report: %v", repo, issues) + } + } + if issues["crux"] == issues["dibs"] { + t.Fatalf("each repo needs its own report, got %v", issues) + } + if forge.calls["hive"] != 0 { + t.Fatal("the primary is owned by the primary path, not the per-repo helper") + } +} + +// TestAdvisoryPerRepo_RestartReusesExistingReports: a restart (empty map, +// fresh gate) adopts each repo's existing report and creates none. +func TestAdvisoryPerRepo_RestartReusesExistingReports(t *testing.T) { + cfg := perRepoConfig(boolPtr(true), "hive", "hive", "crux", "dibs") + forge := newFakeReportForge() + forge.open["crux"], forge.open["dibs"] = 501, 502 + const restarts = 3 + for i := 0; i < restarts; i++ { + issues := map[string]int{} + ensureSecondaryAdvisoryIssues(context.Background(), issues, advisoryPerRepoSecondaryRepos(cfg), + forge.ensure, newAdvisoryRepoIssueBackoff(), newFakeRepoIssueAlerts(), time.Now(), nil) + if issues["crux"] != 501 || issues["dibs"] != 502 { + t.Fatalf("restart %d: issues = %v, want the existing reports", i, issues) + } + } + if len(forge.creates) != 0 { + t.Fatalf("restarts created reports: %v", forge.creates) + } +} + +// TestAdvisoryPerRepo_HumanClosedReportNotReopenedOrReplaced: a closed +// secondary report is left alone, this run and after a restart, and nothing +// is posted to it. +func TestAdvisoryPerRepo_HumanClosedReportNotReopenedOrReplaced(t *testing.T) { + cfg := perRepoConfig(boolPtr(true), "hive", "hive", "crux") + forge := newFakeReportForge() + forge.closed["crux"] = 777 + for restart := 0; restart < 2; restart++ { + issues := map[string]int{"hive": 9} + gate := newAdvisoryRepoIssueBackoff() + for cycle := 0; cycle < 3; cycle++ { + ensureSecondaryAdvisoryIssues(context.Background(), issues, advisoryPerRepoSecondaryRepos(cfg), + forge.ensure, gate, newFakeRepoIssueAlerts(), time.Now(), nil) + } + if _, ok := issues["crux"]; ok { + t.Fatalf("closed report recorded as a post target: %v", issues) + } + var posted []string + postAdvisoryDigestToSecondaryRepos(context.Background(), "digest", issues, advisoryPerRepoSecondaryRepos(cfg), gate, + advisorySecondaryPostDeps{post: func(_ context.Context, repo string, _ int, _ string) error { + posted = append(posted, repo) + return nil + }}, time.Now(), nil) + if len(posted) != 0 { + t.Fatalf("posted to %v, want nothing", posted) + } + } + if forge.creates["crux"] != 0 { + t.Fatalf("a replacement for the closed report was created") + } + if forge.calls["crux"] != 2 { + t.Fatalf("closed report looked up %d times over 2 runs, want once per run", forge.calls["crux"]) + } +} + +// TestAdvisoryPerRepo_ClosedMidRunStopsPosting: a report closed after it was +// resolved is dropped at the next open check and never posted to again. +func TestAdvisoryPerRepo_ClosedMidRunStopsPosting(t *testing.T) { + cfg := perRepoConfig(boolPtr(true), "hive", "hive", "crux", "dibs") + issues := map[string]int{"hive": 9, "crux": 501, "dibs": 502} + gate := newAdvisoryRepoIssueBackoff() + open := map[string]bool{"crux": false, "dibs": true} + var posted []string + deps := advisorySecondaryPostDeps{ + post: func(_ context.Context, repo string, _ int, _ string) error { + posted = append(posted, repo) + return nil + }, + isOpen: func(_ context.Context, repo string, _ int) (bool, error) { return open[repo], nil }, + } + now := time.Now() + postAdvisoryDigestToSecondaryRepos(context.Background(), "digest", issues, advisoryPerRepoSecondaryRepos(cfg), gate, deps, now, nil) + postAdvisoryDigestToSecondaryRepos(context.Background(), "digest", issues, advisoryPerRepoSecondaryRepos(cfg), gate, deps, + now.Add(2*advisorySecondaryOpenCheckInterval), nil) + if fmt.Sprint(posted) != "[dibs dibs]" { + t.Fatalf("posted = %v, want dibs twice and never crux", posted) + } + if _, ok := issues["crux"]; ok { + t.Fatal("closed report still recorded as a post target") + } + forge := newFakeReportForge() + ensureSecondaryAdvisoryIssues(context.Background(), issues, advisoryPerRepoSecondaryRepos(cfg), + forge.ensure, gate, newFakeRepoIssueAlerts(), now, nil) + if forge.calls["crux"] != 0 { + t.Fatal("a report closed mid-run was looked up (and could be re-created) again") + } +} + +// TestAdvisoryPerRepo_IssuesDisabledRepoSkipped: a repo with Issues disabled +// is skipped with one warning log and one dashboard alert, not retried every +// cycle, and does not hold up the other repos. +func TestAdvisoryPerRepo_IssuesDisabledRepoSkipped(t *testing.T) { + cfg := perRepoConfig(boolPtr(true), "hive", "hive", "fork", "crux") + forge := newFakeReportForge() + forge.refuse["fork"] = &github.IssuesDisabledError{Repo: "hivecommons/fork", Fork: true} + issues := map[string]int{"hive": 9} + gate := newAdvisoryRepoIssueBackoff() + alerts := newFakeRepoIssueAlerts() + var logs bytes.Buffer + logger := slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelInfo})) + now := time.Now() + const cycles = 5 + for cycle := 0; cycle < cycles; cycle++ { + ensureSecondaryAdvisoryIssues(context.Background(), issues, advisoryPerRepoSecondaryRepos(cfg), + forge.ensure, gate, alerts, now.Add(time.Duration(cycle)*time.Minute), logger) + } + if forge.calls["fork"] != 1 { + t.Fatalf("Issues-disabled repo asked %d times in %d cycles, want once", forge.calls["fork"], cycles) + } + if forge.creates["fork"] != 0 { + t.Fatal("created a report in an Issues-disabled repo") + } + if issues["crux"] <= 0 { + t.Fatalf("other repos must still get their report: %v", issues) + } + if _, ok := alerts.alerts[advisoryRepoIssueAlertPrefix+"fork"]; !ok { + t.Fatalf("no dashboard warning for the skipped repo: %v", alerts.alerts) + } + // Past the backoff it is checked once more, still without a second warning. + ensureSecondaryAdvisoryIssues(context.Background(), issues, advisoryPerRepoSecondaryRepos(cfg), + forge.ensure, gate, alerts, now.Add(2*advisoryRepoIssueRefusedBackoff), logger) + if forge.calls["fork"] != 2 { + t.Fatalf("repo not re-checked after the backoff: %d calls", forge.calls["fork"]) + } + if n := strings.Count(logs.String(), "per-repo advisory report skipped for repo"); n != 1 { + t.Fatalf("skip warning logged %d times, want once:\n%s", n, logs.String()) + } +} + +// TestAdvisoryPerRepo_SwitchedOffStopsUpdatingAndClearsAlerts: turning the +// setting off stops every per-repo post and clears per-repo alerts, without +// any call that could close a report. +func TestAdvisoryPerRepo_SwitchedOffStopsUpdatingAndClearsAlerts(t *testing.T) { + cfg := perRepoConfig(boolPtr(false), "hive", "hive", "crux") + issues := map[string]int{"hive": 9, "crux": 501} + alerts := newFakeRepoIssueAlerts() + alerts.alerts[advisoryRepoIssueAlertPrefix+"crux"] = "warning: stale" + forge := newFakeReportForge() + ensureSecondaryAdvisoryIssues(context.Background(), issues, advisoryPerRepoSecondaryRepos(cfg), + forge.ensure, newAdvisoryRepoIssueBackoff(), alerts, time.Now(), nil) + if len(alerts.alerts) != 0 { + t.Fatalf("per-repo alerts not cleared when switched off: %v", alerts.alerts) + } + posted := postAdvisoryDigestToSecondaryRepos(context.Background(), "digest", issues, advisoryPerRepoSecondaryRepos(cfg), + newAdvisoryRepoIssueBackoff(), advisorySecondaryPostDeps{post: func(context.Context, string, int, string) error { + return errors.New("must not post") + }}, time.Now(), nil) + if len(posted) != 0 || len(forge.calls) != 0 { + t.Fatalf("switched off still touched secondary repos: posted %v, ensure calls %v", posted, forge.calls) + } +} + +// TestAdvisorySecondaryHelpers_OnlyFedThePerRepoGatedList guards the eval +// cycle source: every call of a per-repo helper in package main must take +// its repo list straight from advisoryPerRepoSecondaryRepos, so the default +// (off) can never fan out to every repo again (#18568). +func TestAdvisorySecondaryHelpers_OnlyFedThePerRepoGatedList(t *testing.T) { + // helpers maps each per-repo helper to the index of its repos argument. + helpers := map[string]int{"ensureSecondaryAdvisoryIssues": 2, "postAdvisoryDigestToSecondaryRepos": 3} + files, err := filepath.Glob("*.go") + if err != nil { + t.Fatal(err) + } + fset := token.NewFileSet() + seen := 0 + for _, name := range files { + if strings.HasSuffix(name, "_test.go") { + continue + } + f, err := parser.ParseFile(fset, name, nil, parser.SkipObjectResolution) + if err != nil { + t.Fatalf("parse %s: %v", name, err) + } + ast.Inspect(f, func(n ast.Node) bool { + call, ok := n.(*ast.CallExpr) + if !ok { + return true + } + repoArg, isHelper := helpers[calledName(call.Fun)] + if !isHelper { + return true + } + seen++ + if len(call.Args) <= repoArg { + t.Errorf("%s: unexpected call shape", fset.Position(call.Pos())) + return true + } + inner, ok := call.Args[repoArg].(*ast.CallExpr) + if !ok || calledName(inner.Fun) != "advisoryPerRepoSecondaryRepos" { + t.Errorf("%s: %s must be fed advisoryPerRepoSecondaryRepos(cfg) so per-repo reports stay opt-in", + fset.Position(call.Pos()), calledName(call.Fun)) + } + return true + }) + } + if seen != len(helpers) { + t.Fatalf("found %d per-repo helper calls in package main, want %d (one each)", seen, len(helpers)) + } +} diff --git a/src/cmd/hive/main_eval_cycle.go b/src/cmd/hive/main_eval_cycle.go index 947db290a7..daefa49be4 100644 --- a/src/cmd/hive/main_eval_cycle.go +++ b/src/cmd/hive/main_eval_cycle.go @@ -550,9 +550,20 @@ func runEvalCycle( if !budgetDecision.SkipOptional { advisoryEnsureErr = ensurePinnedAdvisoryIssue( ctx, advisoryIssues, primaryRepoAtCycleStart, advisoryEnsureDepsForCycle, logger) - // Exactly one advisory report per hive, in the primary repo (#18568). - // No other managed repo is ever ensured: a hive that opened a report - // in every repo of project.repos spammed each of them. + // One advisory report per hive, in the primary repo (#18568), unless + // the owner opted into a report in every repo (#18448, + // governor.advisory.per_repo). Off, the list is empty and only stale + // per-repo alerts are cleared; nothing is created outside the primary. + var repoIssueAlerts advisoryRepoIssueAlerts + if dashSrv != nil { + repoIssueAlerts = dashSrv + } + var ensureSecondary func(ctx context.Context, repo string) (int, error) + if ghClient != nil { + ensureSecondary = ghClient.EnsureSecondaryAdvisoryIssue + } + ensureSecondaryAdvisoryIssues(ctx, advisoryIssues, advisoryPerRepoSecondaryRepos(cfg), + ensureSecondary, advisoryRepoIssueGate, repoIssueAlerts, time.Now(), logger) } var actionable *github.ActionableResult @@ -1616,6 +1627,23 @@ func runEvalCycle( } }, }, logger) + // Opt-in (#18448): the same digest goes to each other repo's own + // report, paced per repo by the post gate. Off, the list is empty. + if advisoryTarget == config.AdvisoryTargetGitHub { + postAdvisoryDigestToSecondaryRepos(ctx, md, advisoryIssues, advisoryPerRepoSecondaryRepos(cfg), advisoryRepoIssueGate, advisorySecondaryPostDeps{ + post: ghClient.PostAdvisoryDigest, + isOpen: ghClient.AdvisoryIssueOpen, + due: func(repo string) bool { + return advisoryPostDue(advCfg, repo, time.Now(), logger) + }, + recordSuccess: func(repo string) { + recordAdvisoryPostSuccess(repo, time.Now()) + }, + recordForbidden: func(repo string) { + recordAdvisoryPostForbidden(repo, time.Now(), advCfg.PostRetryBackoff()) + }, + }, time.Now(), logger) + } } } else if d := dashSrv.GetAdvisoryDigest(); d != nil { statusPayload.AdvisoryDigest = d diff --git a/src/docs/advisory.md b/src/docs/advisory.md index 14276293d9..81c40acfa4 100644 --- a/src/docs/advisory.md +++ b/src/docs/advisory.md @@ -8,10 +8,12 @@ comment each cycle so a repo owner has exactly one place to look. A hive that manages several repos still keeps exactly **one** advisory report, in `project.primary_repo` (or the first repo in `project.repos` when no primary -is set). No other repo in `project.repos` ever gets an advisory issue or a digest -comment. The report is found by its title and the `hive/advisory` label before -anything is created, so a restart reuses the open report instead of filing a new -one ([#18568](https://github.com/hivecommons/hive/issues/18568)). +is set). By default no other repo in `project.repos` gets an advisory issue or a +digest comment. The report is found by its title and the `hive/advisory` label +before anything is created, so a restart reuses the open report instead of +filing a new one ([#18568](https://github.com/hivecommons/hive/issues/18568)). +An owner who wants a report in every repo can opt in; see +[One report, or one per repo](#one-report-or-one-per-repo). Everything on this page lives under `governor.advisory` in `hive.yaml`, and every setting is also editable from **Governor Config → Advisory** in the dashboard @@ -28,8 +30,40 @@ governor: digest_min_interval: 1h # a CHANGED digest is rewritten at most this often; 0 = every changed cycle target: github # github (default) | linear linear_issue: "" # required when target is linear, e.g. ONB-123 + per_repo: false # true = every monitored repo also gets its own report ``` +## One report, or one per repo + +`governor.advisory.per_repo` (Settings, **Governor Config → Advisory**, +"Advisory report in every repo"; default **off**) decides whether a multi-repo +hive files one report or one per repo +([#18448](https://github.com/hivecommons/hive/issues/18448)). + +- **Off** (default): one Hive Advisory Report in your primary repo covers all + repos. Nothing is created in any other repo. +- **On**: each monitored repo also gets its own report, with the same digest + posted to it. Each repo gets exactly one: before creating anything the hive + looks for an existing report by its title together with the `hive/advisory` + label or the `` marker in its body, open or + closed, so a restart never files a second one. A repo added in Settings gets + its report on the next eval cycle. +- **A closed report stays closed.** If someone closes a repo's report, the hive + stops posting to it and never reopens it or files a replacement, on this run + or after a restart. (The primary repo's report is different: it is reopened + when closed, because it is the hive's one place to look.) +- **Repos the hive cannot write to are skipped.** A repo with Issues disabled, + or where the hive's GitHub App has no write access, is logged once and shown + as a dashboard warning; the hive checks it again at most hourly rather than + every cycle, and the other repos are not affected. +- **Turning it off** stops updating the per-repo reports. They are not closed + or deleted; close them by hand if you no longer want them. + +When `per_repo` is absent from `hive.yaml`, the `HIVE_ADVISORY_PER_REPO` +environment variable (`true` / `false`) sets the default. An explicit value in +`hive.yaml`, or a change in Settings, always wins. Every change made in Settings +is recorded in the audit log. + ## Keeping findings current The dashboard shows each finding's original creation time and **last seen** diff --git a/src/docs/env-vars.md b/src/docs/env-vars.md index 01ad1696c6..3587c30279 100644 --- a/src/docs/env-vars.md +++ b/src/docs/env-vars.md @@ -590,6 +590,7 @@ section requires for lookups. | `HIVE_VANITY_MINT_WINDOW` | No | `168h` | Go duration of the rolling window `HIVE_VANITY_MINT_BUDGET` is counted over. | | `HIVE_IMAGE_BUILD_STALE_AFTER` | No | `90m` | Go duration a branch HEAD may stay non-ready (docker workflow queued/building, image not yet on GHCR) before the hub reports its `latest_sha_image_status` as `stale` on the dashboard (`pkg/hub/saas.go`). | | `HIVE_ADVISORY_POST_RETRY_BACKOFF_S` | No | `3600` | Seconds the spoke pauses advisory digest writes after a `403 Resource not accessible by integration`, when `governor.advisory.post_retry_backoff_s` is unset. Negative disables the pause. | +| `HIVE_ADVISORY_PER_REPO` | No | `false` | When `governor.advisory.per_repo` is absent from `hive.yaml`, `true` gives every monitored repo its own Hive Advisory Report in addition to the one in the primary repo. An explicit `per_repo` (or the Settings toggle) wins. See [advisory.md](advisory.md#one-report-or-one-per-repo). | | `HIVE_ADVISORY_ISSUE_AGING_AFTER` | No | `45m` | Age (Go duration) after which a hive's advisory digest is bucketed `aging` in hub fleet-row freshness reporting (`pkg/hub/advisory_issue_activity.go`). Must be set **below** `HIVE_ADVISORY_ISSUE_STALE_AFTER`: if stale ≤ aging, **both** thresholds silently revert to their defaults. | | `HIVE_ADVISORY_ISSUE_STALE_AFTER` | No | `1h30m` | Age (Go duration) after which the same advisory-digest freshness struct is bucketed `stale` and drives the advisory-stale verdict. Same pairing rule as above: a value ≤ the aging threshold makes both revert to defaults. | | `HIVE_HUB_PUBLIC_URL` | No | none (chain continues) | First variable in the hub public-origin chain used to build notification deep links and to match the hub domain suffix. Precedence: `HIVE_HUB_PUBLIC_URL` → `HIVE_PUBLIC_URL` → `HIVE_HUB_BASE_URL` → `HIVE_DASHBOARD_URL` → `HIVE_HUB_URL`, then the compiled-in canonical public origin (links) or the default cluster domain (suffix match). | diff --git a/src/docs/operator-reference/README.md b/src/docs/operator-reference/README.md index ca55cd255b..5b0074aea4 100644 --- a/src/docs/operator-reference/README.md +++ b/src/docs/operator-reference/README.md @@ -288,6 +288,7 @@ Go callers keep reading `cfg.Agents`, `cfg.Governor.Gateways` and so on through | `review.backlog.min_severity` | `P2`; owner-only. `P0`–`P3`; invalid fails load. | Lowest finding priority that is filed as a backlog item. Findings below it (P3 `low`/`info` by default, every docs-currency nit included) go to one upserted `hive:review-followups` comment on the PR instead. `P3` files every finding, as before [#17065](https://github.com/hivecommons/hive/issues/17065). | | `review.reviewers.autoscale.*` | `enabled: false`; when on: `min: 1`, `max: 4` (≤ the replicas cap 5), `target_backlog_per_reviewer: 2`, `scale_up_cycles: 2`, `scale_down_cycles: 5`, `cooldown: 5m`, `cpu_per_reviewer: 1.0`, `mem_per_reviewer_mb: 2048`, `agent` unset (first `role: reviewer` agent, else `reviewer_agents[0]`); owner-only. | Sizes the reviewer lane to its backlog ([#14685](https://github.com/hivecommons/hive/issues/14685)): adds a `reviewer-N` replica after `scale_up_cycles` cycles with more PRs waiting for a free reviewer than `target_backlog_per_reviewer` per reviewer (or a wait older than the review-pending stage dwell), drains the top replica after `scale_down_cycles` idle cycles and retires it only when its turn ends. Scale-ups are refused by named guards (`at-max`, `cooldown`, `saturated` kicks, `provider-budget`, `provider-headroom`, `max-parallel-reviews`, cgroup `cpu` / `memory`). Decisions log as `reviewer autoscale: 2→3 reason=…`; state is in `/api/status.reviewerAutoscale`. See [review-swarm.md](../review-swarm.md#reviewer-autoscale-reviewreviewersautoscale). | | `review.contributor_prs.base_sync` | Off by default; owner-only. | Lets PR follow-up use GitHub's update-branch API on hive-authored lane PRs whose head is in a contributor fork, whose `mergeable_state` is `behind`/`dirty`, and whose author enabled "allow edits by maintainers". `behind` PRs get a GitHub-authored merge commit; `dirty`/422 falls back to the `hive-base-moved` repair note and kick. This requires an agent/proxy mode with `ISSUES_PRS_MERGE` because `PUT /pulls/{n}/update-branch` is merge-scoped. It does not rewrite history and does not add a human/bot-authored DCO commit; the repository's "Sign-off survives the squash" check examines the PR's own commits, not GitHub's update-branch merge commit. | +| `governor.advisory.per_repo` | `false`. `HIVE_ADVISORY_PER_REPO` overrides the default when the key is unset. | Settings, Governor, Advisory: "Advisory report in every repo". Off: one Hive Advisory Report in the primary repo covers all repos. On: each monitored repo also gets its own report, created once and never reopened after a human closes it. Turning it off stops updating those reports without closing them. See [advisory.md](../advisory.md#one-report-or-one-per-repo). | | `governor.advisory.post_retry_backoff_s` | `3600` seconds. `HIVE_ADVISORY_POST_RETRY_BACKOFF_S` overrides the default when the key is unset; a negative value disables the pause. | How long the advisory digest write is paused after GitHub refuses it with `403 Resource not accessible by integration`. That refusal is structural (foreign-authored comment, repo dropped from the installation, or repo transferred to another owner), so per-cycle retries only burn API quota. The failed edit first falls back to creating a fresh App-authored comment; if the create is refused too, the error is recorded for the hub health row — naming the new `owner/repo` when the repository has moved — and attempts pause for this window. See [advisory.md](../advisory.md#when-the-write-is-refused). | | `governor.advisory.digest_min_interval` | `1h`; Go duration. Empty restores the default, `0` disables the floor. | **Settings → Advisory → Rewrite floor for a changed digest.** The one `hive:advisory-digest` comment on the pinned advisory issue is edited in place at most this often when its material content changed (live queue counts in the Advice block drift every eval cycle). An unchanged digest never writes. The baseline is the comment's own last-edit time on GitHub, so a governor restart does not repost inside the window. | | `governor.merge_train_stall_warn_s` | `3600` seconds; `0` disables | **Settings → Auto-merge → Merge train → Stall warning after.** How long the head of a merge train may sit in one stage before the Activity Monitor reads the train **stalled** — the queued chip turns to "Merge queue · stalled — waiting on #15253 (waiting for CI) · 3h", the Merge queue popover marks the train, and the activity log records one `merge train stalled` line per train per hour naming the head, its state and how long. A soaking head is on its clock, not stalled, until that clock itself overruns the window ([#15395](https://github.com/hivecommons/hive/issues/15395)). | diff --git a/src/hive.yaml.example b/src/hive.yaml.example index b18b7fbf83..2e04f27593 100644 --- a/src/hive.yaml.example +++ b/src/hive.yaml.example @@ -653,6 +653,12 @@ governor: # cron: "30 8 * * mon-fri" # five-field cron expression # tz: UTC + # Advisory digest (see docs/advisory.md). A multi-repo hive keeps one + # Hive Advisory Report in project.primary_repo that covers every repo. + # advisory: + # per_repo: false # true: each monitored repo also gets its own + # # report (HIVE_ADVISORY_PER_REPO sets the default) + # Trajectory review lane — periodic second-model review of running agents. # Enabled by default; it no-ops until endpoint+model resolve here or via litellm. trajectory: diff --git a/src/pkg/config/advisory_config_test.go b/src/pkg/config/advisory_config_test.go index 9962467be6..c8f5962456 100644 --- a/src/pkg/config/advisory_config_test.go +++ b/src/pkg/config/advisory_config_test.go @@ -246,3 +246,28 @@ func TestAdvisoryPostRetryBackoff(t *testing.T) { t.Errorf("garbage env falls back to default: got %v", got) } } + +// TestAdvisoryPerRepoEnabled pins governor.advisory.per_repo resolution +// (#18448): default off, an explicit value wins, and the env override only +// applies while the key is absent. +func TestAdvisoryPerRepoEnabled(t *testing.T) { + on, off := true, false + t.Setenv(AdvisoryPerRepoEnvVar, "") + if (AdvisoryConfig{}).PerRepoEnabled() { + t.Error("unset: per_repo must default to off") + } + if !(AdvisoryConfig{PerRepo: &on}).PerRepoEnabled() { + t.Error("explicit true: want on") + } + t.Setenv(AdvisoryPerRepoEnvVar, "true") + if !(AdvisoryConfig{}).PerRepoEnabled() { + t.Error("env true, key absent: want on") + } + if (AdvisoryConfig{PerRepo: &off}).PerRepoEnabled() { + t.Error("explicit false must beat the env") + } + t.Setenv(AdvisoryPerRepoEnvVar, "not-a-bool") + if (AdvisoryConfig{}).PerRepoEnabled() { + t.Error("unparseable env: want off") + } +} diff --git a/src/pkg/config/config.go b/src/pkg/config/config.go index f4eafb9d67..c131674b23 100644 --- a/src/pkg/config/config.go +++ b/src/pkg/config/config.go @@ -2395,6 +2395,17 @@ type AdvisoryConfig struct { // post — the digest is never redirected to GitHub without being asked. // Authentication reuses governor.work_source.linear.api_key. LinearIssue string `yaml:"linear_issue,omitempty" json:"linear_issue,omitempty"` + // PerRepo opts a multi-repo hive into one Hive Advisory Report in EVERY + // monitored repo (#18448), not just project.primary_repo. Off by default: + // the one report in the primary repo covers all repos (#18568). When on, + // each other repo in project.repos gets exactly one report, found by its + // title plus the hive/advisory label or the hive marker before anything is + // created, so a restart never files a second one; a report a human closed + // stays closed. Turning it off stops updating those reports but leaves + // them in place. *bool so absent is distinct from an explicit false: + // absent falls back to AdvisoryPerRepoEnvVar, then off. Resolve it + // through PerRepoEnabled. + PerRepo *bool `yaml:"per_repo,omitempty" json:"per_repo,omitempty"` // QueueHealth tunes the owner-advice queue-health rules (#9103): the // thresholds at which the weekly advice tells the owner to reduce the // blocked PR queue, clear the human gate, and so on. Every field is a @@ -2451,6 +2462,27 @@ func (a AdvisoryConfig) PRAutoCloseEnabled() bool { return a.PRAutoClose == nil || *a.PRAutoClose } +// AdvisoryPerRepoEnvVar overrides the default of governor.advisory.per_repo +// when the key is absent from hive.yaml: a strconv.ParseBool value ("true", +// "1", "false", ...). An explicit per_repo in hive.yaml or a dashboard edit +// always wins. +const AdvisoryPerRepoEnvVar = "HIVE_ADVISORY_PER_REPO" + +// PerRepoEnabled resolves AdvisoryConfig.PerRepo: an explicit value wins, +// otherwise AdvisoryPerRepoEnvVar, otherwise off (one report, in the primary +// repo). An unparseable env value is ignored and resolves to off. +func (a AdvisoryConfig) PerRepoEnabled() bool { + if a.PerRepo != nil { + return *a.PerRepo + } + if v := strings.TrimSpace(os.Getenv(AdvisoryPerRepoEnvVar)); v != "" { + if on, err := strconv.ParseBool(v); err == nil { + return on + } + } + return false +} + // AutoCloseSupersededEnabled resolves GovernorConfig.AutoCloseSuperseded // with its default (on). func (g GovernorConfig) AutoCloseSupersededEnabled() bool { diff --git a/src/pkg/dashboard/api_governor_advisory_test.go b/src/pkg/dashboard/api_governor_advisory_test.go index fd827ed640..e09c1e04f3 100644 --- a/src/pkg/dashboard/api_governor_advisory_test.go +++ b/src/pkg/dashboard/api_governor_advisory_test.go @@ -3,8 +3,11 @@ package dashboard import ( "encoding/json" "net/http" + "strings" "testing" "time" + + "github.com/hivecommons/hive/pkg/config" ) type advisoryAPIResponse struct { @@ -256,3 +259,73 @@ func TestGovAdvisory_TargetRoundTrip(t *testing.T) { t.Fatalf("after switching back = {%q %q}, want {github ONB-123}", a.Target, a.LinearIssue) } } + +// TestGovAdvisory_PerRepoToggle covers "Advisory report in every repo" +// (governor.advisory.per_repo, #18448): default off, the PUT stores an +// explicit value, an unrelated PUT leaves it alone, and each change is +// audited with its before and after. +func TestGovAdvisory_PerRepoToggle(t *testing.T) { + t.Setenv(config.AdvisoryPerRepoEnvVar, "") + s := govServer(t) + type perRepoResp struct { + PerRepo bool `json:"per_repo"` + } + get := func() bool { + t.Helper() + rec := doOwnerGet(s, "/api/config/governor/advisory") + var resp perRepoResp + if rec.Code != http.StatusOK || json.Unmarshal(rec.Body.Bytes(), &resp) != nil { + t.Fatalf("GET advisory: %d — %s", rec.Code, rec.Body.String()) + } + return resp.PerRepo + } + if get() { + t.Fatal("per_repo must default to off") + } + since := time.Now().Add(-time.Minute) + if rec := doPut(s, "/api/config/governor/advisory", map[string]any{"per_repo": true}); rec.Code != http.StatusOK { + t.Fatalf("put per_repo: %d — %s", rec.Code, rec.Body.String()) + } + if !get() || !s.deps.Config.Governor.Advisory.PerRepoEnabled() { + t.Fatal("per_repo: true not applied") + } + if rec := doPut(s, "/api/config/governor/advisory", map[string]any{"max_findings": 7}); rec.Code != http.StatusOK { + t.Fatalf("unrelated put: %d", rec.Code) + } + if !get() { + t.Fatal("an unrelated PUT turned per_repo off") + } + if rec := doPut(s, "/api/config/governor/advisory", map[string]any{"per_repo": false}); rec.Code != http.StatusOK { + t.Fatalf("put per_repo false: %d", rec.Code) + } + if get() || s.deps.Config.Governor.Advisory.PerRepo == nil { + t.Fatal("per_repo: false must be stored as an explicit false") + } + var details []string + for _, e := range s.audit.RecentWithPrefixSince(since, "config_governor_advisory") { + details = append(details, e.Detail) + } + joined := strings.Join(details, " | ") + if !strings.Contains(joined, "per_repo=false->true") || !strings.Contains(joined, "per_repo=true->false") { + t.Fatalf("per_repo changes not audited with before/after: %q", joined) + } +} + +// TestGovAdvisory_PerRepoToggleInSettings pins the Settings control and its +// help text. +func TestGovAdvisory_PerRepoToggleInSettings(t *testing.T) { + raw, err := staticFS.ReadFile("static/index.html") + if err != nil { + t.Fatal(err) + } + html := string(raw) + for _, want := range []string{ + `data-action="toggleConfigSwitch" data-section="advisory" data-key="per_repo"`, + "Advisory report in every repo", + "Off: one Hive Advisory Report in your primary repo covers all repos. On: each monitored repo also gets its own report.", + } { + if !strings.Contains(html, want) { + t.Errorf("index.html missing per_repo toggle piece %q", want) + } + } +} diff --git a/src/pkg/dashboard/api_governor_features.go b/src/pkg/dashboard/api_governor_features.go index df92fcb11d..7ee832ea38 100644 --- a/src/pkg/dashboard/api_governor_features.go +++ b/src/pkg/dashboard/api_governor_features.go @@ -692,6 +692,7 @@ func (s *Server) handleGovernorAdvisoryPut(w http.ResponseWriter, r *http.Reques PostRetryBackoffS *int `json:"post_retry_backoff_s"` Target *string `json:"target"` LinearIssue *string `json:"linear_issue"` + PerRepo *bool `json:"per_repo"` QueueHealth *config.AdvisoryQueueHealthConfig `json:"queue_health"` } if err := decodeBody(r, &body); err != nil { @@ -806,11 +807,20 @@ func (s *Server) handleGovernorAdvisoryPut(w http.ResponseWriter, r *http.Reques if body.QueueHealth != nil { cfg.Governor.Advisory.QueueHealth = *body.QueueHealth } + // per_repo (#18448) decides whether every monitored repo gets its own + // advisory report, so its before/after goes into the audit detail. + auditKV := []string{"section", "advisory"} + if body.PerRepo != nil { + before := cfg.Governor.Advisory.PerRepoEnabled() + v := *body.PerRepo + cfg.Governor.Advisory.PerRepo = &v + auditKV = append(auditKV, "per_repo", fmt.Sprintf("%t->%t", before, v)) + } if err := s.saveConfig(); err != nil { s.logger.Error("failed to persist config after advisory update", "error", err) } - s.auditFromRequest(r, "config_governor_advisory", auditDetail("section", "advisory"), "") + s.auditFromRequest(r, "config_governor_advisory", auditDetail(auditKV...), "") s.refreshAndPersist() jsonResponse(w, advisorySectionResponse(cfg)) } @@ -838,6 +848,7 @@ func advisorySectionResponse(cfg *config.Config) map[string]interface{} { }, "target": a.ResolvedTarget(), "linear_issue": a.LinearIssue, + "per_repo": a.PerRepoEnabled(), } } diff --git a/src/pkg/dashboard/static-src/js/config-sentinel.js b/src/pkg/dashboard/static-src/js/config-sentinel.js index cdb7e13e31..4a53b034f5 100644 --- a/src/pkg/dashboard/static-src/js/config-sentinel.js +++ b/src/pkg/dashboard/static-src/js/config-sentinel.js @@ -461,6 +461,11 @@ +
+
+ Advisory report in every repo ${yamlHint('governor.advisory.per_repo')} +
+
Off: one Hive Advisory Report in your primary repo covers all repos. On: each monitored repo also gets its own report.
diff --git a/src/pkg/dashboard/static-src/js/config-state.js b/src/pkg/dashboard/static-src/js/config-state.js index 72e2f42f92..30f675d11d 100644 --- a/src/pkg/dashboard/static-src/js/config-state.js +++ b/src/pkg/dashboard/static-src/js/config-state.js @@ -87,7 +87,7 @@ { tab: 'Meetings', section: 'Meetings', label: 'Meetings (Quorum)', key: 'meetings.enabled', lazy: true, text: 'meetings quorum vexa discord live meeting notes research google meet teams zoom tenant key bot token meetings.enabled meetings.vexa.url meetings.vexa.api_key_file meetings.discord.token_file' }, { tab: 'Hub', section: 'Contributor Relay', label: 'Fix budget', key: 'contribute.max_parallel_fixes', text: 'fix budget parallel fixes focus window red pr fix-pr preempt spawn contribute.max_parallel_fixes contribute.min_focus_window' }, { tab: 'Logging', section: 'Logging', label: 'Governor logging', key: 'governor.logging.max_size_mb', text: 'governor logging directory level compression max size age backups governor.logging.compress governor.logging.dir governor.logging.level governor.logging.max_age_days governor.logging.max_backups governor.logging.max_size_mb' }, - { tab: 'Advisory', section: 'Advisory', label: 'Advisory digest', key: 'governor.advisory.max_findings', text: 'advisory digest max findings show all staleness pr autoclose target linear issue retry backoff update interval rewrite floor changed digest governor.advisory.update_interval_s governor.advisory.digest_min_interval' }, + { tab: 'Advisory', section: 'Advisory', label: 'Advisory digest', key: 'governor.advisory.max_findings', text: 'advisory digest max findings show all staleness pr autoclose target linear issue retry backoff update interval rewrite floor changed digest governor.advisory.update_interval_s governor.advisory.digest_min_interval advisory report in every repo per repo governor.advisory.per_repo' }, { tab: 'Features', section: 'Work intake & issue lifecycle', label: 'Event triggers', key: 'triggers', text: 'event triggers CEL scanner issue first seen issue.opened pr.opened github webhook rules repository agent priority' }, { tab: 'Features', section: 'Work intake & issue lifecycle', label: 'Stall replan', key: 'governor.replan.enabled', text: 'stall replan approved plans architect interval threshold max replans' }, { tab: 'Advisory', section: 'Queue health', label: 'Queue health thresholds', key: 'governor.advisory.queue_health.blocked_pr_pct', text: 'advisory queue health blocked needs human stale lane share check share thresholds' }, diff --git a/src/pkg/dashboard/static/index.html b/src/pkg/dashboard/static/index.html index 05e4762216..5db646d27e 100644 --- a/src/pkg/dashboard/static/index.html +++ b/src/pkg/dashboard/static/index.html @@ -60651,7 +60651,7 @@

Configure LiteLLM

{ tab: 'Meetings', section: 'Meetings', label: 'Meetings (Quorum)', key: 'meetings.enabled', lazy: true, text: 'meetings quorum vexa discord live meeting notes research google meet teams zoom tenant key bot token meetings.enabled meetings.vexa.url meetings.vexa.api_key_file meetings.discord.token_file' }, { tab: 'Hub', section: 'Contributor Relay', label: 'Fix budget', key: 'contribute.max_parallel_fixes', text: 'fix budget parallel fixes focus window red pr fix-pr preempt spawn contribute.max_parallel_fixes contribute.min_focus_window' }, { tab: 'Logging', section: 'Logging', label: 'Governor logging', key: 'governor.logging.max_size_mb', text: 'governor logging directory level compression max size age backups governor.logging.compress governor.logging.dir governor.logging.level governor.logging.max_age_days governor.logging.max_backups governor.logging.max_size_mb' }, - { tab: 'Advisory', section: 'Advisory', label: 'Advisory digest', key: 'governor.advisory.max_findings', text: 'advisory digest max findings show all staleness pr autoclose target linear issue retry backoff update interval rewrite floor changed digest governor.advisory.update_interval_s governor.advisory.digest_min_interval' }, + { tab: 'Advisory', section: 'Advisory', label: 'Advisory digest', key: 'governor.advisory.max_findings', text: 'advisory digest max findings show all staleness pr autoclose target linear issue retry backoff update interval rewrite floor changed digest governor.advisory.update_interval_s governor.advisory.digest_min_interval advisory report in every repo per repo governor.advisory.per_repo' }, { tab: 'Features', section: 'Work intake & issue lifecycle', label: 'Event triggers', key: 'triggers', text: 'event triggers CEL scanner issue first seen issue.opened pr.opened github webhook rules repository agent priority' }, { tab: 'Features', section: 'Work intake & issue lifecycle', label: 'Stall replan', key: 'governor.replan.enabled', text: 'stall replan approved plans architect interval threshold max replans' }, { tab: 'Advisory', section: 'Queue health', label: 'Queue health thresholds', key: 'governor.advisory.queue_health.blocked_pr_pct', text: 'advisory queue health blocked needs human stale lane share check share thresholds' }, @@ -76115,6 +76115,11 @@

${model.editIndex === null ? 'Add Event trigger' : 'Edit Event trigger'}

+
+
+ Advisory report in every repo ${yamlHint('governor.advisory.per_repo')} +
+
Off: one Hive Advisory Report in your primary repo covers all repos. On: each monitored repo also gets its own report.
diff --git a/src/pkg/github/advisory.go b/src/pkg/github/advisory.go index 692663adec..7778348e70 100644 --- a/src/pkg/github/advisory.go +++ b/src/pkg/github/advisory.go @@ -203,7 +203,21 @@ func (c *Client) EnsureAdvisoryIssue(ctx context.Context, repo string) (int, err c.logger.Info("found existing advisory issue", slog.String("repo", repo), slog.Int("number", num)) return num, nil } + return c.createAdvisoryIssue(ctx, owner, repo, primaryAdvisoryIssueBody) +} + +// primaryAdvisoryIssueBody is the body of the primary repo's advisory report. +const primaryAdvisoryIssueBody = "This issue collects advisory findings from Hive agents.\n\n" + + "At lower ACMM levels, some agents work in advisory mode — they analyze code and post findings " + + "here but do not create issues or PRs. At higher levels, designated agents (e.g. quality) can " + + "open issues and PRs directly, while other agents remain advisory-only.\n\n" + + "The governor posts periodic digest comments summarizing what advisory agents found.\n\n" + + "**Do not close this issue.** It is a living document." +// createAdvisoryIssue files a new advisory report in owner/repo with body, +// after checking the repo can hold issues at all. Callers have already +// searched for an existing report and found none. +func (c *Client) createAdvisoryIssue(ctx context.Context, owner, repo, body string) (int, error) { // Before attempting to create, check whether the repo can hold issues at // all. A fork (or any repo with the Issues feature off) has no Issues tab: // the create below would fail with a 410 that reads like an auth problem, @@ -231,13 +245,6 @@ func (c *Client) EnsureAdvisoryIssue(ctx context.Context, repo string) (int, err c.logger.Warn("could not create advisory label, issue will be created without it", slog.String("error", labelErr.Error())) } - body := "This issue collects advisory findings from Hive agents.\n\n" + - "At lower ACMM levels, some agents work in advisory mode — they analyze code and post findings " + - "here but do not create issues or PRs. At higher levels, designated agents (e.g. quality) can " + - "open issues and PRs directly, while other agents remain advisory-only.\n\n" + - "The governor posts periodic digest comments summarizing what advisory agents found.\n\n" + - "**Do not close this issue.** It is a living document." - // Invocation-attribution trail (attribution.go): this issue is created by // the hive itself, so the "agent" is the governor flow — backend/model are // genuinely unknown here and are omitted rather than guessed. Trailer is diff --git a/src/pkg/github/advisory_secondary.go b/src/pkg/github/advisory_secondary.go new file mode 100644 index 0000000000..c87ec7432f --- /dev/null +++ b/src/pkg/github/advisory_secondary.go @@ -0,0 +1,171 @@ +package github + +import ( + "context" + "fmt" + "log/slog" + "net/http" + "sort" + "strings" + + gh "github.com/google/go-github/v72/github" + "github.com/hivecommons/hive/pkg/forgestate" +) + +// Per-repo advisory reports (#18448, opt-in via governor.advisory.per_repo). +// +// A secondary repo's report differs from the primary's in one rule: a report +// a human closed is RESPECTED. The primary is reopened when closed (#4167) +// because it is the hive's one place to look; a secondary report is an extra +// the repo's maintainers may not want, so closing it is the way to say so. +// The lookup therefore considers closed reports too, so a closed one is +// found and reported rather than replaced by a fresh one, on any restart. + +// AdvisoryReportMarker is stamped into the body of every per-repo advisory +// report the hive files, so the report is recognisable as the hive's even if +// its hive/advisory label is removed. +const AdvisoryReportMarker = "" + +// secondaryAdvisoryLookupPageSize bounds the one labelled list (all states) +// that resolves a secondary repo's report. +const secondaryAdvisoryLookupPageSize = 30 + +// secondaryAdvisoryIssueBody is the body of a per-repo advisory report. +const secondaryAdvisoryIssueBody = "This issue collects advisory findings from Hive agents for this repository.\n\n" + + "The hive that manages this repository keeps its main advisory report in its primary repo; " + + "its owner turned on a report in every repo, so the same digest is posted here too.\n\n" + + "The governor posts periodic digest comments summarizing what advisory agents found.\n\n" + + "Closing this issue stops the hive posting here. It will not be reopened or replaced.\n\n" + + AdvisoryReportMarker + +// AdvisoryReportClosedError reports that the repo's advisory report exists but +// is closed. For a secondary repo that is final: the hive neither reopens it +// nor files a replacement. +type AdvisoryReportClosedError struct { + Repo string + Number int +} + +func (e *AdvisoryReportClosedError) Error() string { + return fmt.Sprintf("advisory report #%d in %s is closed; it is left closed", e.Number, e.Repo) +} + +// isSecondaryAdvisoryReport reports whether issue is a hive advisory report: +// the advisory title together with the hive/advisory label or the hive marker. +func isSecondaryAdvisoryReport(issue *gh.Issue) bool { + if issue == nil || issue.IsPullRequest() || !isAdvisoryTitle(issue.GetTitle()) { + return false + } + if strings.Contains(issue.GetBody(), AdvisoryReportMarker) { + return true + } + for _, l := range issue.Labels { + if l.GetName() == advisoryLabelName { + return true + } + } + return false +} + +// pickSecondaryAdvisoryReport chooses among matching reports: the oldest +// OPEN one wins (stable across restarts); with none open, the newest closed +// one is returned with closed=true. +func pickSecondaryAdvisoryReport(issues []*gh.Issue) (num int, closed bool, ok bool) { + var open, shut []int + for _, issue := range issues { + if !isSecondaryAdvisoryReport(issue) { + continue + } + if issue.GetState() == "closed" { + shut = append(shut, issue.GetNumber()) + } else { + open = append(open, issue.GetNumber()) + } + } + if len(open) > 0 { + sort.Ints(open) + return open[0], false, true + } + if len(shut) > 0 { + sort.Ints(shut) + return shut[len(shut)-1], true, true + } + return 0, false, false +} + +// EnsureSecondaryAdvisoryIssue finds or creates the advisory report of a +// non-primary repo. An open report is reused; a closed one returns +// *AdvisoryReportClosedError and is never reopened; only when no report exists +// in any state is one created. A lookup failure is returned as an error +// rather than treated as "none", so a search blip never files a duplicate. +func (c *Client) EnsureSecondaryAdvisoryIssue(ctx context.Context, repo string) (int, error) { + if c == nil { + return 0, ErrNoGitHubClient + } + owner := c.org + if parts := strings.SplitN(repo, "/", 2); len(parts) == 2 { + owner = parts[0] + repo = parts[1] + } + full := owner + "/" + repo + + // Labelled reports, open and closed, in one list call. + labelled, _, err := c.client.Issues.ListByRepo(ctx, owner, repo, &gh.IssueListByRepoOptions{ + State: "all", + Labels: []string{advisoryLabelName}, + ListOptions: gh.ListOptions{PerPage: secondaryAdvisoryLookupPageSize}, + }) + if err != nil { + return 0, fmt.Errorf("listing advisory reports in %s: %w", full, err) + } + num, closed, ok := pickSecondaryAdvisoryReport(labelled) + if !ok { + // A report whose label was removed still carries the title and the + // marker; the search finds it in any state. + query := fmt.Sprintf(`repo:%s is:issue in:title "%s"`, full, advisoryTitle) + result, _, searchErr := c.client.Search.Issues(ctx, query, &gh.SearchOptions{ + ListOptions: gh.ListOptions{PerPage: secondaryAdvisoryLookupPageSize}, + }) + if searchErr != nil { + return 0, fmt.Errorf("searching advisory reports in %s: %w", full, searchErr) + } + num, closed, ok = pickSecondaryAdvisoryReport(result.Issues) + } + if ok && closed { + return 0, &AdvisoryReportClosedError{Repo: full, Number: num} + } + if ok { + c.rememberAdvisoryIssue(full, num) + c.logger.Info("found existing per-repo advisory report", slog.String("repo", full), slog.Int("number", num)) + return num, nil + } + return c.createAdvisoryIssue(ctx, owner, repo, secondaryAdvisoryIssueBody) +} + +// AdvisoryIssueOpen reports whether advisory issue n in repo is still open, so +// the per-repo post path can stop writing to a report a human closed. The +// ForgeState cache answers when it can; otherwise one targeted GET. +func (c *Client) AdvisoryIssueOpen(ctx context.Context, repo string, n int) (bool, error) { + if c == nil { + return false, ErrNoGitHubClient + } + owner := c.org + if parts := strings.SplitN(repo, "/", 2); len(parts) == 2 { + owner = parts[0] + repo = parts[1] + } + full := owner + "/" + repo + if store := c.advisoryForgeState(); store != nil { + if it, ok, err := store.ReadThrough(ctx, full, forgestate.KindIssue, n, 0); err == nil && ok && it.Open() { + return true, nil + } + } + issue, resp, err := c.client.Issues.Get(ctx, owner, repo, n) + if err != nil { + if resp != nil && resp.StatusCode == http.StatusNotFound { + return false, nil + } + return false, fmt.Errorf("checking advisory report #%d in %s: %w", n, full, err) + } + return issue.GetState() != "closed", nil +} diff --git a/src/pkg/github/advisory_secondary_test.go b/src/pkg/github/advisory_secondary_test.go new file mode 100644 index 0000000000..d9b838abc4 --- /dev/null +++ b/src/pkg/github/advisory_secondary_test.go @@ -0,0 +1,248 @@ +package github + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "strings" + "sync" + "testing" +) + +// secondaryReportForge is a stateful fake GitHub for per-repo advisory +// reports (#18448). Unlike advisoryRestartForge it honours the list's state +// and labels filters and keeps each issue's body, so the closed-report and +// marker rules are exercised against what GitHub would return. +type secondaryReportForge struct { + mu sync.Mutex + issues map[string][]map[string]any + noIssues map[string]bool + creates map[string]int + edits map[string]int + next int + lastCreated map[string]any +} + +func newSecondaryReportForge() *secondaryReportForge { + return &secondaryReportForge{ + issues: map[string][]map[string]any{}, + noIssues: map[string]bool{}, + creates: map[string]int{}, + edits: map[string]int{}, + next: 3000, + } +} + +func (f *secondaryReportForge) seed(full string, number int, state, body string, labelled bool) { + labels := []map[string]any{} + if labelled { + labels = append(labels, map[string]any{"name": advisoryLabelName}) + } + f.issues[full] = append(f.issues[full], map[string]any{ + "number": number, "title": advisoryTitle, "state": state, "body": body, "labels": labels, + }) +} + +func secondaryForgeIssueHasLabel(issue map[string]any, name string) bool { + labels, _ := issue["labels"].([]map[string]any) + for _, l := range labels { + if l["name"] == name { + return true + } + } + return false +} + +func (f *secondaryReportForge) handler() http.Handler { + mux := http.NewServeMux() + mux.HandleFunc("/search/issues", func(w http.ResponseWriter, r *http.Request) { + f.mu.Lock() + defer f.mu.Unlock() + items := []map[string]any{} + for _, field := range strings.Fields(r.URL.Query().Get("q")) { + if full, ok := strings.CutPrefix(field, "repo:"); ok { + items = append(items, f.issues[full]...) + } + } + _ = json.NewEncoder(w).Encode(map[string]any{"total_count": len(items), "items": items}) + }) + mux.HandleFunc("/repos/{owner}/{repo}", func(w http.ResponseWriter, r *http.Request) { + full := r.PathValue("owner") + "/" + r.PathValue("repo") + _ = json.NewEncoder(w).Encode(map[string]any{"full_name": full, "has_issues": !f.noIssues[full]}) + }) + mux.HandleFunc("/repos/{owner}/{repo}/issues", func(w http.ResponseWriter, r *http.Request) { + f.mu.Lock() + defer f.mu.Unlock() + full := r.PathValue("owner") + "/" + r.PathValue("repo") + if r.Method == http.MethodPost { + var req struct { + Title string `json:"title"` + Body string `json:"body"` + Labels []string `json:"labels"` + } + _ = json.NewDecoder(r.Body).Decode(&req) + f.next++ + labels := []map[string]any{} + for _, l := range req.Labels { + labels = append(labels, map[string]any{"name": l}) + } + issue := map[string]any{"number": f.next, "title": req.Title, "state": "open", "body": req.Body, "labels": labels} + f.issues[full] = append(f.issues[full], issue) + f.creates[full]++ + f.lastCreated = issue + w.WriteHeader(http.StatusCreated) + _ = json.NewEncoder(w).Encode(issue) + return + } + state := r.URL.Query().Get("state") + if state == "" { + state = "open" + } + label := r.URL.Query().Get("labels") + items := []map[string]any{} + for _, issue := range f.issues[full] { + if state != "all" && issue["state"] != state { + continue + } + if label != "" && !secondaryForgeIssueHasLabel(issue, label) { + continue + } + items = append(items, issue) + } + _ = json.NewEncoder(w).Encode(items) + }) + mux.HandleFunc("/repos/{owner}/{repo}/issues/{number}", func(w http.ResponseWriter, r *http.Request) { + f.mu.Lock() + defer f.mu.Unlock() + full := r.PathValue("owner") + "/" + r.PathValue("repo") + if r.Method == http.MethodPatch { + f.edits[full]++ + } + for _, issue := range f.issues[full] { + if jsonNumberString(issue["number"]) == r.PathValue("number") { + _ = json.NewEncoder(w).Encode(issue) + return + } + } + w.WriteHeader(http.StatusNotFound) + _ = json.NewEncoder(w).Encode(map[string]any{"message": "Not Found"}) + }) + mux.HandleFunc("/repos/{owner}/{repo}/labels", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusCreated) + _ = json.NewEncoder(w).Encode(map[string]any{"name": advisoryLabelName}) + }) + return mux +} + +const secondaryTestOrg = "hivecommons" + +// TestEnsureSecondaryAdvisoryIssue_CreatesOnceWithMarkerThenReuses: the first +// ensure files one report carrying the hive marker; restarted clients find it. +func TestEnsureSecondaryAdvisoryIssue_CreatesOnceWithMarkerThenReuses(t *testing.T) { + forge := newSecondaryReportForge() + server := httptest.NewServer(forge.handler()) + defer server.Close() + full := secondaryTestOrg + "/crux" + + first, err := newTestClient(t, server, secondaryTestOrg, []string{"hive", "crux"}).EnsureSecondaryAdvisoryIssue(context.Background(), "crux") + if err != nil || first <= 0 { + t.Fatalf("first ensure = (%d, %v), want a created report", first, err) + } + if body, _ := forge.lastCreated["body"].(string); !strings.Contains(body, AdvisoryReportMarker) { + t.Fatalf("created report body lacks the hive marker: %q", body) + } + const restarts = 3 + for i := 0; i < restarts; i++ { + got, err := newTestClient(t, server, secondaryTestOrg, []string{"hive", "crux"}).EnsureSecondaryAdvisoryIssue(context.Background(), "crux") + if err != nil || got != first { + t.Fatalf("restart %d: ensure = (%d, %v), want #%d", i, got, err, first) + } + } + if forge.creates[full] != 1 { + t.Fatalf("%d reports created, want exactly 1", forge.creates[full]) + } +} + +// TestEnsureSecondaryAdvisoryIssue_HumanClosedIsNotReopened: a closed report +// returns AdvisoryReportClosedError; nothing is reopened (no PATCH) and +// nothing is created, on every restart. +func TestEnsureSecondaryAdvisoryIssue_HumanClosedIsNotReopened(t *testing.T) { + for name, labelled := range map[string]bool{"labelled": true, "marker only": false} { + t.Run(name, func(t *testing.T) { + forge := newSecondaryReportForge() + full := secondaryTestOrg + "/crux" + forge.seed(full, 2400, "closed", "old report\n"+AdvisoryReportMarker, labelled) + server := httptest.NewServer(forge.handler()) + defer server.Close() + for i := 0; i < 2; i++ { + n, err := newTestClient(t, server, secondaryTestOrg, []string{"crux"}).EnsureSecondaryAdvisoryIssue(context.Background(), "crux") + var closed *AdvisoryReportClosedError + if !errors.As(err, &closed) || closed.Number != 2400 || n != 0 { + t.Fatalf("restart %d: ensure = (%d, %v), want AdvisoryReportClosedError for #2400", i, n, err) + } + } + if forge.creates[full] != 0 || forge.edits[full] != 0 { + t.Fatalf("closed report: %d creates, %d edits; want none", forge.creates[full], forge.edits[full]) + } + }) + } +} + +// TestEnsureSecondaryAdvisoryIssue_OpenReportWinsOverClosed: with an open and +// a closed report, the open one is reused. +func TestEnsureSecondaryAdvisoryIssue_OpenReportWinsOverClosed(t *testing.T) { + forge := newSecondaryReportForge() + full := secondaryTestOrg + "/crux" + forge.seed(full, 10, "closed", "", true) + forge.seed(full, 20, "open", "", true) + server := httptest.NewServer(forge.handler()) + defer server.Close() + n, err := newTestClient(t, server, secondaryTestOrg, []string{"crux"}).EnsureSecondaryAdvisoryIssue(context.Background(), "crux") + if err != nil || n != 20 { + t.Fatalf("ensure = (%d, %v), want open #20", n, err) + } + if forge.creates[full] != 0 || forge.edits[full] != 0 { + t.Fatal("reusing an open report must not create or edit") + } +} + +// TestEnsureSecondaryAdvisoryIssue_IssuesDisabledSkipped: a repo with Issues +// disabled yields IssuesDisabledError and no create attempt. +func TestEnsureSecondaryAdvisoryIssue_IssuesDisabledSkipped(t *testing.T) { + forge := newSecondaryReportForge() + full := secondaryTestOrg + "/fork" + forge.noIssues[full] = true + server := httptest.NewServer(forge.handler()) + defer server.Close() + _, err := newTestClient(t, server, secondaryTestOrg, []string{"fork"}).EnsureSecondaryAdvisoryIssue(context.Background(), "fork") + var disabled *IssuesDisabledError + if !errors.As(err, &disabled) { + t.Fatalf("ensure error = %v, want IssuesDisabledError", err) + } + if forge.creates[full] != 0 { + t.Fatal("created a report in a repo with Issues disabled") + } +} + +// TestAdvisoryIssueOpen_ReportsClosed: the post path's open check sees a +// human close. +func TestAdvisoryIssueOpen_ReportsClosed(t *testing.T) { + forge := newSecondaryReportForge() + full := secondaryTestOrg + "/crux" + forge.seed(full, 1, "open", "", true) + forge.seed(full, 2, "closed", "", true) + server := httptest.NewServer(forge.handler()) + defer server.Close() + c := newTestClient(t, server, secondaryTestOrg, []string{"crux"}) + if open, err := c.AdvisoryIssueOpen(context.Background(), "crux", 1); err != nil || !open { + t.Fatalf("open report: (%v, %v), want open", open, err) + } + if open, err := c.AdvisoryIssueOpen(context.Background(), "crux", 2); err != nil || open { + t.Fatalf("closed report: (%v, %v), want closed", open, err) + } + if open, err := c.AdvisoryIssueOpen(context.Background(), "crux", 99); err != nil || open { + t.Fatalf("deleted report: (%v, %v), want not open", open, err) + } +}