Skip to content

Commit c7fb557

Browse files
alexsohn1126claude
andauthored
Add list_check_runs_for_ref to the GitHub client (#96)
Adds a `list_check_runs_for_ref` method to the GitHub client that hits `GET /repos/{owner}/{repo}/commits/{ref}/check-runs` to fetch check runs for a commit SHA (or git ref). This will allow us to go from PR number to the latest check runs in just two steps: - `/repos/{owner}/{repo}/pulls/{pull_number}` -> we can get the `head.sha` to get the head commit SHA - `/repos/{owner}/{repo}/commits/{head.sha}/check-runs?filter=latest` -> we can filter by the latest check run, essentially the latest check suite part of CW-1428 Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent a1e59d8 commit c7fb557

6 files changed

Lines changed: 120 additions & 1 deletion

File tree

bin/github-client

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ from scm.types import (
9797
DeleteCommitAction,
9898
DownloadArchiveProtocol,
9999
GetAppInstallationProtocol,
100-
GetAuthenticatedActorProtocol,
101100
GetArchiveLinkProtocol,
101+
GetAuthenticatedActorProtocol,
102102
GetBranchProtocol,
103103
GetCheckRunProtocol,
104104
GetCommitChangesProtocol,
@@ -126,6 +126,7 @@ from scm.types import (
126126
GetRepositoryTopicsProtocol,
127127
GetRepositoryUserPermissionProtocol,
128128
GetReviewCommentsProtocol,
129+
ListCheckRunsForRefProtocol,
129130
ListCheckRunsInCheckSuiteProtocol,
130131
ListRepositoriesProtocol,
131132
ListRepositoryUserPermissionsProtocol,
@@ -333,6 +334,14 @@ def main() -> None:
333334
p.add_argument("--cursor", type=int, default=None)
334335
p.add_argument("--per-page", type=int, default=None)
335336

337+
p = sub.add_parser("list-check-runs-for-ref")
338+
p.add_argument("ref")
339+
p.add_argument("--check-name", default=None)
340+
p.add_argument("--status", choices=["queued", "in_progress", "completed"], default=None)
341+
p.add_argument("--filter", choices=["latest", "all"], default=None)
342+
p.add_argument("--cursor", type=int, default=None)
343+
p.add_argument("--per-page", type=int, default=None)
344+
336345
p = sub.add_parser("get-branch")
337346
p.add_argument("name")
338347

@@ -575,6 +584,23 @@ def main() -> None:
575584
)
576585
)
577586

587+
elif args.command == "list-check-runs-for-ref":
588+
assert isinstance(scm, ListCheckRunsForRefProtocol)
589+
pagination: PaginationParams = {}
590+
if args.cursor is not None:
591+
pagination["cursor"] = args.cursor
592+
if args.per_page is not None:
593+
pagination["per_page"] = args.per_page
594+
dump(
595+
scm.list_check_runs_for_ref(
596+
args.ref,
597+
check_name=args.check_name,
598+
status=args.status,
599+
timestamp_filter=args.filter,
600+
pagination=pagination or None,
601+
)
602+
)
603+
578604
elif args.command == "get-branch":
579605
assert isinstance(scm, GetBranchProtocol)
580606
dump(scm.get_branch(args.name))

src/scm/actions.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
Issue,
103103
IssueState,
104104
Label,
105+
ListCheckRunsForRefProtocol,
105106
ListCheckRunsInCheckSuiteProtocol,
106107
ListRepositoriesProtocol,
107108
ListRepositoryUserPermissionsProtocol,
@@ -850,6 +851,25 @@ def list_check_runs_in_check_suite(
850851
)
851852

852853

854+
def list_check_runs_for_ref(
855+
scm: ListCheckRunsForRefProtocol,
856+
ref: str,
857+
check_name: str | None = None,
858+
status: Literal["queued", "in_progress", "completed"] | None = None,
859+
timestamp_filter: Literal["latest", "all"] = "latest",
860+
pagination: PaginationParams | None = None,
861+
request_options: RequestOptions | None = None,
862+
) -> PaginatedActionResult[list[CheckRun]]:
863+
return scm.list_check_runs_for_ref(
864+
ref,
865+
check_name=check_name,
866+
status=status,
867+
timestamp_filter=timestamp_filter,
868+
pagination=pagination,
869+
request_options=request_options,
870+
)
871+
872+
853873
def minimize_comment(scm: MinimizeCommentProtocol, comment_node_id: str, reason: str) -> None:
854874
return scm.minimize_comment(comment_node_id, reason)
855875

@@ -978,6 +998,7 @@ def download_archive(
978998
"get_repository_topics",
979999
"get_thread_id_from_review_comment_unique_id",
9801000
"get_tree",
1001+
"list_check_runs_for_ref",
9811002
"list_repository_user_permissions",
9821003
"list_repositories",
9831004
"minimize_comment",

src/scm/providers/github/provider.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,31 @@ def list_check_runs_in_check_suite(
15891589
)
15901590
return map_paginated_action(pagination, response, lambda r: [map_check_run(f) for f in r["check_runs"]])
15911591

1592+
def list_check_runs_for_ref(
1593+
self,
1594+
ref: str,
1595+
check_name: str | None = None,
1596+
status: Literal["queued", "in_progress", "completed"] | None = None,
1597+
timestamp_filter: Literal["latest", "all"] = "latest",
1598+
pagination: PaginationParams | None = None,
1599+
request_options: RequestOptions | None = None,
1600+
) -> PaginatedActionResult[list[CheckRun]]:
1601+
params: dict[str, Any] = {}
1602+
if check_name is not None:
1603+
params["check_name"] = check_name
1604+
if status is not None:
1605+
params["status"] = status
1606+
if timestamp_filter is not None:
1607+
params["filter"] = timestamp_filter
1608+
1609+
response = self.get(
1610+
f"/repos/{self.repository['name']}/commits/{ref}/check-runs",
1611+
params=params,
1612+
pagination=pagination,
1613+
request_options=request_options,
1614+
)
1615+
return map_paginated_action(pagination, response, lambda r: [map_check_run(f) for f in r["check_runs"]])
1616+
15921617
def get_archive_link(
15931618
self,
15941619
ref: str,

src/scm/types.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,19 @@ def list_check_runs_in_check_suite(
12531253
) -> PaginatedActionResult[list[CheckRun]]: ...
12541254

12551255

1256+
@runtime_checkable
1257+
class ListCheckRunsForRefProtocol(Protocol):
1258+
def list_check_runs_for_ref(
1259+
self,
1260+
ref: str,
1261+
check_name: str | None = None,
1262+
status: Literal["queued", "in_progress", "completed"] | None = None,
1263+
timestamp_filter: Literal["latest", "all"] = "latest",
1264+
pagination: PaginationParams | None = None,
1265+
request_options: RequestOptions | None = None,
1266+
) -> PaginatedActionResult[list[CheckRun]]: ...
1267+
1268+
12561269
# Review Protocols
12571270

12581271

@@ -1463,6 +1476,7 @@ def update_and_collapse_pull_request_comment(
14631476
GetTreeProtocol,
14641477
ListRepositoriesProtocol,
14651478
ListCheckRunsInCheckSuiteProtocol,
1479+
ListCheckRunsForRefProtocol,
14661480
MinimizeCommentProtocol,
14671481
RequestReviewProtocol,
14681482
ResolveReviewThreadProtocol,

tests/unit/provider/test_github.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,32 @@ def expected_check_run(raw: dict[str, Any]) -> dict[str, Any]:
721721
"expected_data": [expected_check_run(CHECK_RUN_RAW)],
722722
"next_cursor": "4",
723723
},
724+
{
725+
"name": "list_check_runs_for_ref",
726+
"kwargs": {"ref": "abc123"},
727+
"path": "/repos/test-org/test-repo/commits/abc123/check-runs",
728+
"params": {"filter": "latest"},
729+
"pagination": None,
730+
"raw": {"total_count": 1, "check_runs": [CHECK_RUN_RAW]},
731+
"expected_data": [expected_check_run(CHECK_RUN_RAW)],
732+
"next_cursor": "2",
733+
},
734+
{
735+
"name": "list_check_runs_for_ref",
736+
"kwargs": {
737+
"ref": "abc123",
738+
"check_name": "Seer Review",
739+
"status": "completed",
740+
"timestamp_filter": "all",
741+
"pagination": {"cursor": "3", "per_page": 10},
742+
},
743+
"path": "/repos/test-org/test-repo/commits/abc123/check-runs",
744+
"params": {"check_name": "Seer Review", "status": "completed", "filter": "all"},
745+
"pagination": {"cursor": "3", "per_page": 10},
746+
"raw": {"total_count": 1, "check_runs": [CHECK_RUN_RAW]},
747+
"expected_data": [expected_check_run(CHECK_RUN_RAW)],
748+
"next_cursor": "4",
749+
},
724750
]
725751

726752

tests/unit/test_rpc_integration.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,13 @@ def make_client_scm(organization_id, repository_id, server):
694694
200,
695695
None,
696696
),
697+
(
698+
"list_check_runs_for_ref",
699+
lambda scm: actions.list_check_runs_for_ref(scm, "abc123"),
700+
{"total_count": 1, "check_runs": [make_github_check_run()]},
701+
200,
702+
None,
703+
),
697704
# Minimize comment (GraphQL)
698705
(
699706
"minimize_comment",

0 commit comments

Comments
 (0)