Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add review request api #11355

Merged
merged 33 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
d957b8f
Add review request api
a1012112796 May 9, 2020
b5c8f0a
Merge branch 'master' into review_request_api
a1012112796 May 9, 2020
7f877a3
make fmt
a1012112796 May 9, 2020
67ee5f2
Merge branch 'master' into review_request_api
a1012112796 May 10, 2020
1da62b1
fix bug
a1012112796 May 11, 2020
b69744d
fix test code
a1012112796 May 11, 2020
1f4eb57
fix typo
a1012112796 May 11, 2020
266c1c9
Merge branch 'master' into review_request_api
a1012112796 May 11, 2020
5ff74bd
Apply suggestion from code review @jonasfranz
a1012112796 May 11, 2020
233a446
Merge branch 'master' into review_request_api
a1012112796 May 11, 2020
59304f2
fix swagger ref
a1012112796 May 12, 2020
0c10a4d
fix typo
a1012112796 May 12, 2020
58ddaf6
Merge branch 'master' into review_request_api
a1012112796 May 12, 2020
ab85502
fix comment
a1012112796 May 12, 2020
28cdcd6
Merge branch 'master' into review_request_api
a1012112796 May 12, 2020
66498af
Change response message
a1012112796 May 13, 2020
c4c9817
Merge branch 'master' into review_request_api
a1012112796 May 13, 2020
a835527
Merge branch 'master' into review_request_api
a1012112796 May 16, 2020
b283e6f
chang response so some simplfy
a1012112796 May 19, 2020
8350955
Merge branch 'master' into review_request_api
a1012112796 May 19, 2020
dd21972
Add ErrIllLegalReviewRequest
a1012112796 May 21, 2020
d4e28c7
Merge branch 'master' into review_request_api
a1012112796 May 21, 2020
491a197
make fmt
a1012112796 May 21, 2020
49f8a4e
Apply suggestions from code review
a1012112796 Jun 9, 2020
6f989b2
Merge branch 'master' into review_request_api
a1012112796 Oct 17, 2020
c1b39aa
* Add team support
a1012112796 Oct 17, 2020
4ccc8a7
fix nit
a1012112796 Oct 17, 2020
ef9246d
fix test
a1012112796 Oct 17, 2020
134d8d0
Merge branch 'master' into review_request_api
a1012112796 Oct 19, 2020
0d85842
Apply suggestions from code review
a1012112796 Oct 20, 2020
9127291
Merge branch 'master' into review_request_api
zeripath Oct 20, 2020
551fe18
update get api and add test
a1012112796 Oct 20, 2020
415dde7
Merge remote-tracking branch 'master' into review_request_api
a1012112796 Oct 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions integrations/api_pull_review_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,74 @@ func TestAPIPullReview(t *testing.T) {
req = NewRequestf(t, http.MethodDelete, "/api/v1/repos/%s/%s/pulls/%d/reviews/%d?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, review.ID, token)
resp = session.MakeRequest(t, req, http.StatusNoContent)
}

func TestAPIPullReviewRequest(t *testing.T) {
defer prepareTestEnv(t)()
pullIssue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 3}).(*models.Issue)
assert.NoError(t, pullIssue.LoadAttributes())
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue.RepoID}).(*models.Repository)

var result api.PullReviewRequestResult

// Test add Review Request
session := loginUser(t, "user2")
token := getTokenForLoggedInUser(t, session)
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token), &api.PullReviewRequestOptions{
Reviewers: []string{"user4"},
})
resp := session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &result)

assert.EqualValues(t, 0, len(result.Failures))
assert.EqualValues(t, 1, len(result.Successes))
assert.EqualValues(t, "user4", result.Successes[0].Reviewer.UserName)

6543 marked this conversation as resolved.
Show resolved Hide resolved
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token), &api.PullReviewRequestOptions{
Reviewers: []string{"user1", "user2", "user3", "[email protected]", "user8", "testOther"},
})
resp = session.MakeRequest(t, req, http.StatusUnprocessableEntity)
DecodeJSON(t, resp, &result)

assert.EqualValues(t, 5, len(result.Failures))
assert.EqualValues(t, 1, len(result.Successes))

assert.Contains(t, result.Failures[0].Error, "poster of pr can't be reviewer")
assert.Contains(t, result.Failures[1].Error, "doer can't be reviewer")
assert.Contains(t, result.Failures[2].Error, "Organization can't be added as reviewer")
assert.Contains(t, result.Failures[3].Error, "Has been requested to review")
assert.Contains(t, result.Failures[4].Error, "user does not exist")
assert.EqualValues(t, "user8", result.Successes[0].Reviewer.UserName)

