blob: 3d73c8b1c3908853d714b14754f412203eb43ddd [file] [log] [blame]
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +00001#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Get stats about your activity.
7
8Example:
9 - my_activity.py for stats for the current week (last week on mondays).
10 - my_activity.py -Q for stats for last quarter.
11 - my_activity.py -Y for stats for this year.
12 - my_activity.py -b 4/5/12 for stats since 4/5/12.
13 - my_activity.py -b 4/5/12 -e 6/7/12 for stats between 4/5/12 and 6/7/12.
14"""
15
16# These services typically only provide a created time and a last modified time
17# for each item for general queries. This is not enough to determine if there
18# was activity in a given time period. So, we first query for all things created
19# before end and modified after begin. Then, we get the details of each item and
20# check those details to determine if there was activity in the given period.
21# This means that query time scales mostly with (today() - begin).
22
23import cookielib
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +000024import csv
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +000025import datetime
26from datetime import datetime
27from datetime import timedelta
28from functools import partial
29import json
30import optparse
31import os
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +000032import re
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +000033import subprocess
34import sys
35import urllib
36import urllib2
37
38import rietveld
39from third_party import upload
40
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +000041# Imported later, once options are set.
42webkitpy = None
43
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +000044try:
45 from dateutil.relativedelta import relativedelta # pylint: disable=F0401
46except ImportError:
47 print 'python-dateutil package required'
48 exit(1)
49
50# python-keyring provides easy access to the system keyring.
51try:
52 import keyring # pylint: disable=W0611,F0401
53except ImportError:
54 print 'Consider installing python-keyring'
55
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +000056def webkit_account(user):
57 if not webkitpy:
58 return None
59 committer_list = webkitpy.common.config.committers.CommitterList()
60 email = user + "@chromium.org"
61 return committer_list.account_by_email(email)
62
63def user_to_webkit_email(user):
64 account = webkit_account(user)
65 if not account:
66 return None
67 return account.emails[0]
68
69def user_to_webkit_owner_search(user):
70 account = webkit_account(user)
71 if not account:
72 return ['--author=%s@chromium.org' % user]
73 search = []
74 for email in account.emails:
75 search.append('--author=' + email)
76 # commit-bot is author for contributors who are not committers.
77 search.append('--grep=Patch by ' + account.full_name)
78 return search
79
80def user_to_webkit_reviewer_search(user):
81 committer_list = webkitpy.common.config.committers.CommitterList()
82 email = user + "@chromium.org"
83 account = committer_list.reviewer_by_email(email)
84 if not account:
85 return []
86 return ['--grep=Reviewed by ' + account.full_name]
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +000087
88rietveld_instances = [
89 {
90 'url': 'codereview.chromium.org',
91 'shorturl': 'crrev.com',
92 'supports_owner_modified_query': True,
93 'requires_auth': False,
94 'email_domain': 'chromium.org',
95 },
96 {
97 'url': 'chromereviews.googleplex.com',
98 'shorturl': 'go/chromerev',
99 'supports_owner_modified_query': True,
100 'requires_auth': True,
101 'email_domain': 'google.com',
102 },
103 {
104 'url': 'codereview.appspot.com',
105 'supports_owner_modified_query': True,
106 'requires_auth': False,
107 'email_domain': 'chromium.org',
108 },
109 {
110 'url': 'breakpad.appspot.com',
111 'supports_owner_modified_query': False,
112 'requires_auth': False,
113 'email_domain': 'chromium.org',
114 },
115]
116
117gerrit_instances = [
118 {
deymo@chromium.org6c039202013-09-12 12:28:12 +0000119 'url': 'chromium-review.googlesource.com',
deymo@chromium.orge52bd5a2013-08-29 18:05:21 +0000120 'shorturl': 'crosreview.com',
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000121 },
deymo@chromium.org6c039202013-09-12 12:28:12 +0000122 # TODO(deymo): chrome-internal-review requires login credentials. Enable once
123 # login support is added to this client. See crbug.com/281695.
124 #{
125 # 'url': 'chrome-internal-review.googlesource.com',
126 # 'shorturl': 'crosreview.com/i',
127 #},
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000128 {
deymo@chromium.org6c039202013-09-12 12:28:12 +0000129 'host': 'gerrit.chromium.org',
130 'port': 29418,
131 },
132 {
133 'host': 'gerrit-int.chromium.org',
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000134 'port': 29419,
135 },
136]
137
138google_code_projects = [
139 {
140 'name': 'chromium',
141 'shorturl': 'crbug.com',
142 },
143 {
144 'name': 'chromium-os',
deymo@chromium.orgc840e212013-02-13 20:40:22 +0000145 'shorturl': 'crosbug.com',
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000146 },
147 {
148 'name': 'chrome-os-partner',
149 },
150 {
151 'name': 'google-breakpad',
152 },
153 {
154 'name': 'gyp',
enne@chromium.orgf01fad32012-11-26 18:09:38 +0000155 },
156 {
157 'name': 'skia',
158 },
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000159]
160
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000161bugzilla_instances = [
162 {
163 'search_url': 'http://bugs.webkit.org/buglist.cgi',
164 'url': 'wkb.ug',
165 'user_func': user_to_webkit_email,
166 },
167]
168
169git_instances = [
170 {
171 'option': 'webkit_repo',
172 'change_re':
173 r'git-svn-id: http://svn\.webkit\.org/repository/webkit/trunk@(\d*)',
174 'change_url': 'trac.webkit.org/changeset',
175 'review_re': r'https://bugs\.webkit\.org/show_bug\.cgi\?id\=(\d*)',
176 'review_url': 'wkb.ug',
177 'review_prop': 'webkit_bug_id',
178
179 'owner_search_func': user_to_webkit_owner_search,
180 'reviewer_search_func': user_to_webkit_reviewer_search,
181 },
182]
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000183
184# Uses ClientLogin to authenticate the user for Google Code issue trackers.
185def get_auth_token(email):
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000186 # KeyringCreds will use the system keyring on the first try, and prompt for
187 # a password on the next ones.
188 creds = upload.KeyringCreds('code.google.com', 'code.google.com', email)
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000189 for _ in xrange(3):
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000190 email, password = creds.GetUserCredentials()
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000191 url = 'https://www.google.com/accounts/ClientLogin'
192 data = urllib.urlencode({
193 'Email': email,
194 'Passwd': password,
195 'service': 'code',
196 'source': 'chrome-my-activity',
197 'accountType': 'GOOGLE',
198 })
199 req = urllib2.Request(url, data=data, headers={'Accept': 'text/plain'})
200 try:
201 response = urllib2.urlopen(req)
202 response_body = response.read()
203 response_dict = dict(x.split('=')
204 for x in response_body.split('\n') if x)
205 return response_dict['Auth']
206 except urllib2.HTTPError, e:
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000207 print e
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000208
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000209 print 'Unable to authenticate to code.google.com.'
210 print 'Some issues may be missing.'
211 return None
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000212
213
214def username(email):
215 """Keeps the username of an email address."""
216 return email and email.split('@', 1)[0]
217
218
cjhopman@chromium.org426557a2012-10-22 20:18:52 +0000219def datetime_to_midnight(date):
220 return date - timedelta(hours=date.hour, minutes=date.minute,
221 seconds=date.second, microseconds=date.microsecond)
222
223
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000224def get_quarter_of(date):
cjhopman@chromium.org426557a2012-10-22 20:18:52 +0000225 begin = (datetime_to_midnight(date) -
226 relativedelta(months=(date.month % 3) - 1, days=(date.day - 1)))
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000227 return begin, begin + relativedelta(months=3)
228
229
230def get_year_of(date):
cjhopman@chromium.org426557a2012-10-22 20:18:52 +0000231 begin = (datetime_to_midnight(date) -
232 relativedelta(months=(date.month - 1), days=(date.day - 1)))
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000233 return begin, begin + relativedelta(years=1)
234
235
236def get_week_of(date):
cjhopman@chromium.org426557a2012-10-22 20:18:52 +0000237 begin = (datetime_to_midnight(date) - timedelta(days=date.weekday()))
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000238 return begin, begin + timedelta(days=7)
239
240
241def get_yes_or_no(msg):
242 while True:
243 response = raw_input(msg + ' yes/no [no] ')
244 if response == 'y' or response == 'yes':
245 return True
246 elif not response or response == 'n' or response == 'no':
247 return False
248
249
deymo@chromium.org6c039202013-09-12 12:28:12 +0000250def datetime_from_gerrit(date_string):
251 return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f000')
252
253
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000254def datetime_from_rietveld(date_string):
255 return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f')
256
257
258def datetime_from_google_code(date_string):
259 return datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%fZ')
260
261
262class MyActivity(object):
263 def __init__(self, options):
264 self.options = options
265 self.modified_after = options.begin
266 self.modified_before = options.end
267 self.user = options.user
268 self.changes = []
269 self.reviews = []
270 self.issues = []
271 self.check_cookies()
272 self.google_code_auth_token = None
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000273 self.webkit_repo = options.webkit_repo
274 if self.webkit_repo:
275 self.setup_webkit_info()
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000276
277 # Check the codereview cookie jar to determine which Rietveld instances to
278 # authenticate to.
279 def check_cookies(self):
280 cookie_file = os.path.expanduser('~/.codereview_upload_cookies')
281 cookie_jar = cookielib.MozillaCookieJar(cookie_file)
282 if not os.path.exists(cookie_file):
283 exit(1)
284
285 try:
286 cookie_jar.load()
287 print 'Found cookie file: %s' % cookie_file
288 except (cookielib.LoadError, IOError):
289 exit(1)
290
291 filtered_instances = []
292
293 def has_cookie(instance):
294 for cookie in cookie_jar:
295 if cookie.name == 'SACSID' and cookie.domain == instance['url']:
296 return True
297 if self.options.auth:
298 return get_yes_or_no('No cookie found for %s. Authorize for this '
299 'instance? (may require application-specific '
300 'password)' % instance['url'])
301 filtered_instances.append(instance)
302 return False
303
304 for instance in rietveld_instances:
305 instance['auth'] = has_cookie(instance)
306
307 if filtered_instances:
308 print ('No cookie found for the following Rietveld instance%s:' %
309 ('s' if len(filtered_instances) > 1 else ''))
310 for instance in filtered_instances:
311 print '\t' + instance['url']
312 print 'Use --auth if you would like to authenticate to them.\n'
313
314 def rietveld_search(self, instance, owner=None, reviewer=None):
315 if instance['requires_auth'] and not instance['auth']:
316 return []
317
318
319 email = None if instance['auth'] else ''
320 remote = rietveld.Rietveld('https://' + instance['url'], email, None)
321
322 # See def search() in rietveld.py to see all the filters you can use.
323 query_modified_after = None
324
325 if instance['supports_owner_modified_query']:
326 query_modified_after = self.modified_after.strftime('%Y-%m-%d')
327
328 # Rietveld does not allow search by both created_before and modified_after.
329 # (And some instances don't allow search by both owner and modified_after)
330 owner_email = None
331 reviewer_email = None
332 if owner:
333 owner_email = owner + '@' + instance['email_domain']
334 if reviewer:
335 reviewer_email = reviewer + '@' + instance['email_domain']
336 issues = remote.search(
337 owner=owner_email,
338 reviewer=reviewer_email,
339 modified_after=query_modified_after,
340 with_messages=True)
341
342 issues = filter(
343 lambda i: (datetime_from_rietveld(i['created']) < self.modified_before),
344 issues)
345 issues = filter(
346 lambda i: (datetime_from_rietveld(i['modified']) > self.modified_after),
347 issues)
348
349 should_filter_by_user = True
350 issues = map(partial(self.process_rietveld_issue, instance), issues)
351 issues = filter(
352 partial(self.filter_issue, should_filter_by_user=should_filter_by_user),
353 issues)
354 issues = sorted(issues, key=lambda i: i['modified'], reverse=True)
355
356 return issues
357
358 def process_rietveld_issue(self, instance, issue):
359 ret = {}
360 ret['owner'] = issue['owner_email']
361 ret['author'] = ret['owner']
362
cjhopman@chromium.org53c1e562013-03-11 20:02:38 +0000363 ret['reviewers'] = set(issue['reviewers'])
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000364
365 shorturl = instance['url']
366 if 'shorturl' in instance:
367 shorturl = instance['shorturl']
368
369 ret['review_url'] = 'http://%s/%d' % (shorturl, issue['issue'])
cjhopman@chromium.org53c1e562013-03-11 20:02:38 +0000370
371 # Rietveld sometimes has '\r\n' instead of '\n'.
372 ret['header'] = issue['description'].replace('\r', '').split('\n')[0]
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000373
374 ret['modified'] = datetime_from_rietveld(issue['modified'])
375 ret['created'] = datetime_from_rietveld(issue['created'])
376 ret['replies'] = self.process_rietveld_replies(issue['messages'])
377
378 return ret
379
380 @staticmethod
381 def process_rietveld_replies(replies):
382 ret = []
383 for reply in replies:
384 r = {}
385 r['author'] = reply['sender']
386 r['created'] = datetime_from_rietveld(reply['date'])
387 r['content'] = ''
388 ret.append(r)
389 return ret
390
deymo@chromium.org6c039202013-09-12 12:28:12 +0000391 @staticmethod
392 def gerrit_changes_over_ssh(instance, filters):
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000393 # See https://review.openstack.org/Documentation/cmd-query.html
394 # Gerrit doesn't allow filtering by created time, only modified time.
deymo@chromium.org6c039202013-09-12 12:28:12 +0000395 gquery_cmd = ['ssh', '-p', str(instance['port']), instance['host'],
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000396 'gerrit', 'query',
397 '--format', 'JSON',
398 '--comments',
deymo@chromium.org6c039202013-09-12 12:28:12 +0000399 '--'] + filters
400 (stdout, _) = subprocess.Popen(gquery_cmd, stdout=subprocess.PIPE,
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000401 stderr=subprocess.PIPE).communicate()
deymo@chromium.org6c039202013-09-12 12:28:12 +0000402 # Drop the last line of the output with the stats.
403 issues = stdout.splitlines()[:-1]
404 return map(json.loads, issues)
405
406 @staticmethod
407 def gerrit_changes_over_rest(instance, filters):
408 # See https://gerrit-review.googlesource.com/Documentation/rest-api.html
409 # Gerrit doesn't allow filtering by created time, only modified time.
410 args = urllib.urlencode([
411 ('q', ' '.join(filters)),
412 ('o', 'MESSAGES'),
413 ('o', 'LABELS')])
414 rest_url = 'https://%s/changes/?%s' % (instance['url'], args)
415
416 req = urllib2.Request(rest_url, headers={'Accept': 'text/plain'})
417 try:
418 response = urllib2.urlopen(req)
419 stdout = response.read()
420 except urllib2.HTTPError, e:
421 print 'ERROR: Looking up %r: %s' % (rest_url, e)
422 return []
423
424 # Check that the returned JSON starts with the right marker.
425 if stdout[:5] != ")]}'\n":
426 print 'ERROR: Marker not found on REST API response: %r' % stdout[:5]
427 return []
428 return json.loads(stdout[5:])
429
430 def gerrit_search(self, instance, owner=None, reviewer=None):
431 max_age = datetime.today() - self.modified_after
432 max_age = max_age.days * 24 * 3600 + max_age.seconds
433 user_filter = 'owner:%s' % owner if owner else 'reviewer:%s' % reviewer
434 filters = ['-age:%ss' % max_age, user_filter]
435
436 # Determine the gerrit interface to use: SSH or REST API:
437 if 'host' in instance:
438 issues = self.gerrit_changes_over_ssh(instance, filters)
439 issues = [self.process_gerrit_ssh_issue(instance, issue)
440 for issue in issues]
441 elif 'url' in instance:
442 issues = self.gerrit_changes_over_rest(instance, filters)
443 issues = [self.process_gerrit_rest_issue(instance, issue)
444 for issue in issues]
445 else:
446 raise Exception('Invalid gerrit_instances configuration.')
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000447
448 # TODO(cjhopman): should we filter abandoned changes?
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000449 issues = filter(self.filter_issue, issues)
450 issues = sorted(issues, key=lambda i: i['modified'], reverse=True)
451
452 return issues
453
deymo@chromium.org6c039202013-09-12 12:28:12 +0000454 def process_gerrit_ssh_issue(self, instance, issue):
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000455 ret = {}
456 ret['review_url'] = issue['url']
deymo@chromium.orge52bd5a2013-08-29 18:05:21 +0000457 if 'shorturl' in instance:
458 ret['review_url'] = 'http://%s/%s' % (instance['shorturl'],
459 issue['number'])
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000460 ret['header'] = issue['subject']
461 ret['owner'] = issue['owner']['email']
462 ret['author'] = ret['owner']
463 ret['created'] = datetime.fromtimestamp(issue['createdOn'])
464 ret['modified'] = datetime.fromtimestamp(issue['lastUpdated'])
465 if 'comments' in issue:
deymo@chromium.org6c039202013-09-12 12:28:12 +0000466 ret['replies'] = self.process_gerrit_ssh_issue_replies(issue['comments'])
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000467 else:
468 ret['replies'] = []
deymo@chromium.org6c039202013-09-12 12:28:12 +0000469 ret['reviewers'] = set(r['author'] for r in ret['replies'])
470 ret['reviewers'].discard(ret['author'])
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000471 return ret
472
473 @staticmethod
deymo@chromium.org6c039202013-09-12 12:28:12 +0000474 def process_gerrit_ssh_issue_replies(replies):
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000475 ret = []
476 replies = filter(lambda r: 'email' in r['reviewer'], replies)
477 for reply in replies:
deymo@chromium.org6c039202013-09-12 12:28:12 +0000478 ret.append({
479 'author': reply['reviewer']['email'],
480 'created': datetime.fromtimestamp(reply['timestamp']),
481 'content': '',
482 })
483 return ret
484
485 def process_gerrit_rest_issue(self, instance, issue):
486 ret = {}
487 ret['review_url'] = 'https://%s/%s' % (instance['url'], issue['_number'])
488 if 'shorturl' in instance:
489 # TODO(deymo): Move this short link to https once crosreview.com supports
490 # it.
491 ret['review_url'] = 'http://%s/%s' % (instance['shorturl'],
492 issue['_number'])
493 ret['header'] = issue['subject']
494 ret['owner'] = issue['owner']['email']
495 ret['author'] = ret['owner']
496 ret['created'] = datetime_from_gerrit(issue['created'])
497 ret['modified'] = datetime_from_gerrit(issue['updated'])
498 if 'messages' in issue:
499 ret['replies'] = self.process_gerrit_rest_issue_replies(issue['messages'])
500 else:
501 ret['replies'] = []
502 ret['reviewers'] = set(r['author'] for r in ret['replies'])
503 ret['reviewers'].discard(ret['author'])
504 return ret
505
506 @staticmethod
507 def process_gerrit_rest_issue_replies(replies):
508 ret = []
509 replies = filter(lambda r: 'email' in r['author'], replies)
510 for reply in replies:
511 ret.append({
512 'author': reply['author']['email'],
513 'created': datetime_from_gerrit(reply['date']),
514 'content': reply['message'],
515 })
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000516 return ret
517
518 def google_code_issue_search(self, instance):
519 time_format = '%Y-%m-%dT%T'
520 # See http://code.google.com/p/support/wiki/IssueTrackerAPI
521 # q=<owner>@chromium.org does a full text search for <owner>@chromium.org.
522 # This will accept the issue if owner is the owner or in the cc list. Might
523 # have some false positives, though.
524
525 # Don't filter normally on modified_before because it can filter out things
526 # that were modified in the time period and then modified again after it.
527 gcode_url = ('https://code.google.com/feeds/issues/p/%s/issues/full' %
528 instance['name'])
529
530 gcode_data = urllib.urlencode({
531 'alt': 'json',
532 'max-results': '100000',
533 'q': '%s' % self.user,
534 'published-max': self.modified_before.strftime(time_format),
535 'updated-min': self.modified_after.strftime(time_format),
536 })
537
538 opener = urllib2.build_opener()
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000539 if self.google_code_auth_token:
540 opener.addheaders = [('Authorization', 'GoogleLogin auth=%s' %
541 self.google_code_auth_token)]
542 gcode_json = None
543 try:
544 gcode_get = opener.open(gcode_url + '?' + gcode_data)
545 gcode_json = json.load(gcode_get)
546 gcode_get.close()
547 except urllib2.HTTPError, _:
548 print 'Unable to access ' + instance['name'] + ' issue tracker.'
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000549
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000550 if not gcode_json or 'entry' not in gcode_json['feed']:
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000551 return []
552
553 issues = gcode_json['feed']['entry']
554 issues = map(partial(self.process_google_code_issue, instance), issues)
555 issues = filter(self.filter_issue, issues)
556 issues = sorted(issues, key=lambda i: i['modified'], reverse=True)
557 return issues
558
559 def process_google_code_issue(self, project, issue):
560 ret = {}
561 ret['created'] = datetime_from_google_code(issue['published']['$t'])
562 ret['modified'] = datetime_from_google_code(issue['updated']['$t'])
563
564 ret['owner'] = ''
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +0000565 if 'issues$owner' in issue:
566 ret['owner'] = issue['issues$owner']['issues$username']['$t']
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000567 ret['author'] = issue['author'][0]['name']['$t']
568
569 if 'shorturl' in project:
570 issue_id = issue['id']['$t']
571 issue_id = issue_id[issue_id.rfind('/') + 1:]
572 ret['url'] = 'http://%s/%d' % (project['shorturl'], int(issue_id))
573 else:
574 issue_url = issue['link'][1]
575 if issue_url['rel'] != 'alternate':
576 raise RuntimeError
577 ret['url'] = issue_url['href']
578 ret['header'] = issue['title']['$t']
579
580 ret['replies'] = self.get_google_code_issue_replies(issue)
581 return ret
582
583 def get_google_code_issue_replies(self, issue):
584 """Get all the comments on the issue."""
585 replies_url = issue['link'][0]
586 if replies_url['rel'] != 'replies':
587 raise RuntimeError
588
589 replies_data = urllib.urlencode({
590 'alt': 'json',
591 'fields': 'entry(published,author,content)',
592 })
593
594 opener = urllib2.build_opener()
595 opener.addheaders = [('Authorization', 'GoogleLogin auth=%s' %
596 self.google_code_auth_token)]
597 try:
598 replies_get = opener.open(replies_url['href'] + '?' + replies_data)
599 except urllib2.HTTPError, _:
600 return []
601
602 replies_json = json.load(replies_get)
603 replies_get.close()
604 return self.process_google_code_issue_replies(replies_json)
605
606 @staticmethod
607 def process_google_code_issue_replies(replies):
608 if 'entry' not in replies['feed']:
609 return []
610
611 ret = []
612 for entry in replies['feed']['entry']:
613 e = {}
614 e['created'] = datetime_from_google_code(entry['published']['$t'])
615 e['content'] = entry['content']['$t']
616 e['author'] = entry['author'][0]['name']['$t']
617 ret.append(e)
618 return ret
619
620 @staticmethod
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000621 def git_cmd(repo, *args):
622 cmd = ['git', '--git-dir=%s/.git' % repo]
623 cmd.extend(args)
624 [stdout, _] = subprocess.Popen(cmd, stdout=subprocess.PIPE,
625 stderr=subprocess.PIPE).communicate()
626 lines = str(stdout).split('\n')[:-1]
627 return lines
628
629 def git_search(self, instance, owner=None, reviewer=None):
630 repo = getattr(self, instance['option'])
631 if not repo:
632 return []
633
634 search = []
635 if owner:
636 search.extend(instance['owner_search_func'](owner))
637 if reviewer:
638 search.extend(instance['reviewer_search_func'](reviewer))
639 if not len(search):
640 return []
641
642 self.git_cmd(repo, 'fetch', 'origin')
643
644 time_format = '%Y-%m-%d %H:%M:%S'
645 log_args = [
646 '--after=' + self.modified_after.strftime(time_format),
647 '--before=' + self.modified_before.strftime(time_format),
648 '--format=%H'
649 ]
650 commits = set()
651 for query in search:
652 query_args = [query]
653 query_args.extend(log_args)
654 commits |= set(self.git_cmd(repo, 'log', 'origin/master', *query_args))
655
656 ret = []
657 for commit in commits:
658 output = self.git_cmd(repo, 'log', commit + "^!", "--format=%cn%n%cd%n%B")
659 author = output[0]
660 date = datetime.strptime(output[1], "%a %b %d %H:%M:%S %Y +0000")
enne@chromium.orgd69dab92013-06-10 20:19:56 +0000661 processed = self.process_git_commit(instance, author, date, output[2:])
662 if processed:
663 ret.append(processed)
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000664
665 ret = sorted(ret, key=lambda i: i['modified'], reverse=True)
666 return ret
667
668 @staticmethod
669 def process_git_commit(instance, author, date, log):
670 ret = {}
671 ret['owner'] = author
672 ret['author'] = author
673 ret['modified'] = date
674 ret['created'] = date
675 ret['header'] = log[0]
676
677 reviews = []
678 reviewers = []
679 changes = []
680
681 for line in log:
682 match = re.match(r'Reviewed by ([^.]*)', line)
683 if match:
684 reviewers.append(match.group(1))
685 if instance['review_re']:
686 match = re.match(instance['review_re'], line)
687 if match:
688 reviews.append(int(match.group(1)))
689 if instance['change_re']:
690 match = re.match(instance['change_re'], line)
691 if match:
692 changes.append(int(match.group(1)))
693
cjhopman@chromium.org53c1e562013-03-11 20:02:38 +0000694 committer_list = webkitpy.common.config.committers.CommitterList()
695 ret['reviewers'] = set(
696 (committer_list.contributor_by_name(r).emails[0] for r in reviewers))
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000697
698 # Reviews more useful than change link itself, but tricky if multiple
699 # Reviews == bugs for WebKit changes
700 if len(reviews) == 1:
701 url = 'http://%s/%d' % (instance['review_url'], reviews[0])
702 if instance['review_prop']:
703 ret[instance['review_prop']] = reviews[0]
enne@chromium.orgd69dab92013-06-10 20:19:56 +0000704 elif len(changes) == 1:
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000705 url = 'http://%s/%d' % (instance['change_url'], changes[0])
enne@chromium.orgd69dab92013-06-10 20:19:56 +0000706 else:
707 # Couldn't find anything.
708 return None
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000709 ret['review_url'] = url
710
711 return ret
712
713 def bugzilla_issues(self, instance, user):
714 if instance['user_func']:
715 user = instance['user_func'](user)
716 if not user:
717 return []
718
719 # This search is a little iffy, as it returns any bug that has been
720 # modified over a time period in any way and that a user has ever commented
721 # on, but that's the best that Bugzilla can get us. Oops.
722 commented = { 'emaillongdesc1': 1 }
723 issues = self.bugzilla_search(instance, user, commented)
724 issues = filter(lambda issue: issue['owner'] != user, issues)
725
726 reported = { 'emailreporter1': 1, 'chfield': '[Bug creation]' }
727 issues.extend(self.bugzilla_search(instance, user, reported))
728
729 # Remove duplicates by bug id
730 seen = {}
731 pruned = []
732 for issue in issues:
733 bug_id = issue['webkit_bug_id']
734 if bug_id in seen:
735 continue
736 seen[bug_id] = True
737 pruned.append(issue)
738
739 # Bugzilla has no modified time, so sort by id?
740 pruned = sorted(pruned, key=lambda i: i['webkit_bug_id'])
741 return issues
742
743 def bugzilla_search(self, instance, user, params):
744 time_format = '%Y-%m-%d'
745 values = {
746 'chfieldfrom': self.modified_after.strftime(time_format),
747 'chfieldto': self.modified_before.strftime(time_format),
748 'ctype': 'csv',
749 'emailtype1': 'substring',
750 'email1': '%s' % user,
751 }
752 values.update(params)
753
754 # Must be GET not POST
755 data = urllib.urlencode(values)
756 req = urllib2.Request("%s?%s" % (instance['search_url'], data))
757 response = urllib2.urlopen(req)
758 reader = csv.reader(response)
759 reader.next() # skip the header line
760
761 issues = map(partial(self.process_bugzilla_issue, instance), reader)
762 return issues
763
764 @staticmethod
765 def process_bugzilla_issue(instance, issue):
766 bug_id, owner, desc = int(issue[0]), issue[4], issue[7]
767
768 ret = {}
769 ret['owner'] = owner
770 ret['author'] = owner
771 ret['review_url'] = 'http://%s/%d' % (instance['url'], bug_id)
772 ret['url'] = ret['review_url']
773 ret['header'] = desc
774 ret['webkit_bug_id'] = bug_id
775 return ret
776
777 def setup_webkit_info(self):
778 assert(self.webkit_repo)
779 git_dir = os.path.normpath(self.webkit_repo + "/.git")
780 if not os.path.exists(git_dir):
scheib@chromium.orgb299e7c2012-11-13 21:34:34 +0000781 print "%s doesn't exist, skipping WebKit checks." % git_dir
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000782 self.webkit_repo = None
783 return
784
785 try:
786 self.git_cmd(self.webkit_repo, "fetch", "origin")
787 except subprocess.CalledProcessError:
scheib@chromium.orgb299e7c2012-11-13 21:34:34 +0000788 print "Failed to update WebKit repo, skipping WebKit checks."
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000789 self.webkit_repo = None
790 return
791
792 path = "Tools/Scripts"
793 full_path = os.path.normpath("%s/%s" % (self.options.webkit_repo, path))
794 sys.path.append(full_path)
795
796 try:
797 global webkitpy
798 webkitpy = __import__('webkitpy.common.config.committers')
799 except ImportError:
scheib@chromium.orgb299e7c2012-11-13 21:34:34 +0000800 print "Failed to import WebKit committer list, skipping WebKit checks."
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000801 self.webkit_repo = None
802 return
803
804 if not webkit_account(self.user):
805 email = self.user + "@chromium.org"
scheib@chromium.orgb299e7c2012-11-13 21:34:34 +0000806 print "No %s in committers.py, skipping WebKit checks." % email
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000807 self.webkit_repo = None
808
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +0000809 def print_change(self, change):
cjhopman@chromium.org53c1e562013-03-11 20:02:38 +0000810 optional_values = {
811 'reviewers': ', '.join(change['reviewers'])
812 }
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +0000813 self.print_generic(self.options.output_format,
814 self.options.output_format_changes,
815 change['header'],
816 change['review_url'],
cjhopman@chromium.org53c1e562013-03-11 20:02:38 +0000817 change['author'],
818 optional_values)
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +0000819
820 def print_issue(self, issue):
821 optional_values = {
822 'owner': issue['owner'],
823 }
824 self.print_generic(self.options.output_format,
825 self.options.output_format_issues,
826 issue['header'],
827 issue['url'],
828 issue['author'],
829 optional_values)
830
831 def print_review(self, review):
832 self.print_generic(self.options.output_format,
833 self.options.output_format_reviews,
834 review['header'],
835 review['review_url'],
836 review['author'])
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000837
838 @staticmethod
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +0000839 def print_generic(default_fmt, specific_fmt,
840 title, url, author,
841 optional_values=None):
842 output_format = specific_fmt if specific_fmt is not None else default_fmt
843 output_format = unicode(output_format)
844 required_values = {
845 'title': title,
846 'url': url,
847 'author': author,
848 }
849 # Merge required and optional values.
850 if optional_values is not None:
851 values = dict(required_values.items() + optional_values.items())
852 else:
853 values = required_values
854 print output_format.format(**values)
855
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000856
857 def filter_issue(self, issue, should_filter_by_user=True):
858 def maybe_filter_username(email):
859 return not should_filter_by_user or username(email) == self.user
860 if (maybe_filter_username(issue['author']) and
861 self.filter_modified(issue['created'])):
862 return True
863 if (maybe_filter_username(issue['owner']) and
864 (self.filter_modified(issue['created']) or
865 self.filter_modified(issue['modified']))):
866 return True
867 for reply in issue['replies']:
868 if self.filter_modified(reply['created']):
869 if not should_filter_by_user:
870 break
871 if (username(reply['author']) == self.user
872 or (self.user + '@') in reply['content']):
873 break
874 else:
875 return False
876 return True
877
878 def filter_modified(self, modified):
879 return self.modified_after < modified and modified < self.modified_before
880
881 def auth_for_changes(self):
882 #TODO(cjhopman): Move authentication check for getting changes here.
883 pass
884
885 def auth_for_reviews(self):
886 # Reviews use all the same instances as changes so no authentication is
887 # required.
888 pass
889
890 def auth_for_issues(self):
891 self.google_code_auth_token = (
892 get_auth_token(self.options.local_user + '@chromium.org'))
893
894 def get_changes(self):
895 for instance in rietveld_instances:
896 self.changes += self.rietveld_search(instance, owner=self.user)
897
898 for instance in gerrit_instances:
899 self.changes += self.gerrit_search(instance, owner=self.user)
900
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000901 for instance in git_instances:
902 self.changes += self.git_search(instance, owner=self.user)
903
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000904 def print_changes(self):
905 if self.changes:
906 print '\nChanges:'
907 for change in self.changes:
908 self.print_change(change)
909
910 def get_reviews(self):
911 for instance in rietveld_instances:
912 self.reviews += self.rietveld_search(instance, reviewer=self.user)
913
914 for instance in gerrit_instances:
915 reviews = self.gerrit_search(instance, reviewer=self.user)
916 reviews = filter(lambda r: not username(r['owner']) == self.user, reviews)
917 self.reviews += reviews
918
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000919 for instance in git_instances:
920 self.reviews += self.git_search(instance, reviewer=self.user)
921
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000922 def print_reviews(self):
923 if self.reviews:
924 print '\nReviews:'
925 for review in self.reviews:
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +0000926 self.print_review(review)
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000927
928 def get_issues(self):
929 for project in google_code_projects:
930 self.issues += self.google_code_issue_search(project)
931
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000932 for instance in bugzilla_instances:
933 self.issues += self.bugzilla_issues(instance, self.user)
934
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +0000935 def print_issues(self):
936 if self.issues:
937 print '\nIssues:'
938 for issue in self.issues:
939 self.print_issue(issue)
940
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000941 def process_activities(self):
942 # If a webkit bug was a review, don't list it as an issue.
943 ids = {}
944 for review in self.reviews + self.changes:
945 if 'webkit_bug_id' in review:
946 ids[review['webkit_bug_id']] = True
947
948 def duplicate_issue(issue):
949 if 'webkit_bug_id' not in issue:
950 return False
951 return issue['webkit_bug_id'] in ids
952
953 self.issues = filter(lambda issue: not duplicate_issue(issue), self.issues)
954
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000955 def print_activity(self):
956 self.print_changes()
957 self.print_reviews()
958 self.print_issues()
959
960
961def main():
962 # Silence upload.py.
963 rietveld.upload.verbosity = 0
964
965 parser = optparse.OptionParser(description=sys.modules[__name__].__doc__)
966 parser.add_option(
967 '-u', '--user', metavar='<email>',
968 default=os.environ.get('USER'),
969 help='Filter on user, default=%default')
970 parser.add_option(
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000971 '--webkit_repo', metavar='<dir>',
972 default='%s' % os.environ.get('WEBKIT_DIR'),
973 help='Local path to WebKit repository, default=%default')
974 parser.add_option(
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000975 '-b', '--begin', metavar='<date>',
976 help='Filter issues created after the date')
977 parser.add_option(
978 '-e', '--end', metavar='<date>',
979 help='Filter issues created before the date')
980 quarter_begin, quarter_end = get_quarter_of(datetime.today() -
981 relativedelta(months=2))
982 parser.add_option(
983 '-Q', '--last_quarter', action='store_true',
984 help='Use last quarter\'s dates, e.g. %s to %s' % (
985 quarter_begin.strftime('%Y-%m-%d'), quarter_end.strftime('%Y-%m-%d')))
986 parser.add_option(
987 '-Y', '--this_year', action='store_true',
988 help='Use this year\'s dates')
989 parser.add_option(
990 '-w', '--week_of', metavar='<date>',
991 help='Show issues for week of the date')
992 parser.add_option(
993 '-a', '--auth',
994 action='store_true',
995 help='Ask to authenticate for instances with no auth cookie')
996
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +0000997 activity_types_group = optparse.OptionGroup(parser, 'Activity Types',
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000998 'By default, all activity will be looked up and '
999 'printed. If any of these are specified, only '
1000 'those specified will be searched.')
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +00001001 activity_types_group.add_option(
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +00001002 '-c', '--changes',
1003 action='store_true',
1004 help='Show changes.')
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +00001005 activity_types_group.add_option(
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +00001006 '-i', '--issues',
1007 action='store_true',
1008 help='Show issues.')
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +00001009 activity_types_group.add_option(
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +00001010 '-r', '--reviews',
1011 action='store_true',
1012 help='Show reviews.')
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +00001013 parser.add_option_group(activity_types_group)
1014
1015 output_format_group = optparse.OptionGroup(parser, 'Output Format',
1016 'By default, all activity will be printed in the '
1017 'following format: {url} {title}. This can be '
1018 'changed for either all activity types or '
1019 'individually for each activity type. The format '
1020 'is defined as documented for '
1021 'string.format(...). The variables available for '
1022 'all activity types are url, title and author. '
1023 'Format options for specific activity types will '
1024 'override the generic format.')
1025 output_format_group.add_option(
1026 '-f', '--output-format', metavar='<format>',
1027 default=u'{url} {title}',
1028 help='Specifies the format to use when printing all your activity.')
1029 output_format_group.add_option(
1030 '--output-format-changes', metavar='<format>',
1031 default=None,
cjhopman@chromium.org53c1e562013-03-11 20:02:38 +00001032 help='Specifies the format to use when printing changes. Supports the '
1033 'additional variable {reviewers}')
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +00001034 output_format_group.add_option(
1035 '--output-format-issues', metavar='<format>',
1036 default=None,
cjhopman@chromium.org53c1e562013-03-11 20:02:38 +00001037 help='Specifies the format to use when printing issues. Supports the '
1038 'additional variable {owner}.')
nyquist@chromium.org18bc90d2012-12-20 19:26:47 +00001039 output_format_group.add_option(
1040 '--output-format-reviews', metavar='<format>',
1041 default=None,
1042 help='Specifies the format to use when printing reviews.')
1043 parser.add_option_group(output_format_group)
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +00001044
1045 # Remove description formatting
1046 parser.format_description = (
1047 lambda _: parser.description) # pylint: disable=E1101
1048
1049 options, args = parser.parse_args()
1050 options.local_user = os.environ.get('USER')
1051 if args:
1052 parser.error('Args unsupported')
1053 if not options.user:
1054 parser.error('USER is not set, please use -u')
1055
1056 options.user = username(options.user)
1057
1058 if not options.begin:
1059 if options.last_quarter:
1060 begin, end = quarter_begin, quarter_end
1061 elif options.this_year:
1062 begin, end = get_year_of(datetime.today())
1063 elif options.week_of:
1064 begin, end = (get_week_of(datetime.strptime(options.week_of, '%m/%d/%y')))
1065 else:
1066 begin, end = (get_week_of(datetime.today() - timedelta(days=1)))
1067 else:
1068 begin = datetime.strptime(options.begin, '%m/%d/%y')
1069 if options.end:
1070 end = datetime.strptime(options.end, '%m/%d/%y')
1071 else:
1072 end = datetime.today()
1073 options.begin, options.end = begin, end
1074
1075 print 'Searching for activity by %s' % options.user
1076 print 'Using range %s to %s' % (options.begin, options.end)
1077
1078 my_activity = MyActivity(options)
1079
1080 if not (options.changes or options.reviews or options.issues):
1081 options.changes = True
1082 options.issues = True
1083 options.reviews = True
1084
1085 # First do any required authentication so none of the user interaction has to
1086 # wait for actual work.
1087 if options.changes:
1088 my_activity.auth_for_changes()
1089 if options.reviews:
1090 my_activity.auth_for_reviews()
1091 if options.issues:
1092 my_activity.auth_for_issues()
1093
1094 print 'Looking up activity.....'
1095
1096 if options.changes:
1097 my_activity.get_changes()
1098 if options.reviews:
1099 my_activity.get_reviews()
1100 if options.issues:
1101 my_activity.get_issues()
1102
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +00001103 my_activity.process_activities()
1104
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +00001105 print '\n\n\n'
1106
1107 my_activity.print_changes()
1108 my_activity.print_reviews()
1109 my_activity.print_issues()
1110 return 0
1111
1112
1113if __name__ == '__main__':
1114 sys.exit(main())