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 29 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
10 changes: 5 additions & 5 deletions integrations/api_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,15 @@ func TestAPISearchIssues(t *testing.T) {
var apiIssues []*api.Issue
DecodeJSON(t, resp, &apiIssues)

assert.Len(t, apiIssues, 9)
assert.Len(t, apiIssues, 10)

query := url.Values{}
query.Add("token", token)
link.RawQuery = query.Encode()
req = NewRequest(t, "GET", link.String())
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiIssues)
assert.Len(t, apiIssues, 9)
assert.Len(t, apiIssues, 10)

query.Add("state", "closed")
link.RawQuery = query.Encode()
Expand All @@ -182,7 +182,7 @@ func TestAPISearchIssues(t *testing.T) {
req = NewRequest(t, "GET", link.String())
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiIssues)
assert.Len(t, apiIssues, 1)
assert.Len(t, apiIssues, 2)
}

func TestAPISearchIssuesWithLabels(t *testing.T) {
Expand All @@ -197,15 +197,15 @@ func TestAPISearchIssuesWithLabels(t *testing.T) {
var apiIssues []*api.Issue
DecodeJSON(t, resp, &apiIssues)

assert.Len(t, apiIssues, 9)
assert.Len(t, apiIssues, 10)

query := url.Values{}
query.Add("token", token)
link.RawQuery = query.Encode()
req = NewRequest(t, "GET", link.String())
resp = session.MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &apiIssues)
assert.Len(t, apiIssues, 9)
assert.Len(t, apiIssues, 10)

query.Add("labels", "label1")
link.RawQuery = query.Encode()
Expand Down
83 changes: 83 additions & 0 deletions integrations/api_pull_review_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,86 @@ 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)

// 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{"[email protected]", "user8"},
})
session.MakeRequest(t, req, http.StatusCreated)

// poster of pr can't be reviewer
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"},
})
session.MakeRequest(t, req, http.StatusUnprocessableEntity)

6543 marked this conversation as resolved.
Show resolved Hide resolved
// test user not exist
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{"testOther"},
})
session.MakeRequest(t, req, http.StatusNotFound)

// 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{"user4"},
})
session.MakeRequest(t, req, http.StatusNoContent)

// doer is not admin
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{"user8"},
})
session.MakeRequest(t, req, http.StatusUnprocessableEntity)

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"},
})
session.MakeRequest(t, req, http.StatusNoContent)

// Test team review request
pullIssue12 := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 12}).(*models.Issue)
assert.NoError(t, pullIssue12.LoadAttributes())
repo3 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: pullIssue12.RepoID}).(*models.Repository)

// Test add Team Review Request
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo3.OwnerName, repo3.Name, pullIssue12.Index, token), &api.PullReviewRequestOptions{
TeamReviewers: []string{"team1", "owners"},
})
session.MakeRequest(t, req, http.StatusCreated)

// Test add Team Review Request to not allowned
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo3.OwnerName, repo3.Name, pullIssue12.Index, token), &api.PullReviewRequestOptions{
TeamReviewers: []string{"test_team"},
})
session.MakeRequest(t, req, http.StatusUnprocessableEntity)

// Test add Team Review Request to not exist
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo3.OwnerName, repo3.Name, pullIssue12.Index, token), &api.PullReviewRequestOptions{
TeamReviewers: []string{"not_exist_team"},
})
session.MakeRequest(t, req, http.StatusNotFound)

// Test Remove team Review Request
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo3.OwnerName, repo3.Name, pullIssue12.Index, token), &api.PullReviewRequestOptions{
TeamReviewers: []string{"team1"},
})
session.MakeRequest(t, req, http.StatusNoContent)

// empty request test
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo3.OwnerName, repo3.Name, pullIssue12.Index, token), &api.PullReviewRequestOptions{})
session.MakeRequest(t, req, http.StatusCreated)

