Add Create/Delete ReviewRequests (#493)

close #439

Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/493
Reviewed-by: khmarbaise <khmarbaise@noreply.gitea.io>
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: 6543 <6543@obermui.de>
Co-committed-by: 6543 <6543@obermui.de>
This commit is contained in:
6543 2021-02-17 23:03:45 +08:00 committed by Lunny Xiao
parent b0ee740ed0
commit a968e32ca1
2 changed files with 67 additions and 5 deletions

View file

@ -102,6 +102,12 @@ type DismissPullReviewOptions struct {
Message string `json:"message"`
}
// PullReviewRequestOptions are options to add or remove pull review requests
type PullReviewRequestOptions struct {
Reviewers []string `json:"reviewers"`
TeamReviewers []string `json:"team_reviewers"`
}
// ListPullReviewsOptions options for listing PullReviews
type ListPullReviewsOptions struct {
ListOptions
@ -227,6 +233,38 @@ func (c *Client) SubmitPullReview(owner, repo string, index, id int64, opt Submi
return r, resp, err
}
// CreateReviewRequests create review requests to an pull request
func (c *Client) CreateReviewRequests(owner, repo string, index int64, opt PullReviewRequestOptions) (*Response, error) {
if err := c.checkServerVersionGreaterThanOrEqual(version1_14_0); err != nil {
return nil, err
}
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
_, resp, err := c.getResponse("POST",
fmt.Sprintf("/repos/%s/%s/pulls/%d/requested_reviewers", owner, repo, index),
jsonHeader, bytes.NewReader(body))
return resp, err
}
// DeleteReviewRequests delete review requests to an pull request
func (c *Client) DeleteReviewRequests(owner, repo string, index int64, opt PullReviewRequestOptions) (*Response, error) {
if err := c.checkServerVersionGreaterThanOrEqual(version1_14_0); err != nil {
return nil, err
}
body, err := json.Marshal(&opt)
if err != nil {
return nil, err
}
_, resp, err := c.getResponse("DELETE",
fmt.Sprintf("/repos/%s/%s/pulls/%d/requested_reviewers", owner, repo, index),
jsonHeader, bytes.NewReader(body))
return resp, err
}
// DismissPullReview dismiss a review for a pull request
func (c *Client) DismissPullReview(owner, repo string, index, id int64, opt DismissPullReviewOptions) (*Response, error) {
if err := c.checkServerVersionGreaterThanOrEqual(version1_14_0); err != nil {

View file

@ -20,8 +20,6 @@ func TestPullReview(t *testing.T) {
if !success {
return
}
defer c.AdminDeleteUser(reviewer.UserName)
defer c.AdminDeleteUser(submitter.UserName)
// CreatePullReview
r1, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
@ -64,9 +62,7 @@ func TestPullReview(t *testing.T) {
// ListPullReviews
c.SetSudo("")
rl, _, err := c.ListPullReviews(repo.Owner.UserName, repo.Name, pull.Index, ListPullReviewsOptions{})
if !assert.NoError(t, err) {
return
}
assert.NoError(t, err)
assert.Len(t, rl, 3)
for i := range rl {
assert.EqualValues(t, pull.HTMLURL, rl[i].HTMLPullURL)
@ -155,6 +151,34 @@ func TestPullReview(t *testing.T) {
}
r, _, _ = c.GetPullReview(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
assert.False(t, r.Dismissed)
rl, _, err = c.ListPullReviews(repo.Owner.UserName, repo.Name, pull.Index, ListPullReviewsOptions{})
assert.NoError(t, err)
assert.Len(t, rl, 3)
c.SetSudo(submitter.UserName)
resp, err = c.CreateReviewRequests(repo.Owner.UserName, repo.Name, pull.Index, PullReviewRequestOptions{Reviewers: []string{reviewer.UserName}})
assert.NoError(t, err)
assert.NotNil(t, resp)
rl, _, _ = c.ListPullReviews(repo.Owner.UserName, repo.Name, pull.Index, ListPullReviewsOptions{})
if assert.Len(t, rl, 4) {
assert.EqualValues(t, ReviewStateRequestReview, rl[3].State)
}
c.SetSudo(reviewer.UserName)
resp, err = c.DeleteReviewRequests(repo.Owner.UserName, repo.Name, pull.Index, PullReviewRequestOptions{Reviewers: []string{reviewer.UserName}})
assert.NoError(t, err)
assert.NotNil(t, resp)
rl, _, _ = c.ListPullReviews(repo.Owner.UserName, repo.Name, pull.Index, ListPullReviewsOptions{})
assert.Len(t, rl, 3)
c.SetSudo("")
_, err = c.AdminDeleteUser(reviewer.UserName)
assert.NoError(t, err)
_, err = c.AdminDeleteUser(submitter.UserName)
assert.NoError(t, err)
}
func preparePullReviewTest(t *testing.T, c *Client, repoName string) (*Repository, *PullRequest, *User, *User, bool) {