Skip to content

Commit 7c6cd3e

Browse files
committed
Fix tests
1 parent cf26f7b commit 7c6cd3e

File tree

5 files changed

+25
-9
lines changed

5 files changed

+25
-9
lines changed

models/issues/pull.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ func (pr *PullRequest) LoadIssue(ctx context.Context) (err error) {
353353
pr.Issue, err = GetIssueByID(ctx, pr.IssueID)
354354
if err == nil {
355355
pr.Issue.PullRequest = pr
356+
pr.Issue.Repo = pr.BaseRepo
356357
}
357358
return err
358359
}

routers/api/v1/repo/pull_review.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ func CreateReviewRequests(ctx *context.APIContext) {
611611

612612
opts := web.GetForm(ctx).(*api.PullReviewRequestOptions)
613613

614-
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64(":index"))
614+
// this will load issue
615+
pr, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index"))
615616
if err != nil {
616617
if issues_model.IsErrPullRequestNotExist(err) {
617618
ctx.NotFound("GetPullRequestByIndex", err)
@@ -621,6 +622,8 @@ func CreateReviewRequests(ctx *context.APIContext) {
621622
return
622623
}
623624

625+
pr.Issue.Repo = ctx.Repo.Repository
626+
624627
allowedUsers, err := pull_service.GetReviewers(ctx, ctx.Repo.Repository, ctx.Doer.ID, pr.Issue.PosterID)
625628
if err != nil {
626629
ctx.Error(http.StatusInternalServerError, "GetReviewers", err)

services/pull/review_request.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,11 @@ func isValidReviewRequest(ctx context.Context, reviewer, doer *user_model.User,
212212
}
213213
}
214214

215+
// reviewers can remove themself
216+
if !isAdd && doer.ID == reviewer.ID {
217+
return nil
218+
}
219+
215220
if err := issue.LoadRepo(ctx); err != nil {
216221
return err
217222
}

tests/integration/actions_trigger_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,13 +622,14 @@ func TestPullRequestCommitStatusEvent(t *testing.T) {
622622
assert.NoError(t, err)
623623
checkCommitStatusAndInsertFakeStatus(t, repo, sha)
624624

625+
assert.NoError(t, pullIssue.LoadPullRequest(db.DefaultContext))
625626
// review_requested
626-
_, err = issue_service.ReviewRequest(db.DefaultContext, pullIssue, user2, nil, user4, true)
627+
_, err = pull_service.ReviewRequest(db.DefaultContext, pullIssue.PullRequest, user2, nil, user4, true)
627628
assert.NoError(t, err)
628629
checkCommitStatusAndInsertFakeStatus(t, repo, sha)
629630

630631
// review_request_removed
631-
_, err = issue_service.ReviewRequest(db.DefaultContext, pullIssue, user2, nil, user4, false)
632+
_, err = pull_service.ReviewRequest(db.DefaultContext, pullIssue.PullRequest, user2, nil, user4, false)
632633
assert.NoError(t, err)
633634
checkCommitStatusAndInsertFakeStatus(t, repo, sha)
634635
})

tests/integration/api_pull_review_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,25 +296,25 @@ func TestAPIPullReviewRequest(t *testing.T) {
296296
user38Session := loginUser(t, "user38")
297297
user38Token := getTokenForLoggedInUser(t, user38Session, auth_model.AccessTokenScopeWriteRepository)
298298
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", pull21Repo.OwnerName, pull21Repo.Name, pullIssue21.Index), &api.PullReviewRequestOptions{
299-
Reviewers: []string{"user4@example.com"},
299+
Reviewers: []string{"user40@example.com"},
300300
}).AddTokenAuth(user38Token)
301301
MakeRequest(t, req, http.StatusCreated)
302302

303303
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", pull21Repo.OwnerName, pull21Repo.Name, pullIssue21.Index), &api.PullReviewRequestOptions{
304-
Reviewers: []string{"user4@example.com"},
304+
Reviewers: []string{"user40@example.com"},
305305
}).AddTokenAuth(user38Token)
306306
MakeRequest(t, req, http.StatusNoContent)
307307

308308
// the poster of the PR can add/remove a review request
309309
user39Session := loginUser(t, "user39")
310310
user39Token := getTokenForLoggedInUser(t, user39Session, auth_model.AccessTokenScopeWriteRepository)
311311
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", pull21Repo.OwnerName, pull21Repo.Name, pullIssue21.Index), &api.PullReviewRequestOptions{
312-
Reviewers: []string{"user8"},
312+
Reviewers: []string{"user38"},
313313
}).AddTokenAuth(user39Token)
314314
MakeRequest(t, req, http.StatusCreated)
315315

316316
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", pull21Repo.OwnerName, pull21Repo.Name, pullIssue21.Index), &api.PullReviewRequestOptions{
317-
Reviewers: []string{"user8"},
317+
Reviewers: []string{"user38"},
318318
}).AddTokenAuth(user39Token)
319319
MakeRequest(t, req, http.StatusNoContent)
320320

@@ -332,14 +332,20 @@ func TestAPIPullReviewRequest(t *testing.T) {
332332
}).AddTokenAuth(user39Token) // user39 is from a team with read permission on pull requests unit
333333
MakeRequest(t, req, http.StatusNoContent)
334334

335+
// user8 is not a reviewer, so this will return 422
336+
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", pull22Repo.OwnerName, pull22Repo.Name, pullIssue22.Index), &api.PullReviewRequestOptions{
337+
Reviewers: []string{"user8"},
338+
}).AddTokenAuth(user39Token) // user39 is from a team with read permission on pull requests unit
339+
MakeRequest(t, req, http.StatusUnprocessableEntity)
340+
335341
// Test team review request
336342
pullIssue12 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 12})
337343
assert.NoError(t, pullIssue12.LoadAttributes(db.DefaultContext))
338344
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: pullIssue12.RepoID})
339345

340346
// Test add Team Review Request
341347
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo3.OwnerName, repo3.Name, pullIssue12.Index), &api.PullReviewRequestOptions{
342-
TeamReviewers: []string{"team1", "owners"},
348+
TeamReviewers: []string{"team1", "Owners"},
343349
}).AddTokenAuth(token)
344350
MakeRequest(t, req, http.StatusCreated)
345351

@@ -353,7 +359,7 @@ func TestAPIPullReviewRequest(t *testing.T) {
353359
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo3.OwnerName, repo3.Name, pullIssue12.Index), &api.PullReviewRequestOptions{
354360
TeamReviewers: []string{"not_exist_team"},
355361
}).AddTokenAuth(token)
356-
MakeRequest(t, req, http.StatusNotFound)
362+
MakeRequest(t, req, http.StatusUnprocessableEntity)
357363

358364
// Test Remove team Review Request
359365
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo3.OwnerName, repo3.Name, pullIssue12.Index), &api.PullReviewRequestOptions{

0 commit comments

Comments
 (0)