2020-05-21 19:04:48 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPullReview(t *testing.T) {
|
|
|
|
log.Println("== TestPullReview ==")
|
|
|
|
c := newTestClient()
|
|
|
|
|
2022-04-22 08:52:45 +01:00
|
|
|
repoName := "Reviews"
|
2020-05-21 19:04:48 +01:00
|
|
|
repo, pull, submitter, reviewer, success := preparePullReviewTest(t, c, repoName)
|
|
|
|
if !success {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePullReview
|
2020-09-14 03:37:09 +01:00
|
|
|
r1, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
2020-05-21 19:04:48 +01:00
|
|
|
State: ReviewStateComment,
|
|
|
|
Body: "I'll have a look at it later",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NotNil(t, r1) {
|
|
|
|
assert.EqualValues(t, ReviewStateComment, r1.State)
|
|
|
|
assert.EqualValues(t, 1, r1.Reviewer.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SetSudo(submitter.UserName)
|
2020-09-14 03:37:09 +01:00
|
|
|
r2, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
2020-05-21 19:04:48 +01:00
|
|
|
State: ReviewStateApproved,
|
|
|
|
Body: "lgtm it myself",
|
|
|
|
})
|
|
|
|
assert.Error(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
r2, _, err = c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
2020-05-21 19:04:48 +01:00
|
|
|
State: ReviewStateComment,
|
|
|
|
Body: "no seriously please have a look at it",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, r2)
|
|
|
|
|
|
|
|
c.SetSudo(reviewer.UserName)
|
2020-09-14 03:37:09 +01:00
|
|
|
r3, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
2020-05-21 19:04:48 +01:00
|
|
|
State: ReviewStateApproved,
|
|
|
|
Body: "lgtm",
|
2022-04-22 08:52:45 +01:00
|
|
|
Comments: []CreatePullReviewComment{
|
|
|
|
{
|
|
|
|
Path: "WOW-file",
|
|
|
|
Body: "no better name - really?",
|
|
|
|
NewLineNum: 1,
|
|
|
|
},
|
2020-05-21 19:04:48 +01:00
|
|
|
},
|
|
|
|
})
|
2020-05-27 14:04:51 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, r3)
|
2020-05-21 19:04:48 +01:00
|
|
|
|
|
|
|
// ListPullReviews
|
|
|
|
c.SetSudo("")
|
2020-09-14 03:37:09 +01:00
|
|
|
rl, _, err := c.ListPullReviews(repo.Owner.UserName, repo.Name, pull.Index, ListPullReviewsOptions{})
|
2021-02-17 15:03:45 +00:00
|
|
|
assert.NoError(t, err)
|
2020-05-21 19:04:48 +01:00
|
|
|
assert.Len(t, rl, 3)
|
|
|
|
for i := range rl {
|
|
|
|
assert.EqualValues(t, pull.HTMLURL, rl[i].HTMLPullURL)
|
|
|
|
if rl[i].CodeCommentsCount == 1 {
|
|
|
|
assert.EqualValues(t, reviewer.ID, rl[i].Reviewer.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPullReview
|
2020-09-14 03:37:09 +01:00
|
|
|
rNew, _, err := c.GetPullReview(repo.Owner.UserName, repo.Name, pull.Index, r3.ID)
|
2020-05-21 19:04:48 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, r3, rNew)
|
|
|
|
|
|
|
|
// DeletePullReview
|
|
|
|
c.SetSudo(submitter.UserName)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.DeletePullReview(repo.Owner.UserName, repo.Name, pull.Index, r2.ID)
|
2020-05-21 19:04:48 +01:00
|
|
|
assert.NoError(t, err)
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.DeletePullReview(repo.Owner.UserName, repo.Name, pull.Index, r3.ID)
|
2020-05-21 19:04:48 +01:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
// SubmitPullReview
|
|
|
|
c.SetSudo("")
|
2021-02-15 03:15:24 +00:00
|
|
|
r4, resp, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
2020-05-21 19:04:48 +01:00
|
|
|
Body: "...",
|
2022-04-22 08:52:45 +01:00
|
|
|
Comments: []CreatePullReviewComment{
|
|
|
|
{
|
|
|
|
Path: "WOW-file",
|
|
|
|
Body: "its ok",
|
|
|
|
NewLineNum: 1,
|
|
|
|
},
|
2020-05-21 19:04:48 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2021-02-15 03:15:24 +00:00
|
|
|
assert.NotNil(t, resp)
|
2020-09-14 03:37:09 +01:00
|
|
|
r5, _, err := c.CreatePullReview(repo.Owner.UserName, repo.Name, pull.Index, CreatePullReviewOptions{
|
2020-05-21 19:04:48 +01:00
|
|
|
Body: "...",
|
2022-04-22 08:52:45 +01:00
|
|
|
Comments: []CreatePullReviewComment{
|
|
|
|
{
|
|
|
|
Path: "WOW-file",
|
|
|
|
Body: "hehe and here it is",
|
|
|
|
NewLineNum: 3,
|
|
|
|
},
|
2020-05-21 19:04:48 +01:00
|
|
|
},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, r4.ID, r5.ID)
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
r, _, err := c.SubmitPullReview(repo.Owner.UserName, repo.Name, pull.Index, r4.ID, SubmitPullReviewOptions{
|
2020-05-21 19:04:48 +01:00
|
|
|
State: ReviewStateRequestChanges,
|
|
|
|
Body: "one nit",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, r4.ID, r.ID)
|
|
|
|
assert.EqualValues(t, ReviewStateRequestChanges, r.State)
|
|
|
|
|
|
|
|
// ListPullReviewComments
|
2020-09-14 03:37:09 +01:00
|
|
|
rcl, _, err := c.ListPullReviewComments(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
|
2020-05-21 19:04:48 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, r.CodeCommentsCount, len(rcl))
|
|
|
|
for _, rc := range rcl {
|
2020-09-07 04:00:26 +01:00
|
|
|
assert.EqualValues(t, pull.HTMLURL, rc.HTMLPullURL)
|
2020-05-21 19:04:48 +01:00
|
|
|
if rc.LineNum == 3 {
|
|
|
|
assert.EqualValues(t, "hehe and here it is", rc.Body)
|
|
|
|
} else {
|
|
|
|
assert.EqualValues(t, 1, rc.LineNum)
|
|
|
|
assert.EqualValues(t, "its ok", rc.Body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 03:15:24 +00:00
|
|
|
r, _, err = c.GetPullReview(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, r.Dismissed)
|
|
|
|
|
|
|
|
// DismissPullReview
|
|
|
|
resp, err = c.DismissPullReview(repo.Owner.UserName, repo.Name, pull.Index, r.ID, DismissPullReviewOptions{Message: "stale"})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NotNil(t, resp) {
|
|
|
|
assert.EqualValues(t, 200, resp.StatusCode)
|
|
|
|
}
|
|
|
|
r, _, _ = c.GetPullReview(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
|
|
|
|
assert.True(t, r.Dismissed)
|
|
|
|
|
|
|
|
// UnDismissPullReview
|
|
|
|
resp, err = c.UnDismissPullReview(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.NotNil(t, resp) {
|
|
|
|
assert.EqualValues(t, 200, resp.StatusCode)
|
|
|
|
}
|
|
|
|
r, _, _ = c.GetPullReview(repo.Owner.UserName, repo.Name, pull.Index, r.ID)
|
|
|
|
assert.False(t, r.Dismissed)
|
2021-02-17 15:03:45 +00:00
|
|
|
|
|
|
|
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)
|
2020-05-21 19:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func preparePullReviewTest(t *testing.T, c *Client, repoName string) (*Repository, *PullRequest, *User, *User, bool) {
|
|
|
|
repo, err := createTestRepo(t, repoName, c)
|
|
|
|
if !assert.NoError(t, err) {
|
|
|
|
return nil, nil, nil, nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
pullSubmitter := createTestUser(t, "pull_submitter", c)
|
2020-09-06 14:24:45 +01:00
|
|
|
write := AccessModeWrite
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.AddCollaborator(repo.Owner.UserName, repo.Name, pullSubmitter.UserName, AddCollaboratorOption{
|
2020-05-21 19:04:48 +01:00
|
|
|
Permission: &write,
|
|
|
|
})
|
2020-09-07 04:00:26 +01:00
|
|
|
assert.NoError(t, err)
|
2020-05-21 19:04:48 +01:00
|
|
|
|
|
|
|
c.SetSudo("pull_submitter")
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
newFile, _, err := c.CreateFile(repo.Owner.UserName, repo.Name, "WOW-file", CreateFileOptions{
|
2020-05-21 19:04:48 +01:00
|
|
|
Content: "QSBuZXcgRmlsZQoKYW5kIHNvbWUgbGluZXMK",
|
|
|
|
FileOptions: FileOptions{
|
|
|
|
Message: "creat a new file",
|
2022-04-22 08:52:45 +01:00
|
|
|
BranchName: "main",
|
2020-05-21 19:04:48 +01:00
|
|
|
NewBranchName: "new_file",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if !assert.NoError(t, err) || !assert.NotNil(t, newFile) {
|
|
|
|
return nil, nil, nil, nil, false
|
|
|
|
}
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
pull, _, err := c.CreatePullRequest(c.username, repoName, CreatePullRequestOption{
|
2022-04-22 08:52:45 +01:00
|
|
|
Base: "main",
|
2020-05-21 19:04:48 +01:00
|
|
|
Head: "new_file",
|
|
|
|
Title: "Creat a NewFile",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, pull)
|
|
|
|
|
|
|
|
c.SetSudo("")
|
|
|
|
|
|
|
|
reviewer := createTestUser(t, "pull_reviewer", c)
|
2020-09-06 14:24:45 +01:00
|
|
|
admin := AccessModeAdmin
|
2020-09-14 03:37:09 +01:00
|
|
|
_, err = c.AddCollaborator(repo.Owner.UserName, repo.Name, pullSubmitter.UserName, AddCollaboratorOption{
|
2020-05-21 19:04:48 +01:00
|
|
|
Permission: &admin,
|
|
|
|
})
|
2020-09-07 04:00:26 +01:00
|
|
|
assert.NoError(t, err)
|
2020-05-21 19:04:48 +01:00
|
|
|
|
|
|
|
return repo, pull, pullSubmitter, reviewer, pull.Poster.ID == pullSubmitter.ID
|
|
|
|
}
|