Skip to content

Commit 624bc36

Browse files
ZainRizvipytorchmergebot
authored andcommitted
Ensure the comment id is always passed in to trymerge (pytorch#161558)
Pull Request resolved: pytorch#161558 Approved by: https://github.com/seemethere, https://github.com/malfet
1 parent 06c7516 commit 624bc36

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

.github/scripts/test_trymerge.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __init__(self) -> None:
124124
self.force = force
125125
self.pr_num = 76123
126126
self.dry_run = True
127-
self.comment_id = 0
127+
self.comment_id = 12345 # Set to non-zero value
128128
self.reason = "this is for testing"
129129
self.ignore_current = False
130130
self.check_mergeability = False
@@ -152,9 +152,9 @@ def mock_revert(
152152
def mock_merge(
153153
pr: GitHubPR,
154154
repo: GitRepo,
155+
comment_id: int,
155156
dry_run: bool = False,
156157
skip_mandatory_checks: bool = False,
157-
comment_id: Optional[int] = None,
158158
timeout_minutes: int = 400,
159159
stale_pr_days: int = 3,
160160
ignore_current: bool = False,
@@ -470,9 +470,9 @@ def test_main_force(
470470
mock_merge.assert_called_once_with(
471471
mock.ANY,
472472
mock.ANY,
473+
comment_id=mock.ANY,
473474
dry_run=mock.ANY,
474475
skip_mandatory_checks=True,
475-
comment_id=mock.ANY,
476476
ignore_current=False,
477477
)
478478

@@ -485,9 +485,9 @@ def test_main_merge(self, mock_merge: Any, *args: Any) -> None:
485485
mock_merge.assert_called_once_with(
486486
mock.ANY,
487487
mock.ANY,
488+
comment_id=mock.ANY,
488489
dry_run=mock.ANY,
489490
skip_mandatory_checks=False,
490-
comment_id=mock.ANY,
491491
ignore_current=False,
492492
)
493493

.github/scripts/trymerge.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ def merge_into(
11511151
*,
11521152
skip_mandatory_checks: bool = False,
11531153
dry_run: bool = False,
1154-
comment_id: Optional[int] = None,
1154+
comment_id: int,
11551155
ignore_current_checks: Optional[list[str]] = None,
11561156
) -> None:
11571157
# Raises exception if matching rule is not found
@@ -1231,29 +1231,29 @@ def merge_changes(
12311231
branch_to_merge_into = self.default_branch() if branch is None else branch
12321232
if repo.current_branch() != branch_to_merge_into:
12331233
repo.checkout(branch_to_merge_into)
1234-
if not self.is_ghstack_pr():
1235-
msg = self.gen_commit_message()
1236-
pr_branch_name = f"__pull-request-{self.pr_num}__init__"
1237-
repo.fetch(self.last_commit()["oid"], pr_branch_name)
1238-
repo._run_git("merge", "--squash", pr_branch_name)
1239-
repo._run_git("commit", f'--author="{self.get_author()}"', "-m", msg)
1240-
1241-
# Did the PR change since we started the merge?
1242-
pulled_sha = repo.show_ref(pr_branch_name)
1243-
latest_pr_status = GitHubPR(self.org, self.project, self.pr_num)
1244-
if pulled_sha != latest_pr_status.last_commit()["oid"]:
1245-
raise RuntimeError(
1246-
"PR has been updated since CI checks last passed. Please rerun the merge command."
1247-
)
1248-
return []
1249-
else:
1234+
if self.is_ghstack_pr():
12501235
return self.merge_ghstack_into(
12511236
repo,
12521237
skip_mandatory_checks,
12531238
comment_id=comment_id,
12541239
skip_all_rule_checks=skip_all_rule_checks,
12551240
)
12561241

1242+
msg = self.gen_commit_message()
1243+
pr_branch_name = f"__pull-request-{self.pr_num}__init__"
1244+
repo.fetch(self.last_commit()["oid"], pr_branch_name)
1245+
repo._run_git("merge", "--squash", pr_branch_name)
1246+
repo._run_git("commit", f'--author="{self.get_author()}"', "-m", msg)
1247+
1248+
# Did the PR change since we started the merge?
1249+
pulled_sha = repo.show_ref(pr_branch_name)
1250+
latest_pr_status = GitHubPR(self.org, self.project, self.pr_num)
1251+
if pulled_sha != latest_pr_status.last_commit()["oid"]:
1252+
raise RuntimeError(
1253+
"PR has been updated since CI checks last passed. Please rerun the merge command."
1254+
)
1255+
return []
1256+
12571257

12581258
class MergeRuleFailedError(RuntimeError):
12591259
def __init__(self, message: str, rule: Optional["MergeRule"] = None) -> None:
@@ -2156,9 +2156,9 @@ def categorize_checks(
21562156
def merge(
21572157
pr: GitHubPR,
21582158
repo: GitRepo,
2159+
comment_id: int,
21592160
dry_run: bool = False,
21602161
skip_mandatory_checks: bool = False,
2161-
comment_id: Optional[int] = None,
21622162
timeout_minutes: int = 400,
21632163
stale_pr_days: int = 3,
21642164
ignore_current: bool = False,
@@ -2416,12 +2416,18 @@ def handle_exception(e: Exception, title: str = "Merge failed") -> None:
24162416
gh_post_pr_comment(org, project, args.pr_num, message, dry_run=args.dry_run)
24172417
return
24182418
try:
2419+
# Ensure comment id is set, else fail
2420+
if not args.comment_id:
2421+
raise ValueError(
2422+
"Comment ID is required for merging PRs, please provide it using --comment-id"
2423+
)
2424+
24192425
merge(
24202426
pr,
24212427
repo,
2428+
comment_id=args.comment_id,
24222429
dry_run=args.dry_run,
24232430
skip_mandatory_checks=args.force,
2424-
comment_id=args.comment_id,
24252431
ignore_current=args.ignore_current,
24262432
)
24272433
except Exception as e:

.github/workflows/trymerge.yml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,19 @@ jobs:
5959
# on the PR appear in chronological order (timing issues can shuffle them around)
6060
sleep 60
6161
fi
62+
63+
# Require a comment id for merge operations
64+
if [ -z "${COMMENT_ID}" ]; then
65+
echo "Error: merge requires COMMENT_ID to be specified"
66+
exit 1
67+
fi
68+
6269
if [ -n "${FORCE}" ]; then
63-
if [ -n "${COMMENT_ID}" ]; then
64-
python3 .github/scripts/trymerge.py --force --comment-id "${COMMENT_ID}" "${PR_NUM}"
65-
else
66-
python3 .github/scripts/trymerge.py --force "${PR_NUM}"
67-
fi
70+
python3 .github/scripts/trymerge.py --force --comment-id "${COMMENT_ID}" "${PR_NUM}"
6871
elif [ -n "${IGNORE_CURRENT}" ]; then
69-
if [ -n "${COMMENT_ID}" ]; then
70-
python3 .github/scripts/trymerge.py --ignore-current --comment-id "${COMMENT_ID}" "${PR_NUM}"
71-
else
72-
python3 .github/scripts/trymerge.py --ignore-current "${PR_NUM}"
73-
fi
74-
elif [ -n "${COMMENT_ID}" ]; then
75-
python3 .github/scripts/trymerge.py --comment-id "${COMMENT_ID}" "${PR_NUM}"
72+
python3 .github/scripts/trymerge.py --ignore-current --comment-id "${COMMENT_ID}" "${PR_NUM}"
7673
else
77-
python3 .github/scripts/trymerge.py "${PR_NUM}"
74+
python3 .github/scripts/trymerge.py --comment-id "${COMMENT_ID}" "${PR_NUM}"
7875
fi
7976
- name: Comment on Canceled
8077
if: ${{ cancelled() && steps.checkout.outcome == 'success' }}

0 commit comments

Comments
 (0)