-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgerrit_rest.py
55 lines (45 loc) · 2.04 KB
/
gerrit_rest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json
import requests
requests.adapters.DEFAULT_RETRIES = 5
class GerritREST(object):
def __init__(self, url):
""" Basic wrapper around the Gerrit REST API. Takes care of
connections and JSON-decoding. Currently only GET requests
are supported.
Parameters:
* URL - The base URL, e.g. https://gerrit.wikimedia.org/r
"""
self._url = url.rstrip('/')
self._session = requests.Session()
self._session.headers.update({
'Accept': 'application/json',
'User-Agent': 'Gerrit-Reviewer-Bot GerritREST python-requests/%s' % (requests.__version__, )
})
def _request(self, name, **kwargs):
""" Make a request. Parameters:
* name - The name of the REST endpoint. This will be appended to the base URL.
* any parameters taken by the REST endpoint (via kwargs)
"""
r = self._session.get(self._url + '/%s/' % name, params=kwargs)
realjson = r.text[5:] # strips anti-XSS prefix
return json.loads(realjson)
def __getattr__(self, name):
""" Provides access to any APIs not yet implemented """
def wrapper(self, **kwargs):
return self._request(name, **kwargs)
wrapper.__name__ = name
return wrapper
def changes(self, q="", n=25, o=[]):
""" Submits a request to the /changes/ REST API. Parameters:
* q - the query string,
* n - the maximum number of results to return - 25 by default,
* o - the list of options to pass. CURRENT_REVISION and CURRENT_FILES by default.
See https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html
for more details. """
return self._request('changes', q=q, n=n, o=o)
def get_changeset(self, changeid, o=['CURRENT_REVISION', 'CURRENT_FILES', 'DETAILED_ACCOUNTS']):
matchingchanges = self.changes(changeid, n=1, o=o)
if matchingchanges:
return matchingchanges[0]
else:
return None