##// END OF EJS Templates
api: pull-requests, fixed invocation of merge as another user.
ergo -
r3481:b5202911 default
parent child
Show More
@@ -134,6 +134,107 class TestMergePullRequest(object):
134 134 assert_error(id_, expected, given=response.body)
135 135
136 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 238 def test_api_merge_pull_request_repo_error(self, pr_util):
138 239 pull_request = pr_util.create_pull_request()
139 240 id_, params = build_data(
@@ -285,12 +285,13 def merge_pull_request(
285 285 repo = get_repo_or_error(repoid)
286 286 else:
287 287 repo = pull_request.target_repo
288
288 auth_user = apiuser
289 289 if not isinstance(userid, Optional):
290 290 if (has_superadmin_permission(apiuser) or
291 291 HasRepoPermissionAnyApi('repository.admin')(
292 292 user=apiuser, repo_name=repo.repo_name)):
293 293 apiuser = get_user_or_error(userid)
294 auth_user = apiuser.AuthUser()
294 295 else:
295 296 raise JSONRPCError('userid is not the same as your user')
296 297
@@ -301,9 +302,8 def merge_pull_request(
301 302 pull_request.pull_request_state, PullRequest.STATE_CREATED))
302 303
303 304 with pull_request.set_state(PullRequest.STATE_UPDATING):
304 check = MergeCheck.validate(
305 pull_request, auth_user=apiuser,
306 translator=request.translate)
305 check = MergeCheck.validate(pull_request, auth_user=auth_user,
306 translator=request.translate)
307 307 merge_possible = not check.failed
308 308
309 309 if not merge_possible:
@@ -319,14 +319,13 def merge_pull_request(
319 319 target_repo = pull_request.target_repo
320 320 extras = vcs_operation_context(
321 321 request.environ, repo_name=target_repo.repo_name,
322 username=apiuser.username, action='push',
322 username=auth_user.username, action='push',
323 323 scm=target_repo.repo_type)
324 324 with pull_request.set_state(PullRequest.STATE_UPDATING):
325 325 merge_response = PullRequestModel().merge_repo(
326 326 pull_request, apiuser, extras=extras)
327 327 if merge_response.executed:
328 PullRequestModel().close_pull_request(
329 pull_request.pull_request_id, apiuser)
328 PullRequestModel().close_pull_request(pull_request.pull_request_id, auth_user)
330 329
331 330 Session().commit()
332 331
@@ -494,11 +493,13 def comment_pull_request(
494 493 else:
495 494 repo = pull_request.target_repo
496 495
496 auth_user = apiuser
497 497 if not isinstance(userid, Optional):
498 498 if (has_superadmin_permission(apiuser) or
499 499 HasRepoPermissionAnyApi('repository.admin')(
500 500 user=apiuser, repo_name=repo.repo_name)):
501 501 apiuser = get_user_or_error(userid)
502 auth_user = apiuser.AuthUser()
502 503 else:
503 504 raise JSONRPCError('userid is not the same as your user')
504 505
@@ -574,7 +575,7 def comment_pull_request(
574 575 renderer=renderer,
575 576 comment_type=comment_type,
576 577 resolves_comment_id=resolves_comment_id,
577 auth_user=apiuser
578 auth_user=auth_user
578 579 )
579 580
580 581 if allowed_to_change_status and status:
General Comments 0
You need to be logged in to leave comments. Login now