##// END OF EJS Templates
api: pull-requests, fixed invocation of merge as another user.
ergo -
r3481:b5202911 default
parent child Browse files
Show More
@@ -134,6 +134,107 b' class TestMergePullRequest(object):'
134 assert_error(id_, expected, given=response.body)
134 assert_error(id_, expected, given=response.body)
135
135
136 @pytest.mark.backends("git", "hg")
136 @pytest.mark.backends("git", "hg")
137 def test_api_merge_pull_request_as_another_user_no_perms_to_merge(
138 self, pr_util, no_notifications, user_util):
139 merge_user = user_util.create_user()
140 merge_user_id = merge_user.user_id
141 merge_user_username = merge_user.username
142
143 pull_request = pr_util.create_pull_request(mergeable=True, approved=True)
144
145 pull_request_id = pull_request.pull_request_id
146 pull_request_repo = pull_request.target_repo.repo_name
147
148 id_, params = build_data(
149 self.apikey, 'comment_pull_request',
150 repoid=pull_request_repo,
151 pullrequestid=pull_request_id,
152 status='approved')
153
154 response = api_call(self.app, params)
155 expected = {
156 'comment_id': response.json.get('result', {}).get('comment_id'),
157 'pull_request_id': pull_request_id,
158 'status': {'given': 'approved', 'was_changed': True}
159 }
160 assert_ok(id_, expected, given=response.body)
161 id_, params = build_data(
162 self.apikey, 'merge_pull_request',
163 repoid=pull_request_repo,
164 pullrequestid=pull_request_id,
165 userid=merge_user_id
166 )
167
168 response = api_call(self.app, params)
169 expected = 'merge not possible for following reasons: User `{}` ' \
170 'not allowed to perform merge.'.format(merge_user_username)
171 assert_error(id_, expected, response.body)
172
173 @pytest.mark.backends("git", "hg")
174 def test_api_merge_pull_request_as_another_user(self, pr_util, no_notifications, user_util):
175 merge_user = user_util.create_user()
176 merge_user_id = merge_user.user_id
177 pull_request = pr_util.create_pull_request(mergeable=True, approved=True)
178 user_util.grant_user_permission_to_repo(
179 pull_request.target_repo, merge_user, 'repository.write')
180 author = pull_request.user_id
181 repo = pull_request.target_repo.repo_id
182 pull_request_id = pull_request.pull_request_id
183 pull_request_repo = pull_request.target_repo.repo_name
184
185 id_, params = build_data(
186 self.apikey, 'comment_pull_request',
187 repoid=pull_request_repo,
188 pullrequestid=pull_request_id,
189 status='approved')
190
191 response = api_call(self.app, params)
192 expected = {
193 'comment_id': response.json.get('result', {}).get('comment_id'),
194 'pull_request_id': pull_request_id,
195 'status': {'given': 'approved', 'was_changed': True}
196 }
197 assert_ok(id_, expected, given=response.body)
198
199 id_, params = build_data(
200 self.apikey, 'merge_pull_request',
201 repoid=pull_request_repo,
202 pullrequestid=pull_request_id,
203 userid=merge_user_id
204 )
205
206 response = api_call(self.app, params)
207
208 pull_request = PullRequest.get(pull_request_id)
209
210 expected = {
211 'executed': True,
212 'failure_reason': 0,
213 'merge_status_message': 'This pull request can be automatically merged.',
214 'possible': True,
215 'merge_commit_id': pull_request.shadow_merge_ref.commit_id,
216 'merge_ref': pull_request.shadow_merge_ref._asdict()
217 }
218
219 assert_ok(id_, expected, response.body)
220
221 journal = UserLog.query() \
222 .filter(UserLog.user_id == merge_user_id) \
223 .filter(UserLog.repository_id == repo) \
224 .order_by('user_log_id') \
225 .all()
226 assert journal[-2].action == 'repo.pull_request.merge'
227 assert journal[-1].action == 'repo.pull_request.close'
228
229 id_, params = build_data(
230 self.apikey, 'merge_pull_request',
231 repoid=pull_request_repo, pullrequestid=pull_request_id, userid=merge_user_id)
232 response = api_call(self.app, params)
233
234 expected = 'merge not possible for following reasons: This pull request is closed.'
235 assert_error(id_, expected, given=response.body)
236
237 @pytest.mark.backends("git", "hg")
137 def test_api_merge_pull_request_repo_error(self, pr_util):
238 def test_api_merge_pull_request_repo_error(self, pr_util):
138 pull_request = pr_util.create_pull_request()
239 pull_request = pr_util.create_pull_request()
139 id_, params = build_data(
240 id_, params = build_data(
@@ -285,12 +285,13 b' def merge_pull_request('
285 repo = get_repo_or_error(repoid)
285 repo = get_repo_or_error(repoid)
286 else:
286 else:
287 repo = pull_request.target_repo
287 repo = pull_request.target_repo
288
288 auth_user = apiuser
289 if not isinstance(userid, Optional):
289 if not isinstance(userid, Optional):
290 if (has_superadmin_permission(apiuser) or
290 if (has_superadmin_permission(apiuser) or
291 HasRepoPermissionAnyApi('repository.admin')(
291 HasRepoPermissionAnyApi('repository.admin')(
292 user=apiuser, repo_name=repo.repo_name)):
292 user=apiuser, repo_name=repo.repo_name)):
293 apiuser = get_user_or_error(userid)
293 apiuser = get_user_or_error(userid)
294 auth_user = apiuser.AuthUser()
294 else:
295 else:
295 raise JSONRPCError('userid is not the same as your user')
296 raise JSONRPCError('userid is not the same as your user')
296
297
@@ -301,9 +302,8 b' def merge_pull_request('
301 pull_request.pull_request_state, PullRequest.STATE_CREATED))
302 pull_request.pull_request_state, PullRequest.STATE_CREATED))
302
303
303 with pull_request.set_state(PullRequest.STATE_UPDATING):
304 with pull_request.set_state(PullRequest.STATE_UPDATING):
304 check = MergeCheck.validate(
305 check = MergeCheck.validate(pull_request, auth_user=auth_user,
305 pull_request, auth_user=apiuser,
306 translator=request.translate)
306 translator=request.translate)
307 merge_possible = not check.failed
307 merge_possible = not check.failed
308
308
309 if not merge_possible:
309 if not merge_possible:
@@ -319,14 +319,13 b' def merge_pull_request('
319 target_repo = pull_request.target_repo
319 target_repo = pull_request.target_repo
320 extras = vcs_operation_context(
320 extras = vcs_operation_context(
321 request.environ, repo_name=target_repo.repo_name,
321 request.environ, repo_name=target_repo.repo_name,
322 username=apiuser.username, action='push',
322 username=auth_user.username, action='push',
323 scm=target_repo.repo_type)
323 scm=target_repo.repo_type)
324 with pull_request.set_state(PullRequest.STATE_UPDATING):
324 with pull_request.set_state(PullRequest.STATE_UPDATING):
325 merge_response = PullRequestModel().merge_repo(
325 merge_response = PullRequestModel().merge_repo(
326 pull_request, apiuser, extras=extras)
326 pull_request, apiuser, extras=extras)
327 if merge_response.executed:
327 if merge_response.executed:
328 PullRequestModel().close_pull_request(
328 PullRequestModel().close_pull_request(pull_request.pull_request_id, auth_user)
329 pull_request.pull_request_id, apiuser)
330
329
331 Session().commit()
330 Session().commit()
332
331
@@ -494,11 +493,13 b' def comment_pull_request('
494 else:
493 else:
495 repo = pull_request.target_repo
494 repo = pull_request.target_repo
496
495
496 auth_user = apiuser
497 if not isinstance(userid, Optional):
497 if not isinstance(userid, Optional):
498 if (has_superadmin_permission(apiuser) or
498 if (has_superadmin_permission(apiuser) or
499 HasRepoPermissionAnyApi('repository.admin')(
499 HasRepoPermissionAnyApi('repository.admin')(
500 user=apiuser, repo_name=repo.repo_name)):
500 user=apiuser, repo_name=repo.repo_name)):
501 apiuser = get_user_or_error(userid)
501 apiuser = get_user_or_error(userid)
502 auth_user = apiuser.AuthUser()
502 else:
503 else:
503 raise JSONRPCError('userid is not the same as your user')
504 raise JSONRPCError('userid is not the same as your user')
504
505
@@ -574,7 +575,7 b' def comment_pull_request('
574 renderer=renderer,
575 renderer=renderer,
575 comment_type=comment_type,
576 comment_type=comment_type,
576 resolves_comment_id=resolves_comment_id,
577 resolves_comment_id=resolves_comment_id,
577 auth_user=apiuser
578 auth_user=auth_user
578 )
579 )
579
580
580 if allowed_to_change_status and status:
581 if allowed_to_change_status and status:
General Comments 0
You need to be logged in to leave comments. Login now