req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers?token=%s", repo3.OwnerName, repo3.Name, pullIssue12.Index, token), &api.PullReviewRequestOptions{})
session.MakeRequest(t, req, http.StatusNoContent)
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d22b4d4daa5be07329fcef6ed458f00cf3392da0
2 changes: 1 addition & 1 deletion models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2003,7 +2003,7 @@ type ErrNotValidReviewRequest struct {

// IsErrNotValidReviewRequest checks if an error is a ErrNotValidReviewRequest.
func IsErrNotValidReviewRequest(err error) bool {
_, ok := err.(ErrReviewNotExist)
_, ok := err.(ErrNotValidReviewRequest)
return ok
}

Expand Down
12 changes: 12 additions & 0 deletions models/fixtures/issue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,15 @@
is_pull: true
created_unix: 1579194806
updated_unix: 1579194806

-
id: 12
repo_id: 3
index: 2
poster_id: 2
name: pull6
content: content for the a pull request
is_closed: false
is_pull: true
created_unix: 1602935696
updated_unix: 1602935696
13 changes: 13 additions & 0 deletions models/fixtures/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,16 @@
base_branch: branch1
merge_base: 1234567890abcdef
has_merged: false

-
id: 6
type: 0 # gitea pull request
status: 2 # mergable
issue_id: 12
index: 2
head_repo_id: 3
base_repo_id: 3
head_branch: test_branch
base_branch: master
merge_base: 2a47ca4b614a9f5a
has_merged: false
2 changes: 1 addition & 1 deletion models/fixtures/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
is_private: true
num_issues: 1
num_closed_issues: 0
num_pulls: 0
num_pulls: 1
num_closed_pulls: 0
num_watches: 0
num_projects: 1
Expand Down
10 changes: 10 additions & 0 deletions models/fixtures/review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,13 @@
official: true
updated_unix: 946684815
created_unix: 946684815

-
id: 11
type: 4
reviewer_id: 0
reviewer_team_id: 7
issue_id: 12
official: true
updated_unix: 1602936509
created_unix: 1602936509
13 changes: 10 additions & 3 deletions models/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,13 +627,14 @@ func AddReviewRequest(issue *Issue, reviewer, doer *User) (*Comment, error) {
}
}

if _, err = createReview(sess, CreateReviewOptions{
review, err = createReview(sess, CreateReviewOptions{
Type: ReviewTypeRequest,
Issue: issue,
Reviewer: reviewer,
Official: official,
Stale: false,
}); err != nil {
})
if err != nil {
return nil, err
}

Expand All @@ -644,6 +645,7 @@ func AddReviewRequest(issue *Issue, reviewer, doer *User) (*Comment, error) {
Issue: issue,
RemovedAssignee: false, // Use RemovedAssignee as !isRequest
AssigneeID: reviewer.ID, // Use AssigneeID as reviewer ID
ReviewID: review.ID,
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -732,7 +734,7 @@ func AddTeamReviewRequest(issue *Issue, reviewer *Team, doer *User) (*Comment, e
}
}

if _, err = createReview(sess, CreateReviewOptions{
if review, err = createReview(sess, CreateReviewOptions{
Type: ReviewTypeRequest,
Issue: issue,
ReviewerTeam: reviewer,
Expand All @@ -755,6 +757,7 @@ func AddTeamReviewRequest(issue *Issue, reviewer *Team, doer *User) (*Comment, e
Issue: issue,
RemovedAssignee: false, // Use RemovedAssignee as !isRequest
AssigneeTeamID: reviewer.ID, // Use AssigneeTeamID as reviewer team ID
ReviewID: review.ID,
})
if err != nil {
return nil, fmt.Errorf("createComment(): %v", err)
Expand Down Expand Up @@ -894,6 +897,10 @@ func DeleteReview(r *Review) error {
return fmt.Errorf("review is not allowed to be 0")
}

if r.Type == ReviewTypeRequest {
return fmt.Errorf("review request can not be deleted using this method")
}

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

// PullReviewRequestOptions are options to add or remove pull review requests
type PullReviewRequestOptions struct {
Reviewers []string `json:"reviewers"`
TeamReviewers []string `json:"team_reviewers"`
}
4 changes: 3 additions & 1 deletion routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,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
Loading