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
40 changes: 39 additions & 1 deletion internal/prstatus/prstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ type rawCheck struct {
Status string `json:"status"`
Conclusion string `json:"conclusion"`
State string `json:"state"`
// Name, WorkflowName and StartedAt identify re-runs of the same job:
// a run cancelled by a newer one (a concurrency group, a manual
// re-run) stays in the rollup alongside its replacement.
Name string `json:"name"`
WorkflowName string `json:"workflowName"`
StartedAt string `json:"startedAt"`
}

// Fetch reports on a pull request via the gh CLI. With prURL set it looks up
Expand Down Expand Up @@ -201,7 +207,7 @@ func aggregateCI(checks []rawCheck) string {
return "NONE"
}
pending := false
for _, c := range checks {
for _, c := range latestRuns(checks) {
if c.Typename == "StatusContext" {
switch c.State {
case "FAILURE", "ERROR":
Expand All @@ -227,3 +233,35 @@ func aggregateCI(checks []rawCheck) string {
}
return "PASSING"
}

// latestRuns drops CheckRuns superseded by a newer run of the same job, so a
// run cancelled in favour of its replacement doesn't mark the PR failing.
// Unnamed checks and StatusContexts (already latest-per-context) pass through.
func latestRuns(checks []rawCheck) []rawCheck {
started := func(c rawCheck) string {
// A queued run has no start time yet (or Go's zero time) but is
// the newest of its job.
if c.StartedAt == "" || strings.HasPrefix(c.StartedAt, "0001-") {
return "9999"
}
return c.StartedAt // RFC 3339 UTC, so it sorts as a string
}
latest := map[string]int{}
var out []rawCheck
for _, c := range checks {
if c.Typename == "StatusContext" || c.Name == "" {
out = append(out, c)
continue
}
key := c.WorkflowName + "\x00" + c.Name
if i, ok := latest[key]; ok {
if started(c) > started(out[i]) {
out[i] = c
}
continue
}
latest[key] = len(out)
out = append(out, c)
}
return out
}
24 changes: 24 additions & 0 deletions internal/prstatus/prstatus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ func TestAggregateCI(t *testing.T) {
[]rawCheck{{Status: "IN_PROGRESS"}, {Status: "COMPLETED", Conclusion: "FAILURE"}},
"FAILING",
},
{
"run cancelled by a newer successful run of the same job",
[]rawCheck{
{Name: "build", WorkflowName: "CI", StartedAt: "2026-09-30T12:55:43Z", Status: "COMPLETED", Conclusion: "CANCELLED"},
{Name: "build", WorkflowName: "CI", StartedAt: "2026-09-30T12:55:49Z", Status: "COMPLETED", Conclusion: "SUCCESS"},
},
"PASSING",
},
{
"newer run of a job is queued, older one failed",
[]rawCheck{
{Name: "build", WorkflowName: "CI", StartedAt: "2026-09-30T12:55:43Z", Status: "COMPLETED", Conclusion: "FAILURE"},
{Name: "build", WorkflowName: "CI", StartedAt: "0001-01-01T00:00:00Z", Status: "QUEUED"},
},
"PENDING",
},
{
"latest run of a job cancelled still fails",
[]rawCheck{
{Name: "build", WorkflowName: "CI", StartedAt: "2026-09-30T12:55:49Z", Status: "COMPLETED", Conclusion: "CANCELLED"},
{Name: "build", WorkflowName: "CI", StartedAt: "2026-09-30T12:55:43Z", Status: "COMPLETED", Conclusion: "SUCCESS"},
},
"FAILING",
},
{
"legacy StatusContext failure",
[]rawCheck{{Typename: "StatusContext", State: "FAILURE"}},
Expand Down
Loading