-
Notifications
You must be signed in to change notification settings - Fork 1
/
prw
executable file
·303 lines (256 loc) · 9.14 KB
/
prw
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
#!/usr/bin/env python
import irclib
import sys
import os
import time
from getopt import getopt
from datetime import datetime
from subprocess import Popen, PIPE
import readline
import re
import urllib2
import json
from git import Repo, GitCommandError
from rbtools.postreview import ReviewBoardServer, SCMCLIENTS
import rbtools.postreview
AGILEZEN_KEY = '108dd82a8d1b40d99289ef6e9cfc2a6c'
AGILEZEN_PROJECT_ID = '17041'
AGILEZEN_URL = 'https://agilezen.com/api/v1/projects/%s/' % AGILEZEN_PROJECT_ID
IRC_SERVER = 'irc.nextowntech.com'
def post_az_comment(story_number, text):
print 'Posting to AZ story %s... ' % story_number,
request = urllib2.Request(AGILEZEN_URL + 'stories/%s/comments' % story_number,
headers={'X-Zen-ApiKey': AGILEZEN_KEY,
'Content-Type': 'application/json'},
data=json.dumps({'text': text})
)
contents = urllib2.urlopen(request).read()
data = json.loads(contents)
print 'Done'
class rb_options(object):
username = None
password = None
debug = False
diff_filename = None
rbtools.postreview.options = rb_options()
DEBUG=False
AUTO_POST=False
OPEN_BROWSER=True
USE_COLORS=True
FORCE=False
try:
from termcolor import colored
except ImportError, e:
print "Unable to use colors: {0}".format(e)
USE_COLORS=False
COLORS = {
'review': 'green',
'sha': 'green',
'date': None,
'author': 'cyan',
'message': None,
'diff-prompt': 'yellow',
'details-prompt': 'yellow',
'details': 'magenta',
'warning': 'magenta',
}
def main(argv):
opts, args = getopt(argv, 'u:dfp:')
opts = dict(opts)
if '-f' in opts:
global FORCE
FORCE = True
repo = Repo('.')
global REVIEW_BOARD_URL
REVIEW_BOARD_URL = repo.config_reader().get('reviewboard', 'url')
diff_against_branch = opts.get('-p', 'master')
if not update_diff_against_branch(diff_against_branch, repo):
return
if not check_pushed(repo):
return
commits = [commit for commit in repo.iter_commits('{0}..'.format(diff_against_branch), no_merges=True)]
if not commits:
print "Nothing new to post."
return
current_repository = re.search(r'\/([^/]+)\.git', repo.remotes.origin.url).group(1)
update_review = get_update_review(opts, repo.head.ref.name, current_repository)
options = collect_options(repo, commits, update_review)
if options:
review_id = post_review(diff_against_branch, options)
if not review_id:
return
review_url = '%s/r/%s' % (REVIEW_BOARD_URL.rstrip('/'), review_id)
story_number = get_story_number(repo)
post_az_comment(story_number,
'Review request {0}: {1}'.format('changed' if update_review else 'posted', review_url))
post_message_on_irc(
'hey guys, please review #{0} at {1}'.format(
story_number,
review_url,
)
)
def check_pushed(repo):
if FORCE:
return True
branch = repo.head.ref # store current HEAD
if repo.git.log('origin/{0}..HEAD'.format(branch)):
print 'You have unpushed changes. You must push before posting a review'
return False
return True
def update_diff_against_branch(diff_against_branch, repo):
if FORCE:
return True
if repo.is_dirty():
print 'You have uncommitted changes. You must commit or stash before posting a review'
return False
branch = repo.head.ref # store current HEAD
if diff_against_branch is None:
print 'Iteration branch {0} has not yet been created'.format(diff_against_branch)
return False
print 'Bringing {0} up to date...'.format(diff_against_branch),
sys.stdout.flush()
repo.git.checkout(diff_against_branch)
repo.git.pull()
repo.git.checkout(branch.name)
print 'done.\n'
if len(list(repo.iter_commits('..{0}'.format(diff_against_branch), no_merges=True))) > 0:
print 'Your branch is behind {0}. Please run update'.format(diff_against_branch)
return False
return True
def get_iter_branch(repo):
iter_num = datetime.now().strftime('%U')
for branch in repo.heads:
if branch.name.startswith('iteration/{0}-'.format(iter_num)):
return branch.name
return None
def short_sha(repo, rev):
return repo.git.rev_parse(rev, short=True)
def get_message(commit):
return commit.message.split('\n')[0]
def get_review(commit):
try:
return commit.repo.git.notes('show', commit.hexsha, ref='review')
except GitCommandError, e:
if 'No note found' in e.stderr:
return None
else:
raise e
def get_update_review(opts, branch, repository_name):
if '-u' in opts:
review_data = get_existing_review_data(review_id=opts['-u'])
else:
review_data = get_existing_review_data(branch=branch, repository_name=repository_name)
if not review_data:
print color('New review', 'details-prompt')
else:
title = review_data['summary']
print '{0}{1} - {2} (link: {3})\n{4}{5}\n'.format(
color('Updating existing review #', 'details-prompt'),
color(review_data['id'], 'review'),
color(review_data['summary'], 'review'),
color("{0}/r/{1}/".format(REVIEW_BOARD_URL, review_data['id']), 'details-prompt'),
color('on repository ', 'details-prompt'),
color(repository_name, 'review')
)
return review_data
def get_existing_review_data(review_id=None, branch=None, repository_name=None):
if 'APPDATA' in os.environ:
homepath = os.environ['APPDATA']
elif 'HOME' in os.environ:
homepath = os.environ["HOME"]
else:
homepath = ''
cookie_file = os.path.join(homepath, ".post-review-cookies.txt")
rb = ReviewBoardServer(REVIEW_BOARD_URL, SCMCLIENTS[2], cookie_file)
rb.has_valid_cookie() # load cookie
if review_id != None:
return rb.get_review_request(review_id)
else:
results = filter(
lambda r: r['branch'] == branch and r['links']['repository']['title'] == repository_name,
rb.api_get('/api/review-requests')['review_requests'])
return results[0] if results else None
def get_story_number(repo):
match = re.search(r'(?:i/\d+|story)/\w+/(\d+)-', repo.head.ref.name)
if match:
return match.group(1)
return None
def collect_options(repo, commits, update_review):
descriptions = [u'{0} - {1} '.format(short_sha(repo, commit.hexsha), get_message(commit)) for commit in commits]
options = {
'summary': repo.head.ref.name,
'description': '\n'.join(descriptions),
'target-group': 'tech',
'branch': repo.head.ref.name,
}
story = get_story_number(repo)
if story:
options['bugs-closed'] = story
if update_review:
# collect existing additional description
old_descriptions = update_review['description'].split('\n')
while old_descriptions and old_descriptions[0] != '':
del old_descriptions[:1]
descriptions += old_descriptions
options['review-request-id'] = update_review['id']
options['summary'] = update_review['summary']
options['description'] = '\n'.join(descriptions)
just = lambda s: '{0:13}'.format(s)
print '{0} {1}'.format(color(just('Summary:'), 'details'), options['summary'])
for i, desc in enumerate(descriptions):
print u'{0} {1}'.format(color(just('Description:' if i == 0 else ''), 'details'), desc)
print ''
post = raw_input('{0} [Y/n] '.format(color('Post?', 'details-prompt'))).lower()
if not post.startswith('n'):
return [u'--{0}={1}'.format(k,v) for (k,v) in options.items()]
else:
return False
def post_review(diff_against_branch, options):
options.append('--revision-range={0}:HEAD'.format(diff_against_branch))
if AUTO_POST:
options.append('-p')
if OPEN_BROWSER:
options.append('-o')
if DEBUG:
options.append('-n')
options.append('--output-diff')
proc = Popen(['post-review'] + options, stdout=PIPE)
output = []
for line in proc.stdout:
print line,
output.append(line)
proc.wait()
if proc.returncode == 0 and not DEBUG:
for o in output:
found = re.search(r'Review request #(\d+)', o)
if found:
return found.group(1)
else:
return None
def color(text, type):
if USE_COLORS:
text = colored(text, COLORS.get(type, None))
return text
def post_message_on_irc(message, channels=None):
irc = irclib.IRC()
server = irc.server()
print "connecting to " + IRC_SERVER
server.connect(
IRC_SERVER, 6667,
'reviewboard',
ircname='rb-%s' % os.getenv('USER', 'developer'),
)
if not channels:
channels = ['#dev']
for channel in channels:
print "joining %s" % channel
server.join(channel, 'dOSO83uK')
print "asking devs to review the current story"
server.privmsg(channel, message)
server.process_data()
time.sleep(1)
print "disconnecting from irc"
time.sleep(1)
server.disconnect()
if __name__ == '__main__':
main(sys.argv[1:])