Show More
@@ -1,725 +1,730 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2017 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 | validate_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 CommentsModel |
|
34 | 34 | from rhodecode.model.db import Session, ChangesetStatus, ChangesetComment |
|
35 | 35 | from rhodecode.model.pull_request import PullRequestModel, MergeCheck |
|
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 | validate_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 | "executed": "<bool>", |
|
250 | 250 | "failure_reason": "<int>", |
|
251 | 251 | "merge_commit_id": "<merge_commit_id>", |
|
252 | 252 | "possible": "<bool>", |
|
253 | 253 | "merge_ref": { |
|
254 | 254 | "commit_id": "<commit_id>", |
|
255 | 255 | "type": "<type>", |
|
256 | 256 | "name": "<name>" |
|
257 | 257 | } |
|
258 | 258 | }, |
|
259 | 259 | "error": null |
|
260 | 260 | """ |
|
261 | 261 | repo = get_repo_or_error(repoid) |
|
262 | 262 | if not isinstance(userid, Optional): |
|
263 | 263 | if (has_superadmin_permission(apiuser) or |
|
264 | 264 | HasRepoPermissionAnyApi('repository.admin')( |
|
265 | 265 | user=apiuser, repo_name=repo.repo_name)): |
|
266 | 266 | apiuser = get_user_or_error(userid) |
|
267 | 267 | else: |
|
268 | 268 | raise JSONRPCError('userid is not the same as your user') |
|
269 | 269 | |
|
270 | 270 | pull_request = get_pull_request_or_error(pullrequestid) |
|
271 | 271 | |
|
272 | 272 | check = MergeCheck.validate(pull_request, user=apiuser) |
|
273 | 273 | merge_possible = not check.failed |
|
274 | 274 | |
|
275 | 275 | if not merge_possible: |
|
276 | reasons = ','.join([msg for _e, msg in check.errors]) | |
|
276 | error_messages = [] | |
|
277 | for err_type, error_msg in check.errors: | |
|
278 | error_msg = request.translate(error_msg) | |
|
279 | error_messages.append(error_msg) | |
|
280 | ||
|
281 | reasons = ','.join(error_messages) | |
|
277 | 282 | raise JSONRPCError( |
|
278 | 283 | 'merge not possible for following reasons: {}'.format(reasons)) |
|
279 | 284 | |
|
280 | 285 | target_repo = pull_request.target_repo |
|
281 | 286 | extras = vcs_operation_context( |
|
282 | 287 | request.environ, repo_name=target_repo.repo_name, |
|
283 | 288 | username=apiuser.username, action='push', |
|
284 | 289 | scm=target_repo.repo_type) |
|
285 | 290 | merge_response = PullRequestModel().merge( |
|
286 | 291 | pull_request, apiuser, extras=extras) |
|
287 | 292 | if merge_response.executed: |
|
288 | 293 | PullRequestModel().close_pull_request( |
|
289 | 294 | pull_request.pull_request_id, apiuser) |
|
290 | 295 | |
|
291 | 296 | Session().commit() |
|
292 | 297 | |
|
293 | 298 | # In previous versions the merge response directly contained the merge |
|
294 | 299 | # commit id. It is now contained in the merge reference object. To be |
|
295 | 300 | # backwards compatible we have to extract it again. |
|
296 | 301 | merge_response = merge_response._asdict() |
|
297 | 302 | merge_response['merge_commit_id'] = merge_response['merge_ref'].commit_id |
|
298 | 303 | |
|
299 | 304 | return merge_response |
|
300 | 305 | |
|
301 | 306 | |
|
302 | 307 | @jsonrpc_method() |
|
303 | 308 | def close_pull_request(request, apiuser, repoid, pullrequestid, |
|
304 | 309 | userid=Optional(OAttr('apiuser'))): |
|
305 | 310 | """ |
|
306 | 311 | Close the pull request specified by `pullrequestid`. |
|
307 | 312 | |
|
308 | 313 | :param apiuser: This is filled automatically from the |authtoken|. |
|
309 | 314 | :type apiuser: AuthUser |
|
310 | 315 | :param repoid: Repository name or repository ID to which the pull |
|
311 | 316 | request belongs. |
|
312 | 317 | :type repoid: str or int |
|
313 | 318 | :param pullrequestid: ID of the pull request to be closed. |
|
314 | 319 | :type pullrequestid: int |
|
315 | 320 | :param userid: Close the pull request as this user. |
|
316 | 321 | :type userid: Optional(str or int) |
|
317 | 322 | |
|
318 | 323 | Example output: |
|
319 | 324 | |
|
320 | 325 | .. code-block:: bash |
|
321 | 326 | |
|
322 | 327 | "id": <id_given_in_input>, |
|
323 | 328 | "result": { |
|
324 | 329 | "pull_request_id": "<int>", |
|
325 | 330 | "closed": "<bool>" |
|
326 | 331 | }, |
|
327 | 332 | "error": null |
|
328 | 333 | |
|
329 | 334 | """ |
|
330 | 335 | repo = get_repo_or_error(repoid) |
|
331 | 336 | if not isinstance(userid, Optional): |
|
332 | 337 | if (has_superadmin_permission(apiuser) or |
|
333 | 338 | HasRepoPermissionAnyApi('repository.admin')( |
|
334 | 339 | user=apiuser, repo_name=repo.repo_name)): |
|
335 | 340 | apiuser = get_user_or_error(userid) |
|
336 | 341 | else: |
|
337 | 342 | raise JSONRPCError('userid is not the same as your user') |
|
338 | 343 | |
|
339 | 344 | pull_request = get_pull_request_or_error(pullrequestid) |
|
340 | 345 | if not PullRequestModel().check_user_update( |
|
341 | 346 | pull_request, apiuser, api=True): |
|
342 | 347 | raise JSONRPCError( |
|
343 | 348 | 'pull request `%s` close failed, no permission to close.' % ( |
|
344 | 349 | pullrequestid,)) |
|
345 | 350 | if pull_request.is_closed(): |
|
346 | 351 | raise JSONRPCError( |
|
347 | 352 | 'pull request `%s` is already closed' % (pullrequestid,)) |
|
348 | 353 | |
|
349 | 354 | PullRequestModel().close_pull_request( |
|
350 | 355 | pull_request.pull_request_id, apiuser) |
|
351 | 356 | Session().commit() |
|
352 | 357 | data = { |
|
353 | 358 | 'pull_request_id': pull_request.pull_request_id, |
|
354 | 359 | 'closed': True, |
|
355 | 360 | } |
|
356 | 361 | return data |
|
357 | 362 | |
|
358 | 363 | |
|
359 | 364 | @jsonrpc_method() |
|
360 | 365 | def comment_pull_request( |
|
361 | 366 | request, apiuser, repoid, pullrequestid, message=Optional(None), |
|
362 | 367 | commit_id=Optional(None), status=Optional(None), |
|
363 | 368 | comment_type=Optional(ChangesetComment.COMMENT_TYPE_NOTE), |
|
364 | 369 | resolves_comment_id=Optional(None), |
|
365 | 370 | userid=Optional(OAttr('apiuser'))): |
|
366 | 371 | """ |
|
367 | 372 | Comment on the pull request specified with the `pullrequestid`, |
|
368 | 373 | in the |repo| specified by the `repoid`, and optionally change the |
|
369 | 374 | review status. |
|
370 | 375 | |
|
371 | 376 | :param apiuser: This is filled automatically from the |authtoken|. |
|
372 | 377 | :type apiuser: AuthUser |
|
373 | 378 | :param repoid: The repository name or repository ID. |
|
374 | 379 | :type repoid: str or int |
|
375 | 380 | :param pullrequestid: The pull request ID. |
|
376 | 381 | :type pullrequestid: int |
|
377 | 382 | :param commit_id: Specify the commit_id for which to set a comment. If |
|
378 | 383 | given commit_id is different than latest in the PR status |
|
379 | 384 | change won't be performed. |
|
380 | 385 | :type commit_id: str |
|
381 | 386 | :param message: The text content of the comment. |
|
382 | 387 | :type message: str |
|
383 | 388 | :param status: (**Optional**) Set the approval status of the pull |
|
384 | 389 | request. One of: 'not_reviewed', 'approved', 'rejected', |
|
385 | 390 | 'under_review' |
|
386 | 391 | :type status: str |
|
387 | 392 | :param comment_type: Comment type, one of: 'note', 'todo' |
|
388 | 393 | :type comment_type: Optional(str), default: 'note' |
|
389 | 394 | :param userid: Comment on the pull request as this user |
|
390 | 395 | :type userid: Optional(str or int) |
|
391 | 396 | |
|
392 | 397 | Example output: |
|
393 | 398 | |
|
394 | 399 | .. code-block:: bash |
|
395 | 400 | |
|
396 | 401 | id : <id_given_in_input> |
|
397 | 402 | result : { |
|
398 | 403 | "pull_request_id": "<Integer>", |
|
399 | 404 | "comment_id": "<Integer>", |
|
400 | 405 | "status": {"given": <given_status>, |
|
401 | 406 | "was_changed": <bool status_was_actually_changed> }, |
|
402 | 407 | }, |
|
403 | 408 | error : null |
|
404 | 409 | """ |
|
405 | 410 | repo = get_repo_or_error(repoid) |
|
406 | 411 | if not isinstance(userid, Optional): |
|
407 | 412 | if (has_superadmin_permission(apiuser) or |
|
408 | 413 | HasRepoPermissionAnyApi('repository.admin')( |
|
409 | 414 | user=apiuser, repo_name=repo.repo_name)): |
|
410 | 415 | apiuser = get_user_or_error(userid) |
|
411 | 416 | else: |
|
412 | 417 | raise JSONRPCError('userid is not the same as your user') |
|
413 | 418 | |
|
414 | 419 | pull_request = get_pull_request_or_error(pullrequestid) |
|
415 | 420 | if not PullRequestModel().check_user_read( |
|
416 | 421 | pull_request, apiuser, api=True): |
|
417 | 422 | raise JSONRPCError('repository `%s` does not exist' % (repoid,)) |
|
418 | 423 | message = Optional.extract(message) |
|
419 | 424 | status = Optional.extract(status) |
|
420 | 425 | commit_id = Optional.extract(commit_id) |
|
421 | 426 | comment_type = Optional.extract(comment_type) |
|
422 | 427 | resolves_comment_id = Optional.extract(resolves_comment_id) |
|
423 | 428 | |
|
424 | 429 | if not message and not status: |
|
425 | 430 | raise JSONRPCError( |
|
426 | 431 | 'Both message and status parameters are missing. ' |
|
427 | 432 | 'At least one is required.') |
|
428 | 433 | |
|
429 | 434 | if (status not in (st[0] for st in ChangesetStatus.STATUSES) and |
|
430 | 435 | status is not None): |
|
431 | 436 | raise JSONRPCError('Unknown comment status: `%s`' % status) |
|
432 | 437 | |
|
433 | 438 | if commit_id and commit_id not in pull_request.revisions: |
|
434 | 439 | raise JSONRPCError( |
|
435 | 440 | 'Invalid commit_id `%s` for this pull request.' % commit_id) |
|
436 | 441 | |
|
437 | 442 | allowed_to_change_status = PullRequestModel().check_user_change_status( |
|
438 | 443 | pull_request, apiuser) |
|
439 | 444 | |
|
440 | 445 | # if commit_id is passed re-validated if user is allowed to change status |
|
441 | 446 | # based on latest commit_id from the PR |
|
442 | 447 | if commit_id: |
|
443 | 448 | commit_idx = pull_request.revisions.index(commit_id) |
|
444 | 449 | if commit_idx != 0: |
|
445 | 450 | allowed_to_change_status = False |
|
446 | 451 | |
|
447 | 452 | if resolves_comment_id: |
|
448 | 453 | comment = ChangesetComment.get(resolves_comment_id) |
|
449 | 454 | if not comment: |
|
450 | 455 | raise JSONRPCError( |
|
451 | 456 | 'Invalid resolves_comment_id `%s` for this pull request.' |
|
452 | 457 | % resolves_comment_id) |
|
453 | 458 | if comment.comment_type != ChangesetComment.COMMENT_TYPE_TODO: |
|
454 | 459 | raise JSONRPCError( |
|
455 | 460 | 'Comment `%s` is wrong type for setting status to resolved.' |
|
456 | 461 | % resolves_comment_id) |
|
457 | 462 | |
|
458 | 463 | text = message |
|
459 | 464 | status_label = ChangesetStatus.get_status_lbl(status) |
|
460 | 465 | if status and allowed_to_change_status: |
|
461 | 466 | st_message = ('Status change %(transition_icon)s %(status)s' |
|
462 | 467 | % {'transition_icon': '>', 'status': status_label}) |
|
463 | 468 | text = message or st_message |
|
464 | 469 | |
|
465 | 470 | rc_config = SettingsModel().get_all_settings() |
|
466 | 471 | renderer = rc_config.get('rhodecode_markup_renderer', 'rst') |
|
467 | 472 | |
|
468 | 473 | status_change = status and allowed_to_change_status |
|
469 | 474 | comment = CommentsModel().create( |
|
470 | 475 | text=text, |
|
471 | 476 | repo=pull_request.target_repo.repo_id, |
|
472 | 477 | user=apiuser.user_id, |
|
473 | 478 | pull_request=pull_request.pull_request_id, |
|
474 | 479 | f_path=None, |
|
475 | 480 | line_no=None, |
|
476 | 481 | status_change=(status_label if status_change else None), |
|
477 | 482 | status_change_type=(status if status_change else None), |
|
478 | 483 | closing_pr=False, |
|
479 | 484 | renderer=renderer, |
|
480 | 485 | comment_type=comment_type, |
|
481 | 486 | resolves_comment_id=resolves_comment_id |
|
482 | 487 | ) |
|
483 | 488 | |
|
484 | 489 | if allowed_to_change_status and status: |
|
485 | 490 | ChangesetStatusModel().set_status( |
|
486 | 491 | pull_request.target_repo.repo_id, |
|
487 | 492 | status, |
|
488 | 493 | apiuser.user_id, |
|
489 | 494 | comment, |
|
490 | 495 | pull_request=pull_request.pull_request_id |
|
491 | 496 | ) |
|
492 | 497 | Session().flush() |
|
493 | 498 | |
|
494 | 499 | Session().commit() |
|
495 | 500 | data = { |
|
496 | 501 | 'pull_request_id': pull_request.pull_request_id, |
|
497 | 502 | 'comment_id': comment.comment_id if comment else None, |
|
498 | 503 | 'status': {'given': status, 'was_changed': status_change}, |
|
499 | 504 | } |
|
500 | 505 | return data |
|
501 | 506 | |
|
502 | 507 | |
|
503 | 508 | @jsonrpc_method() |
|
504 | 509 | def create_pull_request( |
|
505 | 510 | request, apiuser, source_repo, target_repo, source_ref, target_ref, |
|
506 | 511 | title, description=Optional(''), reviewers=Optional(None)): |
|
507 | 512 | """ |
|
508 | 513 | Creates a new pull request. |
|
509 | 514 | |
|
510 | 515 | Accepts refs in the following formats: |
|
511 | 516 | |
|
512 | 517 | * branch:<branch_name>:<sha> |
|
513 | 518 | * branch:<branch_name> |
|
514 | 519 | * bookmark:<bookmark_name>:<sha> (Mercurial only) |
|
515 | 520 | * bookmark:<bookmark_name> (Mercurial only) |
|
516 | 521 | |
|
517 | 522 | :param apiuser: This is filled automatically from the |authtoken|. |
|
518 | 523 | :type apiuser: AuthUser |
|
519 | 524 | :param source_repo: Set the source repository name. |
|
520 | 525 | :type source_repo: str |
|
521 | 526 | :param target_repo: Set the target repository name. |
|
522 | 527 | :type target_repo: str |
|
523 | 528 | :param source_ref: Set the source ref name. |
|
524 | 529 | :type source_ref: str |
|
525 | 530 | :param target_ref: Set the target ref name. |
|
526 | 531 | :type target_ref: str |
|
527 | 532 | :param title: Set the pull request title. |
|
528 | 533 | :type title: str |
|
529 | 534 | :param description: Set the pull request description. |
|
530 | 535 | :type description: Optional(str) |
|
531 | 536 | :param reviewers: Set the new pull request reviewers list. |
|
532 | 537 | :type reviewers: Optional(list) |
|
533 | 538 | Accepts username strings or objects of the format: |
|
534 | 539 | |
|
535 | 540 | {'username': 'nick', 'reasons': ['original author']} |
|
536 | 541 | """ |
|
537 | 542 | |
|
538 | 543 | source = get_repo_or_error(source_repo) |
|
539 | 544 | target = get_repo_or_error(target_repo) |
|
540 | 545 | if not has_superadmin_permission(apiuser): |
|
541 | 546 | _perms = ('repository.admin', 'repository.write', 'repository.read',) |
|
542 | 547 | validate_repo_permissions(apiuser, source_repo, source, _perms) |
|
543 | 548 | |
|
544 | 549 | full_source_ref = resolve_ref_or_error(source_ref, source) |
|
545 | 550 | full_target_ref = resolve_ref_or_error(target_ref, target) |
|
546 | 551 | source_commit = get_commit_or_error(full_source_ref, source) |
|
547 | 552 | target_commit = get_commit_or_error(full_target_ref, target) |
|
548 | 553 | source_scm = source.scm_instance() |
|
549 | 554 | target_scm = target.scm_instance() |
|
550 | 555 | |
|
551 | 556 | commit_ranges = target_scm.compare( |
|
552 | 557 | target_commit.raw_id, source_commit.raw_id, source_scm, |
|
553 | 558 | merge=True, pre_load=[]) |
|
554 | 559 | |
|
555 | 560 | ancestor = target_scm.get_common_ancestor( |
|
556 | 561 | target_commit.raw_id, source_commit.raw_id, source_scm) |
|
557 | 562 | |
|
558 | 563 | if not commit_ranges: |
|
559 | 564 | raise JSONRPCError('no commits found') |
|
560 | 565 | |
|
561 | 566 | if not ancestor: |
|
562 | 567 | raise JSONRPCError('no common ancestor found') |
|
563 | 568 | |
|
564 | 569 | reviewer_objects = Optional.extract(reviewers) or [] |
|
565 | 570 | if not isinstance(reviewer_objects, list): |
|
566 | 571 | raise JSONRPCError('reviewers should be specified as a list') |
|
567 | 572 | |
|
568 | 573 | reviewers_reasons = [] |
|
569 | 574 | for reviewer_object in reviewer_objects: |
|
570 | 575 | reviewer_reasons = [] |
|
571 | 576 | if isinstance(reviewer_object, (basestring, int)): |
|
572 | 577 | reviewer_username = reviewer_object |
|
573 | 578 | else: |
|
574 | 579 | reviewer_username = reviewer_object['username'] |
|
575 | 580 | reviewer_reasons = reviewer_object.get('reasons', []) |
|
576 | 581 | |
|
577 | 582 | user = get_user_or_error(reviewer_username) |
|
578 | 583 | reviewers_reasons.append((user.user_id, reviewer_reasons)) |
|
579 | 584 | |
|
580 | 585 | pull_request_model = PullRequestModel() |
|
581 | 586 | pull_request = pull_request_model.create( |
|
582 | 587 | created_by=apiuser.user_id, |
|
583 | 588 | source_repo=source_repo, |
|
584 | 589 | source_ref=full_source_ref, |
|
585 | 590 | target_repo=target_repo, |
|
586 | 591 | target_ref=full_target_ref, |
|
587 | 592 | revisions=reversed( |
|
588 | 593 | [commit.raw_id for commit in reversed(commit_ranges)]), |
|
589 | 594 | reviewers=reviewers_reasons, |
|
590 | 595 | title=title, |
|
591 | 596 | description=Optional.extract(description) |
|
592 | 597 | ) |
|
593 | 598 | |
|
594 | 599 | Session().commit() |
|
595 | 600 | data = { |
|
596 | 601 | 'msg': 'Created new pull request `{}`'.format(title), |
|
597 | 602 | 'pull_request_id': pull_request.pull_request_id, |
|
598 | 603 | } |
|
599 | 604 | return data |
|
600 | 605 | |
|
601 | 606 | |
|
602 | 607 | @jsonrpc_method() |
|
603 | 608 | def update_pull_request( |
|
604 | 609 | request, apiuser, repoid, pullrequestid, title=Optional(''), |
|
605 | 610 | description=Optional(''), reviewers=Optional(None), |
|
606 | 611 | update_commits=Optional(None), close_pull_request=Optional(None)): |
|
607 | 612 | """ |
|
608 | 613 | Updates a pull request. |
|
609 | 614 | |
|
610 | 615 | :param apiuser: This is filled automatically from the |authtoken|. |
|
611 | 616 | :type apiuser: AuthUser |
|
612 | 617 | :param repoid: The repository name or repository ID. |
|
613 | 618 | :type repoid: str or int |
|
614 | 619 | :param pullrequestid: The pull request ID. |
|
615 | 620 | :type pullrequestid: int |
|
616 | 621 | :param title: Set the pull request title. |
|
617 | 622 | :type title: str |
|
618 | 623 | :param description: Update pull request description. |
|
619 | 624 | :type description: Optional(str) |
|
620 | 625 | :param reviewers: Update pull request reviewers list with new value. |
|
621 | 626 | :type reviewers: Optional(list) |
|
622 | 627 | :param update_commits: Trigger update of commits for this pull request |
|
623 | 628 | :type: update_commits: Optional(bool) |
|
624 | 629 | :param close_pull_request: Close this pull request with rejected state |
|
625 | 630 | :type: close_pull_request: Optional(bool) |
|
626 | 631 | |
|
627 | 632 | Example output: |
|
628 | 633 | |
|
629 | 634 | .. code-block:: bash |
|
630 | 635 | |
|
631 | 636 | id : <id_given_in_input> |
|
632 | 637 | result : { |
|
633 | 638 | "msg": "Updated pull request `63`", |
|
634 | 639 | "pull_request": <pull_request_object>, |
|
635 | 640 | "updated_reviewers": { |
|
636 | 641 | "added": [ |
|
637 | 642 | "username" |
|
638 | 643 | ], |
|
639 | 644 | "removed": [] |
|
640 | 645 | }, |
|
641 | 646 | "updated_commits": { |
|
642 | 647 | "added": [ |
|
643 | 648 | "<sha1_hash>" |
|
644 | 649 | ], |
|
645 | 650 | "common": [ |
|
646 | 651 | "<sha1_hash>", |
|
647 | 652 | "<sha1_hash>", |
|
648 | 653 | ], |
|
649 | 654 | "removed": [] |
|
650 | 655 | } |
|
651 | 656 | } |
|
652 | 657 | error : null |
|
653 | 658 | """ |
|
654 | 659 | |
|
655 | 660 | repo = get_repo_or_error(repoid) |
|
656 | 661 | pull_request = get_pull_request_or_error(pullrequestid) |
|
657 | 662 | if not PullRequestModel().check_user_update( |
|
658 | 663 | pull_request, apiuser, api=True): |
|
659 | 664 | raise JSONRPCError( |
|
660 | 665 | 'pull request `%s` update failed, no permission to update.' % ( |
|
661 | 666 | pullrequestid,)) |
|
662 | 667 | if pull_request.is_closed(): |
|
663 | 668 | raise JSONRPCError( |
|
664 | 669 | 'pull request `%s` update failed, pull request is closed' % ( |
|
665 | 670 | pullrequestid,)) |
|
666 | 671 | |
|
667 | 672 | reviewer_objects = Optional.extract(reviewers) or [] |
|
668 | 673 | if not isinstance(reviewer_objects, list): |
|
669 | 674 | raise JSONRPCError('reviewers should be specified as a list') |
|
670 | 675 | |
|
671 | 676 | reviewers_reasons = [] |
|
672 | 677 | reviewer_ids = set() |
|
673 | 678 | for reviewer_object in reviewer_objects: |
|
674 | 679 | reviewer_reasons = [] |
|
675 | 680 | if isinstance(reviewer_object, (int, basestring)): |
|
676 | 681 | reviewer_username = reviewer_object |
|
677 | 682 | else: |
|
678 | 683 | reviewer_username = reviewer_object['username'] |
|
679 | 684 | reviewer_reasons = reviewer_object.get('reasons', []) |
|
680 | 685 | |
|
681 | 686 | user = get_user_or_error(reviewer_username) |
|
682 | 687 | reviewer_ids.add(user.user_id) |
|
683 | 688 | reviewers_reasons.append((user.user_id, reviewer_reasons)) |
|
684 | 689 | |
|
685 | 690 | title = Optional.extract(title) |
|
686 | 691 | description = Optional.extract(description) |
|
687 | 692 | if title or description: |
|
688 | 693 | PullRequestModel().edit( |
|
689 | 694 | pull_request, title or pull_request.title, |
|
690 | 695 | description or pull_request.description) |
|
691 | 696 | Session().commit() |
|
692 | 697 | |
|
693 | 698 | commit_changes = {"added": [], "common": [], "removed": []} |
|
694 | 699 | if str2bool(Optional.extract(update_commits)): |
|
695 | 700 | if PullRequestModel().has_valid_update_type(pull_request): |
|
696 | 701 | update_response = PullRequestModel().update_commits( |
|
697 | 702 | pull_request) |
|
698 | 703 | commit_changes = update_response.changes or commit_changes |
|
699 | 704 | Session().commit() |
|
700 | 705 | |
|
701 | 706 | reviewers_changes = {"added": [], "removed": []} |
|
702 | 707 | if reviewer_ids: |
|
703 | 708 | added_reviewers, removed_reviewers = \ |
|
704 | 709 | PullRequestModel().update_reviewers(pull_request, reviewers_reasons) |
|
705 | 710 | |
|
706 | 711 | reviewers_changes['added'] = sorted( |
|
707 | 712 | [get_user_or_error(n).username for n in added_reviewers]) |
|
708 | 713 | reviewers_changes['removed'] = sorted( |
|
709 | 714 | [get_user_or_error(n).username for n in removed_reviewers]) |
|
710 | 715 | Session().commit() |
|
711 | 716 | |
|
712 | 717 | if str2bool(Optional.extract(close_pull_request)): |
|
713 | 718 | PullRequestModel().close_pull_request_with_comment( |
|
714 | 719 | pull_request, apiuser, repo) |
|
715 | 720 | Session().commit() |
|
716 | 721 | |
|
717 | 722 | data = { |
|
718 | 723 | 'msg': 'Updated pull request `{}`'.format( |
|
719 | 724 | pull_request.pull_request_id), |
|
720 | 725 | 'pull_request': pull_request.get_api_data(), |
|
721 | 726 | 'updated_commits': commit_changes, |
|
722 | 727 | 'updated_reviewers': reviewers_changes |
|
723 | 728 | } |
|
724 | 729 | |
|
725 | 730 | return data |
General Comments 0
You need to be logged in to leave comments.
Login now