From ccf0052af54e416ad0aa1a53befc0ae48e47ba33 Mon Sep 17 00:00:00 2001 From: Danathar Date: Thu, 5 Nov 2026 17:33:23 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix(ci):=20attribute=20check?= =?UTF-8?q?=20runs=20by=20PR,=20not=20by=20SHA=20alone?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit projectbluefin/utah-packages#890 (in-repo, danathar/supersede-878) and the fork PR #878 shared head 95b9d22. #878's fork Canary run (run 42111306103) had failed; 41 seconds after #890 opened, before its own Canary started, the contributor lane sent #890 to 072 for "Failing checks: Canary". EvaluateCommitCI, when given a PR number, drops a red check run another PR produced on the head: one whose pull_requests names other PRs but not this one, or one naming none (fork runs) whose check suite ran for a different head branch. Lookups are per suite, only for red unattributed runs, and a failed lookup keeps the run. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 Signed-off-by: Danathar --- changelog.d/fixed-ci-attribution-by-pr.md | 1 + .../stage-labels.md | 2 + src/pkg/github/commit_ci.go | 75 +++++++++ .../github/commit_ci_pr_attribution_test.go | 147 ++++++++++++++++++ 4 files changed, 225 insertions(+) create mode 100644 changelog.d/fixed-ci-attribution-by-pr.md create mode 100644 src/pkg/github/commit_ci_pr_attribution_test.go diff --git a/changelog.d/fixed-ci-attribution-by-pr.md b/changelog.d/fixed-ci-attribution-by-pr.md new file mode 100644 index 0000000000..78c9008099 --- /dev/null +++ b/changelog.d/fixed-ci-attribution-by-pr.md @@ -0,0 +1 @@ +- A red check run another PR produced on a shared head no longer counts against a PR. projectbluefin/utah-packages#890, an in-repo copy of the fork PR #878 at the same SHA, went to `stage/072-returned-to-contributor` for "Failing checks: Canary" on #878's fork Canary run, 41 seconds after it opened and before its own Canary had started. Check runs are now attributed by PR: a run naming other PRs, or a run naming none whose check suite ran for a different head branch, is not this PR's. Failed lookups keep the run. diff --git a/src/docs/labels-and-control-signals/stage-labels.md b/src/docs/labels-and-control-signals/stage-labels.md index 5fbe961593..dbbc9a9b24 100644 --- a/src/docs/labels-and-control-signals/stage-labels.md +++ b/src/docs/labels-and-control-signals/stage-labels.md @@ -382,6 +382,8 @@ used to admit the PR: | `contributor-lane:no-claim` / `claim-*` / `author-association` / `no-linked-issue`, `untrusted-author-role`, `author-permission-missing` | human hand-off, **trust** cause → routed by the head's evidence (below); lands at `090-ready-for-human-merge` when the head is otherwise clean | | hand-offs (`not-app-authored`, `trusted-bot-lane`, `trusted-author:app-authored`, `contributor-lane:app-authored`), transient fetch errors, `base-refresh:merge-guard` (the lane skipped the PR because a merge to its destination was in flight) | no opinion — the owning lane decides; nothing is written. An App-authored PR is handed off by the trusted-author and contributor-lane tiers under their own reason; the self-authored lane owns it, and its `review-gate:no-review-bundle` is the stage's reason while the swarm has not reviewed the head ([#14482](https://github.com/hivecommons/hive/issues/14482)) | +**Whose check run it is.** GitHub lists check runs per commit, and two PRs can share a head (a maintainer's in-repo copy of a fork PR). When a PR is evaluated, a red check run another PR produced on that head does not count against it. A run whose `pull_requests` names other PRs but not this one is dropped, and so is a run naming no PR (how GitHub reports a fork PR's runs) whose check suite ran for a different head branch. projectbluefin/utah-packages#890 went to 072 for "Failing checks: Canary" on the fork PR #878's Canary run at the same SHA, before #890's own Canary had started. A lookup that fails keeps the run. + When several lanes see the same PR the self-authored lane outranks the trusted-author lane, which outranks the contributor lane; the trusted-author lane's bare `untrusted-author-role` is a weak opinion the contributor lane diff --git a/src/pkg/github/commit_ci.go b/src/pkg/github/commit_ci.go index 8891e4ac19..5b9725f0bd 100644 --- a/src/pkg/github/commit_ci.go +++ b/src/pkg/github/commit_ci.go @@ -547,6 +547,9 @@ func EvaluateCommitCI(ctx context.Context, client *gh.Client, owner, repo, sha s } allCheckRuns = checkRunsOnHead(allCheckRuns, sha) + if opts.PRNumber > 0 { + allCheckRuns = checkRunsForPR(ctx, client, owner, repo, allCheckRuns, opts.PRNumber) + } dcoSatisfied := dcoContextSucceeded(allStatuses, allCheckRuns) noteActivity := func(at time.Time) { @@ -1111,6 +1114,78 @@ func groupedCheckRuns(checks []*gh.CheckRun) map[checkRunIdentity][]*gh.CheckRun return groups } +// checkRunsForPR drops the red check runs another pull request produced on +// this head. GitHub lists check runs per commit, and two PRs can share a +// head: projectbluefin/utah-packages#890 (an in-repo branch) was returned to +// its author for "Failing checks: Canary" 41 seconds after it opened, on the +// Canary run of the fork PR #878 at the same SHA, while #890's own Canary had +// not started. A run naming pull requests counts only when it names this one. +// A red run naming none (GitHub omits fork PRs there) counts only when its +// check suite ran for this PR's head branch. Lookups that fail keep the run, +// so a doubt can only over-block. Green and running runs are kept as they +// are: only a red one can return a PR to its author. +func checkRunsForPR(ctx context.Context, client *gh.Client, owner, repo string, checks []*gh.CheckRun, number int) []*gh.CheckRun { + var ( + headRef string + headRefRead bool + suiteBranch = map[int64]string{} + out = make([]*gh.CheckRun, 0, len(checks)) + prHeadBranch = func() string { + if !headRefRead { + headRefRead = true + if pr, _, err := client.PullRequests.Get(ctx, owner, repo, number); err == nil { + headRef = pr.GetHead().GetRef() + } + } + return headRef + } + ) + for _, cr := range checks { + if cr == nil || cr.GetStatus() != "completed" || !checkConclusionIsRed(cr.GetConclusion()) { + out = append(out, cr) + continue + } + if len(cr.PullRequests) > 0 { + if checkRunNamesPR(cr, owner, repo, number) { + out = append(out, cr) + } + continue + } + suiteID := cr.GetCheckSuite().GetID() + if suiteID == 0 { + out = append(out, cr) + continue + } + branch, ok := suiteBranch[suiteID] + if !ok { + if suite, _, err := client.Checks.GetCheckSuite(ctx, owner, repo, suiteID); err == nil { + branch = suite.GetHeadBranch() + } + suiteBranch[suiteID] = branch + } + if ref := prHeadBranch(); branch != "" && ref != "" && branch != ref { + continue + } + out = append(out, cr) + } + return out +} + +// checkRunNamesPR reports whether the check run's pull_requests list names +// owner/repo#number. An entry without a base repository counts on its number. +func checkRunNamesPR(cr *gh.CheckRun, owner, repo string, number int) bool { + for _, pr := range cr.PullRequests { + if pr.GetNumber() != number { + continue + } + base := pr.GetBase().GetRepo() + if base == nil || base.GetName() == "" || strings.EqualFold(base.GetName(), repo) && (base.GetOwner().GetLogin() == "" || strings.EqualFold(base.GetOwner().GetLogin(), owner)) { + return true + } + } + return false +} + func checkRunsOnHead(checks []*gh.CheckRun, head string) []*gh.CheckRun { var out []*gh.CheckRun for _, check := range checks { diff --git a/src/pkg/github/commit_ci_pr_attribution_test.go b/src/pkg/github/commit_ci_pr_attribution_test.go new file mode 100644 index 0000000000..c434bbd90c --- /dev/null +++ b/src/pkg/github/commit_ci_pr_attribution_test.go @@ -0,0 +1,147 @@ +package github + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "net/url" + "strings" + "testing" + + gh "github.com/google/go-github/v72/github" +) + +// projectbluefin/utah-packages#890 (in-repo branch danathar/supersede-878) +// and the fork PR #878 shared head 95b9d22. #878's fork Canary run failed +// (check suite for branch fix/bot-pr-required-check-handoff, no +// pull_requests, as GitHub reports fork runs); #890's own Canary had not +// started, and #890 went to 072 for "Failing checks: Canary". +func attributionServer(t *testing.T, runs []map[string]any, suiteStatus int, calls map[string]int) *httptest.Server { + t.Helper() + return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + calls[r.URL.Path]++ + switch r.URL.Path { + case "/repos/o/r/commits/95b9d22/status": + _ = json.NewEncoder(w).Encode(map[string]any{"state": "success", "statuses": []any{}}) + case "/repos/o/r/commits/95b9d22/check-runs": + _ = json.NewEncoder(w).Encode(map[string]any{"total_count": len(runs), "check_runs": runs}) + case "/repos/o/r/check-suites/878": + if suiteStatus != 0 { + w.WriteHeader(suiteStatus) + return + } + _, _ = w.Write([]byte(`{"id":878,"head_branch":"fix/bot-pr-required-check-handoff","head_sha":"95b9d22"}`)) + case "/repos/o/r/check-suites/890": + _, _ = w.Write([]byte(`{"id":890,"head_branch":"danathar/supersede-878","head_sha":"95b9d22"}`)) + case "/repos/o/r/pulls/890": + _, _ = w.Write([]byte(`{"number":890,"head":{"ref":"danathar/supersede-878","sha":"95b9d22"}}`)) + case "/repos/o/r/pulls/878": + _, _ = w.Write([]byte(`{"number":878,"head":{"ref":"fix/bot-pr-required-check-handoff","sha":"95b9d22"}}`)) + default: + w.WriteHeader(http.StatusNotFound) + } + })) +} + +func checkRunJSON(id int64, name, status, conclusion string, suite int64, prs ...int) map[string]any { + run := map[string]any{"id": id, "name": name, "status": status, "head_sha": "95b9d22", "app": map[string]any{"id": 15368}, "check_suite": map[string]any{"id": suite}, "started_at": "2026-11-05T21:17:39Z"} + if conclusion != "" { + run["conclusion"] = conclusion + } + list := []any{} + for _, n := range prs { + list = append(list, map[string]any{"number": n, "base": map[string]any{"ref": "main", "repo": map[string]any{"name": "r"}}}) + } + run["pull_requests"] = list + return run +} + +func evaluateAttribution(t *testing.T, srv *httptest.Server, pr int) CommitCIState { + t.Helper() + client := gh.NewClient(nil) + base, err := url.Parse(srv.URL + "/") + if err != nil { + t.Fatal(err) + } + client.BaseURL = base + st, err := EvaluateCommitCI(context.Background(), client, "o", "r", "95b9d22", CommitCIOptions{ + PRNumber: pr, + Required: map[string]bool{"build": true}, + RequiredKnown: true, + FailOnAnyFailedCheck: true, + }) + if err != nil { + t.Fatal(err) + } + return st +} + +func TestCheckRunsAttributedByPR(t *testing.T) { + build := checkRunJSON(1, "build", "completed", "success", 890, 890) + forkCanary := checkRunJSON(2, "Canary", "completed", "failure", 878) + for _, tc := range []struct { + name string + pr int + runs []map[string]any + suiteStatus int + wantRed bool + }{ + {name: "#890: #878's fork Canary on the shared head is not #890's", pr: 890, runs: []map[string]any{build, forkCanary}, wantRed: false}, + {name: "#878: its own fork Canary still counts", pr: 878, runs: []map[string]any{build, forkCanary}, wantRed: true}, + {name: "#890: a run naming another PR only is not #890's", pr: 890, runs: []map[string]any{build, checkRunJSON(3, "Canary", "completed", "failure", 878, 878)}, wantRed: false}, + {name: "#890: its own red Canary counts", pr: 890, runs: []map[string]any{build, checkRunJSON(4, "Canary", "completed", "failure", 890, 890)}, wantRed: true}, + {name: "#890: a red run with no PR on its own branch counts (push run)", pr: 890, runs: []map[string]any{build, checkRunJSON(5, "Canary", "completed", "failure", 890)}, wantRed: true}, + {name: "#890: suite lookup fails, the run is kept", pr: 890, runs: []map[string]any{build, forkCanary}, suiteStatus: http.StatusInternalServerError, wantRed: true}, + } { + t.Run(tc.name, func(t *testing.T) { + calls := map[string]int{} + srv := attributionServer(t, tc.runs, tc.suiteStatus, calls) + defer srv.Close() + st := evaluateAttribution(t, srv, tc.pr) + red := strings.HasPrefix(st.Reason, "check-failing:Canary") || len(st.FailingNonRequired) > 0 + if red != tc.wantRed { + t.Fatalf("Canary red = %v (reason %q, failing %v), want %v", red, st.Reason, st.FailingNonRequired, tc.wantRed) + } + }) + } +} + +// Without a PR number (base-branch or commit-only reads), nothing is +// filtered and no extra lookup is made; green runs never cost one either. +func TestCheckRunAttributionLookupsAreBounded(t *testing.T) { + calls := map[string]int{} + srv := attributionServer(t, []map[string]any{checkRunJSON(1, "build", "completed", "success", 890), checkRunJSON(2, "Canary", "completed", "failure", 878)}, 0, calls) + defer srv.Close() + if st := evaluateAttribution(t, srv, 0); !strings.HasPrefix(st.Reason, "check-failing:Canary") { + t.Fatalf("commit-only read must keep every run: reason %q", st.Reason) + } + if calls["/repos/o/r/check-suites/878"] != 0 || calls["/repos/o/r/check-suites/890"] != 0 { + t.Fatalf("lookups without a PR: %v", calls) + } + calls = map[string]int{} + srv2 := attributionServer(t, []map[string]any{checkRunJSON(1, "build", "completed", "success", 890), checkRunJSON(6, "lint", "completed", "success", 878)}, 0, calls) + defer srv2.Close() + evaluateAttribution(t, srv2, 890) + if calls["/repos/o/r/check-suites/878"] != 0 || calls["/repos/o/r/pulls/890"] != 0 { + t.Fatalf("green runs cost lookups: %v", calls) + } +} + +// Two red fork runs from one check suite cost one suite lookup and one PR read. +func TestCheckRunAttributionCachesSuite(t *testing.T) { + calls := map[string]int{} + srv := attributionServer(t, []map[string]any{ + checkRunJSON(1, "build", "completed", "success", 890, 890), + checkRunJSON(2, "Canary", "completed", "failure", 878), + checkRunJSON(3, "Canary-arm", "completed", "failure", 878), + }, 0, calls) + defer srv.Close() + if st := evaluateAttribution(t, srv, 890); st.Reason != "" || len(st.FailingNonRequired) != 0 { + t.Fatalf("reason %q failing %v, want #878's runs dropped", st.Reason, st.FailingNonRequired) + } + if calls["/repos/o/r/check-suites/878"] != 1 || calls["/repos/o/r/pulls/890"] != 1 { + t.Fatalf("lookups = %v, want one suite read and one PR read", calls) + } +} From caefb9396a430218636a72156d7e7ba1c934f9f6 Mon Sep 17 00:00:00 2001 From: Danathar Date: Thu, 5 Nov 2026 18:01:33 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20fix(ci):=20tell=20a=20fork?= =?UTF-8?q?=20run=20from=20an=20in-repo=20copy=20by=20head=20repository?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewer advisory on #16830: an in-repo copy pushed under the fork's branch name (gh pr checkout 878 && git push origin HEAD) shares the branch, so the suite head_branch alone still charged the fork's red run to it. The check-suite API names no head repository; the Actions workflow run behind the suite does (actions/runs?check_suite_id=). A red unattributed run now counts only when that run's head repository and branch match the PR's head; a suite with no workflow run (another app) falls back to its head_branch. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BQYnPGFJx81c8JXR3o1sg1 Signed-off-by: Danathar --- changelog.d/fixed-ci-attribution-by-pr.md | 2 +- .../stage-labels.md | 2 +- src/pkg/github/commit_ci.go | 73 +++++++++++++------ .../github/commit_ci_pr_attribution_test.go | 36 ++++++--- 4 files changed, 77 insertions(+), 36 deletions(-) diff --git a/changelog.d/fixed-ci-attribution-by-pr.md b/changelog.d/fixed-ci-attribution-by-pr.md index 78c9008099..6e060d8029 100644 --- a/changelog.d/fixed-ci-attribution-by-pr.md +++ b/changelog.d/fixed-ci-attribution-by-pr.md @@ -1 +1 @@ -- A red check run another PR produced on a shared head no longer counts against a PR. projectbluefin/utah-packages#890, an in-repo copy of the fork PR #878 at the same SHA, went to `stage/072-returned-to-contributor` for "Failing checks: Canary" on #878's fork Canary run, 41 seconds after it opened and before its own Canary had started. Check runs are now attributed by PR: a run naming other PRs, or a run naming none whose check suite ran for a different head branch, is not this PR's. Failed lookups keep the run. +- A red check run another PR produced on a shared head no longer counts against a PR. projectbluefin/utah-packages#890, an in-repo copy of the fork PR #878 at the same SHA, went to `stage/072-returned-to-contributor` for "Failing checks: Canary" on #878's fork Canary run, 41 seconds after it opened and before its own Canary had started. Check runs are now attributed by PR: a run naming other PRs, or a run naming none whose workflow run had a different head repository or branch (the branch alone for another app's suite), is not this PR's. Failed lookups keep the run. diff --git a/src/docs/labels-and-control-signals/stage-labels.md b/src/docs/labels-and-control-signals/stage-labels.md index fdc37e60c0..de13ccd764 100644 --- a/src/docs/labels-and-control-signals/stage-labels.md +++ b/src/docs/labels-and-control-signals/stage-labels.md @@ -382,7 +382,7 @@ used to admit the PR: | `contributor-lane:no-claim` / `claim-*` / `author-association` / `no-linked-issue`, `untrusted-author-role`, `author-permission-missing` | human hand-off, **trust** cause → routed by the head's evidence (below); lands at `090-ready-for-human-merge` when the head is otherwise clean | | hand-offs (`not-app-authored`, `trusted-bot-lane`, `trusted-author:app-authored`, `contributor-lane:app-authored`), transient fetch errors, `base-refresh:merge-guard` (the lane skipped the PR because a merge to its destination was in flight) | no opinion — the owning lane decides; nothing is written. An App-authored PR is handed off by the trusted-author and contributor-lane tiers under their own reason; the self-authored lane owns it, and its `review-gate:no-review-bundle` is the stage's reason while the swarm has not reviewed the head ([#14482](https://github.com/hivecommons/hive/issues/14482)) | -**Whose check run it is.** GitHub lists check runs per commit, and two PRs can share a head (a maintainer's in-repo copy of a fork PR). When a PR is evaluated, a red check run another PR produced on that head does not count against it. A run whose `pull_requests` names other PRs but not this one is dropped, and so is a run naming no PR (how GitHub reports a fork PR's runs) whose check suite ran for a different head branch. projectbluefin/utah-packages#890 went to 072 for "Failing checks: Canary" on the fork PR #878's Canary run at the same SHA, before #890's own Canary had started. A lookup that fails keeps the run. +**Whose check run it is.** GitHub lists check runs per commit, and two PRs can share a head (a maintainer's in-repo copy of a fork PR). When a PR is evaluated, a red check run another PR produced on that head does not count against it. A run whose `pull_requests` names other PRs but not this one is dropped, and so is a run naming no PR (how GitHub reports a fork PR's runs) that ran for a different head: the Actions workflow run behind its check suite names the head repository and branch, so an in-repo copy pushed under the fork's branch name is still told apart; a suite from another app, with no workflow run, is judged on its head branch. projectbluefin/utah-packages#890 went to 072 for "Failing checks: Canary" on the fork PR #878's Canary run at the same SHA, before #890's own Canary had started. A lookup that fails keeps the run. When several lanes see the same PR the self-authored lane outranks the trusted-author lane, which outranks the contributor lane; the trusted-author diff --git a/src/pkg/github/commit_ci.go b/src/pkg/github/commit_ci.go index 5b9725f0bd..b329aae6ed 100644 --- a/src/pkg/github/commit_ci.go +++ b/src/pkg/github/commit_ci.go @@ -1120,26 +1120,48 @@ func groupedCheckRuns(checks []*gh.CheckRun) map[checkRunIdentity][]*gh.CheckRun // its author for "Failing checks: Canary" 41 seconds after it opened, on the // Canary run of the fork PR #878 at the same SHA, while #890's own Canary had // not started. A run naming pull requests counts only when it names this one. -// A red run naming none (GitHub omits fork PRs there) counts only when its -// check suite ran for this PR's head branch. Lookups that fail keep the run, -// so a doubt can only over-block. Green and running runs are kept as they -// are: only a red one can return a PR to its author. +// A red run naming none (GitHub omits fork PRs there) counts only when it ran +// for this PR's head: the Actions workflow run behind its check suite names +// the head repository and branch (the check-suite API names no head +// repository, and an in-repo copy pushed under the fork's branch name shares +// the branch); a suite with no workflow run (another app) is judged on its +// head branch alone. Lookups that fail keep the run, so a doubt can only +// over-block. Green and running runs are kept as they are: only a red one can +// return a PR to its author. func checkRunsForPR(ctx context.Context, client *gh.Client, owner, repo string, checks []*gh.CheckRun, number int) []*gh.CheckRun { + type head struct{ repo, branch string } var ( - headRef string - headRefRead bool - suiteBranch = map[int64]string{} - out = make([]*gh.CheckRun, 0, len(checks)) - prHeadBranch = func() string { - if !headRefRead { - headRefRead = true - if pr, _, err := client.PullRequests.Get(ctx, owner, repo, number); err == nil { - headRef = pr.GetHead().GetRef() - } + prHead head + prHeadRead bool + suiteHeads = map[int64]head{} + out = make([]*gh.CheckRun, 0, len(checks)) + ) + prHeadOf := func() head { + if !prHeadRead { + prHeadRead = true + if pr, _, err := client.PullRequests.Get(ctx, owner, repo, number); err == nil { + prHead = head{repo: pr.GetHead().GetRepo().GetFullName(), branch: pr.GetHead().GetRef()} } - return headRef } - ) + return prHead + } + suiteHeadOf := func(id int64) head { + if h, ok := suiteHeads[id]; ok { + return h + } + var h head + runs, _, err := client.Actions.ListRepositoryWorkflowRuns(ctx, owner, repo, &gh.ListWorkflowRunsOptions{CheckSuiteID: id, ListOptions: gh.ListOptions{PerPage: 1}}) + if err == nil && len(runs.WorkflowRuns) > 0 { + run := runs.WorkflowRuns[0] + h = head{repo: run.GetHeadRepository().GetFullName(), branch: run.GetHeadBranch()} + } else if err == nil { + if suite, _, err := client.Checks.GetCheckSuite(ctx, owner, repo, id); err == nil { + h.branch = suite.GetHeadBranch() + } + } + suiteHeads[id] = h + return h + } for _, cr := range checks { if cr == nil || cr.GetStatus() != "completed" || !checkConclusionIsRed(cr.GetConclusion()) { out = append(out, cr) @@ -1156,14 +1178,7 @@ func checkRunsForPR(ctx context.Context, client *gh.Client, owner, repo string, out = append(out, cr) continue } - branch, ok := suiteBranch[suiteID] - if !ok { - if suite, _, err := client.Checks.GetCheckSuite(ctx, owner, repo, suiteID); err == nil { - branch = suite.GetHeadBranch() - } - suiteBranch[suiteID] = branch - } - if ref := prHeadBranch(); branch != "" && ref != "" && branch != ref { + if foreignHead(suiteHeadOf(suiteID), prHeadOf()) { continue } out = append(out, cr) @@ -1171,6 +1186,16 @@ func checkRunsForPR(ctx context.Context, client *gh.Client, owner, repo string, return out } +// foreignHead reports whether a check suite's head is known to differ from +// the PR's: a different head repository when both are known, else a +// different head branch when both are known. Unknown is not foreign. +func foreignHead(suite, pr struct{ repo, branch string }) bool { + if suite.repo != "" && pr.repo != "" && !strings.EqualFold(suite.repo, pr.repo) { + return true + } + return suite.branch != "" && pr.branch != "" && suite.branch != pr.branch +} + // checkRunNamesPR reports whether the check run's pull_requests list names // owner/repo#number. An entry without a base repository counts on its number. func checkRunNamesPR(cr *gh.CheckRun, owner, repo string, number int) bool { diff --git a/src/pkg/github/commit_ci_pr_attribution_test.go b/src/pkg/github/commit_ci_pr_attribution_test.go index c434bbd90c..7b56c1a967 100644 --- a/src/pkg/github/commit_ci_pr_attribution_test.go +++ b/src/pkg/github/commit_ci_pr_attribution_test.go @@ -19,6 +19,13 @@ import ( // started, and #890 went to 072 for "Failing checks: Canary". func attributionServer(t *testing.T, runs []map[string]any, suiteStatus int, calls map[string]int) *httptest.Server { t.Helper() + // Workflow runs behind each check suite, as GET actions/runs?check_suite_id= + // returns them; suite 900 is another app's (no workflow run). + workflowRuns := map[string]string{ + "878": `{"total_count":1,"workflow_runs":[{"id":42111306103,"head_branch":"fix/bot-pr-required-check-handoff","head_repository":{"full_name":"kylerankin/r"}}]}`, + "890": `{"total_count":1,"workflow_runs":[{"id":42114117162,"head_branch":"danathar/supersede-878","head_repository":{"full_name":"o/r"}}]}`, + "900": `{"total_count":0,"workflow_runs":[]}`, + } return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") calls[r.URL.Path]++ @@ -27,18 +34,24 @@ func attributionServer(t *testing.T, runs []map[string]any, suiteStatus int, cal _ = json.NewEncoder(w).Encode(map[string]any{"state": "success", "statuses": []any{}}) case "/repos/o/r/commits/95b9d22/check-runs": _ = json.NewEncoder(w).Encode(map[string]any{"total_count": len(runs), "check_runs": runs}) - case "/repos/o/r/check-suites/878": - if suiteStatus != 0 { + case "/repos/o/r/actions/runs": + suite := r.URL.Query().Get("check_suite_id") + calls["suite:"+suite]++ + if suite == "878" && suiteStatus != 0 { w.WriteHeader(suiteStatus) return } - _, _ = w.Write([]byte(`{"id":878,"head_branch":"fix/bot-pr-required-check-handoff","head_sha":"95b9d22"}`)) - case "/repos/o/r/check-suites/890": - _, _ = w.Write([]byte(`{"id":890,"head_branch":"danathar/supersede-878","head_sha":"95b9d22"}`)) + _, _ = w.Write([]byte(workflowRuns[suite])) + case "/repos/o/r/check-suites/900": + _, _ = w.Write([]byte(`{"id":900,"head_branch":"other-app-branch","head_sha":"95b9d22"}`)) case "/repos/o/r/pulls/890": - _, _ = w.Write([]byte(`{"number":890,"head":{"ref":"danathar/supersede-878","sha":"95b9d22"}}`)) + _, _ = w.Write([]byte(`{"number":890,"head":{"ref":"danathar/supersede-878","sha":"95b9d22","repo":{"full_name":"o/r"}}}`)) + case "/repos/o/r/pulls/891": + // An in-repo copy pushed under the fork's branch name + // (gh pr checkout 878 && git push origin HEAD). + _, _ = w.Write([]byte(`{"number":891,"head":{"ref":"fix/bot-pr-required-check-handoff","sha":"95b9d22","repo":{"full_name":"o/r"}}}`)) case "/repos/o/r/pulls/878": - _, _ = w.Write([]byte(`{"number":878,"head":{"ref":"fix/bot-pr-required-check-handoff","sha":"95b9d22"}}`)) + _, _ = w.Write([]byte(`{"number":878,"head":{"ref":"fix/bot-pr-required-check-handoff","sha":"95b9d22","repo":{"full_name":"kylerankin/r"}}}`)) default: w.WriteHeader(http.StatusNotFound) } @@ -94,6 +107,9 @@ func TestCheckRunsAttributedByPR(t *testing.T) { {name: "#890: its own red Canary counts", pr: 890, runs: []map[string]any{build, checkRunJSON(4, "Canary", "completed", "failure", 890, 890)}, wantRed: true}, {name: "#890: a red run with no PR on its own branch counts (push run)", pr: 890, runs: []map[string]any{build, checkRunJSON(5, "Canary", "completed", "failure", 890)}, wantRed: true}, {name: "#890: suite lookup fails, the run is kept", pr: 890, runs: []map[string]any{build, forkCanary}, suiteStatus: http.StatusInternalServerError, wantRed: true}, + {name: "#891: in-repo copy under the fork's branch name: the fork run is not #891's", pr: 891, runs: []map[string]any{build, forkCanary}, wantRed: false}, + {name: "#890: another app's suite on another branch is not #890's", pr: 890, runs: []map[string]any{build, checkRunJSON(7, "Canary", "completed", "failure", 900)}, wantRed: false}, + {name: "#891: another app's suite is judged on branch alone", pr: 891, runs: []map[string]any{build, checkRunJSON(8, "Canary", "completed", "failure", 900)}, wantRed: false}, } { t.Run(tc.name, func(t *testing.T) { calls := map[string]int{} @@ -117,14 +133,14 @@ func TestCheckRunAttributionLookupsAreBounded(t *testing.T) { if st := evaluateAttribution(t, srv, 0); !strings.HasPrefix(st.Reason, "check-failing:Canary") { t.Fatalf("commit-only read must keep every run: reason %q", st.Reason) } - if calls["/repos/o/r/check-suites/878"] != 0 || calls["/repos/o/r/check-suites/890"] != 0 { + if calls["/repos/o/r/actions/runs"] != 0 || calls["/repos/o/r/pulls/890"] != 0 { t.Fatalf("lookups without a PR: %v", calls) } calls = map[string]int{} srv2 := attributionServer(t, []map[string]any{checkRunJSON(1, "build", "completed", "success", 890), checkRunJSON(6, "lint", "completed", "success", 878)}, 0, calls) defer srv2.Close() evaluateAttribution(t, srv2, 890) - if calls["/repos/o/r/check-suites/878"] != 0 || calls["/repos/o/r/pulls/890"] != 0 { + if calls["/repos/o/r/actions/runs"] != 0 || calls["/repos/o/r/pulls/890"] != 0 { t.Fatalf("green runs cost lookups: %v", calls) } } @@ -141,7 +157,7 @@ func TestCheckRunAttributionCachesSuite(t *testing.T) { if st := evaluateAttribution(t, srv, 890); st.Reason != "" || len(st.FailingNonRequired) != 0 { t.Fatalf("reason %q failing %v, want #878's runs dropped", st.Reason, st.FailingNonRequired) } - if calls["/repos/o/r/check-suites/878"] != 1 || calls["/repos/o/r/pulls/890"] != 1 { + if calls["suite:878"] != 1 || calls["/repos/o/r/pulls/890"] != 1 { t.Fatalf("lookups = %v, want one suite read and one PR read", calls) } }