Show More
@@ -1,678 +1,691 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | import logging |
|
23 | 23 | |
|
24 | 24 | from rhodecode.api import jsonrpc_method, JSONRPCError |
|
25 | 25 | from rhodecode.api.utils import ( |
|
26 | 26 | has_superadmin_permission, Optional, OAttr, get_repo_or_error, |
|
27 | 27 | get_pull_request_or_error, get_commit_or_error, get_user_or_error, |
|
28 | 28 | has_repo_permissions, resolve_ref_or_error) |
|
29 | 29 | from rhodecode.lib.auth import (HasRepoPermissionAnyApi) |
|
30 | 30 | from rhodecode.lib.base import vcs_operation_context |
|
31 | 31 | from rhodecode.lib.utils2 import str2bool |
|
32 | 32 | from rhodecode.model.changeset_status import ChangesetStatusModel |
|
33 | 33 | from rhodecode.model.comment import ChangesetCommentsModel |
|
34 | 34 | from rhodecode.model.db import Session, ChangesetStatus |
|
35 | 35 | from rhodecode.model.pull_request import PullRequestModel |
|
36 | 36 | from rhodecode.model.settings import SettingsModel |
|
37 | 37 | |
|
38 | 38 | log = logging.getLogger(__name__) |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | @jsonrpc_method() |
|
42 | 42 | def get_pull_request(request, apiuser, repoid, pullrequestid): |
|
43 | 43 | """ |
|
44 | 44 | Get a pull request based on the given ID. |
|
45 | 45 | |
|
46 | 46 | :param apiuser: This is filled automatically from the |authtoken|. |
|
47 | 47 | :type apiuser: AuthUser |
|
48 | 48 | :param repoid: Repository name or repository ID from where the pull |
|
49 | 49 | request was opened. |
|
50 | 50 | :type repoid: str or int |
|
51 | 51 | :param pullrequestid: ID of the requested pull request. |
|
52 | 52 | :type pullrequestid: int |
|
53 | 53 | |
|
54 | 54 | Example output: |
|
55 | 55 | |
|
56 | 56 | .. code-block:: bash |
|
57 | 57 | |
|
58 | 58 | "id": <id_given_in_input>, |
|
59 | 59 | "result": |
|
60 | 60 | { |
|
61 | 61 | "pull_request_id": "<pull_request_id>", |
|
62 | 62 | "url": "<url>", |
|
63 | 63 | "title": "<title>", |
|
64 | 64 | "description": "<description>", |
|
65 | 65 | "status" : "<status>", |
|
66 | 66 | "created_on": "<date_time_created>", |
|
67 | 67 | "updated_on": "<date_time_updated>", |
|
68 | 68 | "commit_ids": [ |
|
69 | 69 | ... |
|
70 | 70 | "<commit_id>", |
|
71 | 71 | "<commit_id>", |
|
72 | 72 | ... |
|
73 | 73 | ], |
|
74 | 74 | "review_status": "<review_status>", |
|
75 | 75 | "mergeable": { |
|
76 | 76 | "status": "<bool>", |
|
77 | 77 | "message": "<message>", |
|
78 | 78 | }, |
|
79 | 79 | "source": { |
|
80 | 80 | "clone_url": "<clone_url>", |
|
81 | 81 | "repository": "<repository_name>", |
|
82 | 82 | "reference": |
|
83 | 83 | { |
|
84 | 84 | "name": "<name>", |
|
85 | 85 | "type": "<type>", |
|
86 | 86 | "commit_id": "<commit_id>", |
|
87 | 87 | } |
|
88 | 88 | }, |
|
89 | 89 | "target": { |
|
90 | 90 | "clone_url": "<clone_url>", |
|
91 | 91 | "repository": "<repository_name>", |
|
92 | 92 | "reference": |
|
93 | 93 | { |
|
94 | 94 | "name": "<name>", |
|
95 | 95 | "type": "<type>", |
|
96 | 96 | "commit_id": "<commit_id>", |
|
97 | 97 | } |
|
98 | 98 | }, |
|
99 | 99 | "merge": { |
|
100 | 100 | "clone_url": "<clone_url>", |
|
101 | 101 | "reference": |
|
102 | 102 | { |
|
103 | 103 | "name": "<name>", |
|
104 | 104 | "type": "<type>", |
|
105 | 105 | "commit_id": "<commit_id>", |
|
106 | 106 | } |
|
107 | 107 | }, |
|
108 | 108 | "author": <user_obj>, |
|
109 | 109 | "reviewers": [ |
|
110 | 110 | ... |
|
111 | 111 | { |
|
112 | 112 | "user": "<user_obj>", |
|
113 | 113 | "review_status": "<review_status>", |
|
114 | 114 | } |
|
115 | 115 | ... |
|
116 | 116 | ] |
|
117 | 117 | }, |
|
118 | 118 | "error": null |
|
119 | 119 | """ |
|
120 | 120 | get_repo_or_error(repoid) |
|
121 | 121 | pull_request = get_pull_request_or_error(pullrequestid) |
|
122 | 122 | if not PullRequestModel().check_user_read( |
|
123 | 123 | pull_request, apiuser, api=True): |
|
124 | 124 | raise JSONRPCError('repository `%s` does not exist' % (repoid,)) |
|
125 | 125 | data = pull_request.get_api_data() |
|
126 | 126 | return data |
|
127 | 127 | |
|
128 | 128 | |
|
129 | 129 | @jsonrpc_method() |
|
130 | 130 | def get_pull_requests(request, apiuser, repoid, status=Optional('new')): |
|
131 | 131 | """ |
|
132 | 132 | Get all pull requests from the repository specified in `repoid`. |
|
133 | 133 | |
|
134 | 134 | :param apiuser: This is filled automatically from the |authtoken|. |
|
135 | 135 | :type apiuser: AuthUser |
|
136 | 136 | :param repoid: Repository name or repository ID. |
|
137 | 137 | :type repoid: str or int |
|
138 | 138 | :param status: Only return pull requests with the specified status. |
|
139 | 139 | Valid options are. |
|
140 | 140 | * ``new`` (default) |
|
141 | 141 | * ``open`` |
|
142 | 142 | * ``closed`` |
|
143 | 143 | :type status: str |
|
144 | 144 | |
|
145 | 145 | Example output: |
|
146 | 146 | |
|
147 | 147 | .. code-block:: bash |
|
148 | 148 | |
|
149 | 149 | "id": <id_given_in_input>, |
|
150 | 150 | "result": |
|
151 | 151 | [ |
|
152 | 152 | ... |
|
153 | 153 | { |
|
154 | 154 | "pull_request_id": "<pull_request_id>", |
|
155 | 155 | "url": "<url>", |
|
156 | 156 | "title" : "<title>", |
|
157 | 157 | "description": "<description>", |
|
158 | 158 | "status": "<status>", |
|
159 | 159 | "created_on": "<date_time_created>", |
|
160 | 160 | "updated_on": "<date_time_updated>", |
|
161 | 161 | "commit_ids": [ |
|
162 | 162 | ... |
|
163 | 163 | "<commit_id>", |
|
164 | 164 | "<commit_id>", |
|
165 | 165 | ... |
|
166 | 166 | ], |
|
167 | 167 | "review_status": "<review_status>", |
|
168 | 168 | "mergeable": { |
|
169 | 169 | "status": "<bool>", |
|
170 | 170 | "message: "<message>", |
|
171 | 171 | }, |
|
172 | 172 | "source": { |
|
173 | 173 | "clone_url": "<clone_url>", |
|
174 | 174 | "reference": |
|
175 | 175 | { |
|
176 | 176 | "name": "<name>", |
|
177 | 177 | "type": "<type>", |
|
178 | 178 | "commit_id": "<commit_id>", |
|
179 | 179 | } |
|
180 | 180 | }, |
|
181 | 181 | "target": { |
|
182 | 182 | "clone_url": "<clone_url>", |
|
183 | 183 | "reference": |
|
184 | 184 | { |
|
185 | 185 | "name": "<name>", |
|
186 | 186 | "type": "<type>", |
|
187 | 187 | "commit_id": "<commit_id>", |
|
188 | 188 | } |
|
189 | 189 | }, |
|
190 | 190 | "merge": { |
|
191 | 191 | "clone_url": "<clone_url>", |
|
192 | 192 | "reference": |
|
193 | 193 | { |
|
194 | 194 | "name": "<name>", |
|
195 | 195 | "type": "<type>", |
|
196 | 196 | "commit_id": "<commit_id>", |
|
197 | 197 | } |
|
198 | 198 | }, |
|
199 | 199 | "author": <user_obj>, |
|
200 | 200 | "reviewers": [ |
|
201 | 201 | ... |
|
202 | 202 | { |
|
203 | 203 | "user": "<user_obj>", |
|
204 | 204 | "review_status": "<review_status>", |
|
205 | 205 | } |
|
206 | 206 | ... |
|
207 | 207 | ] |
|
208 | 208 | } |
|
209 | 209 | ... |
|
210 | 210 | ], |
|
211 | 211 | "error": null |
|
212 | 212 | |
|
213 | 213 | """ |
|
214 | 214 | repo = get_repo_or_error(repoid) |
|
215 | 215 | if not has_superadmin_permission(apiuser): |
|
216 | 216 | _perms = ( |
|
217 | 217 | 'repository.admin', 'repository.write', 'repository.read',) |
|
218 | 218 | has_repo_permissions(apiuser, repoid, repo, _perms) |
|
219 | 219 | |
|
220 | 220 | status = Optional.extract(status) |
|
221 | 221 | pull_requests = PullRequestModel().get_all(repo, statuses=[status]) |
|
222 | 222 | data = [pr.get_api_data() for pr in pull_requests] |
|
223 | 223 | return data |
|
224 | 224 | |
|
225 | 225 | |
|
226 | 226 | @jsonrpc_method() |
|
227 | 227 | def merge_pull_request(request, apiuser, repoid, pullrequestid, |
|
228 | 228 | userid=Optional(OAttr('apiuser'))): |
|
229 | 229 | """ |
|
230 | 230 | Merge the pull request specified by `pullrequestid` into its target |
|
231 | 231 | repository. |
|
232 | 232 | |
|
233 | 233 | :param apiuser: This is filled automatically from the |authtoken|. |
|
234 | 234 | :type apiuser: AuthUser |
|
235 | 235 | :param repoid: The Repository name or repository ID of the |
|
236 | 236 | target repository to which the |pr| is to be merged. |
|
237 | 237 | :type repoid: str or int |
|
238 | 238 | :param pullrequestid: ID of the pull request which shall be merged. |
|
239 | 239 | :type pullrequestid: int |
|
240 | 240 | :param userid: Merge the pull request as this user. |
|
241 | 241 | :type userid: Optional(str or int) |
|
242 | 242 | |
|
243 | 243 | Example output: |
|
244 | 244 | |
|
245 | 245 | .. code-block:: bash |
|
246 | 246 | |
|
247 | 247 | "id": <id_given_in_input>, |
|
248 | 248 | "result": |
|
249 | 249 | { |
|
250 | 250 | "executed": "<bool>", |
|
251 | 251 | "failure_reason": "<int>", |
|
252 | 252 | "merge_commit_id": "<merge_commit_id>", |
|
253 | "possible": "<bool>" | |
|
253 | "possible": "<bool>", | |
|
254 | "merge_ref": { | |
|
255 | "commit_id": "<commit_id>", | |
|
256 | "type": "<type>", | |
|
257 | "name": "<name>" | |
|
258 | } | |
|
254 | 259 | }, |
|
255 | 260 | "error": null |
|
256 | 261 | |
|
257 | 262 | """ |
|
258 | 263 | repo = get_repo_or_error(repoid) |
|
259 | 264 | if not isinstance(userid, Optional): |
|
260 | 265 | if (has_superadmin_permission(apiuser) or |
|
261 | 266 | HasRepoPermissionAnyApi('repository.admin')( |
|
262 | 267 | user=apiuser, repo_name=repo.repo_name)): |
|
263 | 268 | apiuser = get_user_or_error(userid) |
|
264 | 269 | else: |
|
265 | 270 | raise JSONRPCError('userid is not the same as your user') |
|
266 | 271 | |
|
267 | 272 | pull_request = get_pull_request_or_error(pullrequestid) |
|
268 | 273 | if not PullRequestModel().check_user_merge( |
|
269 | 274 | pull_request, apiuser, api=True): |
|
270 | 275 | raise JSONRPCError('repository `%s` does not exist' % (repoid,)) |
|
271 | 276 | if pull_request.is_closed(): |
|
272 | 277 | raise JSONRPCError( |
|
273 | 278 | 'pull request `%s` merge failed, pull request is closed' % ( |
|
274 | 279 | pullrequestid,)) |
|
275 | 280 | |
|
276 | 281 | target_repo = pull_request.target_repo |
|
277 | 282 | extras = vcs_operation_context( |
|
278 | 283 | request.environ, repo_name=target_repo.repo_name, |
|
279 | 284 | username=apiuser.username, action='push', |
|
280 | 285 | scm=target_repo.repo_type) |
|
281 | data = PullRequestModel().merge(pull_request, apiuser, extras=extras) | |
|
282 | if data.executed: | |
|
286 | merge_response = PullRequestModel().merge( | |
|
287 | pull_request, apiuser, extras=extras) | |
|
288 | if merge_response.executed: | |
|
283 | 289 | PullRequestModel().close_pull_request( |
|
284 | 290 | pull_request.pull_request_id, apiuser) |
|
285 | 291 | |
|
286 | 292 | Session().commit() |
|
287 | return data | |
|
293 | ||
|
294 | # In previous versions the merge response directly contained the merge | |
|
295 | # commit id. It is now contained in the merge reference object. To be | |
|
296 | # backwards compatible we have to extract it again. | |
|
297 | merge_response = merge_response._asdict() | |
|
298 | merge_response['merge_commit_id'] = merge_response['merge_ref'].commit_id | |
|
299 | ||
|
300 | return merge_response | |
|
288 | 301 | |
|
289 | 302 | |
|
290 | 303 | @jsonrpc_method() |
|
291 | 304 | def close_pull_request(request, apiuser, repoid, pullrequestid, |
|
292 | 305 | userid=Optional(OAttr('apiuser'))): |
|
293 | 306 | """ |
|
294 | 307 | Close the pull request specified by `pullrequestid`. |
|
295 | 308 | |
|
296 | 309 | :param apiuser: This is filled automatically from the |authtoken|. |
|
297 | 310 | :type apiuser: AuthUser |
|
298 | 311 | :param repoid: Repository name or repository ID to which the pull |
|
299 | 312 | request belongs. |
|
300 | 313 | :type repoid: str or int |
|
301 | 314 | :param pullrequestid: ID of the pull request to be closed. |
|
302 | 315 | :type pullrequestid: int |
|
303 | 316 | :param userid: Close the pull request as this user. |
|
304 | 317 | :type userid: Optional(str or int) |
|
305 | 318 | |
|
306 | 319 | Example output: |
|
307 | 320 | |
|
308 | 321 | .. code-block:: bash |
|
309 | 322 | |
|
310 | 323 | "id": <id_given_in_input>, |
|
311 | 324 | "result": |
|
312 | 325 | { |
|
313 | 326 | "pull_request_id": "<int>", |
|
314 | 327 | "closed": "<bool>" |
|
315 | 328 | }, |
|
316 | 329 | "error": null |
|
317 | 330 | |
|
318 | 331 | """ |
|
319 | 332 | repo = get_repo_or_error(repoid) |
|
320 | 333 | if not isinstance(userid, Optional): |
|
321 | 334 | if (has_superadmin_permission(apiuser) or |
|
322 | 335 | HasRepoPermissionAnyApi('repository.admin')( |
|
323 | 336 | user=apiuser, repo_name=repo.repo_name)): |
|
324 | 337 | apiuser = get_user_or_error(userid) |
|
325 | 338 | else: |
|
326 | 339 | raise JSONRPCError('userid is not the same as your user') |
|
327 | 340 | |
|
328 | 341 | pull_request = get_pull_request_or_error(pullrequestid) |
|
329 | 342 | if not PullRequestModel().check_user_update( |
|
330 | 343 | pull_request, apiuser, api=True): |
|
331 | 344 | raise JSONRPCError( |
|
332 | 345 | 'pull request `%s` close failed, no permission to close.' % ( |
|
333 | 346 | pullrequestid,)) |
|
334 | 347 | if pull_request.is_closed(): |
|
335 | 348 | raise JSONRPCError( |
|
336 | 349 | 'pull request `%s` is already closed' % (pullrequestid,)) |
|
337 | 350 | |
|
338 | 351 | PullRequestModel().close_pull_request( |
|
339 | 352 | pull_request.pull_request_id, apiuser) |
|
340 | 353 | Session().commit() |
|
341 | 354 | data = { |
|
342 | 355 | 'pull_request_id': pull_request.pull_request_id, |
|
343 | 356 | 'closed': True, |
|
344 | 357 | } |
|
345 | 358 | return data |
|
346 | 359 | |
|
347 | 360 | |
|
348 | 361 | @jsonrpc_method() |
|
349 | 362 | def comment_pull_request(request, apiuser, repoid, pullrequestid, |
|
350 | 363 | message=Optional(None), status=Optional(None), |
|
351 | 364 | userid=Optional(OAttr('apiuser'))): |
|
352 | 365 | """ |
|
353 | 366 | Comment on the pull request specified with the `pullrequestid`, |
|
354 | 367 | in the |repo| specified by the `repoid`, and optionally change the |
|
355 | 368 | review status. |
|
356 | 369 | |
|
357 | 370 | :param apiuser: This is filled automatically from the |authtoken|. |
|
358 | 371 | :type apiuser: AuthUser |
|
359 | 372 | :param repoid: The repository name or repository ID. |
|
360 | 373 | :type repoid: str or int |
|
361 | 374 | :param pullrequestid: The pull request ID. |
|
362 | 375 | :type pullrequestid: int |
|
363 | 376 | :param message: The text content of the comment. |
|
364 | 377 | :type message: str |
|
365 | 378 | :param status: (**Optional**) Set the approval status of the pull |
|
366 | 379 | request. Valid options are: |
|
367 | 380 | * not_reviewed |
|
368 | 381 | * approved |
|
369 | 382 | * rejected |
|
370 | 383 | * under_review |
|
371 | 384 | :type status: str |
|
372 | 385 | :param userid: Comment on the pull request as this user |
|
373 | 386 | :type userid: Optional(str or int) |
|
374 | 387 | |
|
375 | 388 | Example output: |
|
376 | 389 | |
|
377 | 390 | .. code-block:: bash |
|
378 | 391 | |
|
379 | 392 | id : <id_given_in_input> |
|
380 | 393 | result : |
|
381 | 394 | { |
|
382 | 395 | "pull_request_id": "<Integer>", |
|
383 | 396 | "comment_id": "<Integer>" |
|
384 | 397 | } |
|
385 | 398 | error : null |
|
386 | 399 | """ |
|
387 | 400 | repo = get_repo_or_error(repoid) |
|
388 | 401 | if not isinstance(userid, Optional): |
|
389 | 402 | if (has_superadmin_permission(apiuser) or |
|
390 | 403 | HasRepoPermissionAnyApi('repository.admin')( |
|
391 | 404 | user=apiuser, repo_name=repo.repo_name)): |
|
392 | 405 | apiuser = get_user_or_error(userid) |
|
393 | 406 | else: |
|
394 | 407 | raise JSONRPCError('userid is not the same as your user') |
|
395 | 408 | |
|
396 | 409 | pull_request = get_pull_request_or_error(pullrequestid) |
|
397 | 410 | if not PullRequestModel().check_user_read( |
|
398 | 411 | pull_request, apiuser, api=True): |
|
399 | 412 | raise JSONRPCError('repository `%s` does not exist' % (repoid,)) |
|
400 | 413 | message = Optional.extract(message) |
|
401 | 414 | status = Optional.extract(status) |
|
402 | 415 | if not message and not status: |
|
403 | 416 | raise JSONRPCError('message and status parameter missing') |
|
404 | 417 | |
|
405 | 418 | if (status not in (st[0] for st in ChangesetStatus.STATUSES) and |
|
406 | 419 | status is not None): |
|
407 | 420 | raise JSONRPCError('unknown comment status`%s`' % status) |
|
408 | 421 | |
|
409 | 422 | allowed_to_change_status = PullRequestModel().check_user_change_status( |
|
410 | 423 | pull_request, apiuser) |
|
411 | 424 | text = message |
|
412 | 425 | if status and allowed_to_change_status: |
|
413 | 426 | st_message = (('Status change %(transition_icon)s %(status)s') |
|
414 | 427 | % {'transition_icon': '>', |
|
415 | 428 | 'status': ChangesetStatus.get_status_lbl(status)}) |
|
416 | 429 | text = message or st_message |
|
417 | 430 | |
|
418 | 431 | rc_config = SettingsModel().get_all_settings() |
|
419 | 432 | renderer = rc_config.get('rhodecode_markup_renderer', 'rst') |
|
420 | 433 | comment = ChangesetCommentsModel().create( |
|
421 | 434 | text=text, |
|
422 | 435 | repo=pull_request.target_repo.repo_id, |
|
423 | 436 | user=apiuser.user_id, |
|
424 | 437 | pull_request=pull_request.pull_request_id, |
|
425 | 438 | f_path=None, |
|
426 | 439 | line_no=None, |
|
427 | 440 | status_change=(ChangesetStatus.get_status_lbl(status) |
|
428 | 441 | if status and allowed_to_change_status else None), |
|
429 | 442 | status_change_type=(status |
|
430 | 443 | if status and allowed_to_change_status else None), |
|
431 | 444 | closing_pr=False, |
|
432 | 445 | renderer=renderer |
|
433 | 446 | ) |
|
434 | 447 | |
|
435 | 448 | if allowed_to_change_status and status: |
|
436 | 449 | ChangesetStatusModel().set_status( |
|
437 | 450 | pull_request.target_repo.repo_id, |
|
438 | 451 | status, |
|
439 | 452 | apiuser.user_id, |
|
440 | 453 | comment, |
|
441 | 454 | pull_request=pull_request.pull_request_id |
|
442 | 455 | ) |
|
443 | 456 | Session().flush() |
|
444 | 457 | |
|
445 | 458 | Session().commit() |
|
446 | 459 | data = { |
|
447 | 460 | 'pull_request_id': pull_request.pull_request_id, |
|
448 | 461 | 'comment_id': comment.comment_id, |
|
449 | 462 | 'status': status |
|
450 | 463 | } |
|
451 | 464 | return data |
|
452 | 465 | |
|
453 | 466 | |
|
454 | 467 | @jsonrpc_method() |
|
455 | 468 | def create_pull_request( |
|
456 | 469 | request, apiuser, source_repo, target_repo, source_ref, target_ref, |
|
457 | 470 | title, description=Optional(''), reviewers=Optional(None)): |
|
458 | 471 | """ |
|
459 | 472 | Creates a new pull request. |
|
460 | 473 | |
|
461 | 474 | Accepts refs in the following formats: |
|
462 | 475 | |
|
463 | 476 | * branch:<branch_name>:<sha> |
|
464 | 477 | * branch:<branch_name> |
|
465 | 478 | * bookmark:<bookmark_name>:<sha> (Mercurial only) |
|
466 | 479 | * bookmark:<bookmark_name> (Mercurial only) |
|
467 | 480 | |
|
468 | 481 | :param apiuser: This is filled automatically from the |authtoken|. |
|
469 | 482 | :type apiuser: AuthUser |
|
470 | 483 | :param source_repo: Set the source repository name. |
|
471 | 484 | :type source_repo: str |
|
472 | 485 | :param target_repo: Set the target repository name. |
|
473 | 486 | :type target_repo: str |
|
474 | 487 | :param source_ref: Set the source ref name. |
|
475 | 488 | :type source_ref: str |
|
476 | 489 | :param target_ref: Set the target ref name. |
|
477 | 490 | :type target_ref: str |
|
478 | 491 | :param title: Set the pull request title. |
|
479 | 492 | :type title: str |
|
480 | 493 | :param description: Set the pull request description. |
|
481 | 494 | :type description: Optional(str) |
|
482 | 495 | :param reviewers: Set the new pull request reviewers list. |
|
483 | 496 | :type reviewers: Optional(list) |
|
484 | 497 | Accepts username strings or objects of the format: |
|
485 | 498 | { |
|
486 | 499 | 'username': 'nick', 'reasons': ['original author'] |
|
487 | 500 | } |
|
488 | 501 | """ |
|
489 | 502 | |
|
490 | 503 | source = get_repo_or_error(source_repo) |
|
491 | 504 | target = get_repo_or_error(target_repo) |
|
492 | 505 | if not has_superadmin_permission(apiuser): |
|
493 | 506 | _perms = ('repository.admin', 'repository.write', 'repository.read',) |
|
494 | 507 | has_repo_permissions(apiuser, source_repo, source, _perms) |
|
495 | 508 | |
|
496 | 509 | full_source_ref = resolve_ref_or_error(source_ref, source) |
|
497 | 510 | full_target_ref = resolve_ref_or_error(target_ref, target) |
|
498 | 511 | source_commit = get_commit_or_error(full_source_ref, source) |
|
499 | 512 | target_commit = get_commit_or_error(full_target_ref, target) |
|
500 | 513 | source_scm = source.scm_instance() |
|
501 | 514 | target_scm = target.scm_instance() |
|
502 | 515 | |
|
503 | 516 | commit_ranges = target_scm.compare( |
|
504 | 517 | target_commit.raw_id, source_commit.raw_id, source_scm, |
|
505 | 518 | merge=True, pre_load=[]) |
|
506 | 519 | |
|
507 | 520 | ancestor = target_scm.get_common_ancestor( |
|
508 | 521 | target_commit.raw_id, source_commit.raw_id, source_scm) |
|
509 | 522 | |
|
510 | 523 | if not commit_ranges: |
|
511 | 524 | raise JSONRPCError('no commits found') |
|
512 | 525 | |
|
513 | 526 | if not ancestor: |
|
514 | 527 | raise JSONRPCError('no common ancestor found') |
|
515 | 528 | |
|
516 | 529 | reviewer_objects = Optional.extract(reviewers) or [] |
|
517 | 530 | if not isinstance(reviewer_objects, list): |
|
518 | 531 | raise JSONRPCError('reviewers should be specified as a list') |
|
519 | 532 | |
|
520 | 533 | reviewers_reasons = [] |
|
521 | 534 | for reviewer_object in reviewer_objects: |
|
522 | 535 | reviewer_reasons = [] |
|
523 | 536 | if isinstance(reviewer_object, (basestring, int)): |
|
524 | 537 | reviewer_username = reviewer_object |
|
525 | 538 | else: |
|
526 | 539 | reviewer_username = reviewer_object['username'] |
|
527 | 540 | reviewer_reasons = reviewer_object.get('reasons', []) |
|
528 | 541 | |
|
529 | 542 | user = get_user_or_error(reviewer_username) |
|
530 | 543 | reviewers_reasons.append((user.user_id, reviewer_reasons)) |
|
531 | 544 | |
|
532 | 545 | pull_request_model = PullRequestModel() |
|
533 | 546 | pull_request = pull_request_model.create( |
|
534 | 547 | created_by=apiuser.user_id, |
|
535 | 548 | source_repo=source_repo, |
|
536 | 549 | source_ref=full_source_ref, |
|
537 | 550 | target_repo=target_repo, |
|
538 | 551 | target_ref=full_target_ref, |
|
539 | 552 | revisions=reversed( |
|
540 | 553 | [commit.raw_id for commit in reversed(commit_ranges)]), |
|
541 | 554 | reviewers=reviewers_reasons, |
|
542 | 555 | title=title, |
|
543 | 556 | description=Optional.extract(description) |
|
544 | 557 | ) |
|
545 | 558 | |
|
546 | 559 | Session().commit() |
|
547 | 560 | data = { |
|
548 | 561 | 'msg': 'Created new pull request `{}`'.format(title), |
|
549 | 562 | 'pull_request_id': pull_request.pull_request_id, |
|
550 | 563 | } |
|
551 | 564 | return data |
|
552 | 565 | |
|
553 | 566 | |
|
554 | 567 | @jsonrpc_method() |
|
555 | 568 | def update_pull_request( |
|
556 | 569 | request, apiuser, repoid, pullrequestid, title=Optional(''), |
|
557 | 570 | description=Optional(''), reviewers=Optional(None), |
|
558 | 571 | update_commits=Optional(None), close_pull_request=Optional(None)): |
|
559 | 572 | """ |
|
560 | 573 | Updates a pull request. |
|
561 | 574 | |
|
562 | 575 | :param apiuser: This is filled automatically from the |authtoken|. |
|
563 | 576 | :type apiuser: AuthUser |
|
564 | 577 | :param repoid: The repository name or repository ID. |
|
565 | 578 | :type repoid: str or int |
|
566 | 579 | :param pullrequestid: The pull request ID. |
|
567 | 580 | :type pullrequestid: int |
|
568 | 581 | :param title: Set the pull request title. |
|
569 | 582 | :type title: str |
|
570 | 583 | :param description: Update pull request description. |
|
571 | 584 | :type description: Optional(str) |
|
572 | 585 | :param reviewers: Update pull request reviewers list with new value. |
|
573 | 586 | :type reviewers: Optional(list) |
|
574 | 587 | :param update_commits: Trigger update of commits for this pull request |
|
575 | 588 | :type: update_commits: Optional(bool) |
|
576 | 589 | :param close_pull_request: Close this pull request with rejected state |
|
577 | 590 | :type: close_pull_request: Optional(bool) |
|
578 | 591 | |
|
579 | 592 | Example output: |
|
580 | 593 | |
|
581 | 594 | .. code-block:: bash |
|
582 | 595 | |
|
583 | 596 | id : <id_given_in_input> |
|
584 | 597 | result : |
|
585 | 598 | { |
|
586 | 599 | "msg": "Updated pull request `63`", |
|
587 | 600 | "pull_request": <pull_request_object>, |
|
588 | 601 | "updated_reviewers": { |
|
589 | 602 | "added": [ |
|
590 | 603 | "username" |
|
591 | 604 | ], |
|
592 | 605 | "removed": [] |
|
593 | 606 | }, |
|
594 | 607 | "updated_commits": { |
|
595 | 608 | "added": [ |
|
596 | 609 | "<sha1_hash>" |
|
597 | 610 | ], |
|
598 | 611 | "common": [ |
|
599 | 612 | "<sha1_hash>", |
|
600 | 613 | "<sha1_hash>", |
|
601 | 614 | ], |
|
602 | 615 | "removed": [] |
|
603 | 616 | } |
|
604 | 617 | } |
|
605 | 618 | error : null |
|
606 | 619 | """ |
|
607 | 620 | |
|
608 | 621 | repo = get_repo_or_error(repoid) |
|
609 | 622 | pull_request = get_pull_request_or_error(pullrequestid) |
|
610 | 623 | if not PullRequestModel().check_user_update( |
|
611 | 624 | pull_request, apiuser, api=True): |
|
612 | 625 | raise JSONRPCError( |
|
613 | 626 | 'pull request `%s` update failed, no permission to update.' % ( |
|
614 | 627 | pullrequestid,)) |
|
615 | 628 | if pull_request.is_closed(): |
|
616 | 629 | raise JSONRPCError( |
|
617 | 630 | 'pull request `%s` update failed, pull request is closed' % ( |
|
618 | 631 | pullrequestid,)) |
|
619 | 632 | |
|
620 | 633 | reviewer_objects = Optional.extract(reviewers) or [] |
|
621 | 634 | if not isinstance(reviewer_objects, list): |
|
622 | 635 | raise JSONRPCError('reviewers should be specified as a list') |
|
623 | 636 | |
|
624 | 637 | reviewers_reasons = [] |
|
625 | 638 | reviewer_ids = set() |
|
626 | 639 | for reviewer_object in reviewer_objects: |
|
627 | 640 | reviewer_reasons = [] |
|
628 | 641 | if isinstance(reviewer_object, (int, basestring)): |
|
629 | 642 | reviewer_username = reviewer_object |
|
630 | 643 | else: |
|
631 | 644 | reviewer_username = reviewer_object['username'] |
|
632 | 645 | reviewer_reasons = reviewer_object.get('reasons', []) |
|
633 | 646 | |
|
634 | 647 | user = get_user_or_error(reviewer_username) |
|
635 | 648 | reviewer_ids.add(user.user_id) |
|
636 | 649 | reviewers_reasons.append((user.user_id, reviewer_reasons)) |
|
637 | 650 | |
|
638 | 651 | title = Optional.extract(title) |
|
639 | 652 | description = Optional.extract(description) |
|
640 | 653 | if title or description: |
|
641 | 654 | PullRequestModel().edit( |
|
642 | 655 | pull_request, title or pull_request.title, |
|
643 | 656 | description or pull_request.description) |
|
644 | 657 | Session().commit() |
|
645 | 658 | |
|
646 | 659 | commit_changes = {"added": [], "common": [], "removed": []} |
|
647 | 660 | if str2bool(Optional.extract(update_commits)): |
|
648 | 661 | if PullRequestModel().has_valid_update_type(pull_request): |
|
649 | 662 | _version, _commit_changes = PullRequestModel().update_commits( |
|
650 | 663 | pull_request) |
|
651 | 664 | commit_changes = _commit_changes or commit_changes |
|
652 | 665 | Session().commit() |
|
653 | 666 | |
|
654 | 667 | reviewers_changes = {"added": [], "removed": []} |
|
655 | 668 | if reviewer_ids: |
|
656 | 669 | added_reviewers, removed_reviewers = \ |
|
657 | 670 | PullRequestModel().update_reviewers(pull_request, reviewers_reasons) |
|
658 | 671 | |
|
659 | 672 | reviewers_changes['added'] = sorted( |
|
660 | 673 | [get_user_or_error(n).username for n in added_reviewers]) |
|
661 | 674 | reviewers_changes['removed'] = sorted( |
|
662 | 675 | [get_user_or_error(n).username for n in removed_reviewers]) |
|
663 | 676 | Session().commit() |
|
664 | 677 | |
|
665 | 678 | if str2bool(Optional.extract(close_pull_request)): |
|
666 | 679 | PullRequestModel().close_pull_request_with_comment( |
|
667 | 680 | pull_request, apiuser, repo) |
|
668 | 681 | Session().commit() |
|
669 | 682 | |
|
670 | 683 | data = { |
|
671 | 684 | 'msg': 'Updated pull request `{}`'.format( |
|
672 | 685 | pull_request.pull_request_id), |
|
673 | 686 | 'pull_request': pull_request.get_api_data(), |
|
674 | 687 | 'updated_commits': commit_changes, |
|
675 | 688 | 'updated_reviewers': reviewers_changes |
|
676 | 689 | } |
|
677 | 690 | return data |
|
678 | 691 |
General Comments 0
You need to be logged in to leave comments.
Login now