szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """ |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 6 | Utilities for requesting information for a Gerrit server via HTTPS. |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 7 | |
| 8 | https://gerrit-review.googlesource.com/Documentation/rest-api.html |
| 9 | """ |
| 10 | |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 11 | from __future__ import print_function |
| 12 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 13 | import base64 |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 14 | import contextlib |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 15 | import cookielib |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 16 | import httplib # Still used for its constants. |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 17 | import json |
| 18 | import logging |
| 19 | import netrc |
| 20 | import os |
Andrii Shyshkalov | d4c8673 | 2018-09-25 04:29:31 +0000 | [diff] [blame] | 21 | import random |
nodir@chromium.org | ce32b6e | 2014-05-12 20:31:32 +0000 | [diff] [blame] | 22 | import re |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 23 | import socket |
szager@chromium.org | f202a25 | 2014-05-27 18:55:52 +0000 | [diff] [blame] | 24 | import stat |
| 25 | import sys |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 26 | import tempfile |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 27 | import time |
| 28 | import urllib |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 29 | import urlparse |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 30 | from cStringIO import StringIO |
Andrii Shyshkalov | ba7b0a4 | 2018-10-15 03:20:35 +0000 | [diff] [blame] | 31 | from multiprocessing.pool import ThreadPool |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 32 | |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 33 | import auth |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 34 | import gclient_utils |
Edward Lemur | 5a9ff43 | 2018-10-30 19:00:22 +0000 | [diff] [blame] | 35 | import metrics |
| 36 | import metrics_utils |
Aaron Gable | 8797cab | 2018-03-06 13:55:00 -0800 | [diff] [blame] | 37 | import subprocess2 |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 38 | from third_party import httplib2 |
szager@chromium.org | f202a25 | 2014-05-27 18:55:52 +0000 | [diff] [blame] | 39 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 40 | LOGGER = logging.getLogger() |
Steve Kobes | 5611772 | 2018-09-13 18:18:35 +0000 | [diff] [blame] | 41 | # With a starting sleep time of 1.5 seconds, 2^n exponential backoff, and seven |
| 42 | # total tries, the sleep time between the first and last tries will be 94.5 sec. |
| 43 | # TODO(crbug.com/881860): Lower this when crbug.com/877717 is fixed. |
| 44 | TRY_LIMIT = 7 |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 45 | |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 46 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 47 | # Controls the transport protocol used to communicate with Gerrit. |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 48 | # This is parameterized primarily to enable GerritTestCase. |
| 49 | GERRIT_PROTOCOL = 'https' |
| 50 | |
| 51 | |
| 52 | class GerritError(Exception): |
| 53 | """Exception class for errors commuicating with the gerrit-on-borg service.""" |
| 54 | def __init__(self, http_status, *args, **kwargs): |
| 55 | super(GerritError, self).__init__(*args, **kwargs) |
| 56 | self.http_status = http_status |
| 57 | self.message = '(%d) %s' % (self.http_status, self.message) |
| 58 | |
| 59 | |
nodir@chromium.org | ce32b6e | 2014-05-12 20:31:32 +0000 | [diff] [blame] | 60 | class GerritAuthenticationError(GerritError): |
| 61 | """Exception class for authentication errors during Gerrit communication.""" |
| 62 | |
| 63 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 64 | def _QueryString(params, first_param=None): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 65 | """Encodes query parameters in the key:val[+key:val...] format specified here: |
| 66 | |
| 67 | https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes |
| 68 | """ |
| 69 | q = [urllib.quote(first_param)] if first_param else [] |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 70 | q.extend(['%s:%s' % (key, val) for key, val in params]) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 71 | return '+'.join(q) |
| 72 | |
| 73 | |
Aaron Gable | d2db5a2 | 2017-03-24 14:14:15 -0700 | [diff] [blame] | 74 | def GetConnectionObject(protocol=None): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 75 | if protocol is None: |
| 76 | protocol = GERRIT_PROTOCOL |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 77 | if protocol in ('http', 'https'): |
Aaron Gable | d2db5a2 | 2017-03-24 14:14:15 -0700 | [diff] [blame] | 78 | return httplib2.Http() |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 79 | else: |
| 80 | raise RuntimeError( |
| 81 | "Don't know how to work with protocol '%s'" % protocol) |
| 82 | |
| 83 | |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 84 | class Authenticator(object): |
| 85 | """Base authenticator class for authenticator implementations to subclass.""" |
| 86 | |
| 87 | def get_auth_header(self, host): |
| 88 | raise NotImplementedError() |
| 89 | |
| 90 | @staticmethod |
| 91 | def get(): |
| 92 | """Returns: (Authenticator) The identified Authenticator to use. |
| 93 | |
| 94 | Probes the local system and its environment and identifies the |
| 95 | Authenticator instance to use. |
| 96 | """ |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 97 | # LUCI Context takes priority since it's normally present only on bots, |
| 98 | # which then must use it. |
| 99 | if LuciContextAuthenticator.is_luci(): |
| 100 | return LuciContextAuthenticator() |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 101 | if GceAuthenticator.is_gce(): |
| 102 | return GceAuthenticator() |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 103 | return CookiesAuthenticator() |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 104 | |
| 105 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 106 | class CookiesAuthenticator(Authenticator): |
| 107 | """Authenticator implementation that uses ".netrc" or ".gitcookies" for token. |
| 108 | |
| 109 | Expected case for developer workstations. |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 110 | """ |
| 111 | |
Vadim Shtayura | b250ec1 | 2018-10-04 00:21:08 +0000 | [diff] [blame] | 112 | _EMPTY = object() |
| 113 | |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 114 | def __init__(self): |
Vadim Shtayura | b250ec1 | 2018-10-04 00:21:08 +0000 | [diff] [blame] | 115 | # Credentials will be loaded lazily on first use. This ensures Authenticator |
| 116 | # get() can always construct an authenticator, even if something is broken. |
| 117 | # This allows 'creds-check' to proceed to actually checking creds later, |
| 118 | # rigorously (instead of blowing up with a cryptic error if they are wrong). |
| 119 | self._netrc = self._EMPTY |
| 120 | self._gitcookies = self._EMPTY |
| 121 | |
| 122 | @property |
| 123 | def netrc(self): |
| 124 | if self._netrc is self._EMPTY: |
| 125 | self._netrc = self._get_netrc() |
| 126 | return self._netrc |
| 127 | |
| 128 | @property |
| 129 | def gitcookies(self): |
| 130 | if self._gitcookies is self._EMPTY: |
| 131 | self._gitcookies = self._get_gitcookies() |
| 132 | return self._gitcookies |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 133 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 134 | @classmethod |
Andrii Shyshkalov | c817382 | 2017-07-10 12:10:53 +0200 | [diff] [blame] | 135 | def get_new_password_url(cls, host): |
| 136 | assert not host.startswith('http') |
| 137 | # Assume *.googlesource.com pattern. |
| 138 | parts = host.split('.') |
| 139 | if not parts[0].endswith('-review'): |
| 140 | parts[0] += '-review' |
| 141 | return 'https://%s/new-password' % ('.'.join(parts)) |
| 142 | |
| 143 | @classmethod |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 144 | def get_new_password_message(cls, host): |
William Hesse | e9e89e3 | 2019-03-03 19:02:32 +0000 | [diff] [blame] | 145 | if host is None: |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 146 | return ('Git host for Gerrit upload is unknown. Check your remote ' |
William Hesse | e9e89e3 | 2019-03-03 19:02:32 +0000 | [diff] [blame] | 147 | 'and the branch your branch is tracking. This tool assumes ' |
| 148 | 'that you are using a git server at *.googlesource.com.') |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 149 | assert not host.startswith('http') |
| 150 | # Assume *.googlesource.com pattern. |
| 151 | parts = host.split('.') |
| 152 | if not parts[0].endswith('-review'): |
| 153 | parts[0] += '-review' |
| 154 | url = 'https://%s/new-password' % ('.'.join(parts)) |
Andrii Shyshkalov | 0a0b067 | 2017-03-16 16:27:48 +0100 | [diff] [blame] | 155 | return 'You can (re)generate your credentials by visiting %s' % url |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 156 | |
| 157 | @classmethod |
| 158 | def get_netrc_path(cls): |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 159 | path = '_netrc' if sys.platform.startswith('win') else '.netrc' |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 160 | return os.path.expanduser(os.path.join('~', path)) |
| 161 | |
| 162 | @classmethod |
| 163 | def _get_netrc(cls): |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 164 | # Buffer the '.netrc' path. Use an empty file if it doesn't exist. |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 165 | path = cls.get_netrc_path() |
Sylvain Defresne | 2b138f7 | 2018-07-12 08:34:48 +0000 | [diff] [blame] | 166 | if not os.path.exists(path): |
| 167 | return netrc.netrc(os.devnull) |
| 168 | |
| 169 | st = os.stat(path) |
| 170 | if st.st_mode & (stat.S_IRWXG | stat.S_IRWXO): |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 171 | print( |
Sylvain Defresne | 2b138f7 | 2018-07-12 08:34:48 +0000 | [diff] [blame] | 172 | 'WARNING: netrc file %s cannot be used because its file ' |
| 173 | 'permissions are insecure. netrc file permissions should be ' |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 174 | '600.' % path, file=sys.stderr) |
Sylvain Defresne | 2b138f7 | 2018-07-12 08:34:48 +0000 | [diff] [blame] | 175 | with open(path) as fd: |
| 176 | content = fd.read() |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 177 | |
| 178 | # Load the '.netrc' file. We strip comments from it because processing them |
| 179 | # can trigger a bug in Windows. See crbug.com/664664. |
| 180 | content = '\n'.join(l for l in content.splitlines() |
| 181 | if l.strip() and not l.strip().startswith('#')) |
| 182 | with tempdir() as tdir: |
| 183 | netrc_path = os.path.join(tdir, 'netrc') |
| 184 | with open(netrc_path, 'w') as fd: |
| 185 | fd.write(content) |
| 186 | os.chmod(netrc_path, (stat.S_IRUSR | stat.S_IWUSR)) |
| 187 | return cls._get_netrc_from_path(netrc_path) |
| 188 | |
| 189 | @classmethod |
| 190 | def _get_netrc_from_path(cls, path): |
| 191 | try: |
| 192 | return netrc.netrc(path) |
| 193 | except IOError: |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 194 | print('WARNING: Could not read netrc file %s' % path, file=sys.stderr) |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 195 | return netrc.netrc(os.devnull) |
| 196 | except netrc.NetrcParseError as e: |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 197 | print('ERROR: Cannot use netrc file %s due to a parsing error: %s' % |
| 198 | (path, e), file=sys.stderr) |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 199 | return netrc.netrc(os.devnull) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 200 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 201 | @classmethod |
| 202 | def get_gitcookies_path(cls): |
Ravi Mistry | 0bfa9ad | 2016-11-21 12:58:31 -0500 | [diff] [blame] | 203 | if os.getenv('GIT_COOKIES_PATH'): |
| 204 | return os.getenv('GIT_COOKIES_PATH') |
Aaron Gable | 8797cab | 2018-03-06 13:55:00 -0800 | [diff] [blame] | 205 | try: |
| 206 | return subprocess2.check_output( |
| 207 | ['git', 'config', '--path', 'http.cookiefile']).strip() |
| 208 | except subprocess2.CalledProcessError: |
| 209 | return os.path.join(os.environ['HOME'], '.gitcookies') |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 210 | |
| 211 | @classmethod |
| 212 | def _get_gitcookies(cls): |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 213 | gitcookies = {} |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 214 | path = cls.get_gitcookies_path() |
| 215 | if not os.path.exists(path): |
| 216 | return gitcookies |
| 217 | |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 218 | try: |
| 219 | f = open(path, 'rb') |
| 220 | except IOError: |
| 221 | return gitcookies |
| 222 | |
| 223 | with f: |
| 224 | for line in f: |
| 225 | try: |
| 226 | fields = line.strip().split('\t') |
| 227 | if line.strip().startswith('#') or len(fields) != 7: |
| 228 | continue |
| 229 | domain, xpath, key, value = fields[0], fields[2], fields[5], fields[6] |
| 230 | if xpath == '/' and key == 'o': |
Eric Boren | 2fb6310 | 2018-10-05 13:05:03 +0000 | [diff] [blame] | 231 | if value.startswith('git-'): |
| 232 | login, secret_token = value.split('=', 1) |
| 233 | gitcookies[domain] = (login, secret_token) |
| 234 | else: |
| 235 | gitcookies[domain] = ('', value) |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 236 | except (IndexError, ValueError, TypeError) as exc: |
Andrii Shyshkalov | 5b04a57 | 2017-01-23 17:44:41 +0100 | [diff] [blame] | 237 | LOGGER.warning(exc) |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 238 | return gitcookies |
| 239 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 240 | def _get_auth_for_host(self, host): |
Marc-Antoine Ruel | 8e57b4b | 2019-10-11 01:01:36 +0000 | [diff] [blame] | 241 | for domain, creds in self.gitcookies.items(): |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 242 | if cookielib.domain_match(host, domain): |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 243 | return (creds[0], None, creds[1]) |
| 244 | return self.netrc.authenticators(host) |
phajdan.jr@chromium.org | ff7840a | 2015-11-04 16:35:22 +0000 | [diff] [blame] | 245 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 246 | def get_auth_header(self, host): |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 247 | a = self._get_auth_for_host(host) |
| 248 | if a: |
Eric Boren | 2fb6310 | 2018-10-05 13:05:03 +0000 | [diff] [blame] | 249 | if a[0]: |
| 250 | return 'Basic %s' % (base64.b64encode('%s:%s' % (a[0], a[2]))) |
| 251 | else: |
| 252 | return 'Bearer %s' % a[2] |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 253 | return None |
| 254 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 255 | def get_auth_email(self, host): |
| 256 | """Best effort parsing of email to be used for auth for the given host.""" |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 257 | a = self._get_auth_for_host(host) |
| 258 | if not a: |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 259 | return None |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 260 | login = a[0] |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 261 | # login typically looks like 'git-xxx.example.com' |
| 262 | if not login.startswith('git-') or '.' not in login: |
| 263 | return None |
| 264 | username, domain = login[len('git-'):].split('.', 1) |
| 265 | return '%s@%s' % (username, domain) |
| 266 | |
Andrii Shyshkalov | 1897532 | 2017-01-25 16:44:13 +0100 | [diff] [blame] | 267 | |
tandrii@chromium.org | fe30f18 | 2016-04-13 12:15:04 +0000 | [diff] [blame] | 268 | # Backwards compatibility just in case somebody imports this outside of |
| 269 | # depot_tools. |
| 270 | NetrcAuthenticator = CookiesAuthenticator |
| 271 | |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 272 | |
| 273 | class GceAuthenticator(Authenticator): |
| 274 | """Authenticator implementation that uses GCE metadata service for token. |
| 275 | """ |
| 276 | |
| 277 | _INFO_URL = 'http://metadata.google.internal' |
smut | 5e9401b | 2017-08-10 15:22:20 -0700 | [diff] [blame] | 278 | _ACQUIRE_URL = ('%s/computeMetadata/v1/instance/' |
| 279 | 'service-accounts/default/token' % _INFO_URL) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 280 | _ACQUIRE_HEADERS = {"Metadata-Flavor": "Google"} |
| 281 | |
| 282 | _cache_is_gce = None |
| 283 | _token_cache = None |
| 284 | _token_expiration = None |
| 285 | |
| 286 | @classmethod |
| 287 | def is_gce(cls): |
Ravi Mistry | fad941b | 2016-11-15 13:00:47 -0500 | [diff] [blame] | 288 | if os.getenv('SKIP_GCE_AUTH_FOR_GIT'): |
| 289 | return False |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 290 | if cls._cache_is_gce is None: |
| 291 | cls._cache_is_gce = cls._test_is_gce() |
| 292 | return cls._cache_is_gce |
| 293 | |
| 294 | @classmethod |
| 295 | def _test_is_gce(cls): |
| 296 | # Based on https://cloud.google.com/compute/docs/metadata#runninggce |
| 297 | try: |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 298 | resp, _ = cls._get(cls._INFO_URL) |
Sergio Villar Senin | 0d466d2 | 2018-03-23 14:31:48 +0100 | [diff] [blame] | 299 | except (socket.error, httplib2.ServerNotFoundError, |
| 300 | httplib2.socks.HTTPError): |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 301 | # Could not resolve URL. |
| 302 | return False |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 303 | return resp.get('metadata-flavor') == 'Google' |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 304 | |
| 305 | @staticmethod |
| 306 | def _get(url, **kwargs): |
| 307 | next_delay_sec = 1 |
| 308 | for i in xrange(TRY_LIMIT): |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 309 | p = urlparse.urlparse(url) |
Aaron Gable | d2db5a2 | 2017-03-24 14:14:15 -0700 | [diff] [blame] | 310 | c = GetConnectionObject(protocol=p.scheme) |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 311 | resp, contents = c.request(url, 'GET', **kwargs) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 312 | LOGGER.debug('GET [%s] #%d/%d (%d)', url, i+1, TRY_LIMIT, resp.status) |
| 313 | if resp.status < httplib.INTERNAL_SERVER_ERROR: |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 314 | return (resp, contents) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 315 | |
Aaron Gable | 92e9f38 | 2017-12-07 11:47:41 -0800 | [diff] [blame] | 316 | # Retry server error status codes. |
| 317 | LOGGER.warn('Encountered server error') |
| 318 | if TRY_LIMIT - i > 1: |
| 319 | LOGGER.info('Will retry in %d seconds (%d more times)...', |
| 320 | next_delay_sec, TRY_LIMIT - i - 1) |
| 321 | time.sleep(next_delay_sec) |
| 322 | next_delay_sec *= 2 |
| 323 | |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 324 | @classmethod |
| 325 | def _get_token_dict(cls): |
| 326 | if cls._token_cache: |
| 327 | # If it expires within 25 seconds, refresh. |
| 328 | if cls._token_expiration < time.time() - 25: |
| 329 | return cls._token_cache |
| 330 | |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 331 | resp, contents = cls._get(cls._ACQUIRE_URL, headers=cls._ACQUIRE_HEADERS) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 332 | if resp.status != httplib.OK: |
| 333 | return None |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 334 | cls._token_cache = json.loads(contents) |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 335 | cls._token_expiration = cls._token_cache['expires_in'] + time.time() |
| 336 | return cls._token_cache |
| 337 | |
| 338 | def get_auth_header(self, _host): |
| 339 | token_dict = self._get_token_dict() |
| 340 | if not token_dict: |
| 341 | return None |
| 342 | return '%(token_type)s %(access_token)s' % token_dict |
| 343 | |
| 344 | |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 345 | class LuciContextAuthenticator(Authenticator): |
| 346 | """Authenticator implementation that uses LUCI_CONTEXT ambient local auth. |
| 347 | """ |
| 348 | |
| 349 | @staticmethod |
| 350 | def is_luci(): |
| 351 | return auth.has_luci_context_local_auth() |
| 352 | |
| 353 | def __init__(self): |
| 354 | self._access_token = None |
| 355 | self._ensure_fresh() |
| 356 | |
| 357 | def _ensure_fresh(self): |
| 358 | if not self._access_token or self._access_token.needs_refresh(): |
| 359 | self._access_token = auth.get_luci_context_access_token( |
| 360 | scopes=' '.join([auth.OAUTH_SCOPE_EMAIL, auth.OAUTH_SCOPE_GERRIT])) |
| 361 | |
| 362 | def get_auth_header(self, _host): |
| 363 | self._ensure_fresh() |
| 364 | return 'Bearer %s' % self._access_token.token |
| 365 | |
| 366 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 367 | def CreateHttpConn(host, path, reqtype='GET', headers=None, body=None): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 368 | """Opens an HTTPS connection to a Gerrit service, and sends a request.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 369 | headers = headers or {} |
| 370 | bare_host = host.partition(':')[0] |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 371 | |
Andrii Shyshkalov | 733d4ec | 2018-04-19 11:48:58 -0700 | [diff] [blame] | 372 | a = Authenticator.get().get_auth_header(bare_host) |
| 373 | if a: |
| 374 | headers.setdefault('Authorization', a) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 375 | else: |
dnj@chromium.org | a5a2c8a | 2015-09-29 16:22:55 +0000 | [diff] [blame] | 376 | LOGGER.debug('No authorization found for %s.' % bare_host) |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 377 | |
Dan Jacques | 6d5bcc2 | 2016-11-14 15:32:04 -0800 | [diff] [blame] | 378 | url = path |
| 379 | if not url.startswith('/'): |
| 380 | url = '/' + url |
| 381 | if 'Authorization' in headers and not url.startswith('/a/'): |
| 382 | url = '/a%s' % url |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 383 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 384 | if body: |
| 385 | body = json.JSONEncoder().encode(body) |
| 386 | headers.setdefault('Content-Type', 'application/json') |
| 387 | if LOGGER.isEnabledFor(logging.DEBUG): |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 388 | LOGGER.debug('%s %s://%s%s' % (reqtype, GERRIT_PROTOCOL, host, url)) |
Marc-Antoine Ruel | 8e57b4b | 2019-10-11 01:01:36 +0000 | [diff] [blame] | 389 | for key, val in headers.items(): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 390 | if key == 'Authorization': |
| 391 | val = 'HIDDEN' |
| 392 | LOGGER.debug('%s: %s' % (key, val)) |
| 393 | if body: |
| 394 | LOGGER.debug(body) |
Aaron Gable | d2db5a2 | 2017-03-24 14:14:15 -0700 | [diff] [blame] | 395 | conn = GetConnectionObject() |
Andrii Shyshkalov | 86c823e | 2018-09-18 19:51:33 +0000 | [diff] [blame] | 396 | # HACK: httplib.Http has no such attribute; we store req_host here for later |
| 397 | # use in ReadHttpResponse. |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 398 | conn.req_host = host |
| 399 | conn.req_params = { |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 400 | 'uri': urlparse.urljoin('%s://%s' % (GERRIT_PROTOCOL, host), url), |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 401 | 'method': reqtype, |
| 402 | 'headers': headers, |
| 403 | 'body': body, |
| 404 | } |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 405 | return conn |
| 406 | |
| 407 | |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 408 | def ReadHttpResponse(conn, accept_statuses=frozenset([200])): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 409 | """Reads an HTTP response from a connection into a string buffer. |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 410 | |
| 411 | Args: |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 412 | conn: An Http object created by CreateHttpConn above. |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 413 | accept_statuses: Treat any of these statuses as success. Default: [200] |
| 414 | Common additions include 204, 400, and 404. |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 415 | Returns: A string buffer containing the connection's reply. |
| 416 | """ |
Steve Kobes | 5611772 | 2018-09-13 18:18:35 +0000 | [diff] [blame] | 417 | sleep_time = 1.5 |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 418 | for idx in range(TRY_LIMIT): |
Edward Lemur | 5a9ff43 | 2018-10-30 19:00:22 +0000 | [diff] [blame] | 419 | before_response = time.time() |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 420 | response, contents = conn.request(**conn.req_params) |
nodir@chromium.org | ce32b6e | 2014-05-12 20:31:32 +0000 | [diff] [blame] | 421 | |
Edward Lemur | 5a9ff43 | 2018-10-30 19:00:22 +0000 | [diff] [blame] | 422 | response_time = time.time() - before_response |
| 423 | metrics.collector.add_repeated( |
| 424 | 'http_requests', |
| 425 | metrics_utils.extract_http_metrics( |
| 426 | conn.req_params['uri'], conn.req_params['method'], response.status, |
| 427 | response_time)) |
| 428 | |
nodir@chromium.org | ce32b6e | 2014-05-12 20:31:32 +0000 | [diff] [blame] | 429 | # Check if this is an authentication issue. |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 430 | www_authenticate = response.get('www-authenticate') |
nodir@chromium.org | ce32b6e | 2014-05-12 20:31:32 +0000 | [diff] [blame] | 431 | if (response.status in (httplib.UNAUTHORIZED, httplib.FOUND) and |
| 432 | www_authenticate): |
| 433 | auth_match = re.search('realm="([^"]+)"', www_authenticate, re.I) |
| 434 | host = auth_match.group(1) if auth_match else conn.req_host |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 435 | reason = ('Authentication failed. Please make sure your .gitcookies file ' |
nodir@chromium.org | ce32b6e | 2014-05-12 20:31:32 +0000 | [diff] [blame] | 436 | 'has credentials for %s' % host) |
| 437 | raise GerritAuthenticationError(response.status, reason) |
| 438 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 439 | # If response.status < 500 then the result is final; break retry loop. |
Michael Moss | 120b2e4 | 2018-06-21 23:53:42 +0000 | [diff] [blame] | 440 | # If the response is 404/409, it might be because of replication lag, so |
Aaron Gable | 62ca960 | 2017-05-19 17:24:52 -0700 | [diff] [blame] | 441 | # keep trying anyway. |
Michael Moss | 120b2e4 | 2018-06-21 23:53:42 +0000 | [diff] [blame] | 442 | if ((response.status < 500 and response.status not in [404, 409]) |
Michael Moss | b40a451 | 2017-10-10 11:07:17 -0700 | [diff] [blame] | 443 | or response.status in accept_statuses): |
Andrii Shyshkalov | 5b04a57 | 2017-01-23 17:44:41 +0100 | [diff] [blame] | 444 | LOGGER.debug('got response %d for %s %s', response.status, |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 445 | conn.req_params['method'], conn.req_params['uri']) |
Michael Moss | b40a451 | 2017-10-10 11:07:17 -0700 | [diff] [blame] | 446 | # If 404 was in accept_statuses, then it's expected that the file might |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 447 | # not exist, so don't return the gitiles error page because that's not |
| 448 | # the "content" that was actually requested. |
Michael Moss | b40a451 | 2017-10-10 11:07:17 -0700 | [diff] [blame] | 449 | if response.status == 404: |
| 450 | contents = '' |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 451 | break |
Edward Lemur | 49c8eaf | 2018-11-07 22:13:12 +0000 | [diff] [blame] | 452 | # A status >=500 is assumed to be a possible transient error; retry. |
| 453 | http_version = 'HTTP/%s' % ('1.1' if response.version == 11 else '1.0') |
| 454 | LOGGER.warn('A transient error occurred while querying %s:\n' |
| 455 | '%s %s %s\n' |
| 456 | '%s %d %s', |
| 457 | conn.req_host, conn.req_params['method'], |
| 458 | conn.req_params['uri'], |
| 459 | http_version, http_version, response.status, response.reason) |
Andrii Shyshkalov | d4c8673 | 2018-09-25 04:29:31 +0000 | [diff] [blame] | 460 | if response.status == 404: |
| 461 | # TODO(crbug/881860): remove this hack. |
| 462 | # HACK: try different Gerrit mirror as a workaround for potentially |
| 463 | # out-of-date mirror hit through default routing. |
| 464 | if conn.req_host == 'chromium-review.googlesource.com': |
| 465 | conn.req_params['uri'] = _UseGerritMirror( |
| 466 | conn.req_params['uri'], 'chromium-review.googlesource.com') |
| 467 | # And don't increase sleep_time in this case, since we suspect we've |
| 468 | # just asked wrong git mirror before. |
| 469 | sleep_time /= 2.0 |
| 470 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 471 | if TRY_LIMIT - idx > 1: |
Aaron Gable | 92e9f38 | 2017-12-07 11:47:41 -0800 | [diff] [blame] | 472 | LOGGER.info('Will retry in %d seconds (%d more times)...', |
| 473 | sleep_time, TRY_LIMIT - idx - 1) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 474 | time.sleep(sleep_time) |
| 475 | sleep_time = sleep_time * 2 |
Edward Lemur | 83bd7f4 | 2018-10-10 00:14:21 +0000 | [diff] [blame] | 476 | # end of retries loop |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 477 | if response.status not in accept_statuses: |
Andrii Shyshkalov | 4956f79 | 2017-04-10 14:28:38 +0200 | [diff] [blame] | 478 | if response.status in (401, 403): |
| 479 | print('Your Gerrit credentials might be misconfigured. Try: \n' |
| 480 | ' git cl creds-check') |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 481 | reason = '%s: %s' % (response.reason, contents) |
nodir@chromium.org | a779803 | 2014-04-30 23:40:53 +0000 | [diff] [blame] | 482 | raise GerritError(response.status, reason) |
Raphael Kubo da Costa | 89d0485 | 2017-03-23 19:04:31 +0100 | [diff] [blame] | 483 | return StringIO(contents) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 484 | |
| 485 | |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 486 | def ReadHttpJsonResponse(conn, accept_statuses=frozenset([200])): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 487 | """Parses an https response as json.""" |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 488 | fh = ReadHttpResponse(conn, accept_statuses) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 489 | # The first line of the response should always be: )]}' |
| 490 | s = fh.readline() |
| 491 | if s and s.rstrip() != ")]}'": |
| 492 | raise GerritError(200, 'Unexpected json output: %s' % s) |
| 493 | s = fh.read() |
| 494 | if not s: |
| 495 | return None |
| 496 | return json.loads(s) |
| 497 | |
| 498 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 499 | def QueryChanges(host, params, first_param=None, limit=None, o_params=None, |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 500 | start=None): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 501 | """ |
| 502 | Queries a gerrit-on-borg server for changes matching query terms. |
| 503 | |
| 504 | Args: |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 505 | params: A list of key:value pairs for search parameters, as documented |
| 506 | here (e.g. ('is', 'owner') for a parameter 'is:owner'): |
| 507 | https://gerrit-review.googlesource.com/Documentation/user-search.html#search-operators |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 508 | first_param: A change identifier |
| 509 | limit: Maximum number of results to return. |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 510 | start: how many changes to skip (starting with the most recent) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 511 | o_params: A list of additional output specifiers, as documented here: |
| 512 | https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-changes |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 513 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 514 | Returns: |
| 515 | A list of json-decoded query results. |
| 516 | """ |
| 517 | # Note that no attempt is made to escape special characters; YMMV. |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 518 | if not params and not first_param: |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 519 | raise RuntimeError('QueryChanges requires search parameters') |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 520 | path = 'changes/?q=%s' % _QueryString(params, first_param) |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 521 | if start: |
| 522 | path = '%s&start=%s' % (path, start) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 523 | if limit: |
| 524 | path = '%s&n=%d' % (path, limit) |
| 525 | if o_params: |
| 526 | path = '%s&%s' % (path, '&'.join(['o=%s' % p for p in o_params])) |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 527 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 528 | |
| 529 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 530 | def GenerateAllChanges(host, params, first_param=None, limit=500, |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 531 | o_params=None, start=None): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 532 | """Queries a gerrit-on-borg server for all the changes matching the query |
| 533 | terms. |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 534 | |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 535 | WARNING: this is unreliable if a change matching the query is modified while |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 536 | this function is being called. |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 537 | |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 538 | A single query to gerrit-on-borg is limited on the number of results by the |
| 539 | limit parameter on the request (see QueryChanges) and the server maximum |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 540 | limit. |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 541 | |
| 542 | Args: |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 543 | params, first_param: Refer to QueryChanges(). |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 544 | limit: Maximum number of requested changes per query. |
| 545 | o_params: Refer to QueryChanges(). |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 546 | start: Refer to QueryChanges(). |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 547 | |
| 548 | Returns: |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 549 | A generator object to the list of returned changes. |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 550 | """ |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 551 | already_returned = set() |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 552 | |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 553 | def at_most_once(cls): |
| 554 | for cl in cls: |
| 555 | if cl['_number'] not in already_returned: |
| 556 | already_returned.add(cl['_number']) |
| 557 | yield cl |
| 558 | |
| 559 | start = start or 0 |
| 560 | cur_start = start |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 561 | more_changes = True |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 562 | |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 563 | while more_changes: |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 564 | # This will fetch changes[start..start+limit] sorted by most recently |
| 565 | # updated. Since the rank of any change in this list can be changed any time |
| 566 | # (say user posting comment), subsequent calls may overalp like this: |
| 567 | # > initial order ABCDEFGH |
| 568 | # query[0..3] => ABC |
| 569 | # > E get's updated. New order: EABCDFGH |
| 570 | # query[3..6] => CDF # C is a dup |
| 571 | # query[6..9] => GH # E is missed. |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 572 | page = QueryChanges(host, params, first_param, limit, o_params, |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 573 | cur_start) |
| 574 | for cl in at_most_once(page): |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 575 | yield cl |
| 576 | |
| 577 | more_changes = [cl for cl in page if '_more_changes' in cl] |
| 578 | if len(more_changes) > 1: |
| 579 | raise GerritError( |
| 580 | 200, |
| 581 | 'Received %d changes with a _more_changes attribute set but should ' |
| 582 | 'receive at most one.' % len(more_changes)) |
| 583 | if more_changes: |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 584 | cur_start += len(page) |
| 585 | |
| 586 | # If we paged through, query again the first page which in most circumstances |
| 587 | # will fetch all changes that were modified while this function was run. |
| 588 | if start != cur_start: |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 589 | page = QueryChanges(host, params, first_param, limit, o_params, start) |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 590 | for cl in at_most_once(page): |
| 591 | yield cl |
deymo@chromium.org | f8be276 | 2013-11-06 01:01:59 +0000 | [diff] [blame] | 592 | |
| 593 | |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 594 | def MultiQueryChanges(host, params, change_list, limit=None, o_params=None, |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 595 | start=None): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 596 | """Initiate a query composed of multiple sets of query parameters.""" |
| 597 | if not change_list: |
| 598 | raise RuntimeError( |
| 599 | "MultiQueryChanges requires a list of change numbers/id's") |
| 600 | q = ['q=%s' % '+OR+'.join([urllib.quote(str(x)) for x in change_list])] |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 601 | if params: |
| 602 | q.append(_QueryString(params)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 603 | if limit: |
| 604 | q.append('n=%d' % limit) |
Andrii Shyshkalov | 892e9c2 | 2017-03-08 16:21:21 +0100 | [diff] [blame] | 605 | if start: |
| 606 | q.append('S=%s' % start) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 607 | if o_params: |
| 608 | q.extend(['o=%s' % p for p in o_params]) |
| 609 | path = 'changes/?%s' % '&'.join(q) |
| 610 | try: |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 611 | result = ReadHttpJsonResponse(CreateHttpConn(host, path)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 612 | except GerritError as e: |
| 613 | msg = '%s:\n%s' % (e.message, path) |
| 614 | raise GerritError(e.http_status, msg) |
| 615 | return result |
| 616 | |
| 617 | |
| 618 | def GetGerritFetchUrl(host): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 619 | """Given a Gerrit host name returns URL of a Gerrit instance to fetch from.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 620 | return '%s://%s/' % (GERRIT_PROTOCOL, host) |
| 621 | |
| 622 | |
Edward Lemur | 687ca90 | 2018-12-05 02:30:30 +0000 | [diff] [blame] | 623 | def GetCodeReviewTbrScore(host, project): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 624 | """Given a Gerrit host name and project, return the Code-Review score for TBR. |
Edward Lemur | 687ca90 | 2018-12-05 02:30:30 +0000 | [diff] [blame] | 625 | """ |
| 626 | conn = CreateHttpConn(host, '/projects/%s' % urllib.quote(project, safe='')) |
| 627 | project = ReadHttpJsonResponse(conn) |
| 628 | if ('labels' not in project |
| 629 | or 'Code-Review' not in project['labels'] |
| 630 | or 'values' not in project['labels']['Code-Review']): |
| 631 | return 1 |
| 632 | return max([int(x) for x in project['labels']['Code-Review']['values']]) |
| 633 | |
| 634 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 635 | def GetChangePageUrl(host, change_number): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 636 | """Given a Gerrit host name and change number, returns change page URL.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 637 | return '%s://%s/#/c/%d/' % (GERRIT_PROTOCOL, host, change_number) |
| 638 | |
| 639 | |
| 640 | def GetChangeUrl(host, change): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 641 | """Given a Gerrit host name and change ID, returns a URL for the change.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 642 | return '%s://%s/a/changes/%s' % (GERRIT_PROTOCOL, host, change) |
| 643 | |
| 644 | |
| 645 | def GetChange(host, change): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 646 | """Queries a Gerrit server for information about a single change.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 647 | path = 'changes/%s' % change |
| 648 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 649 | |
| 650 | |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 651 | def GetChangeDetail(host, change, o_params=None): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 652 | """Queries a Gerrit server for extended information about a single change.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 653 | path = 'changes/%s/detail' % change |
| 654 | if o_params: |
| 655 | path += '?%s' % '&'.join(['o=%s' % p for p in o_params]) |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 656 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 657 | |
| 658 | |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 659 | def GetChangeCommit(host, change, revision='current'): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 660 | """Query a Gerrit server for a revision associated with a change.""" |
agable | 32978d9 | 2016-11-01 12:55:02 -0700 | [diff] [blame] | 661 | path = 'changes/%s/revisions/%s/commit?links' % (change, revision) |
| 662 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 663 | |
| 664 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 665 | def GetChangeCurrentRevision(host, change): |
| 666 | """Get information about the latest revision for a given change.""" |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 667 | return QueryChanges(host, [], change, o_params=('CURRENT_REVISION',)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 668 | |
| 669 | |
| 670 | def GetChangeRevisions(host, change): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 671 | """Gets information about all revisions associated with a change.""" |
Michael Achenbach | 6fbf12f | 2017-07-06 10:54:11 +0200 | [diff] [blame] | 672 | return QueryChanges(host, [], change, o_params=('ALL_REVISIONS',)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 673 | |
| 674 | |
| 675 | def GetChangeReview(host, change, revision=None): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 676 | """Gets the current review information for a change.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 677 | if not revision: |
| 678 | jmsg = GetChangeRevisions(host, change) |
| 679 | if not jmsg: |
| 680 | return None |
| 681 | elif len(jmsg) > 1: |
| 682 | raise GerritError(200, 'Multiple changes found for ChangeId %s.' % change) |
| 683 | revision = jmsg[0]['current_revision'] |
| 684 | path = 'changes/%s/revisions/%s/review' |
| 685 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 686 | |
| 687 | |
Aaron Gable | 0ffdf2d | 2017-06-05 13:01:17 -0700 | [diff] [blame] | 688 | def GetChangeComments(host, change): |
| 689 | """Get the line- and file-level comments on a change.""" |
| 690 | path = 'changes/%s/comments' % change |
| 691 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 692 | |
| 693 | |
Quinten Yearsley | 0e617c0 | 2019-02-20 00:37:03 +0000 | [diff] [blame] | 694 | def GetChangeRobotComments(host, change): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 695 | """Gets the line- and file-level robot comments on a change.""" |
Quinten Yearsley | 0e617c0 | 2019-02-20 00:37:03 +0000 | [diff] [blame] | 696 | path = 'changes/%s/robotcomments' % change |
| 697 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 698 | |
| 699 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 700 | def AbandonChange(host, change, msg=''): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 701 | """Abandons a Gerrit change.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 702 | path = 'changes/%s/abandon' % change |
tandrii@chromium.org | c7da66a | 2016-03-24 09:52:24 +0000 | [diff] [blame] | 703 | body = {'message': msg} if msg else {} |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 704 | conn = CreateHttpConn(host, path, reqtype='POST', body=body) |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 705 | return ReadHttpJsonResponse(conn) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 706 | |
| 707 | |
| 708 | def RestoreChange(host, change, msg=''): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 709 | """Restores a previously abandoned change.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 710 | path = 'changes/%s/restore' % change |
tandrii@chromium.org | c7da66a | 2016-03-24 09:52:24 +0000 | [diff] [blame] | 711 | body = {'message': msg} if msg else {} |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 712 | conn = CreateHttpConn(host, path, reqtype='POST', body=body) |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 713 | return ReadHttpJsonResponse(conn) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 714 | |
| 715 | |
| 716 | def SubmitChange(host, change, wait_for_merge=True): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 717 | """Submits a Gerrit change via Gerrit.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 718 | path = 'changes/%s/submit' % change |
| 719 | body = {'wait_for_merge': wait_for_merge} |
| 720 | conn = CreateHttpConn(host, path, reqtype='POST', body=body) |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 721 | return ReadHttpJsonResponse(conn) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 722 | |
| 723 | |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 724 | def HasPendingChangeEdit(host, change): |
| 725 | conn = CreateHttpConn(host, 'changes/%s/edit' % change) |
| 726 | try: |
Aaron Gable | 6f5a8d9 | 2017-04-18 14:49:05 -0700 | [diff] [blame] | 727 | ReadHttpResponse(conn) |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 728 | except GerritError as e: |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 729 | # 204 No Content means no pending change. |
| 730 | if e.http_status == 204: |
| 731 | return False |
| 732 | raise |
| 733 | return True |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 734 | |
| 735 | |
| 736 | def DeletePendingChangeEdit(host, change): |
| 737 | conn = CreateHttpConn(host, 'changes/%s/edit' % change, reqtype='DELETE') |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 738 | # On success, Gerrit returns status 204; if the edit was already deleted it |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 739 | # returns 404. Anything else is an error. |
| 740 | ReadHttpResponse(conn, accept_statuses=[204, 404]) |
dsansome | e2d6fd9 | 2016-09-08 00:10:47 -0700 | [diff] [blame] | 741 | |
| 742 | |
Andrii Shyshkalov | ea4fc83 | 2016-12-01 14:53:23 +0100 | [diff] [blame] | 743 | def SetCommitMessage(host, change, description, notify='ALL'): |
scottmg@chromium.org | 6d1266e | 2016-04-26 11:12:26 +0000 | [diff] [blame] | 744 | """Updates a commit message.""" |
Aaron Gable | 7625d88 | 2017-06-26 09:47:26 -0700 | [diff] [blame] | 745 | assert notify in ('ALL', 'NONE') |
| 746 | path = 'changes/%s/message' % change |
Aaron Gable | 5a4ef45 | 2017-08-24 13:19:56 -0700 | [diff] [blame] | 747 | body = {'message': description, 'notify': notify} |
Aaron Gable | 7625d88 | 2017-06-26 09:47:26 -0700 | [diff] [blame] | 748 | conn = CreateHttpConn(host, path, reqtype='PUT', body=body) |
scottmg@chromium.org | 6d1266e | 2016-04-26 11:12:26 +0000 | [diff] [blame] | 749 | try: |
Aaron Gable | 7625d88 | 2017-06-26 09:47:26 -0700 | [diff] [blame] | 750 | ReadHttpResponse(conn, accept_statuses=[200, 204]) |
| 751 | except GerritError as e: |
| 752 | raise GerritError( |
| 753 | e.http_status, |
| 754 | 'Received unexpected http status while editing message ' |
| 755 | 'in change %s' % change) |
scottmg@chromium.org | 6d1266e | 2016-04-26 11:12:26 +0000 | [diff] [blame] | 756 | |
| 757 | |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 758 | def GetReviewers(host, change): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 759 | """Gets information about all reviewers attached to a change.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 760 | path = 'changes/%s/reviewers' % change |
| 761 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 762 | |
| 763 | |
| 764 | def GetReview(host, change, revision): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 765 | """Gets review information about a specific revision of a change.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 766 | path = 'changes/%s/revisions/%s/review' % (change, revision) |
| 767 | return ReadHttpJsonResponse(CreateHttpConn(host, path)) |
| 768 | |
| 769 | |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 770 | def AddReviewers(host, change, reviewers=None, ccs=None, notify=True, |
| 771 | accept_statuses=frozenset([200, 400, 422])): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 772 | """Add reviewers to a change.""" |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 773 | if not reviewers and not ccs: |
Aaron Gable | df86e30 | 2016-11-08 10:48:03 -0800 | [diff] [blame] | 774 | return None |
Wiktor Garbacz | 6d0d044 | 2017-05-15 12:34:40 +0200 | [diff] [blame] | 775 | if not change: |
| 776 | return None |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 777 | reviewers = frozenset(reviewers or []) |
| 778 | ccs = frozenset(ccs or []) |
| 779 | path = 'changes/%s/revisions/current/review' % change |
| 780 | |
| 781 | body = { |
Jonathan Nieder | 1ea2132 | 2017-11-10 11:45:42 -0800 | [diff] [blame] | 782 | 'drafts': 'KEEP', |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 783 | 'reviewers': [], |
| 784 | 'notify': 'ALL' if notify else 'NONE', |
| 785 | } |
| 786 | for r in sorted(reviewers | ccs): |
| 787 | state = 'REVIEWER' if r in reviewers else 'CC' |
| 788 | body['reviewers'].append({ |
| 789 | 'reviewer': r, |
| 790 | 'state': state, |
| 791 | 'notify': 'NONE', # We handled `notify` argument above. |
Raul Tambre | 80ee78e | 2019-05-06 22:41:05 +0000 | [diff] [blame] | 792 | }) |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 793 | |
| 794 | conn = CreateHttpConn(host, path, reqtype='POST', body=body) |
| 795 | # Gerrit will return 400 if one or more of the requested reviewers are |
| 796 | # unprocessable. We read the response object to see which were rejected, |
| 797 | # warn about them, and retry with the remainder. |
| 798 | resp = ReadHttpJsonResponse(conn, accept_statuses=accept_statuses) |
| 799 | |
| 800 | errored = set() |
Marc-Antoine Ruel | 8e57b4b | 2019-10-11 01:01:36 +0000 | [diff] [blame] | 801 | for result in resp.get('reviewers', {}).values(): |
Aaron Gable | 6dadfbf | 2017-05-09 14:27:58 -0700 | [diff] [blame] | 802 | r = result.get('input') |
| 803 | state = 'REVIEWER' if r in reviewers else 'CC' |
| 804 | if result.get('error'): |
| 805 | errored.add(r) |
| 806 | LOGGER.warn('Note: "%s" not added as a %s' % (r, state.lower())) |
| 807 | if errored: |
| 808 | # Try again, adding only those that didn't fail, and only accepting 200. |
| 809 | AddReviewers(host, change, reviewers=(reviewers-errored), |
| 810 | ccs=(ccs-errored), notify=notify, accept_statuses=[200]) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 811 | |
| 812 | |
| 813 | def RemoveReviewers(host, change, remove=None): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 814 | """Removes reviewers from a change.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 815 | if not remove: |
| 816 | return |
| 817 | if isinstance(remove, basestring): |
| 818 | remove = (remove,) |
| 819 | for r in remove: |
| 820 | path = 'changes/%s/reviewers/%s' % (change, r) |
| 821 | conn = CreateHttpConn(host, path, reqtype='DELETE') |
| 822 | try: |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 823 | ReadHttpResponse(conn, accept_statuses=[204]) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 824 | except GerritError as e: |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 825 | raise GerritError( |
Aaron Gable | 19ee16c | 2017-04-18 11:56:35 -0700 | [diff] [blame] | 826 | e.http_status, |
| 827 | 'Received unexpected http status while deleting reviewer "%s" ' |
| 828 | 'from change %s' % (r, change)) |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 829 | |
| 830 | |
Aaron Gable | 636b13f | 2017-07-14 10:42:48 -0700 | [diff] [blame] | 831 | def SetReview(host, change, msg=None, labels=None, notify=None, ready=None): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 832 | """Sets labels and/or adds a message to a code review.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 833 | if not msg and not labels: |
| 834 | return |
| 835 | path = 'changes/%s/revisions/current/review' % change |
Jonathan Nieder | 1ea2132 | 2017-11-10 11:45:42 -0800 | [diff] [blame] | 836 | body = {'drafts': 'KEEP'} |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 837 | if msg: |
| 838 | body['message'] = msg |
| 839 | if labels: |
| 840 | body['labels'] = labels |
Aaron Gable | fc62f76 | 2017-07-17 11:12:07 -0700 | [diff] [blame] | 841 | if notify is not None: |
Aaron Gable | 75e7872 | 2017-06-09 10:40:16 -0700 | [diff] [blame] | 842 | body['notify'] = 'ALL' if notify else 'NONE' |
Aaron Gable | 636b13f | 2017-07-14 10:42:48 -0700 | [diff] [blame] | 843 | if ready: |
| 844 | body['ready'] = True |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 845 | conn = CreateHttpConn(host, path, reqtype='POST', body=body) |
| 846 | response = ReadHttpJsonResponse(conn) |
| 847 | if labels: |
Marc-Antoine Ruel | 8e57b4b | 2019-10-11 01:01:36 +0000 | [diff] [blame] | 848 | for key, val in labels.items(): |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 849 | if ('labels' not in response or key not in response['labels'] or |
| 850 | int(response['labels'][key] != int(val))): |
| 851 | raise GerritError(200, 'Unable to set "%s" label on change %s.' % ( |
| 852 | key, change)) |
| 853 | |
| 854 | |
| 855 | def ResetReviewLabels(host, change, label, value='0', message=None, |
| 856 | notify=None): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 857 | """Resets the value of a given label for all reviewers on a change.""" |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 858 | # This is tricky, because we want to work on the "current revision", but |
| 859 | # there's always the risk that "current revision" will change in between |
| 860 | # API calls. So, we check "current revision" at the beginning and end; if |
| 861 | # it has changed, raise an exception. |
| 862 | jmsg = GetChangeCurrentRevision(host, change) |
| 863 | if not jmsg: |
| 864 | raise GerritError( |
| 865 | 200, 'Could not get review information for change "%s"' % change) |
| 866 | value = str(value) |
| 867 | revision = jmsg[0]['current_revision'] |
| 868 | path = 'changes/%s/revisions/%s/review' % (change, revision) |
| 869 | message = message or ( |
| 870 | '%s label set to %s programmatically.' % (label, value)) |
| 871 | jmsg = GetReview(host, change, revision) |
| 872 | if not jmsg: |
| 873 | raise GerritError(200, 'Could not get review information for revison %s ' |
| 874 | 'of change %s' % (revision, change)) |
| 875 | for review in jmsg.get('labels', {}).get(label, {}).get('all', []): |
| 876 | if str(review.get('value', value)) != value: |
| 877 | body = { |
Jonathan Nieder | 1ea2132 | 2017-11-10 11:45:42 -0800 | [diff] [blame] | 878 | 'drafts': 'KEEP', |
szager@chromium.org | b469623 | 2013-10-16 19:45:35 +0000 | [diff] [blame] | 879 | 'message': message, |
| 880 | 'labels': {label: value}, |
| 881 | 'on_behalf_of': review['_account_id'], |
| 882 | } |
| 883 | if notify: |
| 884 | body['notify'] = notify |
| 885 | conn = CreateHttpConn( |
| 886 | host, path, reqtype='POST', body=body) |
| 887 | response = ReadHttpJsonResponse(conn) |
| 888 | if str(response['labels'][label]) != value: |
| 889 | username = review.get('email', jmsg.get('name', '')) |
| 890 | raise GerritError(200, 'Unable to set %s label for user "%s"' |
| 891 | ' on change %s.' % (label, username, change)) |
| 892 | jmsg = GetChangeCurrentRevision(host, change) |
| 893 | if not jmsg: |
| 894 | raise GerritError( |
| 895 | 200, 'Could not get review information for change "%s"' % change) |
| 896 | elif jmsg[0]['current_revision'] != revision: |
| 897 | raise GerritError(200, 'While resetting labels on change "%s", ' |
| 898 | 'a new patchset was uploaded.' % change) |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 899 | |
| 900 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 901 | def CreateGerritBranch(host, project, branch, commit): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 902 | """Creates a new branch from given project and commit |
| 903 | |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 904 | https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#create-branch |
| 905 | |
| 906 | Returns: |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 907 | A JSON object with 'ref' key. |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 908 | """ |
| 909 | path = 'projects/%s/branches/%s' % (project, branch) |
| 910 | body = {'revision': commit} |
| 911 | conn = CreateHttpConn(host, path, reqtype='PUT', body=body) |
dimu | 7d1af2b | 2017-04-19 16:01:17 -0700 | [diff] [blame] | 912 | response = ReadHttpJsonResponse(conn, accept_statuses=[201]) |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 913 | if response: |
| 914 | return response |
| 915 | raise GerritError(200, 'Unable to create gerrit branch') |
| 916 | |
| 917 | |
| 918 | def GetGerritBranch(host, project, branch): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 919 | """Gets a branch from given project and commit. |
| 920 | |
| 921 | See: |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 922 | https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#get-branch |
| 923 | |
| 924 | Returns: |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 925 | A JSON object with 'revision' key. |
dimu | 833c94c | 2017-01-18 17:36:15 -0800 | [diff] [blame] | 926 | """ |
| 927 | path = 'projects/%s/branches/%s' % (project, branch) |
| 928 | conn = CreateHttpConn(host, path, reqtype='GET') |
| 929 | response = ReadHttpJsonResponse(conn) |
| 930 | if response: |
| 931 | return response |
| 932 | raise GerritError(200, 'Unable to get gerrit branch') |
| 933 | |
| 934 | |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 935 | def GetAccountDetails(host, account_id='self'): |
| 936 | """Returns details of the account. |
| 937 | |
| 938 | If account_id is not given, uses magic value 'self' which corresponds to |
| 939 | whichever account user is authenticating as. |
| 940 | |
| 941 | Documentation: |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 942 | https://gerrit-review.googlesource.com/Documentation/rest-api-accounts.html#get-account |
Andrii Shyshkalov | ba7b0a4 | 2018-10-15 03:20:35 +0000 | [diff] [blame] | 943 | |
| 944 | Returns None if account is not found (i.e., Gerrit returned 404). |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 945 | """ |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 946 | conn = CreateHttpConn(host, '/accounts/%s' % account_id) |
Andrii Shyshkalov | ba7b0a4 | 2018-10-15 03:20:35 +0000 | [diff] [blame] | 947 | return ReadHttpJsonResponse(conn, accept_statuses=[200, 404]) |
| 948 | |
| 949 | |
| 950 | def ValidAccounts(host, accounts, max_threads=10): |
| 951 | """Returns a mapping from valid account to its details. |
| 952 | |
| 953 | Invalid accounts, either not existing or without unique match, |
| 954 | are not present as returned dictionary keys. |
| 955 | """ |
| 956 | assert not isinstance(accounts, basestring), type(accounts) |
| 957 | accounts = list(set(accounts)) |
| 958 | if not accounts: |
| 959 | return {} |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 960 | |
Andrii Shyshkalov | ba7b0a4 | 2018-10-15 03:20:35 +0000 | [diff] [blame] | 961 | def get_one(account): |
| 962 | try: |
| 963 | return account, GetAccountDetails(host, account) |
| 964 | except GerritError: |
| 965 | return None, None |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 966 | |
Andrii Shyshkalov | ba7b0a4 | 2018-10-15 03:20:35 +0000 | [diff] [blame] | 967 | valid = {} |
| 968 | with contextlib.closing(ThreadPool(min(max_threads, len(accounts)))) as pool: |
| 969 | for account, details in pool.map(get_one, accounts): |
| 970 | if account and details: |
| 971 | valid[account] = details |
| 972 | return valid |
Andrii Shyshkalov | bb86fbb | 2017-03-24 14:59:28 +0100 | [diff] [blame] | 973 | |
| 974 | |
Nick Carter | 8692b18 | 2017-11-06 16:30:38 -0800 | [diff] [blame] | 975 | def PercentEncodeForGitRef(original): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 976 | """Applies percent-encoding for strings sent to Gerrit via git ref metadata. |
Nick Carter | 8692b18 | 2017-11-06 16:30:38 -0800 | [diff] [blame] | 977 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 978 | The encoding used is based on but stricter than URL encoding (Section 2.1 of |
| 979 | RFC 3986). The only non-escaped characters are alphanumerics, and 'SPACE' |
| 980 | (U+0020) can be represented as 'LOW LINE' (U+005F) or 'PLUS SIGN' (U+002B). |
Nick Carter | 8692b18 | 2017-11-06 16:30:38 -0800 | [diff] [blame] | 981 | |
| 982 | For more information, see the Gerrit docs here: |
| 983 | |
| 984 | https://gerrit-review.googlesource.com/Documentation/user-upload.html#message |
| 985 | """ |
| 986 | safe = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ' |
| 987 | encoded = ''.join(c if c in safe else '%%%02X' % ord(c) for c in original) |
| 988 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 989 | # Spaces are not allowed in git refs; gerrit will interpret either '_' or |
Nick Carter | 8692b18 | 2017-11-06 16:30:38 -0800 | [diff] [blame] | 990 | # '+' (or '%20') as space. Use '_' since that has been supported the longest. |
| 991 | return encoded.replace(' ', '_') |
| 992 | |
| 993 | |
Dan Jacques | 8d11e48 | 2016-11-15 14:25:56 -0800 | [diff] [blame] | 994 | @contextlib.contextmanager |
| 995 | def tempdir(): |
| 996 | tdir = None |
| 997 | try: |
| 998 | tdir = tempfile.mkdtemp(suffix='gerrit_util') |
| 999 | yield tdir |
| 1000 | finally: |
| 1001 | if tdir: |
| 1002 | gclient_utils.rmtree(tdir) |
Andrii Shyshkalov | 0ec9d15 | 2018-08-23 00:22:58 +0000 | [diff] [blame] | 1003 | |
| 1004 | |
| 1005 | def ChangeIdentifier(project, change_number): |
Edward Lemur | 687ca90 | 2018-12-05 02:30:30 +0000 | [diff] [blame] | 1006 | """Returns change identifier "project~number" suitable for |change| arg of |
Andrii Shyshkalov | 0ec9d15 | 2018-08-23 00:22:58 +0000 | [diff] [blame] | 1007 | this module API. |
| 1008 | |
| 1009 | Such format is allows for more efficient Gerrit routing of HTTP requests, |
| 1010 | comparing to specifying just change_number. |
| 1011 | """ |
| 1012 | assert int(change_number) |
| 1013 | return '%s~%s' % (urllib.quote(project, safe=''), change_number) |
Andrii Shyshkalov | d4c8673 | 2018-09-25 04:29:31 +0000 | [diff] [blame] | 1014 | |
| 1015 | |
| 1016 | # TODO(crbug/881860): remove this hack. |
Andrii Shyshkalov | 94faf32 | 2018-10-12 21:35:38 +0000 | [diff] [blame] | 1017 | _GERRIT_MIRROR_PREFIXES = ['us1', 'us2', 'us3', 'eu1'] |
Andrii Shyshkalov | d4c8673 | 2018-09-25 04:29:31 +0000 | [diff] [blame] | 1018 | assert all(3 == len(p) for p in _GERRIT_MIRROR_PREFIXES) |
| 1019 | |
| 1020 | |
| 1021 | def _UseGerritMirror(url, host): |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 1022 | """Returns a new URL which uses randomly selected mirror for a Gerrit host. |
Andrii Shyshkalov | d4c8673 | 2018-09-25 04:29:31 +0000 | [diff] [blame] | 1023 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 1024 | The URL's host should be for a given host or a result of prior call to this |
Andrii Shyshkalov | d4c8673 | 2018-09-25 04:29:31 +0000 | [diff] [blame] | 1025 | function. |
| 1026 | |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 1027 | Assumes that the URL has a single occurence of the host substring. |
Andrii Shyshkalov | d4c8673 | 2018-09-25 04:29:31 +0000 | [diff] [blame] | 1028 | """ |
| 1029 | assert host in url |
| 1030 | suffix = '-mirror-' + host |
| 1031 | prefixes = set(_GERRIT_MIRROR_PREFIXES) |
| 1032 | prefix_len = len(_GERRIT_MIRROR_PREFIXES[0]) |
| 1033 | st = url.find(suffix) |
| 1034 | if st == -1: |
| 1035 | actual_host = host |
| 1036 | else: |
| 1037 | # Already uses some mirror. |
Quinten Yearsley | d9cbe7a | 2019-09-03 16:49:11 +0000 | [diff] [blame] | 1038 | assert st >= prefix_len, (url, host, st, prefix_len) |
Andrii Shyshkalov | d4c8673 | 2018-09-25 04:29:31 +0000 | [diff] [blame] | 1039 | prefixes.remove(url[st-prefix_len:st]) |
| 1040 | actual_host = url[st-prefix_len:st+len(suffix)] |
| 1041 | return url.replace(actual_host, random.choice(list(prefixes)) + suffix) |