-
Notifications
You must be signed in to change notification settings - Fork 3
/
reviews.py
521 lines (414 loc) · 16 KB
/
reviews.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
from typing import Callable, Dict, List, Optional, Tuple, Union
from helixswarm.exceptions import SwarmCompatibleError, SwarmError
from helixswarm.helpers import minimal_version
class Reviews:
def __init__(self, swarm):
self.swarm = swarm
def get(self,
*,
after: Optional[int] = None,
limit: Optional[int] = None,
fields: Optional[List[str]] = None,
authors: Optional[List[str]] = None,
changes: Optional[List[int]] = None,
has_reviewers: Optional[bool] = None,
ids: Optional[List[int]] = None,
keywords: Optional[str] = None,
participants: Optional[List[str]] = None,
projects: Optional[List[str]] = None,
states: Optional[List[str]] = None,
passes_tests: Optional[bool] = None,
not_updated_since: Optional[str] = None,
has_voted: Optional[str] = None,
my_comments: Optional[bool] = None
) -> dict:
"""
Get list of available reviews.
* after: ``int`` (optional)
A review ID to seek to. Reviews up to and including the specified ``id``
are excluded from the results and do not count towards ``limit``. Useful
for pagination. Commonly set to the ``lastSeen`` property from a previous
query.
* limit: ``int`` (optional)
Maximum number of reviews to return. This does not guarantee that ``limit``
reviews are returned. It does guarantee that the number of reviews
returned won’t exceed ``limit``. Server-side filtering may exclude some
reviews for permissions reasons. Default: 1000
* fields: ``List[str]`` (optional)
Fields to show, Omitting this parameter or passing an empty value
shows all fields.
* author: ``List[str]`` (optional)
One or more authors to limit reviews by.
Reviews with any of the specified authors are returned. (**API v1.2+**)
* changes: ``List[str]`` (optional)
One or more change IDs to limit reviews by.
Reviews associated with any of the specified changes are returned.
* has_reviewers: ``bool`` (optional)
Limit reviews list to those with or without reviewers.
* ids: ``List[int]`` (optional)
One or more review IDs to fetch. Only the specified reviews are returned.
This filter cannot be combined with the ``limit`` parameter.
* keywords: ``str`` (optional)
Keywords to limit reviews by. Only reviews where the description,
participants list or project list contain the specified keywords are returned.
* participants: ``List[str]`` (optional)
One or more participants to limit reviews by.
Reviews with any of the specified participants are returned.
* projects: ``List[str]`` (optional)
One or more projects to limit reviews by. Reviews affecting any of the
specified projects are returned.
* states: ``List[str]`` (optional)
One or more states to limit reviews by. Reviews in any of the specified
states are returned.
* passes_tests: ``bool`` (optional)
Option to limit reviews by tests passing or failing.
* not_updated_since: ``str`` (optional)
Option to fetch unchanged reviews. Requires the date to be in the format
YYYY-mm-dd, for example 2017-01-01. Reviews to be returned are determined
by looking at the last updated date of the review.
* has_voted: ``str`` (optional)
Should have the value ``up`` or ``down`` to filter reviews that have been
voted up or down by the current authenticated user.
* my_comments: ``bool`` (optional)
Filtering reviews that include comments by the current authenticated user.
:returns: ``dict``
:raises: ``SwarmError``
"""
params = dict() # type: Dict[str, Union[int, str, bool, List[str], List[int]]]
if after:
params['after'] = after
if limit:
params['max'] = limit
if fields:
params['fields'] = ','.join(fields)
if authors:
params['author'] = authors
if float(self.swarm.version) < 2:
raise SwarmCompatibleError(
'author field is supported from API version >= 2'
)
if changes:
params['change'] = changes
if has_reviewers is not None:
params['hasReviewers'] = has_reviewers
if ids:
params['ids'] = ids
if keywords:
params['keywords'] = keywords
if participants:
params['participants'] = participants
if projects:
params['project'] = projects
if states:
params['state'] = states
if passes_tests is not None:
params['passesTests'] = passes_tests
if not_updated_since:
params['notUpdatedSince'] = not_updated_since
if has_voted:
params['hasVoted'] = has_voted
if my_comments is not None:
params['myComments'] = my_comments
return self.swarm._request('GET', 'reviews', params=params)
@minimal_version(6)
def get_for_dashboard(self) -> dict:
"""
Gets reviews for the action dashboard for the authenticated user
:returns: ``dict``
:raises: ``SwarmError``
"""
return self.swarm._request('GET', 'dashboards/action')
def _get_info(self,
review_id: int,
fields: Optional[List[str]] = None,
callback: Optional[Callable] = None
) -> dict:
"""
Retrieve information about a review.
* review_id: ``int``
Review id getting information from.
* fields: ``List[str]`` (optional)
List of fields to show. Omitting this parameter or passing an empty
value shows all fields.
* callback: ``Callable`` (optional)
Function callback for support both sync and async syntax.
:returns: ``dict``
:raises: ``SwarmError``
"""
params = dict() # type: Dict[str, str]
if fields:
params['fields'] = ','.join(fields)
response = self.swarm._request(
'GET',
'reviews/{}'.format(review_id),
fcb=callback,
params=params
)
return response
def get_info(self,
review_id: int,
*,
fields: Optional[List[str]] = None
) -> dict:
"""
Retrieve information about a review.
* review_id: ``int``
Review id getting information from.
* fields: ``List[str]`` (optional)
List of fields to show. Omitting this parameter or passing an empty
value shows all fields.
:returns: ``dict``
:raises: ``SwarmError``
"""
return self._get_info(review_id, fields)
@minimal_version(9)
def get_transitions(self,
review_id: int,
*,
up_voters: Optional[str] = None
) -> dict:
"""
Get transitions for a review (**v9+**)
* review_id: ``int``
Review id getting information from.
* up_voters: ``str`` (optional)
A list of users whose vote up will be assumed when determining the
transitions. For example if a user has not yet voted but would be the
last required vote and asked for possible transitions we would want to
include 'approve'
:returns: ``dict``
:raises: ``SwarmError``
"""
params = dict()
if up_voters:
params['upVoters'] = up_voters
response = self.swarm._request(
'GET',
'reviews/{}/transitions'.format(review_id),
params=params
)
return response
def get_latest_revision_and_change(self, review_id: int) -> Tuple[int, int]:
"""
Get latest revision and change (changelist) for a review.
* review_id: ``int``
Review id getting information from.
:returns: ``Tuple[int, int]`` revision and change respectively.
:raises: ``SwarmError``
"""
def callback(response: dict) -> Tuple[int, int]:
review = response.get('review')
if review is None or 'versions' not in review:
raise SwarmError('can`t find `versions` field in review data')
latest_revision = len(review['versions'])
if latest_revision == 0:
raise SwarmError('can`t get review revision, `versions` is empty')
latest_version = review['versions'][-1]
if 'change' not in latest_version:
raise SwarmError('no `change` field in latest versions block')
if 'archiveChange' in latest_version:
latest_change = int(latest_version['archiveChange'])
else:
latest_change = int(latest_version['change'])
return latest_revision, latest_change
response = self._get_info(
review_id,
fields=['versions'],
callback=callback
)
return response # type: ignore
def create(self,
change: int,
*,
description: Optional[str] = None,
reviewers: Optional[List[str]] = None,
required_reviewers: Optional[List[str]] = None,
reviewer_groups: Optional[List[str]] = None
) -> dict:
"""
Create a review.
* fields: ``int``
Change ID to create a review from.
* description: ``str`` (optional)
Description for the new review (defaults to change description).
* reviewers: ``List[str]`` (optional)
A list of reviewers for the new review.
* required_reviewers: ``List[str]`` (optional)
A list of required reviewers for the new review (**v1.1+**)
* reviewer_groups: ``List[str]`` (optional)
A list of required reviewers for the new review (**v7+**)
:returns: ``dict``
:raises: ``SwarmError``
"""
data = dict(change=change) # type: Dict[str, Union[int, str, List[str]]]
if description:
data['description'] = description
if reviewers:
data['reviewers'] = reviewers
if required_reviewers:
data['requiredReviewers'] = required_reviewers
if float(self.swarm.version) == 1:
raise SwarmCompatibleError(
'required_reviewers field is supported from API version > 1'
)
if reviewer_groups:
data['reviewerGroups'] = reviewer_groups
if float(self.swarm.version) < 7:
raise SwarmCompatibleError(
'reviewer_groups field is supported from API version > 6'
)
return self.swarm._request('POST', 'reviews', data=data)
@minimal_version(9)
def vote(self,
review_id: int,
vote: str,
*,
version: Optional[str] = None
) -> dict:
"""
Set the vote for the authenticated user to be up, down or cleared.
* review_id: ``int``
Review ID.
* vote: ``str``
Valid votes are 'up', 'down' and 'clear.
* version: ``str`` (optional)
Expected to be a valid review revision to vote on if supplied, ignored
if the revision does not exist and the vote will apply to the latest
revision.
:returns: ``dict``
:raises: ``SwarmError``
"""
data = dict() # type: Dict[str, str]
if vote:
data['vote[value]'] = vote
if version:
data['vote[version]'] = version
response = self.swarm._request(
'POST',
'reviews/{}/vote'.format(review_id),
data=data
)
return response
def add_change(self,
review_id: int,
change: int,
*,
mode: Optional[str] = None
) -> dict:
"""
Add change to a review, links the given change to the review and
schedules an update.
* review_id: ``int``
Review ID.
* change: ``int``
Change ID.
* mode: ``str``
The mode of operation, currently ``replace`` or `'append``.
:returns: ``dict``
:raises: ``SwarmError``
"""
data = dict(
change=change,
) # type: Dict[str, Union[str, int]]
if mode:
data['mode'] = mode
response = self.swarm._request(
'POST',
'reviews/{}/changes/'.format(review_id),
data=data
)
return response
@minimal_version(6)
def archive(self,
*,
not_updated_since: str,
description: str
) -> dict:
"""
Archiving the inactive reviews (**v6+**).
* not_updated_since: ``str``
Updated since date. Requires the date to be in the format YYYY-mm-dd
Example ``2017-01-01``
* description: ``str``
A description that is posted as a comment for archiving.
:returns: ``dict``
:raises: ``SwarmError``
"""
data = dict(
notUpdatedSince=not_updated_since,
description=description
)
return self.swarm._request('POST', 'reviews/archive', data=data)
def update(self,
review_id: int,
*,
author: Optional[str] = None,
description: Optional[str] = None
) -> dict:
"""
Archiving the inactive reviews (**v6+**).
* review_id: ``int``
Review ID.
* author: ``str`` (optional)
The new author for the specified review.
* description: ``str`` (optional)
The new description for the specified review.
:returns: ``dict``
:raises: ``SwarmError``
"""
if author is description is None:
raise SwarmError('At least one of description or author are required')
data = dict() # type: Dict[str, str]
if author:
data['author'] = author
if description:
data['description'] = description
response = self.swarm._request(
'PATCH',
'reviews/{}'.format(review_id),
data=data
)
return response
@minimal_version(6)
def cleanup(self,
review_id: int,
*,
reopen: Optional[bool] = None
) -> dict:
"""
Clean up a review for the given id (**v6+**).
* review_id: ``int``
Review id getting information from.
* reopen: ``bool`` (optional)
Expected to be a boolean (defaulting to false). If true then an attempt
will be made to reopen files into a default changelist
:returns: ``dict``
:raises: ``SwarmError``
"""
data = dict()
if reopen:
data['reopen'] = reopen
response = self.swarm._request(
'POST',
'reviews/{}/cleanup'.format(review_id),
data=data
)
return response
@minimal_version(9)
def obliterate(self,
review_id: int,
*,
reopen: Optional[bool] = None
) -> dict:
"""
Obliterate a review for the given id (**v9+**).
* review_id: ``int``
Review id getting information from.
:returns: ``dict``
:raises: ``SwarmError``
"""
response = self.swarm._request(
'POST',
'reviews/{}/obliterate'.format(review_id)
)
return response