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