forked from sivel/prmove
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprmove.py
359 lines (273 loc) · 11.4 KB
/
prmove.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright 2016 Matt Martz
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import re
import json
import shutil
import logging
import urllib.parse
import tempfile
import requests
from git import Repo, GitCommandError
from functools import wraps
from flask_github import GitHub
from flask_sslify import SSLify
from flask import (Flask, Markup, session, request, url_for, redirect, flash,
render_template, abort)
GITHUB_API_BASE = 'https://api.github.com'
DIFF_GIT_RE = re.compile(r'^(diff --git a/)([^ ]+ b/)([^ ]+)$', re.M)
STAT_RE = re.compile(r'^(\s+)([^ ]+\s+\|\s+\d+\s+[+-]+)$', re.M)
MINUS_PLUS_RE = re.compile(r'^((?:-|\+){3} [ab]/)(.+)$', re.M)
app = Flask('prmove')
app.config.from_envvar('PRMOVE_CONFIG')
github = GitHub(app)
sslify = SSLify(app)
LOG = logging.getLogger('prmove')
class Mover(object):
def __init__(self, token, username, pr_url, close_original=False):
self.username = username
self.token = token
self.pr_url = pr_url.rstrip('/')
self.close_original = close_original
self.upstream_dir = app.config['UPSTREAM_DIR']
self.upstream_account = None
self.upstream_branch = None
self.urlparts = urllib.parse.urlparse(self.pr_url)
self.branch_name = self.urlparts.path.split('/', 2)[-1]
self.patch = None
self.working_dir = tempfile.mkdtemp()
self.original_pull_request_url = '%s/repos%s' % (
GITHUB_API_BASE, self.urlparts.path.replace('/pull/', '/pulls/'))
self.original_pull_request = None
self.is_migration_by_owner = None
def check_already_migrated(self):
params = {
'access_token': self.token
}
url = '%s/repos/%s/ansible/branches/%s' % (GITHUB_API_BASE, self.username, self.branch_name)
r = requests.get(url, params=params)
if r.status_code == 404:
return
if r.status_code == 200:
raise Exception('Branch %s already exists. Has this pull request already been migrated?' %
self.branch_name)
r.raise_for_status()
def get_original_pull_request(self):
params = {
'access_token': self.token
}
r = requests.get(self.original_pull_request_url, params=params)
r.raise_for_status()
self.original_pull_request = r.json()
self.mergeable_state = self.original_pull_request['mergeable_state']
self.is_migration_by_owner = self.original_pull_request['user']['login'] == self.username
return self.original_pull_request
def get_patch(self):
r = requests.get('%s.patch' % self.pr_url)
r.raise_for_status()
self.patch = r.text
self.patch = DIFF_GIT_RE.sub(
r'\1lib/ansible/modules/\2lib/ansible/modules/\3',
self.patch
)
self.patch = STAT_RE.sub(r'\1lib/ansible/modules/\2', self.patch)
self.patch = MINUS_PLUS_RE.sub(r'\1lib/ansible/modules/\2', self.patch)
with open('%s/patch.patch' % self.working_dir, 'w+') as f:
f.write(self.patch)
return self.patch
def clone_ansible(self):
clone_dir = '%s/ansible' % self.working_dir
origin_url = 'https://%s@github.com/%s/ansible.git' % (self.token, self.username)
shutil.copytree(self.upstream_dir, clone_dir, symlinks=True)
try:
clone = Repo(clone_dir)
except GitCommandError as e:
raise Exception('Failed to open clone of ansible/ansible repository:',
'\n%s\n%s' % (e.stdout, e.stderr)) from e
try:
self.upstream_account = urllib.parse.urlparse(list(clone.remote('upstream').urls)[0]).path.split('/')[1]
except GitCommandError as e:
raise Exception('Failed to get upstream from ansible/ansible repository:',
'\n%s\n%s' % (e.stdout, e.stderr)) from e
try:
self.upstream_branch = clone.active_branch.name
except GitCommandError as e:
raise Exception('Failed to get active branch from ansible/ansible repository:',
'\n%s\n%s' % (e.stdout, e.stderr)) from e
try:
clone.create_remote('origin', origin_url)
except GitCommandError as e:
raise Exception('Failed to add origin to clone of ansible/ansible repository:'
'\n%s\n%s' % (e.stdout, e.stderr)) from e
try:
if requests.get(origin_url).status_code != 200:
raise Exception('You must have a fork of ansible/ansible at: %s' % origin_url)
except GitCommandError as e:
raise Exception('Failed to verify origin exists:'
'\n%s\n%s' % (e.stdout, e.stderr)) from e
try:
clone.git.checkout(b=self.branch_name)
except GitCommandError as e:
raise Exception('Failed to create new branch:'
'\n%s\n%s' % (e.stdout, e.stderr)) from e
try:
clone.git.am('%s/patch.patch' % self.working_dir, '--3way')
except GitCommandError as e:
raise Exception('Failed to apply patch:'
'\n%s\n%s' % (e.stdout, e.stderr)) from e
try:
clone.git.push(['origin', self.branch_name])
except GitCommandError as e:
raise Exception('Failed to push new branch for pull request:'
'\n%s\n%s' % (e.stdout, e.stderr)) from e
def create_pull_request(self):
params = {
'access_token': self.token
}
data = {
'title': self.original_pull_request['title'],
'body': self.original_pull_request['body'],
'head': '%s:%s' % (self.username, self.branch_name),
'base': self.upstream_branch,
}
url = '%s/repos/%s/ansible/pulls' % (GITHUB_API_BASE, self.upstream_account)
r = requests.post(url, data=json.dumps(data), params=params)
r.raise_for_status()
pull = r.json()
comment = {
'body': 'Migrated from %s' % self.original_pull_request['html_url']
}
if not self.is_migration_by_owner:
comment['body'] += ' by %s (not original author)' % self.username
r = requests.post(pull['comments_url'], data=json.dumps(comment),
params=params)
r.raise_for_status()
return pull
def close_original_pull_request(self):
if not self.close_original:
return None
params = {
'access_token': self.token
}
data = {
'state': 'closed'
}
r = requests.post(self.original_pull_request_url, data=json.dumps(data), params=params)
r.raise_for_status()
def __enter__(self):
return self
def __exit__(self, ex_type, value, traceback):
try:
shutil.rmtree(self.working_dir)
except OSError:
LOG.exception('Failure removing working dir: %s' %
self.working_dir)
@github.access_token_getter
def token_getter():
return session.get('token')
def login_required(f):
@wraps(f)
def wrapped(*args, **kwargs):
if not session.get('token'):
return redirect(url_for('login'))
return f(*args, **kwargs)
return wrapped
@app.before_first_request
def logger():
app.logger.addHandler(logging.StreamHandler())
app.logger.setLevel(logging.INFO)
app.logger.handlers.extend(logging.getLogger("gunicorn.error").handlers)
@app.errorhandler(500)
def internal_server_error(e):
app.logger.exception(e)
return abort(500)
@app.route('/')
def index():
if session.get('token'):
return redirect('/move')
return render_template('index.html')
@app.route('/login')
def login():
return github.authorize(scope='user:email public_repo')
@app.route('/login/authorized')
@github.authorized_handler
def authorized(oauth_token):
if oauth_token is None:
flash('Authorization failed.', 'danger')
return redirect('index')
session['token'] = oauth_token
session.update(github.get('user'))
return redirect(url_for('move'))
@app.route('/move', methods=['GET', 'POST'])
def move():
if request.method == 'POST':
try:
move_post()
except MarkupException as e:
LOG.exception(e)
flash(Markup(e.markup), 'danger')
except Exception as e:
LOG.exception(e)
flash(e, 'danger')
return render_template('move.html')
def move_post():
pr_url = request.form.get('prurl')
close_original = request.form.get('closeorig')
with Mover(session['token'], session['login'], pr_url, close_original == '1') as mover:
mover.check_already_migrated()
try:
mover.get_original_pull_request()
except Exception as e:
raise Exception('Failure validating pull request (%s) for %s: %s' %
(pr_url, session['login'], e)) from e
if mover.mergeable_state == 'dirty':
raise MarkupException('Please rebase your branch and update your PR before migrating. '
'Tests will fail for your old PR after rebasing. '
'This is expected and can be ignored. '
'For more information please consult the <a href="'
'http://docs.ansible.com/ansible/dev_guide/repomerge.html#move-issues-and-prs-to-new-repo'
'">repo merge</a> documentation. ')
try:
mover.get_patch()
except Exception as e:
raise Exception('Failure getting patch (%s) for %s: %s' %
(pr_url, session['login'], e)) from e
try:
mover.clone_ansible()
except Exception as e:
raise Exception('Failure handling git repository (%s) for %s: %s' %
(pr_url, session['login'], e)) from e
try:
pull = mover.create_pull_request()
except Exception as e:
raise Exception('Failure creating pull request (%s) for %s: %s' %
(pr_url, session['login'], e)) from e
flash(
Markup('Your pull request has been migrated to '
'<a href="%(html_url)s">%(html_url)s</a>' % pull),
'success'
)
try:
mover.close_original_pull_request()
except Exception as e:
raise Exception('Failure closing original pull request (%s) for %s: %s' %
(pr_url, session['login'], e)) from e
class MarkupException(Exception):
def __init__(self, markup):
super(MarkupException, self).__init__(markup)
self.markup = markup
if __name__ == '__main__':
app.run('0.0.0.0', 5000, debug=True)