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