Skip to content

Commit bbb2964

Browse files
swang392pytorchmergebot
authored andcommitted
Add functionality for rebasebot to rebase onto viable/strict branch (pytorch#78276)
Fixes ROCm#325 **Summary**: Currently, the pytorchbot only allows for rebasing to the master branch. These modifications add functionality for rebasing to the 'viable/strict' branch of pytorch/pytorch by adding a flag to the comment. **Test Plan:** tested manually on personal fork ([ROCm#1](swang392#1)), and included a test case in test_tryrebase.py that checks if rebasing to viable/strict branch was successful. Pull Request resolved: pytorch#78276 Approved by: https://github.com/clee2000, https://github.com/janeyx99
1 parent 49979c4 commit bbb2964

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

.github/scripts/test_tryrebase.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ def test_rebase(self, mocked_post_comment: Any, mocked_run_git: Any, mocked_gql:
2222
mocked_run_git.assert_has_calls(calls)
2323
self.assertTrue("Successfully rebased `master` onto `master`" in mocked_post_comment.call_args[0][3])
2424

25+
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)
26+
@mock.patch('gitutils.GitRepo._run_git')
27+
@mock.patch('tryrebase.gh_post_comment')
28+
def test_rebase_to_stable(self, mocked_post_comment: Any, mocked_run_git: Any, mocked_gql: Any) -> None:
29+
"Tests rebase to viable/strict successfully"
30+
pr = GitHubPR("pytorch", "pytorch", 31093)
31+
repo = GitRepo(get_git_repo_dir(), get_git_remote_name())
32+
rebase_onto(pr, repo, False, True)
33+
calls = [mock.call('fetch', 'origin', 'pull/31093/head:pull/31093/head'),
34+
mock.call('rebase', 'viable/strict', 'pull/31093/head'),
35+
mock.call('push', '-f', 'https://github.com/mingxiaoh/pytorch.git', 'pull/31093/head:master')]
36+
mocked_run_git.assert_has_calls(calls)
37+
self.assertTrue(
38+
"Successfully rebased `master` onto `viable/strict`" in mocked_post_comment.call_args[0][3])
39+
2540
@mock.patch('trymerge.gh_graphql', side_effect=mocked_gh_graphql)
2641
@mock.patch('gitutils.GitRepo._run_git', return_value="Everything up-to-date")
2742
@mock.patch('tryrebase.gh_post_comment')

.github/scripts/tryrebase.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ def parse_args() -> Any:
1313
from argparse import ArgumentParser
1414
parser = ArgumentParser("Rebase PR into branch")
1515
parser.add_argument("--dry-run", action="store_true")
16+
parser.add_argument("--stable", action="store_true")
1617
parser.add_argument("pr_num", type=int)
1718
return parser.parse_args()
1819

1920

20-
def rebase_onto(pr: GitHubPR, repo: GitRepo, dry_run: bool = False) -> None:
21+
def rebase_onto(pr: GitHubPR, repo: GitRepo, dry_run: bool = False, stable: bool = False) -> None:
2122
branch = f"pull/{pr.pr_num}/head"
22-
onto_branch = pr.default_branch()
23+
onto_branch = "viable/strict" if stable else pr.default_branch()
2324
remote_url = f"https://github.com/{pr.info['headRepository']['nameWithOwner']}.git"
2425
refspec = f"{branch}:{pr.head_ref()}"
2526

@@ -39,11 +40,11 @@ def rebase_onto(pr: GitHubPR, repo: GitRepo, dry_run: bool = False) -> None:
3940
"git pull --rebase`)", dry_run=dry_run)
4041

4142

42-
def rebase_ghstack_onto(pr: GitHubPR, repo: GitRepo, dry_run: bool = False) -> None:
43+
def rebase_ghstack_onto(pr: GitHubPR, repo: GitRepo, dry_run: bool = False, stable: bool = False) -> None:
4344
if subprocess.run([sys.executable, "-m", "ghstack", "--help"], capture_output=True).returncode != 0:
4445
subprocess.run([sys.executable, "-m", "pip", "install", "ghstack"])
4546
orig_ref = f"{re.sub(r'/head$', '/orig', pr.head_ref())}"
46-
onto_branch = pr.default_branch()
47+
onto_branch = "viable/strict" if stable else pr.default_branch()
4748

4849
repo.fetch(orig_ref, orig_ref)
4950
repo._run_git("rebase", onto_branch, orig_ref)
@@ -102,9 +103,9 @@ def main() -> None:
102103

103104
try:
104105
if pr.is_ghstack_pr():
105-
rebase_ghstack_onto(pr, repo, dry_run=args.dry_run)
106+
rebase_ghstack_onto(pr, repo, dry_run=args.dry_run, stable=args.stable)
106107
return
107-
rebase_onto(pr, repo, dry_run=args.dry_run)
108+
rebase_onto(pr, repo, dry_run=args.dry_run, stable=args.stable)
108109
except Exception as e:
109110
msg = f"Rebase failed due to {e}"
110111
run_url = os.getenv("GH_RUN_URL")

.github/workflows/tryrebase.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,11 @@ jobs:
3030
GITHUB_TOKEN: ${{ secrets.MERGEBOT_TOKEN }}
3131
PR_NUM: ${{ github.event.client_payload.pr_num }}
3232
GH_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
33+
STABLE: ${{ github.event.client_payload.stable}}
3334
run: |
34-
python3 .github/scripts/tryrebase.py "${PR_NUM}"
35+
set -ex
36+
if [ -n "${STABLE}" ]; then
37+
python3 .github/scripts/tryrebase.py --stable "${PR_NUM}"
38+
else
39+
python3 .github/scripts/tryrebase.py "${PR_NUM}"
40+
fi

0 commit comments

Comments
 (0)