// Test Remove Review Request
session2 := loginUser(t, "user4")
token2 := getTokenForLoggedInUser(t, session2)
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token2), &api.PullReviewRequestOptions{
Reviewers: []string{"[email protected]", "user4", "testOther"},
})
resp = session.MakeRequest(t, req, http.StatusUnprocessableEntity)
DecodeJSON(t, resp, &result)
assert.EqualValues(t, 2, len(result.Failures))
assert.EqualValues(t, 1, len(result.Successes))

assert.Contains(t, result.Failures[0].Error, "Doer is not admin")
assert.Contains(t, result.Failures[1].Error, "user does not exist")
assert.EqualValues(t, "user4", result.Successes[0].Reviewer.UserName)

req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token), &api.PullReviewRequestOptions{
Reviewers: []string{"user8"},
})
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &result)
assert.EqualValues(t, 0, len(result.Failures))
assert.EqualValues(t, 1, len(result.Successes))
assert.EqualValues(t, "user8", result.Successes[0].Reviewer.UserName)

req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo.OwnerName, repo.Name, pullIssue.Index, token), &api.PullReviewRequestOptions{
Reviewers: []string{"user8"},
})
resp = session.MakeRequest(t, req, http.StatusUnprocessableEntity)
DecodeJSON(t, resp, &result)
assert.EqualValues(t, 1, len(result.Failures))
assert.EqualValues(t, 0, len(result.Successes))
assert.Contains(t, result.Failures[0].Error, "Haven't been requested to review")
}
9 changes: 8 additions & 1 deletion models/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ func AddReviewRequest(issue *Issue, reviewer *User, doer *User) (comment *Commen
}
}

