Skip to content

Commit 4f2a475

Browse files
Catherine Leepytorchmergebot
authored andcommitted
refactor add_conclusions in trymerge.py (#82371)
### Description There were two very similar functions in trymerge so I tried to refactor so that they was less duplicated code. ### Issue <!-- Link to Issue ticket or RFP --> ### Testing <!-- How did you test your change? --> Ran `python test_trymerge.py` to test Pull Request resolved: #82371 Approved by: https://github.com/huydhn
1 parent 69a32e6 commit 4f2a475

2 files changed

Lines changed: 79 additions & 81 deletions

File tree

.github/scripts/test_trymerge.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def test_internal_changes(self, mocked_gql: Any) -> None:
194194
def test_checksuites_pagination(self, mocked_gql: Any) -> None:
195195
"Tests that PR with lots of checksuits can be fetched"
196196
pr = GitHubPR("pytorch", "pytorch", 73811)
197-
self.assertGreater(len(pr.get_checkrun_conclusions()), 0)
197+
self.assertEqual(len(pr.get_checkrun_conclusions()), 104)
198198

199199
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)
200200
def test_comments_pagination(self, mocked_gql: Any) -> None:
@@ -258,6 +258,7 @@ def test_get_checkruns_many_runs(self, mocked_gql: Any) -> None:
258258
"""
259259
pr = GitHubPR("pytorch", "pytorch", 77700)
260260
conclusions = pr.get_checkrun_conclusions()
261+
self.assertEqual(len(conclusions), 83)
261262
self.assertTrue("pull / linux-docs / build-docs (cpp)" in conclusions.keys())
262263

263264
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)
@@ -274,7 +275,7 @@ def test_get_many_land_checks(self, mocked_gql: Any) -> None:
274275
""" Tests that all checkruns can be fetched for a commit
275276
"""
276277
conclusions = get_land_checkrun_conclusions('pytorch', 'pytorch', '6882717f73deffb692219ccd1fd6db258d8ed684')
277-
self.assertGreater(len(conclusions), 100)
278+
self.assertEqual(len(conclusions), 101)
278279
self.assertTrue("pull / linux-docs / build-docs (cpp)" in conclusions.keys())
279280

280281
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)

.github/scripts/trymerge.py

Lines changed: 76 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,50 @@ def get_check_run_name_prefix(workflow_run: Any) -> str:
464464
else:
465465
return f'{workflow_run["workflow"]["name"]} / '
466466

467+
468+
def add_workflow_conclusions(
469+
checksuites: Any,
470+
get_next_checkruns_page: Callable[[List[Dict[str, Dict[str, Any]]], int, Any], Any],
471+
get_next_checksuites: Callable[[Any], Any]
472+
) -> Dict[str, Tuple[str, str]]:
473+
conclusions = {}
474+
475+
def add_conclusions(edges: Any) -> None:
476+
for edge_idx, edge in enumerate(edges):
477+
node = edge["node"]
478+
workflow_run = node["workflowRun"]
479+
checkruns = node["checkRuns"]
480+
if workflow_run is not None:
481+
workflow_name = workflow_run["workflow"]["name"]
482+
workflow_conclusion = node["conclusion"]
483+
# Do not override existing status with cancelled
484+
if workflow_conclusion == "CANCELLED" and workflow_name in conclusions:
485+
continue
486+
conclusions[workflow_name] = (workflow_conclusion, node["url"])
487+
has_failing_check = False
488+
while checkruns is not None:
489+
for checkrun_node in checkruns["nodes"]:
490+
if checkrun_node["conclusion"] == 'FAILURE':
491+
has_failing_check = True
492+
conclusions[f'{get_check_run_name_prefix(workflow_run)}{checkrun_node["name"]}'] = (
493+
checkrun_node["conclusion"], checkrun_node["detailsUrl"]
494+
)
495+
if bool(checkruns["pageInfo"]["hasNextPage"]):
496+
checkruns = get_next_checkruns_page(edges, edge_idx, checkruns)
497+
else:
498+
checkruns = None
499+
# Github doesn't set conclusion to failure if a job is still pending
500+
if workflow_run is not None and has_failing_check:
501+
conclusions[workflow_run["workflow"]["name"]] = ("FAILURE", node["url"])
502+
503+
add_conclusions(checksuites["edges"])
504+
while bool(checksuites["pageInfo"]["hasNextPage"]):
505+
checksuites = get_next_checksuites(checksuites)
506+
add_conclusions(checksuites["edges"])
507+
508+
return conclusions
509+
510+
467511
def parse_args() -> Any:
468512
from argparse import ArgumentParser
469513
parser = ArgumentParser("Merge PR into default branch")
@@ -638,46 +682,19 @@ def get_checkrun_conclusions(self) -> Dict[str, Tuple[str, str]]:
638682
if self.conclusions is not None:
639683
return self.conclusions
640684
orig_last_commit = self.info["commits"]["nodes"][-1]["commit"]
641-
checksuites = orig_last_commit["checkSuites"]
642-
conclusions = {}
643-
644-
def add_conclusions(edges: List[Dict[str, Dict[str, Any]]]) -> None:
645-
for edge_idx, edge in enumerate(edges):
646-
node = edge["node"]
647-
workflow_run = node["workflowRun"]
648-
checkruns = node["checkRuns"]
649-
if workflow_run is not None:
650-
workflow_name = workflow_run["workflow"]["name"]
651-
workflow_conclusion = node["conclusion"]
652-
# Do not override existing status with cancelled
653-
if workflow_conclusion == "CANCELLED" and workflow_name in conclusions:
654-
continue
655-
conclusions[workflow_name] = (workflow_conclusion, node["url"])
656-
has_failing_check = False
657-
while checkruns is not None:
658-
for checkrun_node in checkruns["nodes"]:
659-
if checkrun_node["conclusion"] == 'FAILURE':
660-
has_failing_check = True
661-
conclusions[f'{get_check_run_name_prefix(workflow_run)}{checkrun_node["name"]}'] = (
662-
checkrun_node["conclusion"], checkrun_node["detailsUrl"]
663-
)
664-
if bool(checkruns["pageInfo"]["hasNextPage"]):
665-
rc = gh_graphql(GH_GET_PR_NEXT_CHECK_RUNS,
666-
name=self.project,
667-
owner=self.org,
668-
number=self.pr_num,
669-
cs_cursor=edges[edge_idx - 1]["cursor"] if edge_idx > 0 else None,
670-
cr_cursor=checkruns["pageInfo"]["endCursor"])
671-
last_commit = rc["data"]["repository"]["pullRequest"]["commits"]["nodes"][-1]["commit"]
672-
checkruns = last_commit["checkSuites"]["nodes"][-1]["checkRuns"]
673-
else:
674-
checkruns = None
675-
# Github doesn't set conclusion to failure if a job is still pending
676-
if workflow_run is not None and has_failing_check:
677-
conclusions[workflow_run["workflow"]["name"]] = ("FAILURE", node["url"])
678685

679-
add_conclusions(checksuites["edges"])
680-
while bool(checksuites["pageInfo"]["hasNextPage"]):
686+
def get_pr_next_check_runs(edges: List[Dict[str, Dict[str, Any]]], edge_idx: int, checkruns: Any) -> Any:
687+
rc = gh_graphql(GH_GET_PR_NEXT_CHECK_RUNS,
688+
name=self.project,
689+
owner=self.org,
690+
number=self.pr_num,
691+
cs_cursor=edges[edge_idx - 1]["cursor"] if edge_idx > 0 else None,
692+
cr_cursor=checkruns["pageInfo"]["endCursor"])
693+
last_commit = rc["data"]["repository"]["pullRequest"]["commits"]["nodes"][-1]["commit"]
694+
checkruns = last_commit["checkSuites"]["nodes"][-1]["checkRuns"]
695+
return checkruns
696+
697+
def get_pr_next_checksuites(checksuites: Any) -> Any:
681698
rc = gh_graphql(GH_GET_PR_NEXT_CHECKSUITES,
682699
name=self.project,
683700
owner=self.org,
@@ -687,10 +704,12 @@ def add_conclusions(edges: List[Dict[str, Dict[str, Any]]]) -> None:
687704
last_commit = info["commits"]["nodes"][-1]["commit"]
688705
if last_commit["oid"] != orig_last_commit["oid"]:
689706
raise RuntimeError("Last commit changed on PR")
690-
checksuites = last_commit["checkSuites"]
691-
add_conclusions(checksuites["edges"])
692-
self.conclusions = conclusions
693-
return conclusions
707+
return last_commit["checkSuites"]
708+
709+
checksuites = orig_last_commit["checkSuites"]
710+
711+
self.conclusions = add_workflow_conclusions(checksuites, get_pr_next_check_runs, get_pr_next_checksuites)
712+
return self.conclusions
694713

695714
def get_authors(self) -> Dict[str, str]:
696715
rc = {}
@@ -988,51 +1007,29 @@ def find_matching_merge_rule(pr: GitHubPR,
9881007

9891008

9901009
def get_land_checkrun_conclusions(org: str, project: str, commit: str) -> Dict[str, Tuple[str, str]]:
991-
land_check_info = gh_get_land_check_info(org, project, commit)
992-
checksuites = land_check_info["checkSuites"]
993-
conclusions = {}
9941010

995-
def add_conclusions(edges: List[Dict[str, Dict[str, Any]]]) -> None:
996-
for edge_idx, edge in enumerate(edges):
997-
node = edge["node"]
998-
workflow_run = node["workflowRun"]
999-
checkruns = node["checkRuns"]
1000-
if workflow_run is not None:
1001-
conclusions[workflow_run["workflow"]["name"]] = (node["conclusion"], node["url"])
1002-
has_failing_check = False
1003-
while checkruns is not None:
1004-
for checkrun_node in checkruns["nodes"]:
1005-
if checkrun_node["conclusion"] == 'FAILURE':
1006-
has_failing_check = True
1007-
conclusions[f'{get_check_run_name_prefix(workflow_run)}{checkrun_node["name"]}'] = (
1008-
checkrun_node["conclusion"], checkrun_node["detailsUrl"]
1009-
)
1010-
if bool(checkruns["pageInfo"]["hasNextPage"]):
1011-
rc = gh_graphql(GH_GET_COMMIT_NEXT_CHECK_RUNS,
1012-
name=project,
1013-
owner=org,
1014-
cs_cursor=edges[edge_idx - 1]["cursor"] if edge_idx > 0 else None,
1015-
cr_cursor=checkruns["pageInfo"]["endCursor"],
1016-
commit=commit)
1017-
checkruns = rc["data"]["repository"]["object"]["checkSuites"]["nodes"][-1]["checkRuns"]
1018-
else:
1019-
checkruns = None
1020-
# Github doesn't set conclusion to failure if a job is still pending
1021-
if workflow_run is not None and has_failing_check:
1022-
conclusions[workflow_run["workflow"]["name"]] = ("FAILURE", node["url"])
1011+
def get_commit_next_check_runs(edges: List[Dict[str, Dict[str, Any]]], edge_idx: int, checkruns: Any) -> Any:
1012+
rc = gh_graphql(GH_GET_COMMIT_NEXT_CHECK_RUNS,
1013+
name=project,
1014+
owner=org,
1015+
cs_cursor=edges[edge_idx - 1]["cursor"] if edge_idx > 0 else None,
1016+
cr_cursor=checkruns["pageInfo"]["endCursor"],
1017+
commit=commit)
1018+
return rc["data"]["repository"]["object"]["checkSuites"]["nodes"][-1]["checkRuns"]
10231019

1024-
add_conclusions(checksuites["edges"])
1025-
while bool(checksuites["pageInfo"]["hasNextPage"]):
1020+
def get_commit_next_checksuites(checksuites: Any) -> Any:
10261021
rc = gh_graphql(GH_GET_COMMIT_NEXT_CHECKSUITES,
10271022
name=project,
10281023
owner=org,
10291024
commit=commit,
10301025
cursor=checksuites["edges"][-1]["cursor"])
10311026
info = rc["data"]["repository"]["object"]
1032-
checksuites = info["checkSuites"]
1033-
add_conclusions(checksuites["edges"])
1027+
return info["checkSuites"]
10341028

1035-
return conclusions
1029+
land_check_info = gh_get_land_check_info(org, project, commit)
1030+
checksuites = land_check_info["checkSuites"]
1031+
1032+
return add_workflow_conclusions(checksuites, get_commit_next_check_runs, get_commit_next_checksuites)
10361033

10371034

10381035
def checks_to_str(checks: List[Tuple[str, Optional[str]]]) -> str:

0 commit comments

Comments
 (0)