Skip to content

Commit fc733f9

Browse files
committed
Take advantage of features from API v1.1
This allows us to avoid making multiple queries for a given operation. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent c3e8758 commit fc733f9

File tree

5 files changed

+72
-45
lines changed

5 files changed

+72
-45
lines changed

git_pw/api.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
if 0: # noqa
1515
from typing import Dict # noqa
1616
from typing import List # noqa
17+
from typing import Optional # noqa
1718
from typing import Tuple # noqa
1819

1920
Filters = List[Tuple[str, str]]
@@ -116,6 +117,19 @@ def _handle_error(operation, exc):
116117
sys.exit(1)
117118

118119

120+
def version():
121+
# type: () -> Optional[Tuple[int, int]]
122+
"""Get the version of the server from the URL, if present."""
123+
server = _get_server()
124+
125+
version = re.match(r'.*/(\d)\.(\d)$', server)
126+
if version:
127+
return (int(version.group(1)), int(version.group(2)))
128+
129+
# return the oldest version we support if no version provided
130+
return (1, 0)
131+
132+
119133
def get(url, params=None, stream=False):
120134
# type: (str, Filters, bool) -> requests.Response
121135
"""Make GET request and handle errors."""

git_pw/bundle.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def _format_patch(patch):
9595
output = [
9696
('ID', bundle.get('id')),
9797
('Name', bundle.get('name')),
98+
('URL', bundle.get('web_url')),
9899
('Owner', bundle.get('owner').get('username')),
99100
('Project', bundle.get('project').get('name')),
100101
('Public', bundle.get('public'))]
@@ -131,17 +132,19 @@ def list_cmd(owner, limit, page, sort, name):
131132

132133
params = []
133134

134-
# TODO(stephenfin): It should be possible to filter bundles by owner email
135-
for own in owner:
136-
users = api.index('users', [('q', own)])
137-
if len(users) == 0:
138-
LOG.error('No matching owner found: %s', own)
139-
sys.exit(1)
140-
elif len(users) > 1:
141-
LOG.error('More than one owner found: %s', own)
142-
sys.exit(1)
143-
144-
params.append(('owner', users[0]['id']))
135+
if api.version() >= (1, 1):
136+
params.extend([('owner', own) for own in owner])
137+
else:
138+
for own in owner:
139+
users = api.index('users', [('q', own)])
140+
if len(users) == 0:
141+
LOG.error('No matching owner found: %s', own)
142+
sys.exit(1)
143+
elif len(users) > 1:
144+
LOG.error('More than one owner found: %s', own)
145+
sys.exit(1)
146+
147+
params.append(('owner', users[0]['id']))
145148

146149
params.extend([
147150
('q', name),

git_pw/patch.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def _format_series(series):
9595
('Message ID', patch.get('msgid')),
9696
('Date', patch.get('date')),
9797
('Name', patch.get('name')),
98+
('URL', patch.get('web_url')),
9899
('Submitter', '%s (%s)' % (patch.get('submitter').get('name'),
99100
patch.get('submitter').get('email'))),
100101
('State', patch.get('state')),
@@ -220,28 +221,31 @@ def list_cmd(state, submitter, delegate, archived, limit, page, sort, name):
220221
for state in state:
221222
params.append(('state', state))
222223

223-
# TODO(stephenfin): It should be possible to filter patches submitter email
224-
for subm in submitter:
225-
people = api.index('people', [('q', subm)])
226-
if len(people) == 0:
227-
LOG.error('No matching submitter found: %s', subm)
228-
sys.exit(1)
229-
elif len(people) > 1:
230-
LOG.error('More than one submitter found: %s', subm)
231-
sys.exit(1)
232-
233-
params.append(('submitter', people[0]['id']))
234-
235-
for delg in delegate:
236-
users = api.index('users', [('q', delg)])
237-
if len(users) == 0:
238-
LOG.error('No matching delegates found: %s', delg)
239-
sys.exit(1)
240-
elif len(users) > 1:
241-
LOG.error('More than one delegate found: %s', delg)
242-
sys.exit(1)
243-
244-
params.append(('delegate', users[0]['id']))
224+
if api.version() >= (1, 1):
225+
params.extend([('submitter', subm) for subm in submitter])
226+
params.extend([('delegate', delg) for delg in delegate])
227+
else:
228+
for subm in submitter:
229+
people = api.index('people', [('q', subm)])
230+
if len(people) == 0:
231+
LOG.error('No matching submitter found: %s', subm)
232+
sys.exit(1)
233+
elif len(people) > 1:
234+
LOG.error('More than one submitter found: %s', subm)
235+
sys.exit(1)
236+
237+
params.append(('submitter', people[0]['id']))
238+
239+
for delg in delegate:
240+
users = api.index('users', [('q', delg)])
241+
if len(users) == 0:
242+
LOG.error('No matching delegates found: %s', delg)
243+
sys.exit(1)
244+
elif len(users) > 1:
245+
LOG.error('More than one delegate found: %s', delg)
246+
sys.exit(1)
247+
248+
params.append(('delegate', users[0]['id']))
245249

246250
params.extend([
247251
('q', name),

git_pw/series.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def _format_submission(submission):
7676
('ID', series.get('id')),
7777
('Date', series.get('date')),
7878
('Name', series.get('name')),
79+
('URL', series.get('web_url')),
7980
('Submitter', '%s (%s)' % (series.get('submitter').get('name'),
8081
series.get('submitter').get('email'))),
8182
('Project', series.get('project').get('name')),
@@ -118,18 +119,19 @@ def list_cmd(submitter, limit, page, sort, name):
118119

119120
params = []
120121

121-
# TODO(stephenfin): It should be possible to filter series by submitter
122-
# email
123-
for subm in submitter:
124-
people = api.index('people', [('q', subm)])
125-
if len(people) == 0:
126-
LOG.error('No matching submitter found: %s', subm)
127-
sys.exit(1)
128-
elif len(people) > 1:
129-
LOG.error('More than one submitter found: %s', subm)
130-
sys.exit(1)
131-
132-
params.append(('submitter', people[0]['id']))
122+
if api.version() >= (1, 1):
123+
params.extend([('submitter', subm) for subm in submitter])
124+
else:
125+
for subm in submitter:
126+
people = api.index('people', [('q', subm)])
127+
if len(people) == 0:
128+
LOG.error('No matching submitter found: %s', subm)
129+
sys.exit(1)
130+
elif len(people) > 1:
131+
LOG.error('More than one submitter found: %s', subm)
132+
sys.exit(1)
133+
134+
params.append(('submitter', people[0]['id']))
133135

134136
params.extend([
135137
('q', name),
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- |
4+
Patchwork API v1.1 is now supported.

0 commit comments

Comments
 (0)