Show More
@@ -1,176 +1,189 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | """ |
|
3 | 3 | Backport pull requests to a particular branch. |
|
4 | 4 | |
|
5 | Usage: backport_pr.py branch [PR] [PR2] | |
|
5 | Usage: backport_pr.py [org/repository] branch [PR] [PR2] | |
|
6 | 6 | |
|
7 | 7 | e.g.: |
|
8 | 8 | |
|
9 | 9 | python tools/backport_pr.py 0.13.1 123 155 |
|
10 | 10 | |
|
11 | to backport PR #123 onto branch 0.13.1 | |
|
11 | to backport PRs #123 and #155 onto branch 0.13.1 | |
|
12 | 12 | |
|
13 | 13 | or |
|
14 | 14 | |
|
15 | 15 | python tools/backport_pr.py 2.1 |
|
16 | 16 | |
|
17 | 17 | to see what PRs are marked for backport with milestone=2.1 that have yet to be applied |
|
18 |
to branch 2.x |
|
|
18 | to branch 2.x | |
|
19 | ||
|
20 | or | |
|
21 | ||
|
22 | python tools/backport_pr.py jupyter/notebook 0.13.1 123 155 | |
|
23 | ||
|
24 | to backport PRs #123 and #155 of the `jupyter/notebook` repo onto branch 0.13.1 | |
|
25 | of that repo. | |
|
19 | 26 | |
|
20 | 27 | """ |
|
21 | 28 | |
|
22 | 29 | from __future__ import print_function |
|
23 | 30 | |
|
24 | 31 | import os |
|
25 | 32 | import re |
|
26 | 33 | import sys |
|
27 | 34 | |
|
28 | 35 | from subprocess import Popen, PIPE, check_call, check_output |
|
29 | 36 | try: |
|
30 | 37 | from urllib.request import urlopen |
|
31 | 38 | except: |
|
32 | 39 | from urllib import urlopen |
|
33 | 40 | |
|
34 | 41 | from gh_api import ( |
|
35 | 42 | get_issues_list, |
|
36 | 43 | get_pull_request, |
|
37 | 44 | get_pull_request_files, |
|
38 | 45 | is_pull_request, |
|
39 | 46 | get_milestone_id, |
|
40 | 47 | ) |
|
41 | 48 | |
|
42 | 49 | def find_rejects(root='.'): |
|
43 | 50 | for dirname, dirs, files in os.walk(root): |
|
44 | 51 | for fname in files: |
|
45 | 52 | if fname.endswith('.rej'): |
|
46 | 53 | yield os.path.join(dirname, fname) |
|
47 | 54 | |
|
48 | 55 | def get_current_branch(): |
|
49 | 56 | branches = check_output(['git', 'branch']) |
|
50 | 57 | for branch in branches.splitlines(): |
|
51 | 58 | if branch.startswith(b'*'): |
|
52 | 59 | return branch[1:].strip().decode('utf-8') |
|
53 | 60 | |
|
54 | 61 | def backport_pr(branch, num, project='ipython/ipython'): |
|
55 | 62 | current_branch = get_current_branch() |
|
56 | 63 | if branch != current_branch: |
|
57 | 64 | check_call(['git', 'checkout', branch]) |
|
58 | 65 | check_call(['git', 'pull']) |
|
59 | 66 | pr = get_pull_request(project, num, auth=True) |
|
60 | 67 | files = get_pull_request_files(project, num, auth=True) |
|
61 | 68 | patch_url = pr['patch_url'] |
|
62 | 69 | title = pr['title'] |
|
63 | 70 | description = pr['body'] |
|
64 | 71 | fname = "PR%i.patch" % num |
|
65 | 72 | if os.path.exists(fname): |
|
66 | 73 | print("using patch from {fname}".format(**locals())) |
|
67 | 74 | with open(fname, 'rb') as f: |
|
68 | 75 | patch = f.read() |
|
69 | 76 | else: |
|
70 | 77 | req = urlopen(patch_url) |
|
71 | 78 | patch = req.read() |
|
72 | 79 | |
|
73 | 80 | lines = description.splitlines() |
|
74 | 81 | if len(lines) > 5: |
|
75 | 82 | lines = lines[:5] + ['...'] |
|
76 | 83 | description = '\n'.join(lines) |
|
77 | 84 | |
|
78 | 85 | msg = "Backport PR #%i: %s" % (num, title) + '\n\n' + description |
|
79 | 86 | check = Popen(['git', 'apply', '--check', '--verbose'], stdin=PIPE) |
|
80 | 87 | a,b = check.communicate(patch) |
|
81 | 88 | |
|
82 | 89 | if check.returncode: |
|
83 | 90 | print("patch did not apply, saving to {fname}".format(**locals())) |
|
84 | 91 | print("edit {fname} until `cat {fname} | git apply --check` succeeds".format(**locals())) |
|
85 | 92 | print("then run tools/backport_pr.py {num} again".format(**locals())) |
|
86 | 93 | if not os.path.exists(fname): |
|
87 | 94 | with open(fname, 'wb') as f: |
|
88 | 95 | f.write(patch) |
|
89 | 96 | return 1 |
|
90 | 97 | |
|
91 | 98 | p = Popen(['git', 'apply'], stdin=PIPE) |
|
92 | 99 | a,b = p.communicate(patch) |
|
93 | 100 | |
|
94 | 101 | filenames = [ f['filename'] for f in files ] |
|
95 | 102 | |
|
96 | 103 | check_call(['git', 'add'] + filenames) |
|
97 | 104 | |
|
98 | 105 | check_call(['git', 'commit', '-m', msg]) |
|
99 | 106 | |
|
100 | 107 | print("PR #%i applied, with msg:" % num) |
|
101 | 108 | print() |
|
102 | 109 | print(msg) |
|
103 | 110 | print() |
|
104 | 111 | |
|
105 | 112 | if branch != current_branch: |
|
106 | 113 | check_call(['git', 'checkout', current_branch]) |
|
107 | 114 | |
|
108 | 115 | return 0 |
|
109 | 116 | |
|
110 | 117 | backport_re = re.compile(r"(?:[Bb]ackport|[Mm]erge).*#(\d+)") |
|
111 | 118 | |
|
112 | 119 | def already_backported(branch, since_tag=None): |
|
113 | 120 | """return set of PRs that have been backported already""" |
|
114 | 121 | if since_tag is None: |
|
115 | 122 | since_tag = check_output(['git','describe', branch, '--abbrev=0']).decode('utf8').strip() |
|
116 | 123 | cmd = ['git', 'log', '%s..%s' % (since_tag, branch), '--oneline'] |
|
117 | 124 | lines = check_output(cmd).decode('utf8') |
|
118 | 125 | return set(int(num) for num in backport_re.findall(lines)) |
|
119 | 126 | |
|
120 | 127 | def should_backport(labels=None, milestone=None, project='ipython/ipython'): |
|
121 | 128 | """return set of PRs marked for backport""" |
|
122 | 129 | if labels is None and milestone is None: |
|
123 | 130 | raise ValueError("Specify one of labels or milestone.") |
|
124 | 131 | elif labels is not None and milestone is not None: |
|
125 | 132 | raise ValueError("Specify only one of labels or milestone.") |
|
126 | 133 | if labels is not None: |
|
127 | 134 | issues = get_issues_list(project, |
|
128 | 135 | labels=labels, |
|
129 | 136 | state='closed', |
|
130 | 137 | auth=True, |
|
131 | 138 | ) |
|
132 | 139 | else: |
|
133 | 140 | milestone_id = get_milestone_id(project, milestone, |
|
134 | 141 | auth=True) |
|
135 | 142 | issues = get_issues_list(project, |
|
136 | 143 | milestone=milestone_id, |
|
137 | 144 | state='closed', |
|
138 | 145 | auth=True, |
|
139 | 146 | ) |
|
140 | 147 | |
|
141 | 148 | should_backport = set() |
|
142 | 149 | for issue in issues: |
|
143 | 150 | if not is_pull_request(issue): |
|
144 | 151 | continue |
|
145 | 152 | pr = get_pull_request(project, issue['number'], |
|
146 | 153 | auth=True) |
|
147 | 154 | if not pr['merged']: |
|
148 | 155 | print ("Marked PR closed without merge: %i" % pr['number']) |
|
149 | 156 | continue |
|
150 | 157 | if pr['base']['ref'] != 'master': |
|
151 | 158 | continue |
|
152 | 159 | should_backport.add(pr['number']) |
|
153 | 160 | return should_backport |
|
154 | 161 | |
|
155 | 162 | if __name__ == '__main__': |
|
156 | ||
|
157 |
|
|
|
163 | project = 'ipython/ipython' | |
|
164 | args = list(sys.argv) | |
|
165 | if len(args) >= 2: | |
|
166 | if '/' in args[1]: | |
|
167 | project = args[1] | |
|
168 | del args[1] | |
|
169 | ||
|
170 | if len(args) < 2: | |
|
158 | 171 | print(__doc__) |
|
159 | 172 | sys.exit(1) |
|
160 | 173 | |
|
161 |
if len( |
|
|
162 |
milestone = |
|
|
174 | if len(args) < 3: | |
|
175 | milestone = args[1] | |
|
163 | 176 | branch = milestone.split('.')[0] + '.x' |
|
164 | 177 | already = already_backported(branch) |
|
165 | should = should_backport(milestone=milestone) | |
|
178 | should = should_backport(milestone=milestone, project=project) | |
|
166 | 179 | print ("The following PRs should be backported:") |
|
167 | 180 | for pr in sorted(should.difference(already)): |
|
168 | 181 | print (pr) |
|
169 | 182 | sys.exit(0) |
|
170 | ||
|
171 |
for prno in map(int, |
|
|
183 | ||
|
184 | for prno in map(int, args[2:]): | |
|
172 | 185 | print("Backporting PR #%i" % prno) |
|
173 |
rc = backport_pr( |
|
|
186 | rc = backport_pr(args[1], prno, project=project) | |
|
174 | 187 | if rc: |
|
175 | 188 | print("Backporting PR #%i failed" % prno) |
|
176 | 189 | sys.exit(rc) |
General Comments 0
You need to be logged in to leave comments.
Login now