Skip to content

Commit 3114cf7

Browse files
committed
Update
[ghstack-poisoned]
1 parent 929a3d7 commit 3114cf7

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 corresponding 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+
ghstack_begin = (
56+
"Stack from [ghstack](https://github.com/ezyang/ghstack) (oldest at bottom):"
57+
)
58+
ghstack_begin_seen = False
59+
for line in pr_body.splitlines():
60+
if ghstack_begin in line:
61+
ghstack_begin_seen = True
62+
if not ghstack_begin_seen:
63+
continue
64+
match = re.match(r"\*(?:.*?)? #(\d+)", line)
65+
if match:
66+
# It's a bullet followed by an integer.
67+
prs.append(int(match.group(1)))
68+
return list(reversed(prs))
69+
70+
71+
def get_pr_stack_from_number(pr_number: int, repo: Repository) -> List[int]:
72+
pr_stack = extract_stack_from_body(repo.get_pull(pr_number).body)
73+
74+
if not pr_stack:
75+
raise Exception(
76+
f"Could not find PR stack in body of #{pr_number}. "
77+
+ "Please make sure that the PR was created with ghstack."
78+
)
79+
80+
return pr_stack
81+
82+
83+
def create_prs_for_orig_branch(pr_stack: List[int], repo: Repository):
84+
# For the first PR, we want to merge to `main` branch, and we will update
85+
# as we go through the stack
86+
orig_branch_merge_base = "main"
87+
for i in range(len(pr_stack)):
88+
pr = repo.get_pull(pr_stack[i])
89+
if not pr.is_merged():
90+
print("The PR (and stack above) is not merged yet, skipping")
91+
return
92+
# Check for invariant: For the current PR, it must be gh/user/x/base <- gh/user/x/head
93+
assert pr.base.ref.replace("base", "head") == pr.head.ref
94+
# The PR we want to create is then "branch_to_merge" <- gh/user/x/orig
95+
# gh/user/x/orig is the clean diff between gh/user/x/base <- gh/user/x/head
96+
orig_branch_merge_head = pr.base.ref.replace("base", "orig")
97+
bot_metadata = f"""This PR was created by the merge bot to help merge the original PR into the main branch.
98+
ghstack PR number: https://github.com/pytorch/executorch/pull/{pr.number}
99+
^ Please use this as the source of truth for the PR details, comments, and reviews
100+
ghstack PR base: https://github.com/pytorch/executorch/tree/{pr.base.ref}
101+
ghstack PR head: https://github.com/pytorch/executorch/tree/{pr.head.ref}
102+
Merge bot PR base: https://github.com/pytorch/executorch/tree/{orig_branch_merge_base}
103+
Merge bot PR head: https://github.com/pytorch/executorch/tree/{orig_branch_merge_head}"""
104+
105+
existing_orig_pr = repo.get_pulls(
106+
head="pytorch:" + orig_branch_merge_head,
107+
base=orig_branch_merge_base,
108+
state="open",
109+
)
110+
if existing_orig_pr.totalCount > 0:
111+
print(
112+
f"PR for {orig_branch_merge_head} already exists {existing_orig_pr[0]}"
113+
)
114+
# We don't need to create/edit because the head PR is merged and orig is finalized.
115+
else:
116+
repo.create_pull(
117+
base=orig_branch_merge_base,
118+
head=orig_branch_merge_head,
119+
title=pr.title,
120+
body=bot_metadata,
121+
)
122+
# Advance the base for the next PR
123+
orig_branch_merge_base = orig_branch_merge_head
124+
125+
126+
def main():
127+
args = parse_args()
128+
129+
with Github(auth=Auth.Token(os.environ["GITHUB_TOKEN"])) as gh:
130+
repo = gh.get_repo(args.repo)
131+
create_prs_for_orig_branch(get_pr_stack_from_number(args.pr, repo), repo)
132+
133+
134+
if __name__ == "__main__":
135+
main()

.github/workflows/ghstack_land.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Propose to merge ghstack orig PRs to main
2+
on:
3+
pull_request:
4+
types: [closed]
5+
branches:
6+
- 'gh/cccclai/[0-9]+/base'
7+
- 'gh/dbort/[0-9]+/base'
8+
- 'gh/guangy10/[0-9]+/base'
9+
- 'gh/helunwencser/[0-9]+/base'
10+
- 'gh/jorgep31415/[0-9]+/base'
11+
- 'gh/kimishpatel/[0-9]+/base'
12+
- 'gh/kirklandsign/[0-9]+/base'
13+
- 'gh/larryliu0820/[0-9]+/base'
14+
- 'gh/manuelcandales/[0-9]+/base'
15+
- 'gh/mcr229/[0-9]+/base'
16+
- 'gh/swolchok/[0-9]+/base'
17+
- 'gh/SS-JIA/[0-9]+/base'
18+
19+
jobs:
20+
ghstack_merge_to_main:
21+
name: Try to create a PR with ghstack /orig branch
22+
runs-on: ubuntu-22.04
23+
environment: cherry-pick-bot
24+
steps:
25+
- uses: actions/checkout@v3
26+
with:
27+
fetch-depth: '0'
28+
- uses: actions/setup-python@v4
29+
with:
30+
python-version: '3.10'
31+
- name: Try to merge PR to main
32+
run: |
33+
pip install pygithub
34+
35+
PR_NUMBER=$(echo "$GITHUB_REF" | grep -oE '[0-9]+')
36+
37+
python .github/scripts/propose_ghstack_orig_pr.py --pr $PR_NUMBER --repo pytorch/executorch
38+
env:
39+
GITHUB_TOKEN: ${{ secrets.GH_PYTORCHBOT_CHERRY_PICK_TOKEN }}
40+
GITHUB_REF: ${{ github.ref }}

0 commit comments

Comments
 (0)