Skip to content

Commit 0308656

Browse files
committed
Script to ghstack land
1 parent fe1b2e7 commit 0308656

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
import argparse
8+
import os
9+
import re
10+
11+
from typing import List
12+
13+
# Provided by the PyGithub pip package.
14+
from github import Auth, Github
15+
from github.Repository import Repository
16+
17+
18+
def parse_args():
19+
parser = argparse.ArgumentParser(
20+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
21+
)
22+
parser.add_argument(
23+
"--repo",
24+
type=str,
25+
help='The github repo to modify: e.g. "pytorch/executorch".',
26+
required=True,
27+
)
28+
parser.add_argument(
29+
"--pr",
30+
type=int,
31+
help="Number of the PR in the stack to check and create follow up PR",
32+
required=True,
33+
)
34+
return parser.parse_args()
35+
36+
37+
def extract_stack_from_body(pr_body: str) -> List[int]:
38+
"""Extracts a list of PR numbers from a ghexport-generated PR body.
39+
40+
The base of the stack is in index 0.
41+
"""
42+
43+
# Expected format. The `__->__` could appear on any line. Stop parsing
44+
# after the blank line. This would return [1, 2, 3].
45+
"""
46+
Stack from [ghstack](https://github.com/ezyang/ghstack) (oldest at bottom):
47+
* #3
48+
* __->__ #2
49+
* #1
50+
51+
<PR description details>
52+
"""
53+
54+
prs = []
55+
for line in pr_body.splitlines():
56+
match = re.match(r"\*(?:.*?)? #(\d+)", line)
57+
if match:
58+
# It's a bullet followed by an integer.
59+
prs.append(int(match.group(1)))
60+
elif line.strip() == "":
61+
# End of the stack.
62+
break
63+
return list(reversed(prs))
64+
65+
66+
def get_pr_stack_from_number(pr_number: int, repo: Repository) -> List[int]:
67+
return extract_stack_from_body(repo.get_pull(pr_number).body)
68+
69+
70+
def create_prs_for_orig_branch(pr_stack: List[int], repo: Repository):
71+
# For the first PR, we want to merge to `main` branch, and we will update
72+
# as we go through the stack
73+
orig_branch_merge_base = "main"
74+
for i in range(len(pr_stack)):
75+
pr = repo.get_pull(pr_stack[i])
76+
# Check for invariant: For the current PR, it must be gh/user/x/base <- gh/user/x/head
77+
assert pr.base.ref.replace("base", "head") == pr.head.ref
78+
# The PR we want to create is then "branch_to_merge" <- gh/user/x/orig
79+
# gh/user/x/orig is the clean diff between gh/user/x/base <- gh/user/x/head
80+
orig_branch_merge_head = pr.base.ref.replace("base", "orig")
81+
bot_metadata = f"""\nThis PR was created by the merge bot to help merge the original PR into the base branch.
82+
ghstack PR number: https://github.com/pytorch/executorch/pull/{pr.number}
83+
ghstack PR base: https://github.com/pytorch/executorch/tree/{pr.base.ref}
84+
ghstack PR head: https://github.com/pytorch/executorch/tree/{pr.head.ref}
85+
Merge bot PR base: https://github.com/pytorch/executorch/tree/{orig_branch_merge_base}
86+
Merge bot PR head: https://github.com/pytorch/executorch/tree/{orig_branch_merge_head}
87+
"""
88+
repo.create_pull(base=orig_branch_merge_base, head=orig_branch_merge_head, title=pr.title, body=pr.body + bot_metadata)
89+
# Advance the base for the next PR
90+
orig_branch_merge_base = orig_branch_merge_head
91+
92+
def main():
93+
args = parse_args()
94+
95+
with Github(auth=Auth.Token(os.environ['GITHUB_TOKEN'])) as gh:
96+
repo = gh.get_repo(args.repo)
97+
create_prs_for_orig_branch(get_pr_stack_from_number(args.pr, repo), repo)
98+
99+
100+
if __name__ == "__main__":
101+
main()

.github/workflows/ghstack_land.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Propose to merge ghstack orig PRs to main
2+
on:
3+
pull_request:
4+
types: [opened, synchronize, closed]
5+
branches:
6+
- 'gh/*/[0-9]+/base'
7+
- 'kirklandsigntest/*'
8+
jobs:
9+
ghstack_merge_to_main:
10+
name: ghstack-land
11+
runs-on: ubuntu-22.04
12+
permissions:
13+
pull-requests: write
14+
steps:
15+
- uses: actions/checkout@v3
16+
with:
17+
fetch-depth: '0'
18+
- uses: actions/setup-python@v4
19+
with:
20+
python-version: '3.10'
21+
- name: Try to merge PR to main
22+
run: |
23+
pip install ghstack
24+
pip install pygithub
25+
26+
PR_NUMBER=$(echo "$GITHUB_REF" | grep -oE '[0-9]+')
27+
28+
echo "Checking whether PR $PR_NUMBER is merged"
29+
PR_MERGED=$(python -c "from github import Github; g = Github(); repo = g.get_repo('pytorch/executorch'); pr = repo.get_pull(${PR_NUMBER}); print(1 if pr.merged else 0)")
30+
if [ "$PR_MERGED" = "0" ]; then
31+
echo $"PR $PR_NUMBER is not merged. No op here."
32+
exit 0
33+
fi
34+
35+
python .github/scripts/propose_ghstack_orig_pr.py --pr $PR_NUMBER --repo pytorch/executorch
36+
env:
37+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38+
GITHUB_REF: ${{ github.ref }}

0 commit comments

Comments
 (0)