cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 1 | #!/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 | |
| 8 | Example: |
| 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. |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 14 | - my_activity.py -jd to output stats for the week to json with deltas data. |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 15 | """ |
| 16 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 17 | # TODO(vadimsh): This script knows too much about ClientLogin and cookies. It |
| 18 | # will stop to work on ~20 Apr 2015. |
| 19 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 20 | # These services typically only provide a created time and a last modified time |
| 21 | # for each item for general queries. This is not enough to determine if there |
| 22 | # was activity in a given time period. So, we first query for all things created |
| 23 | # before end and modified after begin. Then, we get the details of each item and |
| 24 | # check those details to determine if there was activity in the given period. |
| 25 | # This means that query time scales mostly with (today() - begin). |
| 26 | |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 27 | import collections |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 28 | from datetime import datetime |
| 29 | from datetime import timedelta |
| 30 | from functools import partial |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 31 | import itertools |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 32 | import json |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 33 | import logging |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 34 | import optparse |
| 35 | import os |
| 36 | import subprocess |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 37 | from string import Formatter |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 38 | import sys |
| 39 | import urllib |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 40 | import re |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 41 | |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 42 | import auth |
stevefung@chromium.org | 832d51e | 2015-05-27 12:52:51 +0000 | [diff] [blame] | 43 | import fix_encoding |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 44 | import gerrit_util |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 45 | import rietveld |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 46 | |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 47 | from third_party import httplib2 |
| 48 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 49 | try: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 50 | import dateutil # pylint: disable=import-error |
| 51 | import dateutil.parser |
| 52 | from dateutil.relativedelta import relativedelta |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 53 | except ImportError: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 54 | logging.error('python-dateutil package required') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 55 | exit(1) |
| 56 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 57 | |
| 58 | class DefaultFormatter(Formatter): |
| 59 | def __init__(self, default = ''): |
| 60 | super(DefaultFormatter, self).__init__() |
| 61 | self.default = default |
| 62 | |
| 63 | def get_value(self, key, args, kwds): |
| 64 | if isinstance(key, basestring) and key not in kwds: |
| 65 | return self.default |
| 66 | return Formatter.get_value(self, key, args, kwds) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 67 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 68 | rietveld_instances = [ |
| 69 | { |
| 70 | 'url': 'codereview.chromium.org', |
| 71 | 'shorturl': 'crrev.com', |
| 72 | 'supports_owner_modified_query': True, |
| 73 | 'requires_auth': False, |
| 74 | 'email_domain': 'chromium.org', |
Varun Khaneja | d9f97bc | 2017-08-02 12:55:01 -0700 | [diff] [blame] | 75 | 'short_url_protocol': 'https', |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 76 | }, |
| 77 | { |
| 78 | 'url': 'chromereviews.googleplex.com', |
| 79 | 'shorturl': 'go/chromerev', |
| 80 | 'supports_owner_modified_query': True, |
| 81 | 'requires_auth': True, |
| 82 | 'email_domain': 'google.com', |
| 83 | }, |
| 84 | { |
| 85 | 'url': 'codereview.appspot.com', |
| 86 | 'supports_owner_modified_query': True, |
| 87 | 'requires_auth': False, |
| 88 | 'email_domain': 'chromium.org', |
| 89 | }, |
| 90 | { |
| 91 | 'url': 'breakpad.appspot.com', |
| 92 | 'supports_owner_modified_query': False, |
| 93 | 'requires_auth': False, |
| 94 | 'email_domain': 'chromium.org', |
| 95 | }, |
| 96 | ] |
| 97 | |
| 98 | gerrit_instances = [ |
| 99 | { |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 100 | 'url': 'chromium-review.googlesource.com', |
Jeremy Roman | f475a47 | 2017-06-06 16:49:11 -0400 | [diff] [blame] | 101 | 'shorturl': 'crrev.com/c', |
Varun Khaneja | d9f97bc | 2017-08-02 12:55:01 -0700 | [diff] [blame] | 102 | 'short_url_protocol': 'https', |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 103 | }, |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 104 | { |
| 105 | 'url': 'chrome-internal-review.googlesource.com', |
Jeremy Roman | f475a47 | 2017-06-06 16:49:11 -0400 | [diff] [blame] | 106 | 'shorturl': 'crrev.com/i', |
Varun Khaneja | d9f97bc | 2017-08-02 12:55:01 -0700 | [diff] [blame] | 107 | 'short_url_protocol': 'https', |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 108 | }, |
deymo@chromium.org | 56dc57a | 2015-09-10 18:26:54 +0000 | [diff] [blame] | 109 | { |
| 110 | 'url': 'android-review.googlesource.com', |
| 111 | }, |
Ryan Harrison | 897602a | 2017-09-18 16:23:41 -0400 | [diff] [blame] | 112 | { |
| 113 | 'url': 'pdfium-review.googlesource.com', |
| 114 | }, |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 115 | ] |
| 116 | |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 117 | monorail_projects = { |
| 118 | 'chromium': { |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 119 | 'shorturl': 'crbug.com', |
Varun Khaneja | d9f97bc | 2017-08-02 12:55:01 -0700 | [diff] [blame] | 120 | 'short_url_protocol': 'https', |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 121 | }, |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 122 | 'google-breakpad': {}, |
| 123 | 'gyp': {}, |
| 124 | 'skia': {}, |
| 125 | 'pdfium': { |
Ryan Harrison | 897602a | 2017-09-18 16:23:41 -0400 | [diff] [blame] | 126 | 'shorturl': 'crbug.com/pdfium', |
| 127 | 'short_url_protocol': 'https', |
| 128 | }, |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 129 | 'v8': { |
| 130 | 'shorturl': 'crbug.com/v8', |
| 131 | 'short_url_protocol': 'https', |
| 132 | }, |
| 133 | } |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 134 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 135 | def username(email): |
| 136 | """Keeps the username of an email address.""" |
| 137 | return email and email.split('@', 1)[0] |
| 138 | |
| 139 | |
cjhopman@chromium.org | 426557a | 2012-10-22 20:18:52 +0000 | [diff] [blame] | 140 | def datetime_to_midnight(date): |
| 141 | return date - timedelta(hours=date.hour, minutes=date.minute, |
| 142 | seconds=date.second, microseconds=date.microsecond) |
| 143 | |
| 144 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 145 | def get_quarter_of(date): |
cjhopman@chromium.org | 426557a | 2012-10-22 20:18:52 +0000 | [diff] [blame] | 146 | begin = (datetime_to_midnight(date) - |
| 147 | relativedelta(months=(date.month % 3) - 1, days=(date.day - 1))) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 148 | return begin, begin + relativedelta(months=3) |
| 149 | |
| 150 | |
| 151 | def get_year_of(date): |
cjhopman@chromium.org | 426557a | 2012-10-22 20:18:52 +0000 | [diff] [blame] | 152 | begin = (datetime_to_midnight(date) - |
| 153 | relativedelta(months=(date.month - 1), days=(date.day - 1))) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 154 | return begin, begin + relativedelta(years=1) |
| 155 | |
| 156 | |
| 157 | def get_week_of(date): |
cjhopman@chromium.org | 426557a | 2012-10-22 20:18:52 +0000 | [diff] [blame] | 158 | begin = (datetime_to_midnight(date) - timedelta(days=date.weekday())) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 159 | return begin, begin + timedelta(days=7) |
| 160 | |
| 161 | |
| 162 | def get_yes_or_no(msg): |
| 163 | while True: |
| 164 | response = raw_input(msg + ' yes/no [no] ') |
| 165 | if response == 'y' or response == 'yes': |
| 166 | return True |
| 167 | elif not response or response == 'n' or response == 'no': |
| 168 | return False |
| 169 | |
| 170 | |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 171 | def datetime_from_gerrit(date_string): |
| 172 | return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f000') |
| 173 | |
| 174 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 175 | def datetime_from_rietveld(date_string): |
deymo@chromium.org | 29eb6e6 | 2014-03-20 01:55:55 +0000 | [diff] [blame] | 176 | try: |
| 177 | return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f') |
| 178 | except ValueError: |
| 179 | # Sometimes rietveld returns a value without the milliseconds part, so we |
| 180 | # attempt to parse those cases as well. |
| 181 | return datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 182 | |
| 183 | |
| 184 | def datetime_from_google_code(date_string): |
| 185 | return datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S.%fZ') |
| 186 | |
| 187 | |
| 188 | class MyActivity(object): |
| 189 | def __init__(self, options): |
| 190 | self.options = options |
| 191 | self.modified_after = options.begin |
| 192 | self.modified_before = options.end |
| 193 | self.user = options.user |
| 194 | self.changes = [] |
| 195 | self.reviews = [] |
| 196 | self.issues = [] |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 197 | self.referenced_issues = [] |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 198 | self.check_cookies() |
| 199 | self.google_code_auth_token = None |
| 200 | |
| 201 | # Check the codereview cookie jar to determine which Rietveld instances to |
| 202 | # authenticate to. |
| 203 | def check_cookies(self): |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 204 | filtered_instances = [] |
| 205 | |
| 206 | def has_cookie(instance): |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 207 | auth_config = auth.extract_auth_config_from_options(self.options) |
| 208 | a = auth.get_authenticator_for_host(instance['url'], auth_config) |
| 209 | return a.has_cached_credentials() |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 210 | |
| 211 | for instance in rietveld_instances: |
| 212 | instance['auth'] = has_cookie(instance) |
| 213 | |
| 214 | if filtered_instances: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 215 | logging.warning('No cookie found for the following Rietveld instance%s:', |
| 216 | 's' if len(filtered_instances) > 1 else '') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 217 | for instance in filtered_instances: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 218 | logging.warning('\t' + instance['url']) |
| 219 | logging.warning('Use --auth if you would like to authenticate to them.') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 220 | |
| 221 | def rietveld_search(self, instance, owner=None, reviewer=None): |
| 222 | if instance['requires_auth'] and not instance['auth']: |
| 223 | return [] |
| 224 | |
| 225 | |
| 226 | email = None if instance['auth'] else '' |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 227 | auth_config = auth.extract_auth_config_from_options(self.options) |
| 228 | remote = rietveld.Rietveld('https://' + instance['url'], auth_config, email) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 229 | |
| 230 | # See def search() in rietveld.py to see all the filters you can use. |
| 231 | query_modified_after = None |
| 232 | |
| 233 | if instance['supports_owner_modified_query']: |
| 234 | query_modified_after = self.modified_after.strftime('%Y-%m-%d') |
| 235 | |
| 236 | # Rietveld does not allow search by both created_before and modified_after. |
| 237 | # (And some instances don't allow search by both owner and modified_after) |
| 238 | owner_email = None |
| 239 | reviewer_email = None |
| 240 | if owner: |
| 241 | owner_email = owner + '@' + instance['email_domain'] |
| 242 | if reviewer: |
| 243 | reviewer_email = reviewer + '@' + instance['email_domain'] |
| 244 | issues = remote.search( |
| 245 | owner=owner_email, |
| 246 | reviewer=reviewer_email, |
| 247 | modified_after=query_modified_after, |
| 248 | with_messages=True) |
| 249 | |
| 250 | issues = filter( |
| 251 | lambda i: (datetime_from_rietveld(i['created']) < self.modified_before), |
| 252 | issues) |
| 253 | issues = filter( |
| 254 | lambda i: (datetime_from_rietveld(i['modified']) > self.modified_after), |
| 255 | issues) |
| 256 | |
| 257 | should_filter_by_user = True |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 258 | issues = map(partial(self.process_rietveld_issue, remote, instance), issues) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 259 | issues = filter( |
| 260 | partial(self.filter_issue, should_filter_by_user=should_filter_by_user), |
| 261 | issues) |
| 262 | issues = sorted(issues, key=lambda i: i['modified'], reverse=True) |
| 263 | |
| 264 | return issues |
| 265 | |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 266 | def extract_bug_number_from_description(self, issue): |
| 267 | description = None |
| 268 | |
| 269 | if 'description' in issue: |
| 270 | # Getting the description for Rietveld |
| 271 | description = issue['description'] |
| 272 | elif 'revisions' in issue: |
| 273 | # Getting the description for REST Gerrit |
| 274 | revision = issue['revisions'][issue['current_revision']] |
| 275 | description = revision['commit']['message'] |
| 276 | |
| 277 | bugs = [] |
| 278 | if description: |
Nicolas Dossou-gbete | 903ea73 | 2017-07-10 16:46:59 +0100 | [diff] [blame] | 279 | # Handle both "Bug: 99999" and "BUG=99999" bug notations |
| 280 | # Multiple bugs can be noted on a single line or in multiple ones. |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 281 | matches = re.findall( |
| 282 | r'BUG[=:]\s?((((?:[a-zA-Z0-9-]+:)?\d+)(,\s?)?)+)', description, |
| 283 | flags=re.IGNORECASE) |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 284 | if matches: |
| 285 | for match in matches: |
| 286 | bugs.extend(match[0].replace(' ', '').split(',')) |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 287 | # Add default chromium: prefix if none specified. |
| 288 | bugs = [bug if ':' in bug else 'chromium:%s' % bug for bug in bugs] |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 289 | |
| 290 | return bugs |
| 291 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 292 | def process_rietveld_issue(self, remote, instance, issue): |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 293 | ret = {} |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 294 | if self.options.deltas: |
| 295 | patchset_props = remote.get_patchset_properties( |
| 296 | issue['issue'], |
| 297 | issue['patchsets'][-1]) |
| 298 | ret['delta'] = '+%d,-%d' % ( |
| 299 | sum(f['num_added'] for f in patchset_props['files'].itervalues()), |
| 300 | sum(f['num_removed'] for f in patchset_props['files'].itervalues())) |
| 301 | |
| 302 | if issue['landed_days_ago'] != 'unknown': |
| 303 | ret['status'] = 'committed' |
| 304 | elif issue['closed']: |
| 305 | ret['status'] = 'closed' |
| 306 | elif len(issue['reviewers']) and issue['all_required_reviewers_approved']: |
| 307 | ret['status'] = 'ready' |
| 308 | else: |
| 309 | ret['status'] = 'open' |
| 310 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 311 | ret['owner'] = issue['owner_email'] |
| 312 | ret['author'] = ret['owner'] |
| 313 | |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 314 | ret['reviewers'] = set(issue['reviewers']) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 315 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 316 | if 'shorturl' in instance: |
Varun Khaneja | d9f97bc | 2017-08-02 12:55:01 -0700 | [diff] [blame] | 317 | url = instance['shorturl'] |
| 318 | protocol = instance.get('short_url_protocol', 'http') |
| 319 | else: |
| 320 | url = instance['url'] |
| 321 | protocol = 'https' |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 322 | |
Varun Khaneja | d9f97bc | 2017-08-02 12:55:01 -0700 | [diff] [blame] | 323 | ret['review_url'] = '%s://%s/%d' % (protocol, url, issue['issue']) |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 324 | |
| 325 | # Rietveld sometimes has '\r\n' instead of '\n'. |
| 326 | ret['header'] = issue['description'].replace('\r', '').split('\n')[0] |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 327 | |
| 328 | ret['modified'] = datetime_from_rietveld(issue['modified']) |
| 329 | ret['created'] = datetime_from_rietveld(issue['created']) |
| 330 | ret['replies'] = self.process_rietveld_replies(issue['messages']) |
| 331 | |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 332 | ret['bugs'] = self.extract_bug_number_from_description(issue) |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 333 | ret['landed_days_ago'] = issue['landed_days_ago'] |
| 334 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 335 | return ret |
| 336 | |
| 337 | @staticmethod |
| 338 | def process_rietveld_replies(replies): |
| 339 | ret = [] |
| 340 | for reply in replies: |
| 341 | r = {} |
| 342 | r['author'] = reply['sender'] |
| 343 | r['created'] = datetime_from_rietveld(reply['date']) |
| 344 | r['content'] = '' |
| 345 | ret.append(r) |
| 346 | return ret |
| 347 | |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 348 | @staticmethod |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 349 | def gerrit_changes_over_rest(instance, filters): |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 350 | # Convert the "key:value" filter to a list of (key, value) pairs. |
| 351 | req = list(f.split(':', 1) for f in filters) |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 352 | try: |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 353 | # Instantiate the generator to force all the requests now and catch the |
| 354 | # errors here. |
| 355 | return list(gerrit_util.GenerateAllChanges(instance['url'], req, |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 356 | o_params=['MESSAGES', 'LABELS', 'DETAILED_ACCOUNTS', |
| 357 | 'CURRENT_REVISION', 'CURRENT_COMMIT'])) |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 358 | except gerrit_util.GerritError, e: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 359 | logging.error('Looking up %r: %s', instance['url'], e) |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 360 | return [] |
| 361 | |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 362 | def gerrit_search(self, instance, owner=None, reviewer=None): |
| 363 | max_age = datetime.today() - self.modified_after |
| 364 | max_age = max_age.days * 24 * 3600 + max_age.seconds |
| 365 | user_filter = 'owner:%s' % owner if owner else 'reviewer:%s' % reviewer |
| 366 | filters = ['-age:%ss' % max_age, user_filter] |
| 367 | |
Aaron Gable | 2979a87 | 2017-09-05 17:38:32 -0700 | [diff] [blame] | 368 | issues = self.gerrit_changes_over_rest(instance, filters) |
| 369 | issues = [self.process_gerrit_issue(instance, issue) |
| 370 | for issue in issues] |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 371 | |
| 372 | # TODO(cjhopman): should we filter abandoned changes? |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 373 | issues = filter(self.filter_issue, issues) |
| 374 | issues = sorted(issues, key=lambda i: i['modified'], reverse=True) |
| 375 | |
| 376 | return issues |
| 377 | |
Aaron Gable | 2979a87 | 2017-09-05 17:38:32 -0700 | [diff] [blame] | 378 | def process_gerrit_issue(self, instance, issue): |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 379 | ret = {} |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 380 | if self.options.deltas: |
| 381 | ret['delta'] = DefaultFormatter().format( |
| 382 | '+{insertions},-{deletions}', |
| 383 | **issue) |
| 384 | ret['status'] = issue['status'] |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 385 | if 'shorturl' in instance: |
Varun Khaneja | d9f97bc | 2017-08-02 12:55:01 -0700 | [diff] [blame] | 386 | protocol = instance.get('short_url_protocol', 'http') |
| 387 | url = instance['shorturl'] |
| 388 | else: |
| 389 | protocol = 'https' |
| 390 | url = instance['url'] |
| 391 | ret['review_url'] = '%s://%s/%s' % (protocol, url, issue['_number']) |
| 392 | |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 393 | ret['header'] = issue['subject'] |
| 394 | ret['owner'] = issue['owner']['email'] |
| 395 | ret['author'] = ret['owner'] |
| 396 | ret['created'] = datetime_from_gerrit(issue['created']) |
| 397 | ret['modified'] = datetime_from_gerrit(issue['updated']) |
| 398 | if 'messages' in issue: |
Aaron Gable | 2979a87 | 2017-09-05 17:38:32 -0700 | [diff] [blame] | 399 | ret['replies'] = self.process_gerrit_issue_replies(issue['messages']) |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 400 | else: |
| 401 | ret['replies'] = [] |
| 402 | ret['reviewers'] = set(r['author'] for r in ret['replies']) |
| 403 | ret['reviewers'].discard(ret['author']) |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 404 | ret['bugs'] = self.extract_bug_number_from_description(issue) |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 405 | return ret |
| 406 | |
| 407 | @staticmethod |
Aaron Gable | 2979a87 | 2017-09-05 17:38:32 -0700 | [diff] [blame] | 408 | def process_gerrit_issue_replies(replies): |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 409 | ret = [] |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 410 | replies = filter(lambda r: 'author' in r and 'email' in r['author'], |
| 411 | replies) |
deymo@chromium.org | 6c03920 | 2013-09-12 12:28:12 +0000 | [diff] [blame] | 412 | for reply in replies: |
| 413 | ret.append({ |
| 414 | 'author': reply['author']['email'], |
| 415 | 'created': datetime_from_gerrit(reply['date']), |
| 416 | 'content': reply['message'], |
| 417 | }) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 418 | return ret |
| 419 | |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 420 | def monorail_query_issues(self, project, query): |
| 421 | project_config = monorail_projects.get(project, {}) |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 422 | auth_config = auth.extract_auth_config_from_options(self.options) |
| 423 | authenticator = auth.get_authenticator_for_host( |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 424 | 'bugs.chromium.org', auth_config) |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 425 | http = authenticator.authorize(httplib2.Http()) |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 426 | url = ('https://monorail-prod.appspot.com/_ah/api/monorail/v1/projects' |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 427 | '/%s/issues') % project |
| 428 | query_data = urllib.urlencode(query) |
| 429 | url = url + '?' + query_data |
| 430 | _, body = http.request(url) |
| 431 | content = json.loads(body) |
| 432 | if not content: |
| 433 | logging.error('Unable to parse %s response from projecthosting.', project) |
| 434 | return [] |
| 435 | |
| 436 | issues = [] |
| 437 | for item in content.get('items', []): |
| 438 | if project_config.get('shorturl'): |
| 439 | protocol = project_config.get('short_url_protocol', 'http') |
| 440 | item_url = '%s://%s/%d' % ( |
| 441 | protocol, project_config['shorturl'], item['id']) |
| 442 | else: |
| 443 | item_url = 'https://bugs.chromium.org/p/%s/issues/detail?id=%d' % ( |
| 444 | project, item['id']) |
| 445 | issue = { |
| 446 | 'uid': '%s:%s' % (project, item['id']), |
| 447 | 'header': item['title'], |
| 448 | 'created': dateutil.parser.parse(item['published']), |
| 449 | 'modified': dateutil.parser.parse(item['updated']), |
| 450 | 'author': item['author']['name'], |
| 451 | 'url': item_url, |
| 452 | 'comments': [], |
| 453 | 'status': item['status'], |
| 454 | 'labels': [], |
| 455 | 'components': [] |
| 456 | } |
| 457 | if 'owner' in item: |
| 458 | issue['owner'] = item['owner']['name'] |
| 459 | else: |
| 460 | issue['owner'] = 'None' |
| 461 | if 'labels' in item: |
| 462 | issue['labels'] = item['labels'] |
| 463 | if 'components' in item: |
| 464 | issue['components'] = item['components'] |
| 465 | issues.append(issue) |
| 466 | |
| 467 | return issues |
| 468 | |
| 469 | def monorail_issue_search(self, project): |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 470 | epoch = datetime.utcfromtimestamp(0) |
| 471 | user_str = '%s@chromium.org' % self.user |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 472 | |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 473 | issues = self.monorail_query_issues(project, { |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 474 | 'maxResults': 10000, |
| 475 | 'q': user_str, |
| 476 | 'publishedMax': '%d' % (self.modified_before - epoch).total_seconds(), |
| 477 | 'updatedMin': '%d' % (self.modified_after - epoch).total_seconds(), |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 478 | }) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 479 | |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 480 | return [ |
| 481 | issue for issue in issues |
| 482 | if issue['author'] == user_str or issue['owner'] == user_str] |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 483 | |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 484 | def monorail_get_issues(self, project, issue_ids): |
| 485 | return self.monorail_query_issues(project, { |
| 486 | 'maxResults': 10000, |
| 487 | 'q': 'id:%s' % ','.join(issue_ids) |
| 488 | }) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 489 | |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 490 | def print_heading(self, heading): |
| 491 | print |
| 492 | print self.options.output_format_heading.format(heading=heading) |
| 493 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 494 | def match(self, author): |
| 495 | if '@' in self.user: |
| 496 | return author == self.user |
| 497 | return author.startswith(self.user + '@') |
| 498 | |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 499 | def print_change(self, change): |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 500 | activity = len([ |
| 501 | reply |
| 502 | for reply in change['replies'] |
| 503 | if self.match(reply['author']) |
| 504 | ]) |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 505 | optional_values = { |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 506 | 'created': change['created'].date().isoformat(), |
| 507 | 'modified': change['modified'].date().isoformat(), |
| 508 | 'reviewers': ', '.join(change['reviewers']), |
| 509 | 'status': change['status'], |
| 510 | 'activity': activity, |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 511 | } |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 512 | if self.options.deltas: |
| 513 | optional_values['delta'] = change['delta'] |
| 514 | |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 515 | self.print_generic(self.options.output_format, |
| 516 | self.options.output_format_changes, |
| 517 | change['header'], |
| 518 | change['review_url'], |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 519 | change['author'], |
| 520 | optional_values) |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 521 | |
| 522 | def print_issue(self, issue): |
| 523 | optional_values = { |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 524 | 'created': issue['created'].date().isoformat(), |
| 525 | 'modified': issue['modified'].date().isoformat(), |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 526 | 'owner': issue['owner'], |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 527 | 'status': issue['status'], |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 528 | } |
| 529 | self.print_generic(self.options.output_format, |
| 530 | self.options.output_format_issues, |
| 531 | issue['header'], |
| 532 | issue['url'], |
| 533 | issue['author'], |
| 534 | optional_values) |
| 535 | |
| 536 | def print_review(self, review): |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 537 | activity = len([ |
| 538 | reply |
| 539 | for reply in review['replies'] |
| 540 | if self.match(reply['author']) |
| 541 | ]) |
| 542 | optional_values = { |
| 543 | 'created': review['created'].date().isoformat(), |
| 544 | 'modified': review['modified'].date().isoformat(), |
Nicolas Boichat | 23c165f | 2018-01-26 10:04:27 +0800 | [diff] [blame] | 545 | 'status': review['status'], |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 546 | 'activity': activity, |
| 547 | } |
Nicolas Boichat | 23c165f | 2018-01-26 10:04:27 +0800 | [diff] [blame] | 548 | if self.options.deltas: |
| 549 | optional_values['delta'] = review['delta'] |
| 550 | |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 551 | self.print_generic(self.options.output_format, |
| 552 | self.options.output_format_reviews, |
| 553 | review['header'], |
| 554 | review['review_url'], |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 555 | review['author'], |
| 556 | optional_values) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 557 | |
| 558 | @staticmethod |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 559 | def print_generic(default_fmt, specific_fmt, |
| 560 | title, url, author, |
| 561 | optional_values=None): |
| 562 | output_format = specific_fmt if specific_fmt is not None else default_fmt |
| 563 | output_format = unicode(output_format) |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 564 | values = { |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 565 | 'title': title, |
| 566 | 'url': url, |
| 567 | 'author': author, |
| 568 | } |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 569 | if optional_values is not None: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 570 | values.update(optional_values) |
| 571 | print DefaultFormatter().format(output_format, **values).encode( |
| 572 | sys.getdefaultencoding()) |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 573 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 574 | |
| 575 | def filter_issue(self, issue, should_filter_by_user=True): |
| 576 | def maybe_filter_username(email): |
| 577 | return not should_filter_by_user or username(email) == self.user |
| 578 | if (maybe_filter_username(issue['author']) and |
| 579 | self.filter_modified(issue['created'])): |
| 580 | return True |
| 581 | if (maybe_filter_username(issue['owner']) and |
| 582 | (self.filter_modified(issue['created']) or |
| 583 | self.filter_modified(issue['modified']))): |
| 584 | return True |
| 585 | for reply in issue['replies']: |
| 586 | if self.filter_modified(reply['created']): |
| 587 | if not should_filter_by_user: |
| 588 | break |
| 589 | if (username(reply['author']) == self.user |
| 590 | or (self.user + '@') in reply['content']): |
| 591 | break |
| 592 | else: |
| 593 | return False |
| 594 | return True |
| 595 | |
| 596 | def filter_modified(self, modified): |
| 597 | return self.modified_after < modified and modified < self.modified_before |
| 598 | |
| 599 | def auth_for_changes(self): |
| 600 | #TODO(cjhopman): Move authentication check for getting changes here. |
| 601 | pass |
| 602 | |
| 603 | def auth_for_reviews(self): |
| 604 | # Reviews use all the same instances as changes so no authentication is |
| 605 | # required. |
| 606 | pass |
| 607 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 608 | def get_changes(self): |
| 609 | for instance in rietveld_instances: |
| 610 | self.changes += self.rietveld_search(instance, owner=self.user) |
| 611 | |
| 612 | for instance in gerrit_instances: |
| 613 | self.changes += self.gerrit_search(instance, owner=self.user) |
| 614 | |
| 615 | def print_changes(self): |
| 616 | if self.changes: |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 617 | self.print_heading('Changes') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 618 | for change in self.changes: |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 619 | self.print_change(change) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 620 | |
| 621 | def get_reviews(self): |
| 622 | for instance in rietveld_instances: |
| 623 | self.reviews += self.rietveld_search(instance, reviewer=self.user) |
| 624 | |
| 625 | for instance in gerrit_instances: |
| 626 | reviews = self.gerrit_search(instance, reviewer=self.user) |
| 627 | reviews = filter(lambda r: not username(r['owner']) == self.user, reviews) |
| 628 | self.reviews += reviews |
| 629 | |
| 630 | def print_reviews(self): |
| 631 | if self.reviews: |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 632 | self.print_heading('Reviews') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 633 | for review in self.reviews: |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 634 | self.print_review(review) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 635 | |
| 636 | def get_issues(self): |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 637 | for project in monorail_projects: |
| 638 | self.issues += self.monorail_issue_search(project) |
| 639 | |
| 640 | def get_referenced_issues(self): |
| 641 | if not self.issues: |
| 642 | self.get_issues() |
| 643 | |
| 644 | if not self.changes: |
| 645 | self.get_changes() |
| 646 | |
| 647 | referenced_issue_uids = set(itertools.chain.from_iterable( |
| 648 | change['bugs'] for change in self.changes)) |
| 649 | fetched_issue_uids = set(issue['uid'] for issue in self.issues) |
| 650 | missing_issue_uids = referenced_issue_uids - fetched_issue_uids |
| 651 | |
| 652 | missing_issues_by_project = collections.defaultdict(list) |
| 653 | for issue_uid in missing_issue_uids: |
| 654 | project, issue_id = issue_uid.split(':') |
| 655 | missing_issues_by_project[project].append(issue_id) |
| 656 | |
| 657 | for project, issue_ids in missing_issues_by_project.iteritems(): |
| 658 | self.referenced_issues += self.monorail_get_issues(project, issue_ids) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 659 | |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 660 | def print_issues(self): |
| 661 | if self.issues: |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 662 | self.print_heading('Issues') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 663 | for issue in self.issues: |
| 664 | self.print_issue(issue) |
| 665 | |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 666 | def print_changes_by_issue(self, skip_empty_own): |
| 667 | if not self.issues or not self.changes: |
| 668 | return |
| 669 | |
| 670 | self.print_heading('Changes by referenced issue(s)') |
| 671 | issues = {issue['uid']: issue for issue in self.issues} |
| 672 | ref_issues = {issue['uid']: issue for issue in self.referenced_issues} |
| 673 | changes_by_issue_uid = collections.defaultdict(list) |
| 674 | changes_by_ref_issue_uid = collections.defaultdict(list) |
| 675 | changes_without_issue = [] |
| 676 | for change in self.changes: |
| 677 | added = False |
| 678 | for issue_uid in change['bugs']: |
| 679 | if issue_uid in issues: |
| 680 | changes_by_issue_uid[issue_uid].append(change) |
| 681 | added = True |
| 682 | if issue_uid in ref_issues: |
| 683 | changes_by_ref_issue_uid[issue_uid].append(change) |
| 684 | added = True |
| 685 | if not added: |
| 686 | changes_without_issue.append(change) |
| 687 | |
| 688 | # Changes referencing own issues. |
| 689 | for issue_uid in issues: |
| 690 | if changes_by_issue_uid[issue_uid] or not skip_empty_own: |
| 691 | self.print_issue(issues[issue_uid]) |
| 692 | for change in changes_by_issue_uid[issue_uid]: |
| 693 | print '', # this prints one space due to comma, but no newline |
| 694 | self.print_change(change) |
| 695 | |
| 696 | # Changes referencing others' issues. |
| 697 | for issue_uid in ref_issues: |
| 698 | assert changes_by_ref_issue_uid[issue_uid] |
| 699 | self.print_issue(ref_issues[issue_uid]) |
| 700 | for change in changes_by_ref_issue_uid[issue_uid]: |
| 701 | print '', # this prints one space due to comma, but no newline |
| 702 | self.print_change(change) |
| 703 | |
| 704 | # Changes referencing no issues. |
| 705 | if changes_without_issue: |
| 706 | print self.options.output_format_no_url.format(title='Other changes') |
| 707 | for change in changes_without_issue: |
| 708 | print '', # this prints one space due to comma, but no newline |
| 709 | self.print_change(change) |
| 710 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 711 | def print_activity(self): |
| 712 | self.print_changes() |
| 713 | self.print_reviews() |
| 714 | self.print_issues() |
| 715 | |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 716 | def dump_json(self, ignore_keys=None): |
| 717 | if ignore_keys is None: |
| 718 | ignore_keys = ['replies'] |
| 719 | |
| 720 | def format_for_json_dump(in_array): |
| 721 | output = {} |
| 722 | for item in in_array: |
| 723 | url = item.get('url') or item.get('review_url') |
| 724 | if not url: |
| 725 | raise Exception('Dumped item %s does not specify url' % item) |
| 726 | output[url] = dict( |
| 727 | (k, v) for k,v in item.iteritems() if k not in ignore_keys) |
| 728 | return output |
| 729 | |
| 730 | class PythonObjectEncoder(json.JSONEncoder): |
| 731 | def default(self, obj): # pylint: disable=method-hidden |
| 732 | if isinstance(obj, datetime): |
| 733 | return obj.isoformat() |
| 734 | if isinstance(obj, set): |
| 735 | return list(obj) |
| 736 | return json.JSONEncoder.default(self, obj) |
| 737 | |
| 738 | output = { |
| 739 | 'reviews': format_for_json_dump(self.reviews), |
| 740 | 'changes': format_for_json_dump(self.changes), |
| 741 | 'issues': format_for_json_dump(self.issues) |
| 742 | } |
| 743 | print json.dumps(output, indent=2, cls=PythonObjectEncoder) |
| 744 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 745 | |
| 746 | def main(): |
| 747 | # Silence upload.py. |
| 748 | rietveld.upload.verbosity = 0 |
| 749 | |
| 750 | parser = optparse.OptionParser(description=sys.modules[__name__].__doc__) |
| 751 | parser.add_option( |
| 752 | '-u', '--user', metavar='<email>', |
| 753 | default=os.environ.get('USER'), |
| 754 | help='Filter on user, default=%default') |
| 755 | parser.add_option( |
| 756 | '-b', '--begin', metavar='<date>', |
wychen@chromium.org | 85cab63 | 2015-05-28 21:04:37 +0000 | [diff] [blame] | 757 | help='Filter issues created after the date (mm/dd/yy)') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 758 | parser.add_option( |
| 759 | '-e', '--end', metavar='<date>', |
wychen@chromium.org | 85cab63 | 2015-05-28 21:04:37 +0000 | [diff] [blame] | 760 | help='Filter issues created before the date (mm/dd/yy)') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 761 | quarter_begin, quarter_end = get_quarter_of(datetime.today() - |
| 762 | relativedelta(months=2)) |
| 763 | parser.add_option( |
| 764 | '-Q', '--last_quarter', action='store_true', |
jsbell@chromium.org | 74bfde0 | 2014-04-09 17:14:54 +0000 | [diff] [blame] | 765 | help='Use last quarter\'s dates, i.e. %s to %s' % ( |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 766 | quarter_begin.strftime('%Y-%m-%d'), quarter_end.strftime('%Y-%m-%d'))) |
| 767 | parser.add_option( |
| 768 | '-Y', '--this_year', action='store_true', |
| 769 | help='Use this year\'s dates') |
| 770 | parser.add_option( |
| 771 | '-w', '--week_of', metavar='<date>', |
wychen@chromium.org | 85cab63 | 2015-05-28 21:04:37 +0000 | [diff] [blame] | 772 | help='Show issues for week of the date (mm/dd/yy)') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 773 | parser.add_option( |
wychen@chromium.org | 8ba1ddb | 2015-04-29 00:04:25 +0000 | [diff] [blame] | 774 | '-W', '--last_week', action='count', |
| 775 | help='Show last week\'s issues. Use more times for more weeks.') |
jsbell@chromium.org | 74bfde0 | 2014-04-09 17:14:54 +0000 | [diff] [blame] | 776 | parser.add_option( |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 777 | '-a', '--auth', |
| 778 | action='store_true', |
| 779 | help='Ask to authenticate for instances with no auth cookie') |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 780 | parser.add_option( |
| 781 | '-d', '--deltas', |
| 782 | action='store_true', |
Nicolas Boichat | 23c165f | 2018-01-26 10:04:27 +0800 | [diff] [blame] | 783 | help='Fetch deltas for changes.') |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 784 | parser.add_option( |
| 785 | '--no-referenced-issues', |
| 786 | action='store_true', |
| 787 | help='Do not fetch issues referenced by owned changes. Useful in ' |
| 788 | 'combination with --changes-by-issue when you only want to list ' |
| 789 | 'issues that are your own in the output.') |
| 790 | parser.add_option( |
| 791 | '--skip-own-issues-without-changes', |
| 792 | action='store_true', |
| 793 | help='Skips listing own issues without changes when showing changes ' |
| 794 | 'grouped by referenced issue(s). See --changes-by-issue for more ' |
| 795 | 'details.') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 796 | |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 797 | activity_types_group = optparse.OptionGroup(parser, 'Activity Types', |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 798 | 'By default, all activity will be looked up and ' |
| 799 | 'printed. If any of these are specified, only ' |
| 800 | 'those specified will be searched.') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 801 | activity_types_group.add_option( |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 802 | '-c', '--changes', |
| 803 | action='store_true', |
| 804 | help='Show changes.') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 805 | activity_types_group.add_option( |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 806 | '-i', '--issues', |
| 807 | action='store_true', |
| 808 | help='Show issues.') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 809 | activity_types_group.add_option( |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 810 | '-r', '--reviews', |
| 811 | action='store_true', |
| 812 | help='Show reviews.') |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 813 | activity_types_group.add_option( |
| 814 | '--changes-by-issue', action='store_true', |
| 815 | help='Show changes grouped by referenced issue(s).') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 816 | parser.add_option_group(activity_types_group) |
| 817 | |
| 818 | output_format_group = optparse.OptionGroup(parser, 'Output Format', |
| 819 | 'By default, all activity will be printed in the ' |
| 820 | 'following format: {url} {title}. This can be ' |
| 821 | 'changed for either all activity types or ' |
| 822 | 'individually for each activity type. The format ' |
| 823 | 'is defined as documented for ' |
| 824 | 'string.format(...). The variables available for ' |
| 825 | 'all activity types are url, title and author. ' |
| 826 | 'Format options for specific activity types will ' |
| 827 | 'override the generic format.') |
| 828 | output_format_group.add_option( |
| 829 | '-f', '--output-format', metavar='<format>', |
| 830 | default=u'{url} {title}', |
| 831 | help='Specifies the format to use when printing all your activity.') |
| 832 | output_format_group.add_option( |
| 833 | '--output-format-changes', metavar='<format>', |
| 834 | default=None, |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 835 | help='Specifies the format to use when printing changes. Supports the ' |
| 836 | 'additional variable {reviewers}') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 837 | output_format_group.add_option( |
| 838 | '--output-format-issues', metavar='<format>', |
| 839 | default=None, |
cjhopman@chromium.org | 53c1e56 | 2013-03-11 20:02:38 +0000 | [diff] [blame] | 840 | help='Specifies the format to use when printing issues. Supports the ' |
| 841 | 'additional variable {owner}.') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 842 | output_format_group.add_option( |
| 843 | '--output-format-reviews', metavar='<format>', |
| 844 | default=None, |
| 845 | help='Specifies the format to use when printing reviews.') |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 846 | output_format_group.add_option( |
| 847 | '--output-format-heading', metavar='<format>', |
| 848 | default=u'{heading}:', |
| 849 | help='Specifies the format to use when printing headings.') |
| 850 | output_format_group.add_option( |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 851 | '--output-format-no-url', default='{title}', |
| 852 | help='Specifies the format to use when printing activity without url.') |
| 853 | output_format_group.add_option( |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 854 | '-m', '--markdown', action='store_true', |
| 855 | help='Use markdown-friendly output (overrides --output-format ' |
| 856 | 'and --output-format-heading)') |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 857 | output_format_group.add_option( |
| 858 | '-j', '--json', action='store_true', |
| 859 | help='Output json data (overrides other format options)') |
nyquist@chromium.org | 18bc90d | 2012-12-20 19:26:47 +0000 | [diff] [blame] | 860 | parser.add_option_group(output_format_group) |
vadimsh@chromium.org | cf6a5d2 | 2015-04-09 22:02:00 +0000 | [diff] [blame] | 861 | auth.add_auth_options(parser) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 862 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 863 | parser.add_option( |
| 864 | '-v', '--verbose', |
| 865 | action='store_const', |
| 866 | dest='verbosity', |
| 867 | default=logging.WARN, |
| 868 | const=logging.INFO, |
| 869 | help='Output extra informational messages.' |
| 870 | ) |
| 871 | parser.add_option( |
| 872 | '-q', '--quiet', |
| 873 | action='store_const', |
| 874 | dest='verbosity', |
| 875 | const=logging.ERROR, |
| 876 | help='Suppress non-error messages.' |
| 877 | ) |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 878 | parser.add_option( |
| 879 | '-o', '--output', metavar='<file>', |
| 880 | help='Where to output the results. By default prints to stdout.') |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 881 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 882 | # Remove description formatting |
| 883 | parser.format_description = ( |
Quinten Yearsley | b2cc4a9 | 2016-12-15 13:53:26 -0800 | [diff] [blame] | 884 | lambda _: parser.description) # pylint: disable=no-member |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 885 | |
| 886 | options, args = parser.parse_args() |
| 887 | options.local_user = os.environ.get('USER') |
| 888 | if args: |
| 889 | parser.error('Args unsupported') |
| 890 | if not options.user: |
| 891 | parser.error('USER is not set, please use -u') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 892 | options.user = username(options.user) |
| 893 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 894 | logging.basicConfig(level=options.verbosity) |
| 895 | |
| 896 | # python-keyring provides easy access to the system keyring. |
| 897 | try: |
| 898 | import keyring # pylint: disable=unused-import,unused-variable,F0401 |
| 899 | except ImportError: |
| 900 | logging.warning('Consider installing python-keyring') |
| 901 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 902 | if not options.begin: |
| 903 | if options.last_quarter: |
| 904 | begin, end = quarter_begin, quarter_end |
| 905 | elif options.this_year: |
| 906 | begin, end = get_year_of(datetime.today()) |
| 907 | elif options.week_of: |
| 908 | begin, end = (get_week_of(datetime.strptime(options.week_of, '%m/%d/%y'))) |
jsbell@chromium.org | 74bfde0 | 2014-04-09 17:14:54 +0000 | [diff] [blame] | 909 | elif options.last_week: |
wychen@chromium.org | 8ba1ddb | 2015-04-29 00:04:25 +0000 | [diff] [blame] | 910 | begin, end = (get_week_of(datetime.today() - |
| 911 | timedelta(days=1 + 7 * options.last_week))) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 912 | else: |
| 913 | begin, end = (get_week_of(datetime.today() - timedelta(days=1))) |
| 914 | else: |
Daniel Cheng | 4b37ce6 | 2017-09-07 12:00:02 -0700 | [diff] [blame] | 915 | begin = dateutil.parser.parse(options.begin) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 916 | if options.end: |
Daniel Cheng | 4b37ce6 | 2017-09-07 12:00:02 -0700 | [diff] [blame] | 917 | end = dateutil.parser.parse(options.end) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 918 | else: |
| 919 | end = datetime.today() |
| 920 | options.begin, options.end = begin, end |
| 921 | |
jsbell@chromium.org | c92f582 | 2014-01-06 23:49:11 +0000 | [diff] [blame] | 922 | if options.markdown: |
| 923 | options.output_format = ' * [{title}]({url})' |
| 924 | options.output_format_heading = '### {heading} ###' |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 925 | options.output_format_no_url = ' * {title}' |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 926 | logging.info('Searching for activity by %s', options.user) |
| 927 | logging.info('Using range %s to %s', options.begin, options.end) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 928 | |
| 929 | my_activity = MyActivity(options) |
| 930 | |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 931 | if not (options.changes or options.reviews or options.issues or |
| 932 | options.changes_by_issue): |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 933 | options.changes = True |
| 934 | options.issues = True |
| 935 | options.reviews = True |
| 936 | |
| 937 | # First do any required authentication so none of the user interaction has to |
| 938 | # wait for actual work. |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 939 | if options.changes or options.changes_by_issue: |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 940 | my_activity.auth_for_changes() |
| 941 | if options.reviews: |
| 942 | my_activity.auth_for_reviews() |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 943 | |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 944 | logging.info('Looking up activity.....') |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 945 | |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 946 | try: |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 947 | if options.changes or options.changes_by_issue: |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 948 | my_activity.get_changes() |
| 949 | if options.reviews: |
| 950 | my_activity.get_reviews() |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 951 | if options.issues or options.changes_by_issue: |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 952 | my_activity.get_issues() |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 953 | if not options.no_referenced_issues: |
| 954 | my_activity.get_referenced_issues() |
seanmccullough@chromium.org | 3e4a581 | 2015-06-11 17:48:47 +0000 | [diff] [blame] | 955 | except auth.AuthenticationError as e: |
Tobias Sargeant | ffb3c43 | 2017-03-08 14:09:14 +0000 | [diff] [blame] | 956 | logging.error('auth.AuthenticationError: %s', e) |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 957 | |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 958 | output_file = None |
| 959 | try: |
| 960 | if options.output: |
| 961 | output_file = open(options.output, 'w') |
| 962 | logging.info('Printing output to "%s"', options.output) |
| 963 | sys.stdout = output_file |
| 964 | except (IOError, OSError) as e: |
Varun Khaneja | d9f97bc | 2017-08-02 12:55:01 -0700 | [diff] [blame] | 965 | logging.error('Unable to write output: %s', e) |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 966 | else: |
| 967 | if options.json: |
| 968 | my_activity.dump_json() |
| 969 | else: |
Sergiy Byelozyorov | 544b744 | 2018-03-16 21:44:58 +0100 | [diff] [blame] | 970 | if options.changes: |
| 971 | my_activity.print_changes() |
| 972 | if options.reviews: |
| 973 | my_activity.print_reviews() |
| 974 | if options.issues: |
| 975 | my_activity.print_issues() |
| 976 | if options.changes_by_issue: |
| 977 | my_activity.print_changes_by_issue( |
| 978 | options.skip_own_issues_without_changes) |
Nicolas Dossou-gbete | e5deedf | 2017-03-15 16:26:47 +0000 | [diff] [blame] | 979 | finally: |
| 980 | if output_file: |
| 981 | logging.info('Done printing to file.') |
| 982 | sys.stdout = sys.__stdout__ |
| 983 | output_file.close() |
| 984 | |
cjhopman@chromium.org | 04d119d | 2012-10-17 22:41:53 +0000 | [diff] [blame] | 985 | return 0 |
| 986 | |
| 987 | |
| 988 | if __name__ == '__main__': |
stevefung@chromium.org | 832d51e | 2015-05-27 12:52:51 +0000 | [diff] [blame] | 989 | # Fix encoding to support non-ascii issue titles. |
| 990 | fix_encoding.fix_encoding() |
| 991 | |
sbc@chromium.org | 013731e | 2015-02-26 18:28:43 +0000 | [diff] [blame] | 992 | try: |
| 993 | sys.exit(main()) |
| 994 | except KeyboardInterrupt: |
| 995 | sys.stderr.write('interrupted\n') |
| 996 | sys.exit(1) |