Skip to content

Commit 7b68431

Browse files
malfetpytorchmergebot
authored andcommitted
[BE][GHF] Do not call GraphQL twice (#100434)
During regular merge process, `GitHubPR` and `GitHubRepo` objects are first created in main() and than re-created in `merge()` instead of being passed by reference, which results in making the same GraphQL requests to the repo twice <!-- copilot:poem --> ### <samp>🤖 Generated by Copilot at ee4e23e</samp> > _Sing, O Muse, of the skillful coder who refactored_ > _The `merge` function, to accept a `GitHubPR` object,_ > _And thus reduced the calls to the divine API_ > _And the duplication of code, that source of errors._ Pull Request resolved: #100434 Approved by: https://github.com/kit1980, https://github.com/PaliC, https://github.com/huydhn, https://github.com/ZainRizvi
1 parent dc9c79d commit 7b68431

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

.github/scripts/test_trymerge.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def mock_revert(
136136

137137

138138
def mock_merge(
139-
pr_num: int,
139+
pr: GitHubPR,
140140
repo: GitRepo,
141141
dry_run: bool = False,
142142
skip_mandatory_checks: bool = False,

.github/scripts/trymerge.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,7 +1756,7 @@ def categorize_checks(
17561756

17571757

17581758
def merge(
1759-
pr_num: int,
1759+
pr: GitHubPR,
17601760
repo: GitRepo,
17611761
dry_run: bool = False,
17621762
skip_mandatory_checks: bool = False,
@@ -1765,17 +1765,19 @@ def merge(
17651765
stale_pr_days: int = 3,
17661766
ignore_current: bool = False,
17671767
) -> None:
1768-
repo = GitRepo(get_git_repo_dir(), get_git_remote_name())
1769-
org, project = repo.gh_owner_and_name()
1770-
pr = GitHubPR(org, project, pr_num)
17711768
initial_commit_sha = pr.last_commit()["oid"]
17721769
print(f"Attempting merge of {initial_commit_sha}")
17731770

17741771
if MERGE_IN_PROGRESS_LABEL not in pr.get_labels():
1775-
gh_add_labels(org, project, pr_num, [MERGE_IN_PROGRESS_LABEL])
1772+
gh_add_labels(pr.org, pr.project, pr.pr_num, [MERGE_IN_PROGRESS_LABEL])
17761773

17771774
explainer = TryMergeExplainer(
1778-
skip_mandatory_checks, pr.get_labels(), pr.pr_num, org, project, ignore_current
1775+
skip_mandatory_checks,
1776+
pr.get_labels(),
1777+
pr.pr_num,
1778+
pr.org,
1779+
pr.project,
1780+
ignore_current,
17791781
)
17801782

17811783
# probably a bad name, but this is a list of current checks that should be
@@ -1785,12 +1787,16 @@ def merge(
17851787
if pr.is_ghstack_pr():
17861788
get_ghstack_prs(repo, pr) # raises error if out of sync
17871789

1788-
check_for_sev(org, project, skip_mandatory_checks)
1790+
check_for_sev(pr.org, pr.project, skip_mandatory_checks)
17891791

17901792
if skip_mandatory_checks or can_skip_internal_checks(pr, comment_id):
17911793
# do not wait for any pending signals if PR is closed as part of co-development process
17921794
gh_post_pr_comment(
1793-
org, project, pr.pr_num, explainer.get_merge_message(), dry_run=dry_run
1795+
pr.org,
1796+
pr.project,
1797+
pr.pr_num,
1798+
explainer.get_merge_message(),
1799+
dry_run=dry_run,
17941800
)
17951801
return pr.merge_into(
17961802
repo,
@@ -1811,8 +1817,8 @@ def merge(
18111817
ignore_current_checks_info = failing
18121818

18131819
gh_post_pr_comment(
1814-
org,
1815-
project,
1820+
pr.org,
1821+
pr.project,
18161822
pr.pr_num,
18171823
explainer.get_merge_message(ignore_current_checks_info),
18181824
dry_run=dry_run,
@@ -1838,13 +1844,13 @@ def merge(
18381844
x[0] for x in ignore_current_checks_info
18391845
] # convert to List[str] for convenience
18401846
while elapsed_time < timeout_minutes * 60:
1841-
check_for_sev(org, project, skip_mandatory_checks)
1847+
check_for_sev(pr.org, pr.project, skip_mandatory_checks)
18421848
current_time = time.time()
18431849
elapsed_time = current_time - start_time
18441850
print(
1845-
f"Attempting merge of https://github.com/{org}/{project}/pull/{pr_num} ({elapsed_time / 60} minutes elapsed)"
1851+
f"Attempting merge of https://github.com/{pr.org}/{pr.project}/pull/{pr.pr_num} ({elapsed_time / 60} minutes elapsed)"
18461852
)
1847-
pr = GitHubPR(org, project, pr_num)
1853+
pr = GitHubPR(pr.org, pr.project, pr.pr_num)
18481854
if initial_commit_sha != pr.last_commit()["oid"]:
18491855
raise RuntimeError(
18501856
"New commits were pushed while merging. Please rerun the merge command."
@@ -1913,14 +1919,14 @@ def merge(
19131919
except MandatoryChecksMissingError as ex:
19141920
last_exception = str(ex)
19151921
print(
1916-
f"Merge of https://github.com/{org}/{project}/pull/{pr_num} failed due to: {ex}. Retrying in 5 min"
1922+
f"Merge of https://github.com/{pr.org}/{pr.project}/pull/{pr.pr_num} failed due to: {ex}. Retrying in 5 min"
19171923
)
19181924
time.sleep(5 * 60)
19191925
# Finally report timeout back
19201926
msg = f"Merged timed out after {timeout_minutes} minutes. Please contact the pytorch_dev_infra team."
19211927
msg += f"The last exception was: {last_exception}"
19221928
if not dry_run:
1923-
gh_add_labels(org, project, pr_num, ["land-failed"])
1929+
gh_add_labels(pr.org, pr.project, pr.pr_num, ["land-failed"])
19241930
raise RuntimeError(msg)
19251931

19261932

@@ -2008,7 +2014,7 @@ def handle_exception(e: Exception, title: str = "Merge failed") -> None:
20082014
return
20092015
try:
20102016
merge(
2011-
args.pr_num,
2017+
pr,
20122018
repo,
20132019
dry_run=args.dry_run,
20142020
skip_mandatory_checks=args.force,

0 commit comments

Comments
 (0)