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