backport_pr.py
177 lines
| 5.3 KiB
| text/x-python
|
PythonLexer
/ tools / backport_pr.py
|
r8347 | #!/usr/bin/env python | ||
|
r8346 | """ | ||
Backport pull requests to a particular branch. | ||||
|
r16761 | Usage: backport_pr.py branch [PR] [PR2] | ||
|
r8346 | |||
e.g.: | ||||
|
r16761 | python tools/backport_pr.py 0.13.1 123 155 | ||
|
r8346 | |||
to backport PR #123 onto branch 0.13.1 | ||||
|
r12424 | or | ||
|
r16167 | python tools/backport_pr.py 2.1 | ||
|
r12424 | |||
|
r16167 | to see what PRs are marked for backport with milestone=2.1 that have yet to be applied | ||
to branch 2.x. | ||||
|
r12424 | |||
|
r8346 | """ | ||
from __future__ import print_function | ||||
import os | ||||
|
r12424 | import re | ||
|
r8346 | import sys | ||
|
r12424 | |||
|
r8346 | from subprocess import Popen, PIPE, check_call, check_output | ||
|
r16178 | try: | ||
from urllib.request import urlopen | ||||
except: | ||||
|
r16761 | from urllib import urlopen | ||
|
r8346 | |||
|
r12424 | from gh_api import ( | ||
get_issues_list, | ||||
get_pull_request, | ||||
get_pull_request_files, | ||||
is_pull_request, | ||||
|
r13169 | get_milestone_id, | ||
|
r12424 | ) | ||
|
r8346 | |||
def find_rejects(root='.'): | ||||
for dirname, dirs, files in os.walk(root): | ||||
for fname in files: | ||||
if fname.endswith('.rej'): | ||||
yield os.path.join(dirname, fname) | ||||
def get_current_branch(): | ||||
branches = check_output(['git', 'branch']) | ||||
for branch in branches.splitlines(): | ||||
|
r16178 | if branch.startswith(b'*'): | ||
return branch[1:].strip().decode('utf-8') | ||||
|
r8346 | |||
def backport_pr(branch, num, project='ipython/ipython'): | ||||
current_branch = get_current_branch() | ||||
if branch != current_branch: | ||||
check_call(['git', 'checkout', branch]) | ||||
|
r12276 | check_call(['git', 'pull']) | ||
|
r12129 | pr = get_pull_request(project, num, auth=True) | ||
files = get_pull_request_files(project, num, auth=True) | ||||
|
r8346 | patch_url = pr['patch_url'] | ||
title = pr['title'] | ||||
|
r21462 | description = pr['body'] or '' | ||
|
r8346 | fname = "PR%i.patch" % num | ||
if os.path.exists(fname): | ||||
print("using patch from {fname}".format(**locals())) | ||||
|
r18786 | with open(fname, 'rb') as f: | ||
|
r8346 | patch = f.read() | ||
else: | ||||
req = urlopen(patch_url) | ||||
patch = req.read() | ||||
|
r13169 | |||
|
r16762 | lines = description.splitlines() | ||
if len(lines) > 5: | ||||
lines = lines[:5] + ['...'] | ||||
description = '\n'.join(lines) | ||||
|
r8346 | msg = "Backport PR #%i: %s" % (num, title) + '\n\n' + description | ||
check = Popen(['git', 'apply', '--check', '--verbose'], stdin=PIPE) | ||||
a,b = check.communicate(patch) | ||||
|
r13169 | |||
|
r8346 | if check.returncode: | ||
print("patch did not apply, saving to {fname}".format(**locals())) | ||||
print("edit {fname} until `cat {fname} | git apply --check` succeeds".format(**locals())) | ||||
print("then run tools/backport_pr.py {num} again".format(**locals())) | ||||
if not os.path.exists(fname): | ||||
with open(fname, 'wb') as f: | ||||
f.write(patch) | ||||
return 1 | ||||
|
r13169 | |||
|
r8346 | p = Popen(['git', 'apply'], stdin=PIPE) | ||
a,b = p.communicate(patch) | ||||
|
r13169 | |||
|
r12129 | filenames = [ f['filename'] for f in files ] | ||
|
r21462 | filenames = [ f.replace('jupyter_notebook', 'IPython/html') for f in filenames ] | ||
|
r8346 | |||
|
r12341 | check_call(['git', 'add'] + filenames) | ||
|
r13169 | |||
|
r12341 | check_call(['git', 'commit', '-m', msg]) | ||
|
r13169 | |||
|
r12341 | print("PR #%i applied, with msg:" % num) | ||
print() | ||||
print(msg) | ||||
print() | ||||
|
r13169 | |||
|
r8346 | if branch != current_branch: | ||
check_call(['git', 'checkout', current_branch]) | ||||
|
r13169 | |||
|
r8346 | return 0 | ||
|
r20977 | backport_re = re.compile(r"(?:[Bb]ackport|[Mm]erge).*#(\d+)") | ||
|
r12424 | |||
def already_backported(branch, since_tag=None): | ||||
"""return set of PRs that have been backported already""" | ||||
if since_tag is None: | ||||
since_tag = check_output(['git','describe', branch, '--abbrev=0']).decode('utf8').strip() | ||||
cmd = ['git', 'log', '%s..%s' % (since_tag, branch), '--oneline'] | ||||
lines = check_output(cmd).decode('utf8') | ||||
return set(int(num) for num in backport_re.findall(lines)) | ||||
|
r13169 | def should_backport(labels=None, milestone=None): | ||
|
r12424 | """return set of PRs marked for backport""" | ||
|
r13169 | if labels is None and milestone is None: | ||
raise ValueError("Specify one of labels or milestone.") | ||||
elif labels is not None and milestone is not None: | ||||
raise ValueError("Specify only one of labels or milestone.") | ||||
if labels is not None: | ||||
issues = get_issues_list("ipython/ipython", | ||||
labels=labels, | ||||
state='closed', | ||||
auth=True, | ||||
) | ||||
else: | ||||
milestone_id = get_milestone_id("ipython/ipython", milestone, | ||||
auth=True) | ||||
issues = get_issues_list("ipython/ipython", | ||||
milestone=milestone_id, | ||||
state='closed', | ||||
auth=True, | ||||
) | ||||
|
r12424 | should_backport = set() | ||
for issue in issues: | ||||
if not is_pull_request(issue): | ||||
continue | ||||
|
r13169 | pr = get_pull_request("ipython/ipython", issue['number'], | ||
auth=True) | ||||
|
r12424 | if not pr['merged']: | ||
print ("Marked PR closed without merge: %i" % pr['number']) | ||||
continue | ||||
|
r16673 | if pr['base']['ref'] != 'master': | ||
continue | ||||
|
r12424 | should_backport.add(pr['number']) | ||
return should_backport | ||||
|
r8346 | if __name__ == '__main__': | ||
|
r13169 | |||
|
r12424 | if len(sys.argv) < 2: | ||
|
r8346 | print(__doc__) | ||
sys.exit(1) | ||||
|
r13169 | |||
|
r12424 | if len(sys.argv) < 3: | ||
|
r16167 | milestone = sys.argv[1] | ||
branch = milestone.split('.')[0] + '.x' | ||||
|
r12424 | already = already_backported(branch) | ||
|
r16167 | should = should_backport(milestone=milestone) | ||
|
r12424 | print ("The following PRs should be backported:") | ||
|
r13952 | for pr in sorted(should.difference(already)): | ||
|
r12424 | print (pr) | ||
sys.exit(0) | ||||
|
r13169 | |||
|
r16761 | for prno in map(int, sys.argv[2:]): | ||
print("Backporting PR #%i" % prno) | ||||
rc = backport_pr(sys.argv[1], prno) | ||||
if rc: | ||||
print("Backporting PR #%i failed" % prno) | ||||
sys.exit(rc) | ||||