OLD | NEW |
1 # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """ | 5 """ |
6 Utilities for requesting information for a gerrit server via https. | 6 Utilities for requesting information for a gerrit server via https. |
7 | 7 |
8 https://gerrit-review.googlesource.com/Documentation/rest-api.html | 8 https://gerrit-review.googlesource.com/Documentation/rest-api.html |
9 """ | 9 """ |
10 | 10 |
(...skipping 47 matching lines...) Loading... |
58 else: | 58 else: |
59 raise RuntimeError( | 59 raise RuntimeError( |
60 "Don't know how to work with protocol '%s'" % protocol) | 60 "Don't know how to work with protocol '%s'" % protocol) |
61 | 61 |
62 | 62 |
63 def CreateHttpConn(host, path, reqtype='GET', headers=None, body=None): | 63 def CreateHttpConn(host, path, reqtype='GET', headers=None, body=None): |
64 """Opens an https connection to a gerrit service, and sends a request.""" | 64 """Opens an https connection to a gerrit service, and sends a request.""" |
65 headers = headers or {} | 65 headers = headers or {} |
66 bare_host = host.partition(':')[0] | 66 bare_host = host.partition(':')[0] |
67 auth = NETRC.authenticators(bare_host) | 67 auth = NETRC.authenticators(bare_host) |
| 68 review_auth = NETRC.authenticators(bare_host.replace('-review', '')) |
| 69 |
68 if auth: | 70 if auth: |
69 headers.setdefault('Authorization', 'Basic %s' % ( | 71 headers.setdefault('Authorization', 'Basic %s' % ( |
70 base64.b64encode('%s:%s' % (auth[0], auth[2])))) | 72 base64.b64encode('%s:%s' % (auth[0], auth[2])))) |
| 73 url = '/a/%s' % path |
| 74 elif review_auth: |
| 75 headers.setdefault('Authorization', 'Basic %s' % ( |
| 76 base64.b64encode('%s:%s' % (review_auth[0], review_auth[2])))) |
| 77 url = '/a/%s' % path |
71 else: | 78 else: |
72 LOGGER.debug('No authorization found') | 79 LOGGER.debug('No authorization found') |
| 80 url = '/%s' % path |
73 if body: | 81 if body: |
74 body = json.JSONEncoder().encode(body) | 82 body = json.JSONEncoder().encode(body) |
75 headers.setdefault('Content-Type', 'application/json') | 83 headers.setdefault('Content-Type', 'application/json') |
76 if LOGGER.isEnabledFor(logging.DEBUG): | 84 if LOGGER.isEnabledFor(logging.DEBUG): |
77 LOGGER.debug('%s %s://%s/a/%s' % (reqtype, GERRIT_PROTOCOL, host, path)) | 85 LOGGER.debug('%s %s://%s/a/%s' % (reqtype, GERRIT_PROTOCOL, host, path)) |
78 for key, val in headers.iteritems(): | 86 for key, val in headers.iteritems(): |
79 if key == 'Authorization': | 87 if key == 'Authorization': |
80 val = 'HIDDEN' | 88 val = 'HIDDEN' |
81 LOGGER.debug('%s: %s' % (key, val)) | 89 LOGGER.debug('%s: %s' % (key, val)) |
82 if body: | 90 if body: |
83 LOGGER.debug(body) | 91 LOGGER.debug(body) |
84 conn = GetConnectionClass()(host) | 92 conn = GetConnectionClass()(host) |
85 conn.req_host = host | 93 conn.req_host = host |
86 conn.req_params = { | 94 conn.req_params = { |
87 'url': '/a/%s' % path, | 95 'url': url, |
88 'method': reqtype, | 96 'method': reqtype, |
89 'headers': headers, | 97 'headers': headers, |
90 'body': body, | 98 'body': body, |
91 } | 99 } |
92 conn.request(**conn.req_params) | 100 conn.request(**conn.req_params) |
93 return conn | 101 return conn |
94 | 102 |
95 | 103 |
96 def ReadHttpResponse(conn, expect_status=200, ignore_404=True): | 104 def ReadHttpResponse(conn, expect_status=200, ignore_404=True): |
97 """Reads an http response from a connection into a string buffer. | 105 """Reads an http response from a connection into a string buffer. |
(...skipping 287 matching lines...) Loading... |
385 username = review.get('email', jmsg.get('name', '')) | 393 username = review.get('email', jmsg.get('name', '')) |
386 raise GerritError(200, 'Unable to set %s label for user "%s"' | 394 raise GerritError(200, 'Unable to set %s label for user "%s"' |
387 ' on change %s.' % (label, username, change)) | 395 ' on change %s.' % (label, username, change)) |
388 jmsg = GetChangeCurrentRevision(host, change) | 396 jmsg = GetChangeCurrentRevision(host, change) |
389 if not jmsg: | 397 if not jmsg: |
390 raise GerritError( | 398 raise GerritError( |
391 200, 'Could not get review information for change "%s"' % change) | 399 200, 'Could not get review information for change "%s"' % change) |
392 elif jmsg[0]['current_revision'] != revision: | 400 elif jmsg[0]['current_revision'] != revision: |
393 raise GerritError(200, 'While resetting labels on change "%s", ' | 401 raise GerritError(200, 'While resetting labels on change "%s", ' |
394 'a new patchset was uploaded.' % change) | 402 'a new patchset was uploaded.' % change) |
OLD | NEW |