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