_, err = createReview(sess, CreateReviewOptions{
review, err = createReview(sess, CreateReviewOptions{
Type: ReviewTypeRequest,
Issue: issue,
Reviewer: reviewer,
Expand All @@ -539,6 +539,7 @@ func AddReviewRequest(issue *Issue, reviewer *User, doer *User) (comment *Commen
Issue: issue,
RemovedAssignee: false, // Use RemovedAssignee as !isRequest
AssigneeID: reviewer.ID, // Use AssigneeID as reviewer ID
ReviewID: review.ID,
})

if err != nil {
Expand Down Expand Up @@ -605,6 +606,8 @@ func RemoveReviewRequest(issue *Issue, reviewer *User, doer *User) (comment *Com
AssigneeID: reviewer.ID, // Use AssigneeID as reviewer ID
})

comment.Review = review

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -684,6 +687,10 @@ func DeleteReview(r *Review) error {
return fmt.Errorf("review is not allowed to be 0")
}

if r.Type == ReviewTypeRequest {
return fmt.Errorf("Can't delete review request by this way")
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
}

opts := FindCommentsOptions{
Type: CommentTypeCode,
IssueID: r.IssueID,
Expand Down
17 changes: 17 additions & 0 deletions modules/structs/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,20 @@ type SubmitPullReviewOptions struct {
Event ReviewStateType `json:"event"`
Body string `json:"body"`
}

// PullReviewRequestOptions are options to add or remove pull review request
type PullReviewRequestOptions struct {
Reviewers []string `json:"reviewers"`
}

// PullReviewRequestErr are error result for add or remove pull review request
type PullReviewRequestErr struct {
Reviewer string `json:"reviewer"`
Error string `json:"error"`
}

// PullReviewRequestResult results for add or remove pull review request
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
type PullReviewRequestResult struct {
Successes []*PullReview
Failures []*PullReviewRequestErr
}
4 changes: 3 additions & 1 deletion routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,9 @@ func RegisterRoutes(m *macaron.Macaron) {
Get(repo.GetPullReviewComments)
})
})

m.Combo("/requested_reviewers").
zeripath marked this conversation as resolved.
Show resolved Hide resolved
Delete(reqToken(), bind(api.PullReviewRequestOptions{}), repo.DeletReviewRequests).
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
Post(reqToken(), bind(api.PullReviewRequestOptions{}), repo.CreatReviewRequests)
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
})
}, mustAllowPulls, reqRepoReader(models.UnitTypeCode), context.ReferencesGitRepo(false))
m.Group("/statuses", func() {
Expand Down
166 changes: 166 additions & 0 deletions routers/api/v1/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/routers/api/v1/utils"
issue_service "code.gitea.io/gitea/services/issue"
pull_service "code.gitea.io/gitea/services/pull"
)

Expand Down Expand Up @@ -539,3 +540,168 @@ func prepareSingleReview(ctx *context.APIContext) (*models.Review, *models.PullR

return review, pr, false
}

// CreatReviewRequests create review requests to an pull request
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
func CreatReviewRequests(ctx *context.APIContext, opts api.PullReviewRequestOptions) {
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
// swagger:operation POST /repos/{owner}/{repo}/pulls/{index}/requested_reviewers repository repoCreatePullReviewRequests
// ---
// summary: create review requests to an pull request
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: index
// in: path
// description: index of the pull request
// type: integer
// format: int64
// required: true
// - name: body
// in: body
// required: true
// schema:
// "$ref": "#/definitions/PullReviewRequestOptions"
// responses:
// "200":
// "$ref": "#/responses/PullReviewRequestResult"
// "422":
// "$ref": "#/responses/PullReviewRequestResult"
apiReviewRequest(ctx, opts, true)
}

// DeletReviewRequests delet review requests to an pull request
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
func DeletReviewRequests(ctx *context.APIContext, opts api.PullReviewRequestOptions) {
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
// swagger:operation DELETE /repos/{owner}/{repo}/pulls/{index}/requested_reviewers repository repoDeletPullReviewRequests
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
// ---
// summary: delet review requests to an pull request
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: index
// in: path
// description: index of the pull request
// type: integer
// format: int64
// required: true
// - name: body
// in: body
// required: true
// schema:
// "$ref": "#/definitions/PullReviewRequestOptions"
// responses:
// "200":
// "$ref": "#/responses/PullReviewRequestResult"
// "422":
// "$ref": "#/responses/PullReviewRequestResult"
apiReviewRequest(ctx, opts, false)
}

func apiReviewRequest(ctx *context.APIContext, opts api.PullReviewRequestOptions, isAdd bool) {
pr, err := models.GetPullRequestByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
if models.IsErrPullRequestNotExist(err) {
ctx.NotFound("GetPullRequestByIndex", err)
} else {
ctx.Error(http.StatusInternalServerError, "GetPullRequestByIndex", err)
}
return
}

if err := pr.Issue.LoadRepo(); err != nil {
ctx.Error(http.StatusInternalServerError, "pr.Issue.LoadRepo", err)
return
}

reviews := make([]*api.PullReview, 0, len(opts.Reviewers))
reviewerErrs := make([]*api.PullReviewRequestErr, 0, 5)

for _, r := range opts.Reviewers {
var reviewer *models.User
if strings.Contains(r, "@") {
reviewer, err = models.GetUserByEmail(r)
} else {
reviewer, err = models.GetUserByName(r)
}

if err != nil {
reviewerErrs = append(reviewerErrs, &api.PullReviewRequestErr{
Reviewer: r,
Error: err.Error(),
})
continue
}

err = issue_service.IsLegalReviewRequest(reviewer, ctx.User, isAdd, pr.Issue)
if err != nil {
reviewerErrs = append(reviewerErrs, &api.PullReviewRequestErr{
Reviewer: r,
Error: err.Error(),
})
continue
}

comment, err := issue_service.ReviewRequest(pr.Issue, ctx.User, reviewer, isAdd)
if err != nil {
ctx.ServerError("ReviewRequest", err)
return
}

if comment != nil {
if err = comment.LoadReview(); err != nil {
ctx.ServerError("ReviewRequest", err)
return
}
} else {
if isAdd {
reviewerErrs = append(reviewerErrs, &api.PullReviewRequestErr{
Reviewer: r,
Error: "Has been requested to review",
})
} else {
reviewerErrs = append(reviewerErrs, &api.PullReviewRequestErr{
Reviewer: r,
Error: "Haven't been requested to review",
})
}
continue
}

apiReview, err := convert.ToPullReview(comment.Review, ctx.User)
if err != nil {
ctx.Error(http.StatusInternalServerError, "convertToPullReview", err)
return
}
reviews = append(reviews, apiReview)
}

if len(reviewerErrs) == 0 {
ctx.JSON(http.StatusOK, &api.PullReviewRequestResult{
Successes: reviews,
Failures: reviewerErrs,
})
} else {
ctx.JSON(http.StatusUnprocessableEntity, &api.PullReviewRequestResult{
Successes: reviews,
Failures: reviewerErrs,
})
}
}
7 changes: 7 additions & 0 deletions routers/api/v1/swagger/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ type swaggerResponsePullReviewCommentList struct {
Body []api.PullReviewComment `json:"body"`
}

// PullReviewRequest
// swagger:response PullReviewRequestResult
type swaggerResponsePullReviewRequest struct {
// in:body
Body []api.PullReviewRequestOptions `json:"body"`
}

// Status
// swagger:response Status
type swaggerResponseStatus struct {
Expand Down
Loading