From f37f469b685ac80b945bef27f6ee9d7aca7c5584 2013-09-04 22:06:17 From: MinRK Date: 2013-09-04 22:06:17 Subject: [PATCH] add ability to check what PRs should be backported in backport_pr just run: python tools/backport_pr.py 1.x to see what PRs are marked for backport, but not yet applied. --- diff --git a/tools/backport_pr.py b/tools/backport_pr.py index 751f6bc..60dc1e5 100755 --- a/tools/backport_pr.py +++ b/tools/backport_pr.py @@ -2,24 +2,37 @@ """ Backport pull requests to a particular branch. -Usage: backport_pr.py branch PR +Usage: backport_pr.py branch [PR] e.g.: - backport_pr.py 0.13.1 123 + python tools/backport_pr.py 0.13.1 123 to backport PR #123 onto branch 0.13.1 +or + + python tools/backport_pr.py 1.x + +to see what PRs are marked for backport that have yet to be applied. + """ from __future__ import print_function import os +import re import sys + from subprocess import Popen, PIPE, check_call, check_output from urllib import urlopen -from gh_api import get_pull_request, get_pull_request_files +from gh_api import ( + get_issues_list, + get_pull_request, + get_pull_request_files, + is_pull_request, +) def find_rejects(root='.'): for dirname, dirs, files in os.walk(root): @@ -84,9 +97,47 @@ def backport_pr(branch, num, project='ipython/ipython'): return 0 +backport_re = re.compile(r"[Bb]ackport.*?(\d+)") + +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)) + +def should_backport(labels): + """return set of PRs marked for backport""" + issues = get_issues_list("ipython/ipython", + labels=labels, + state='closed', + auth=True, + ) + should_backport = set() + for issue in issues: + if not is_pull_request(issue): + continue + pr = get_pull_request("ipython/ipython", issue['number'], auth=True) + if not pr['merged']: + print ("Marked PR closed without merge: %i" % pr['number']) + continue + should_backport.add(pr['number']) + return should_backport + if __name__ == '__main__': - if len(sys.argv) < 3: + + if len(sys.argv) < 2: print(__doc__) sys.exit(1) + if len(sys.argv) < 3: + branch = sys.argv[1] + already = already_backported(branch) + should = should_backport("backport-1.1") + print ("The following PRs should be backported:") + for pr in should.difference(already): + print (pr) + sys.exit(0) + sys.exit(backport_pr(sys.argv[1], int(sys.argv[2])))