Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-ci-attribution-by-pr.md
Original file line number Diff line number Diff line change
@@ -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 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.
2 changes: 2 additions & 0 deletions src/docs/labels-and-control-signals/stage-labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) 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
lane's bare `untrusted-author-role` is a weak opinion the contributor lane
Expand Down
100 changes: 100 additions & 0 deletions src/pkg/github/commit_ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -1111,6 +1114,103 @@ 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 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 (
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 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)
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
}
if foreignHead(suiteHeadOf(suiteID), prHeadOf()) {
continue
}
out = append(out, cr)
}
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 {
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 {
Expand Down
163 changes: 163 additions & 0 deletions src/pkg/github/commit_ci_pr_attribution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
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()
// 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]++
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/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(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","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","repo":{"full_name":"kylerankin/r"}}}`))
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},
{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{}
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/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/actions/runs"] != 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["suite:878"] != 1 || calls["/repos/o/r/pulls/890"] != 1 {
t.Fatalf("lookups = %v, want one suite read and one PR read", calls)
}
}
Loading