blob: 67354948e360722e46bbb251ef54e58262b2188a [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 {
119 'url': 'gerrit.chromium.org',
120 'port': 29418,
121 },
122 {
123 'url': 'gerrit-int.chromium.org',
124 'port': 29419,
125 },
126]
127
128google_code_projects = [
129 {
130 'name': 'chromium',
131 'shorturl': 'crbug.com',
132 },
133 {
134 'name': 'chromium-os',
135 },
136 {
137 'name': 'chrome-os-partner',
138 },
139 {
140 'name': 'google-breakpad',
141 },
142 {
143 'name': 'gyp',
144 }
145]
146
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000147bugzilla_instances = [
148 {
149 'search_url': 'http://bugs.webkit.org/buglist.cgi',
150 'url': 'wkb.ug',
151 'user_func': user_to_webkit_email,
152 },
153]
154
155git_instances = [
156 {
157 'option': 'webkit_repo',
158 'change_re':
159 r'git-svn-id: http://svn\.webkit\.org/repository/webkit/trunk@(\d*)',
160 'change_url': 'trac.webkit.org/changeset',
161 'review_re': r'https://bugs\.webkit\.org/show_bug\.cgi\?id\=(\d*)',
162 'review_url': 'wkb.ug',
163 'review_prop': 'webkit_bug_id',
164
165 'owner_search_func': user_to_webkit_owner_search,
166 'reviewer_search_func': user_to_webkit_reviewer_search,
167 },
168]
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000169
170# Uses ClientLogin to authenticate the user for Google Code issue trackers.
171def get_auth_token(email):
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000172 # KeyringCreds will use the system keyring on the first try, and prompt for
173 # a password on the next ones.
174 creds = upload.KeyringCreds('code.google.com', 'code.google.com', email)
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000175 for _ in xrange(3):
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000176 email, password = creds.GetUserCredentials()
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000177 url = 'https://www.google.com/accounts/ClientLogin'
178 data = urllib.urlencode({
179 'Email': email,
180 'Passwd': password,
181 'service': 'code',
182 'source': 'chrome-my-activity',
183 'accountType': 'GOOGLE',
184 })
185 req = urllib2.Request(url, data=data, headers={'Accept': 'text/plain'})
186 try:
187 response = urllib2.urlopen(req)
188 response_body = response.read()
189 response_dict = dict(x.split('=')
190 for x in response_body.split('\n') if x)
191 return response_dict['Auth']
192 except urllib2.HTTPError, e:
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000193 print e
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000194
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000195 print 'Unable to authenticate to code.google.com.'
196 print 'Some issues may be missing.'
197 return None
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000198
199
200def username(email):
201 """Keeps the username of an email address."""
202 return email and email.split('@', 1)[0]
203
204
cjhopman@chromium.org426557a2012-10-22 20:18:52 +0000205def datetime_to_midnight(date):
206 return date - timedelta(hours=date.hour, minutes=date.minute,
207 seconds=date.second, microseconds=date.microsecond)
208
209
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000210def get_quarter_of(date):
cjhopman@chromium.org426557a2012-10-22 20:18:52 +0000211 begin = (datetime_to_midnight(date) -
212 relativedelta(months=(date.month % 3) - 1, days=(date.day - 1)))
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000213 return begin, begin + relativedelta(months=3)
214
215
216def get_year_of(date):
cjhopman@chromium.org426557a2012-10-22 20:18:52 +0000217 begin = (datetime_to_midnight(date) -
218 relativedelta(months=(date.month - 1), days=(date.day - 1)))
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000219 return begin, begin + relativedelta(years=1)
220
221
222def get_week_of(date):
cjhopman@chromium.org426557a2012-10-22 20:18:52 +0000223 begin = (datetime_to_midnight(date) - timedelta(days=date.weekday()))
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000224 return begin, begin + timedelta(days=7)
225
226
227def get_yes_or_no(msg):
228 while True:
229 response = raw_input(msg + ' yes/no [no] ')
230 if response == 'y' or response == 'yes':
231 return True
232 elif not response or response == 'n' or response == 'no':
233 return False
234
235
236def datetime_from_rietveld(date_string):
237 return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f')
238
239
240def datetime_from_google_code(date_string):
241 return datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%fZ')
242
243
244class MyActivity(object):
245 def __init__(self, options):
246 self.options = options
247 self.modified_after = options.begin
248 self.modified_before = options.end
249 self.user = options.user
250 self.changes = []
251 self.reviews = []
252 self.issues = []
253 self.check_cookies()
254 self.google_code_auth_token = None
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000255 self.webkit_repo = options.webkit_repo
256 if self.webkit_repo:
257 self.setup_webkit_info()
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000258
259 # Check the codereview cookie jar to determine which Rietveld instances to
260 # authenticate to.
261 def check_cookies(self):
262 cookie_file = os.path.expanduser('~/.codereview_upload_cookies')
263 cookie_jar = cookielib.MozillaCookieJar(cookie_file)
264 if not os.path.exists(cookie_file):
265 exit(1)
266
267 try:
268 cookie_jar.load()
269 print 'Found cookie file: %s' % cookie_file
270 except (cookielib.LoadError, IOError):
271 exit(1)
272
273 filtered_instances = []
274
275 def has_cookie(instance):
276 for cookie in cookie_jar:
277 if cookie.name == 'SACSID' and cookie.domain == instance['url']:
278 return True
279 if self.options.auth:
280 return get_yes_or_no('No cookie found for %s. Authorize for this '
281 'instance? (may require application-specific '
282 'password)' % instance['url'])
283 filtered_instances.append(instance)
284 return False
285
286 for instance in rietveld_instances:
287 instance['auth'] = has_cookie(instance)
288
289 if filtered_instances:
290 print ('No cookie found for the following Rietveld instance%s:' %
291 ('s' if len(filtered_instances) > 1 else ''))
292 for instance in filtered_instances:
293 print '\t' + instance['url']
294 print 'Use --auth if you would like to authenticate to them.\n'
295
296 def rietveld_search(self, instance, owner=None, reviewer=None):
297 if instance['requires_auth'] and not instance['auth']:
298 return []
299
300
301 email = None if instance['auth'] else ''
302 remote = rietveld.Rietveld('https://' + instance['url'], email, None)
303
304 # See def search() in rietveld.py to see all the filters you can use.
305 query_modified_after = None
306
307 if instance['supports_owner_modified_query']:
308 query_modified_after = self.modified_after.strftime('%Y-%m-%d')
309
310 # Rietveld does not allow search by both created_before and modified_after.
311 # (And some instances don't allow search by both owner and modified_after)
312 owner_email = None
313 reviewer_email = None
314 if owner:
315 owner_email = owner + '@' + instance['email_domain']
316 if reviewer:
317 reviewer_email = reviewer + '@' + instance['email_domain']
318 issues = remote.search(
319 owner=owner_email,
320 reviewer=reviewer_email,
321 modified_after=query_modified_after,
322 with_messages=True)
323
324 issues = filter(
325 lambda i: (datetime_from_rietveld(i['created']) < self.modified_before),
326 issues)
327 issues = filter(
328 lambda i: (datetime_from_rietveld(i['modified']) > self.modified_after),
329 issues)
330
331 should_filter_by_user = True
332 issues = map(partial(self.process_rietveld_issue, instance), issues)
333 issues = filter(
334 partial(self.filter_issue, should_filter_by_user=should_filter_by_user),
335 issues)
336 issues = sorted(issues, key=lambda i: i['modified'], reverse=True)
337
338 return issues
339
340 def process_rietveld_issue(self, instance, issue):
341 ret = {}
342 ret['owner'] = issue['owner_email']
343 ret['author'] = ret['owner']
344
345 ret['reviewers'] = set(username(r) for r in issue['reviewers'])
346
347 shorturl = instance['url']
348 if 'shorturl' in instance:
349 shorturl = instance['shorturl']
350
351 ret['review_url'] = 'http://%s/%d' % (shorturl, issue['issue'])
352 ret['header'] = issue['description'].split('\n')[0]
353
354 ret['modified'] = datetime_from_rietveld(issue['modified'])
355 ret['created'] = datetime_from_rietveld(issue['created'])
356 ret['replies'] = self.process_rietveld_replies(issue['messages'])
357
358 return ret
359
360 @staticmethod
361 def process_rietveld_replies(replies):
362 ret = []
363 for reply in replies:
364 r = {}
365 r['author'] = reply['sender']
366 r['created'] = datetime_from_rietveld(reply['date'])
367 r['content'] = ''
368 ret.append(r)
369 return ret
370
371 def gerrit_search(self, instance, owner=None, reviewer=None):
372 max_age = datetime.today() - self.modified_after
373 max_age = max_age.days * 24 * 3600 + max_age.seconds
374
375 # See https://review.openstack.org/Documentation/cmd-query.html
376 # Gerrit doesn't allow filtering by created time, only modified time.
377 user_filter = 'owner:%s' % owner if owner else 'reviewer:%s' % reviewer
378 gquery_cmd = ['ssh', '-p', str(instance['port']), instance['url'],
379 'gerrit', 'query',
380 '--format', 'JSON',
381 '--comments',
382 '--',
383 '-age:%ss' % str(max_age),
384 user_filter]
385 [stdout, _] = subprocess.Popen(gquery_cmd, stdout=subprocess.PIPE,
386 stderr=subprocess.PIPE).communicate()
387 issues = str(stdout).split('\n')[:-2]
388 issues = map(json.loads, issues)
389
390 # TODO(cjhopman): should we filter abandoned changes?
391 issues = map(self.process_gerrit_issue, issues)
392 issues = filter(self.filter_issue, issues)
393 issues = sorted(issues, key=lambda i: i['modified'], reverse=True)
394
395 return issues
396
397 def process_gerrit_issue(self, issue):
398 ret = {}
399 ret['review_url'] = issue['url']
400 ret['header'] = issue['subject']
401 ret['owner'] = issue['owner']['email']
402 ret['author'] = ret['owner']
403 ret['created'] = datetime.fromtimestamp(issue['createdOn'])
404 ret['modified'] = datetime.fromtimestamp(issue['lastUpdated'])
405 if 'comments' in issue:
406 ret['replies'] = self.process_gerrit_issue_replies(issue['comments'])
407 else:
408 ret['replies'] = []
409 return ret
410
411 @staticmethod
412 def process_gerrit_issue_replies(replies):
413 ret = []
414 replies = filter(lambda r: 'email' in r['reviewer'], replies)
415 for reply in replies:
416 r = {}
417 r['author'] = reply['reviewer']['email']
418 r['created'] = datetime.fromtimestamp(reply['timestamp'])
419 r['content'] = ''
420 ret.append(r)
421 return ret
422
423 def google_code_issue_search(self, instance):
424 time_format = '%Y-%m-%dT%T'
425 # See http://code.google.com/p/support/wiki/IssueTrackerAPI
426 # q=<owner>@chromium.org does a full text search for <owner>@chromium.org.
427 # This will accept the issue if owner is the owner or in the cc list. Might
428 # have some false positives, though.
429
430 # Don't filter normally on modified_before because it can filter out things
431 # that were modified in the time period and then modified again after it.
432 gcode_url = ('https://code.google.com/feeds/issues/p/%s/issues/full' %
433 instance['name'])
434
435 gcode_data = urllib.urlencode({
436 'alt': 'json',
437 'max-results': '100000',
438 'q': '%s' % self.user,
439 'published-max': self.modified_before.strftime(time_format),
440 'updated-min': self.modified_after.strftime(time_format),
441 })
442
443 opener = urllib2.build_opener()
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000444 if self.google_code_auth_token:
445 opener.addheaders = [('Authorization', 'GoogleLogin auth=%s' %
446 self.google_code_auth_token)]
447 gcode_json = None
448 try:
449 gcode_get = opener.open(gcode_url + '?' + gcode_data)
450 gcode_json = json.load(gcode_get)
451 gcode_get.close()
452 except urllib2.HTTPError, _:
453 print 'Unable to access ' + instance['name'] + ' issue tracker.'
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000454
cjhopman@chromium.org3365f2d2012-11-01 18:53:13 +0000455 if not gcode_json or 'entry' not in gcode_json['feed']:
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000456 return []
457
458 issues = gcode_json['feed']['entry']
459 issues = map(partial(self.process_google_code_issue, instance), issues)
460 issues = filter(self.filter_issue, issues)
461 issues = sorted(issues, key=lambda i: i['modified'], reverse=True)
462 return issues
463
464 def process_google_code_issue(self, project, issue):
465 ret = {}
466 ret['created'] = datetime_from_google_code(issue['published']['$t'])
467 ret['modified'] = datetime_from_google_code(issue['updated']['$t'])
468
469 ret['owner'] = ''
470 if 'issues:owner' in issue:
471 ret['owner'] = issue['issues:owner'][0]['issues:username'][0]['$t']
472 ret['author'] = issue['author'][0]['name']['$t']
473
474 if 'shorturl' in project:
475 issue_id = issue['id']['$t']
476 issue_id = issue_id[issue_id.rfind('/') + 1:]
477 ret['url'] = 'http://%s/%d' % (project['shorturl'], int(issue_id))
478 else:
479 issue_url = issue['link'][1]
480 if issue_url['rel'] != 'alternate':
481 raise RuntimeError
482 ret['url'] = issue_url['href']
483 ret['header'] = issue['title']['$t']
484
485 ret['replies'] = self.get_google_code_issue_replies(issue)
486 return ret
487
488 def get_google_code_issue_replies(self, issue):
489 """Get all the comments on the issue."""
490 replies_url = issue['link'][0]
491 if replies_url['rel'] != 'replies':
492 raise RuntimeError
493
494 replies_data = urllib.urlencode({
495 'alt': 'json',
496 'fields': 'entry(published,author,content)',
497 })
498
499 opener = urllib2.build_opener()
500 opener.addheaders = [('Authorization', 'GoogleLogin auth=%s' %
501 self.google_code_auth_token)]
502 try:
503 replies_get = opener.open(replies_url['href'] + '?' + replies_data)
504 except urllib2.HTTPError, _:
505 return []
506
507 replies_json = json.load(replies_get)
508 replies_get.close()
509 return self.process_google_code_issue_replies(replies_json)
510
511 @staticmethod
512 def process_google_code_issue_replies(replies):
513 if 'entry' not in replies['feed']:
514 return []
515
516 ret = []
517 for entry in replies['feed']['entry']:
518 e = {}
519 e['created'] = datetime_from_google_code(entry['published']['$t'])
520 e['content'] = entry['content']['$t']
521 e['author'] = entry['author'][0]['name']['$t']
522 ret.append(e)
523 return ret
524
525 @staticmethod
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000526 def git_cmd(repo, *args):
527 cmd = ['git', '--git-dir=%s/.git' % repo]
528 cmd.extend(args)
529 [stdout, _] = subprocess.Popen(cmd, stdout=subprocess.PIPE,
530 stderr=subprocess.PIPE).communicate()
531 lines = str(stdout).split('\n')[:-1]
532 return lines
533
534 def git_search(self, instance, owner=None, reviewer=None):
535 repo = getattr(self, instance['option'])
536 if not repo:
537 return []
538
539 search = []
540 if owner:
541 search.extend(instance['owner_search_func'](owner))
542 if reviewer:
543 search.extend(instance['reviewer_search_func'](reviewer))
544 if not len(search):
545 return []
546
547 self.git_cmd(repo, 'fetch', 'origin')
548
549 time_format = '%Y-%m-%d %H:%M:%S'
550 log_args = [
551 '--after=' + self.modified_after.strftime(time_format),
552 '--before=' + self.modified_before.strftime(time_format),
553 '--format=%H'
554 ]
555 commits = set()
556 for query in search:
557 query_args = [query]
558 query_args.extend(log_args)
559 commits |= set(self.git_cmd(repo, 'log', 'origin/master', *query_args))
560
561 ret = []
562 for commit in commits:
563 output = self.git_cmd(repo, 'log', commit + "^!", "--format=%cn%n%cd%n%B")
564 author = output[0]
565 date = datetime.strptime(output[1], "%a %b %d %H:%M:%S %Y +0000")
566 ret.append(self.process_git_commit(instance, author, date, output[2:]))
567
568 ret = sorted(ret, key=lambda i: i['modified'], reverse=True)
569 return ret
570
571 @staticmethod
572 def process_git_commit(instance, author, date, log):
573 ret = {}
574 ret['owner'] = author
575 ret['author'] = author
576 ret['modified'] = date
577 ret['created'] = date
578 ret['header'] = log[0]
579
580 reviews = []
581 reviewers = []
582 changes = []
583
584 for line in log:
585 match = re.match(r'Reviewed by ([^.]*)', line)
586 if match:
587 reviewers.append(match.group(1))
588 if instance['review_re']:
589 match = re.match(instance['review_re'], line)
590 if match:
591 reviews.append(int(match.group(1)))
592 if instance['change_re']:
593 match = re.match(instance['change_re'], line)
594 if match:
595 changes.append(int(match.group(1)))
596
597 # TODO(enne): should convert full names to usernames via CommitterList.
598 ret['reviewers'] = set(reviewers)
599
600 # Reviews more useful than change link itself, but tricky if multiple
601 # Reviews == bugs for WebKit changes
602 if len(reviews) == 1:
603 url = 'http://%s/%d' % (instance['review_url'], reviews[0])
604 if instance['review_prop']:
605 ret[instance['review_prop']] = reviews[0]
606 else:
607 url = 'http://%s/%d' % (instance['change_url'], changes[0])
608 ret['review_url'] = url
609
610 return ret
611
612 def bugzilla_issues(self, instance, user):
613 if instance['user_func']:
614 user = instance['user_func'](user)
615 if not user:
616 return []
617
618 # This search is a little iffy, as it returns any bug that has been
619 # modified over a time period in any way and that a user has ever commented
620 # on, but that's the best that Bugzilla can get us. Oops.
621 commented = { 'emaillongdesc1': 1 }
622 issues = self.bugzilla_search(instance, user, commented)
623 issues = filter(lambda issue: issue['owner'] != user, issues)
624
625 reported = { 'emailreporter1': 1, 'chfield': '[Bug creation]' }
626 issues.extend(self.bugzilla_search(instance, user, reported))
627
628 # Remove duplicates by bug id
629 seen = {}
630 pruned = []
631 for issue in issues:
632 bug_id = issue['webkit_bug_id']
633 if bug_id in seen:
634 continue
635 seen[bug_id] = True
636 pruned.append(issue)
637
638 # Bugzilla has no modified time, so sort by id?
639 pruned = sorted(pruned, key=lambda i: i['webkit_bug_id'])
640 return issues
641
642 def bugzilla_search(self, instance, user, params):
643 time_format = '%Y-%m-%d'
644 values = {
645 'chfieldfrom': self.modified_after.strftime(time_format),
646 'chfieldto': self.modified_before.strftime(time_format),
647 'ctype': 'csv',
648 'emailtype1': 'substring',
649 'email1': '%s' % user,
650 }
651 values.update(params)
652
653 # Must be GET not POST
654 data = urllib.urlencode(values)
655 req = urllib2.Request("%s?%s" % (instance['search_url'], data))
656 response = urllib2.urlopen(req)
657 reader = csv.reader(response)
658 reader.next() # skip the header line
659
660 issues = map(partial(self.process_bugzilla_issue, instance), reader)
661 return issues
662
663 @staticmethod
664 def process_bugzilla_issue(instance, issue):
665 bug_id, owner, desc = int(issue[0]), issue[4], issue[7]
666
667 ret = {}
668 ret['owner'] = owner
669 ret['author'] = owner
670 ret['review_url'] = 'http://%s/%d' % (instance['url'], bug_id)
671 ret['url'] = ret['review_url']
672 ret['header'] = desc
673 ret['webkit_bug_id'] = bug_id
674 return ret
675
676 def setup_webkit_info(self):
677 assert(self.webkit_repo)
678 git_dir = os.path.normpath(self.webkit_repo + "/.git")
679 if not os.path.exists(git_dir):
scheib@chromium.orgb299e7c2012-11-13 21:34:34 +0000680 print "%s doesn't exist, skipping WebKit checks." % git_dir
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000681 self.webkit_repo = None
682 return
683
684 try:
685 self.git_cmd(self.webkit_repo, "fetch", "origin")
686 except subprocess.CalledProcessError:
scheib@chromium.orgb299e7c2012-11-13 21:34:34 +0000687 print "Failed to update WebKit repo, skipping WebKit checks."
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000688 self.webkit_repo = None
689 return
690
691 path = "Tools/Scripts"
692 full_path = os.path.normpath("%s/%s" % (self.options.webkit_repo, path))
693 sys.path.append(full_path)
694
695 try:
696 global webkitpy
697 webkitpy = __import__('webkitpy.common.config.committers')
698 except ImportError:
scheib@chromium.orgb299e7c2012-11-13 21:34:34 +0000699 print "Failed to import WebKit committer list, skipping WebKit checks."
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000700 self.webkit_repo = None
701 return
702
703 if not webkit_account(self.user):
704 email = self.user + "@chromium.org"
scheib@chromium.orgb299e7c2012-11-13 21:34:34 +0000705 print "No %s in committers.py, skipping WebKit checks." % email
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000706 self.webkit_repo = None
707
708 @staticmethod
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000709 def print_change(change):
710 print '%s %s' % (
711 change['review_url'],
712 change['header'],
713 )
714
715 @staticmethod
716 def print_issue(issue):
717 print '%s %s' % (
718 issue['url'],
719 issue['header'],
720 )
721
722 def filter_issue(self, issue, should_filter_by_user=True):
723 def maybe_filter_username(email):
724 return not should_filter_by_user or username(email) == self.user
725 if (maybe_filter_username(issue['author']) and
726 self.filter_modified(issue['created'])):
727 return True
728 if (maybe_filter_username(issue['owner']) and
729 (self.filter_modified(issue['created']) or
730 self.filter_modified(issue['modified']))):
731 return True
732 for reply in issue['replies']:
733 if self.filter_modified(reply['created']):
734 if not should_filter_by_user:
735 break
736 if (username(reply['author']) == self.user
737 or (self.user + '@') in reply['content']):
738 break
739 else:
740 return False
741 return True
742
743 def filter_modified(self, modified):
744 return self.modified_after < modified and modified < self.modified_before
745
746 def auth_for_changes(self):
747 #TODO(cjhopman): Move authentication check for getting changes here.
748 pass
749
750 def auth_for_reviews(self):
751 # Reviews use all the same instances as changes so no authentication is
752 # required.
753 pass
754
755 def auth_for_issues(self):
756 self.google_code_auth_token = (
757 get_auth_token(self.options.local_user + '@chromium.org'))
758
759 def get_changes(self):
760 for instance in rietveld_instances:
761 self.changes += self.rietveld_search(instance, owner=self.user)
762
763 for instance in gerrit_instances:
764 self.changes += self.gerrit_search(instance, owner=self.user)
765
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000766 for instance in git_instances:
767 self.changes += self.git_search(instance, owner=self.user)
768
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000769 def print_changes(self):
770 if self.changes:
771 print '\nChanges:'
772 for change in self.changes:
773 self.print_change(change)
774
775 def get_reviews(self):
776 for instance in rietveld_instances:
777 self.reviews += self.rietveld_search(instance, reviewer=self.user)
778
779 for instance in gerrit_instances:
780 reviews = self.gerrit_search(instance, reviewer=self.user)
781 reviews = filter(lambda r: not username(r['owner']) == self.user, reviews)
782 self.reviews += reviews
783
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000784 for instance in git_instances:
785 self.reviews += self.git_search(instance, reviewer=self.user)
786
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000787 def print_reviews(self):
788 if self.reviews:
789 print '\nReviews:'
790 for review in self.reviews:
791 self.print_change(review)
792
793 def get_issues(self):
794 for project in google_code_projects:
795 self.issues += self.google_code_issue_search(project)
796
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000797 for instance in bugzilla_instances:
798 self.issues += self.bugzilla_issues(instance, self.user)
799
800 def process_activities(self):
801 # If a webkit bug was a review, don't list it as an issue.
802 ids = {}
803 for review in self.reviews + self.changes:
804 if 'webkit_bug_id' in review:
805 ids[review['webkit_bug_id']] = True
806
807 def duplicate_issue(issue):
808 if 'webkit_bug_id' not in issue:
809 return False
810 return issue['webkit_bug_id'] in ids
811
812 self.issues = filter(lambda issue: not duplicate_issue(issue), self.issues)
813
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000814 def print_issues(self):
815 if self.issues:
816 print '\nIssues:'
817 for c in self.issues:
818 self.print_issue(c)
819
820 def print_activity(self):
821 self.print_changes()
822 self.print_reviews()
823 self.print_issues()
824
825
826def main():
827 # Silence upload.py.
828 rietveld.upload.verbosity = 0
829
830 parser = optparse.OptionParser(description=sys.modules[__name__].__doc__)
831 parser.add_option(
832 '-u', '--user', metavar='<email>',
833 default=os.environ.get('USER'),
834 help='Filter on user, default=%default')
835 parser.add_option(
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000836 '--webkit_repo', metavar='<dir>',
837 default='%s' % os.environ.get('WEBKIT_DIR'),
838 help='Local path to WebKit repository, default=%default')
839 parser.add_option(
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000840 '-b', '--begin', metavar='<date>',
841 help='Filter issues created after the date')
842 parser.add_option(
843 '-e', '--end', metavar='<date>',
844 help='Filter issues created before the date')
845 quarter_begin, quarter_end = get_quarter_of(datetime.today() -
846 relativedelta(months=2))
847 parser.add_option(
848 '-Q', '--last_quarter', action='store_true',
849 help='Use last quarter\'s dates, e.g. %s to %s' % (
850 quarter_begin.strftime('%Y-%m-%d'), quarter_end.strftime('%Y-%m-%d')))
851 parser.add_option(
852 '-Y', '--this_year', action='store_true',
853 help='Use this year\'s dates')
854 parser.add_option(
855 '-w', '--week_of', metavar='<date>',
856 help='Show issues for week of the date')
857 parser.add_option(
858 '-a', '--auth',
859 action='store_true',
860 help='Ask to authenticate for instances with no auth cookie')
861
862 group = optparse.OptionGroup(parser, 'Activity Types',
863 'By default, all activity will be looked up and '
864 'printed. If any of these are specified, only '
865 'those specified will be searched.')
866 group.add_option(
867 '-c', '--changes',
868 action='store_true',
869 help='Show changes.')
870 group.add_option(
871 '-i', '--issues',
872 action='store_true',
873 help='Show issues.')
874 group.add_option(
875 '-r', '--reviews',
876 action='store_true',
877 help='Show reviews.')
878 parser.add_option_group(group)
879
880 # Remove description formatting
881 parser.format_description = (
882 lambda _: parser.description) # pylint: disable=E1101
883
884 options, args = parser.parse_args()
885 options.local_user = os.environ.get('USER')
886 if args:
887 parser.error('Args unsupported')
888 if not options.user:
889 parser.error('USER is not set, please use -u')
890
891 options.user = username(options.user)
892
893 if not options.begin:
894 if options.last_quarter:
895 begin, end = quarter_begin, quarter_end
896 elif options.this_year:
897 begin, end = get_year_of(datetime.today())
898 elif options.week_of:
899 begin, end = (get_week_of(datetime.strptime(options.week_of, '%m/%d/%y')))
900 else:
901 begin, end = (get_week_of(datetime.today() - timedelta(days=1)))
902 else:
903 begin = datetime.strptime(options.begin, '%m/%d/%y')
904 if options.end:
905 end = datetime.strptime(options.end, '%m/%d/%y')
906 else:
907 end = datetime.today()
908 options.begin, options.end = begin, end
909
910 print 'Searching for activity by %s' % options.user
911 print 'Using range %s to %s' % (options.begin, options.end)
912
913 my_activity = MyActivity(options)
914
915 if not (options.changes or options.reviews or options.issues):
916 options.changes = True
917 options.issues = True
918 options.reviews = True
919
920 # First do any required authentication so none of the user interaction has to
921 # wait for actual work.
922 if options.changes:
923 my_activity.auth_for_changes()
924 if options.reviews:
925 my_activity.auth_for_reviews()
926 if options.issues:
927 my_activity.auth_for_issues()
928
929 print 'Looking up activity.....'
930
931 if options.changes:
932 my_activity.get_changes()
933 if options.reviews:
934 my_activity.get_reviews()
935 if options.issues:
936 my_activity.get_issues()
937
enne@chromium.orgcb55d8a2012-11-06 01:11:40 +0000938 my_activity.process_activities()
939
cjhopman@chromium.org04d119d2012-10-17 22:41:53 +0000940 print '\n\n\n'
941
942 my_activity.print_changes()
943 my_activity.print_reviews()
944 my_activity.print_issues()
945 return 0
946
947
948if __name__ == '__main__':
949 sys.exit(main())