2020-05-02 00:20:51 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-05-02 00:20:51 +00:00
|
|
|
|
2022-09-02 19:18:23 +00:00
|
|
|
package integration
|
2020-05-02 00:20:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2023-01-17 21:46:03 +00:00
|
|
|
auth_model "code.gitea.io/gitea/models/auth"
|
2022-06-13 09:37:59 +00:00
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-10 01:27:50 +00:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-16 08:53:21 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2024-02-19 13:42:18 +00:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-24 16:03:58 +00:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2020-05-02 00:20:51 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2024-02-19 13:42:18 +00:00
|
|
|
issue_service "code.gitea.io/gitea/services/issue"
|
2022-09-02 19:18:23 +00:00
|
|
|
"code.gitea.io/gitea/tests"
|
2021-07-24 16:03:58 +00:00
|
|
|
|
2020-05-02 00:20:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-01-09 11:49:18 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-02-19 13:42:18 +00:00
|
|
|
"xorm.io/builder"
|
2020-05-02 00:20:51 +00:00
|
|
|
)
|
|
|
|
|
2024-01-16 10:28:09 +00:00
|
|
|
func TestAPIPullReviewCreateDeleteComment(t *testing.T) {
|
2024-01-09 11:49:18 +00:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
pullIssue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 3})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, pullIssue.LoadAttributes(db.DefaultContext))
|
2024-01-09 11:49:18 +00:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: pullIssue.RepoID})
|
|
|
|
|
|
|
|
username := "user2"
|
|
|
|
session := loginUser(t, username)
|
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
|
|
|
|
|
|
|
|
// as of e522e774cae2240279fc48c349fc513c9d3353ee
|
|
|
|
// There should be no reason for CreateComment to behave differently
|
|
|
|
// depending on the event associated with the review. But the logic of the implementation
|
|
|
|
// at this point in time is very involved and deserves these seemingly redundant
|
|
|
|
// test.
|
|
|
|
for _, event := range []api.ReviewStateType{
|
|
|
|
api.ReviewStatePending,
|
|
|
|
api.ReviewStateRequestChanges,
|
|
|
|
api.ReviewStateApproved,
|
|
|
|
api.ReviewStateComment,
|
|
|
|
} {
|
|
|
|
t.Run("Event_"+string(event), func(t *testing.T) {
|
|
|
|
path := "README.md"
|
|
|
|
var review api.PullReview
|
|
|
|
var reviewLine int64 = 1
|
|
|
|
|
2024-01-16 10:28:09 +00:00
|
|
|
// cleanup
|
|
|
|
{
|
|
|
|
session := loginUser(t, "user1")
|
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll)
|
|
|
|
|
|
|
|
req := NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/pulls/%d/reviews", repo.FullName(), pullIssue.Index).AddTokenAuth(token)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
var reviews []*api.PullReview
|
|
|
|
DecodeJSON(t, resp, &reviews)
|
|
|
|
for _, review := range reviews {
|
|
|
|
req := NewRequestf(t, http.MethodDelete, "/api/v1/repos/%s/pulls/%d/reviews/%d", repo.FullName(), pullIssue.Index, review.ID).
|
|
|
|
AddTokenAuth(token)
|
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
requireReviewCount := func(count int) {
|
|
|
|
req := NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/pulls/%d/reviews", repo.FullName(), pullIssue.Index).AddTokenAuth(token)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
var reviews []*api.PullReview
|
|
|
|
DecodeJSON(t, resp, &reviews)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.Len(t, reviews, count)
|
2024-01-16 10:28:09 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 11:49:18 +00:00
|
|
|
{
|
|
|
|
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/pulls/%d/reviews", repo.FullName(), pullIssue.Index), &api.CreatePullReviewOptions{
|
|
|
|
Body: "body1",
|
|
|
|
Event: event,
|
|
|
|
}).AddTokenAuth(token)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
DecodeJSON(t, resp, &review)
|
|
|
|
require.EqualValues(t, string(event), review.State)
|
|
|
|
require.EqualValues(t, 0, review.CodeCommentsCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
req := NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/pulls/%d/reviews/%d", repo.FullName(), pullIssue.Index, review.ID).
|
|
|
|
AddTokenAuth(token)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
var getReview api.PullReview
|
|
|
|
DecodeJSON(t, resp, &getReview)
|
|
|
|
require.EqualValues(t, getReview, review)
|
|
|
|
}
|
2024-01-16 10:28:09 +00:00
|
|
|
requireReviewCount(1)
|
2024-01-09 11:49:18 +00:00
|
|
|
|
|
|
|
newCommentBody := "first new line"
|
2024-01-11 11:32:18 +00:00
|
|
|
var reviewComment api.PullReviewComment
|
2024-01-09 11:49:18 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/pulls/%d/reviews/%d/comments", repo.FullName(), pullIssue.Index, review.ID), &api.CreatePullReviewCommentOptions{
|
|
|
|
Path: path,
|
|
|
|
Body: newCommentBody,
|
|
|
|
OldLineNum: reviewLine,
|
|
|
|
}).AddTokenAuth(token)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
DecodeJSON(t, resp, &reviewComment)
|
|
|
|
assert.EqualValues(t, review.ID, reviewComment.ReviewID)
|
2024-01-11 11:32:18 +00:00
|
|
|
assert.EqualValues(t, newCommentBody, reviewComment.Body)
|
|
|
|
assert.EqualValues(t, reviewLine, reviewComment.OldLineNum)
|
|
|
|
assert.EqualValues(t, 0, reviewComment.LineNum)
|
|
|
|
assert.EqualValues(t, path, reviewComment.Path)
|
2024-01-09 11:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-01-11 11:32:18 +00:00
|
|
|
req := NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/pulls/%d/reviews/%d/comments/%d", repo.FullName(), pullIssue.Index, review.ID, reviewComment.ID).
|
2024-01-09 11:49:18 +00:00
|
|
|
AddTokenAuth(token)
|
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
2024-01-11 11:32:18 +00:00
|
|
|
var comment api.PullReviewComment
|
|
|
|
DecodeJSON(t, resp, &comment)
|
|
|
|
assert.EqualValues(t, reviewComment, comment)
|
2024-01-09 11:49:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 10:28:09 +00:00
|
|
|
{
|
|
|
|
req := NewRequestf(t, http.MethodDelete, "/api/v1/repos/%s/pulls/%d/reviews/%d/comments/%d", repo.FullName(), pullIssue.Index, review.ID, reviewComment.ID).
|
|
|
|
AddTokenAuth(token)
|
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
req := NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/pulls/%d/reviews/%d/comments/%d", repo.FullName(), pullIssue.Index, review.ID, reviewComment.ID).
|
|
|
|
AddTokenAuth(token)
|
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
2024-01-09 11:49:18 +00:00
|
|
|
{
|
|
|
|
req := NewRequestf(t, http.MethodDelete, "/api/v1/repos/%s/pulls/%d/reviews/%d", repo.FullName(), pullIssue.Index, review.ID).
|
|
|
|
AddTokenAuth(token)
|
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
|
|
|
}
|
2024-01-16 10:28:09 +00:00
|
|
|
requireReviewCount(0)
|
2024-01-09 11:49:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 00:20:51 +00:00
|
|
|
func TestAPIPullReview(t *testing.T) {
|
2022-09-02 19:18:23 +00:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2022-08-16 02:22:25 +00:00
|
|
|
pullIssue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 3})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, pullIssue.LoadAttributes(db.DefaultContext))
|
2022-08-16 02:22:25 +00:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: pullIssue.RepoID})
|
2020-05-02 00:20:51 +00:00
|
|
|
|
|
|
|
// test ListPullReviews
|
|
|
|
session := loginUser(t, "user2")
|
Redesign Scoped Access Tokens (#24767)
## Changes
- Adds the following high level access scopes, each with `read` and
`write` levels:
- `activitypub`
- `admin` (hidden if user is not a site admin)
- `misc`
- `notification`
- `organization`
- `package`
- `issue`
- `repository`
- `user`
- Adds new middleware function `tokenRequiresScopes()` in addition to
`reqToken()`
- `tokenRequiresScopes()` is used for each high-level api section
- _if_ a scoped token is present, checks that the required scope is
included based on the section and HTTP method
- `reqToken()` is used for individual routes
- checks that required authentication is present (but does not check
scope levels as this will already have been handled by
`tokenRequiresScopes()`
- Adds migration to convert old scoped access tokens to the new set of
scopes
- Updates the user interface for scope selection
### User interface example
<img width="903" alt="Screen Shot 2023-05-31 at 1 56 55 PM"
src="https://github.com/go-gitea/gitea/assets/23248839/654766ec-2143-4f59-9037-3b51600e32f3">
<img width="917" alt="Screen Shot 2023-05-31 at 1 56 43 PM"
src="https://github.com/go-gitea/gitea/assets/23248839/1ad64081-012c-4a73-b393-66b30352654c">
## tokenRequiresScopes Design Decision
- `tokenRequiresScopes()` was added to more reliably cover api routes.
For an incoming request, this function uses the given scope category
(say `AccessTokenScopeCategoryOrganization`) and the HTTP method (say
`DELETE`) and verifies that any scoped tokens in use include
`delete:organization`.
- `reqToken()` is used to enforce auth for individual routes that
require it. If a scoped token is not present for a request,
`tokenRequiresScopes()` will not return an error
## TODO
- [x] Alphabetize scope categories
- [x] Change 'public repos only' to a radio button (private vs public).
Also expand this to organizations
- [X] Disable token creation if no scopes selected. Alternatively, show
warning
- [x] `reqToken()` is missing from many `POST/DELETE` routes in the api.
`tokenRequiresScopes()` only checks that a given token has the correct
scope, `reqToken()` must be used to check that a token (or some other
auth) is present.
- _This should be addressed in this PR_
- [x] The migration should be reviewed very carefully in order to
minimize access changes to existing user tokens.
- _This should be addressed in this PR_
- [x] Link to api to swagger documentation, clarify what
read/write/delete levels correspond to
- [x] Review cases where more than one scope is needed as this directly
deviates from the api definition.
- _This should be addressed in this PR_
- For example:
```go
m.Group("/users/{username}/orgs", func() {
m.Get("", reqToken(), org.ListUserOrgs)
m.Get("/{org}/permissions", reqToken(), org.GetUserOrgsPermissions)
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser,
auth_model.AccessTokenScopeCategoryOrganization),
context_service.UserAssignmentAPI())
```
## Future improvements
- [ ] Add required scopes to swagger documentation
- [ ] Redesign `reqToken()` to be opt-out rather than opt-in
- [ ] Subdivide scopes like `repository`
- [ ] Once a token is created, if it has no scopes, we should display
text instead of an empty bullet point
- [ ] If the 'public repos only' option is selected, should read
categories be selected by default
Closes #24501
Closes #24799
Co-authored-by: Jonathan Tran <jon@allspice.io>
Co-authored-by: Kyle D <kdumontnu@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
2023-06-04 18:57:16 +00:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
|
2023-12-21 23:59:59 +00:00
|
|
|
req := NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/%s/pulls/%d/reviews", repo.OwnerName, repo.Name, pullIssue.Index).
|
|
|
|
AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
resp := MakeRequest(t, req, http.StatusOK)
|
2020-05-02 00:20:51 +00:00
|
|
|
|
|
|
|
var reviews []*api.PullReview
|
|
|
|
DecodeJSON(t, resp, &reviews)
|
|
|
|
if !assert.Len(t, reviews, 6) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, r := range reviews {
|
|
|
|
assert.EqualValues(t, pullIssue.HTMLURL(), r.HTMLPullURL)
|
|
|
|
}
|
|
|
|
assert.EqualValues(t, 8, reviews[3].ID)
|
|
|
|
assert.EqualValues(t, "APPROVED", reviews[3].State)
|
|
|
|
assert.EqualValues(t, 0, reviews[3].CodeCommentsCount)
|
2021-06-07 05:27:09 +00:00
|
|
|
assert.True(t, reviews[3].Stale)
|
|
|
|
assert.False(t, reviews[3].Official)
|
2020-05-02 00:20:51 +00:00
|
|
|
|
|
|
|
assert.EqualValues(t, 10, reviews[5].ID)
|
|
|
|
assert.EqualValues(t, "REQUEST_CHANGES", reviews[5].State)
|
|
|
|
assert.EqualValues(t, 1, reviews[5].CodeCommentsCount)
|
2020-09-17 21:33:23 +00:00
|
|
|
assert.EqualValues(t, -1, reviews[5].Reviewer.ID) // ghost user
|
2021-06-07 05:27:09 +00:00
|
|
|
assert.False(t, reviews[5].Stale)
|
|
|
|
assert.True(t, reviews[5].Official)
|
2020-05-02 00:20:51 +00:00
|
|
|
|
|
|
|
// test GetPullReview
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/%s/pulls/%d/reviews/%d", repo.OwnerName, repo.Name, pullIssue.Index, reviews[3].ID).
|
|
|
|
AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-05-02 00:20:51 +00:00
|
|
|
var review api.PullReview
|
|
|
|
DecodeJSON(t, resp, &review)
|
|
|
|
assert.EqualValues(t, *reviews[3], review)
|
|
|
|
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestf(t, "GET", "/api/v1/repos/%s/%s/pulls/%d/reviews/%d", repo.OwnerName, repo.Name, pullIssue.Index, reviews[5].ID).
|
|
|
|
AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-05-02 00:20:51 +00:00
|
|
|
DecodeJSON(t, resp, &review)
|
|
|
|
assert.EqualValues(t, *reviews[5], review)
|
|
|
|
|
|
|
|
// test GetPullReviewComments
|
2022-08-16 02:22:25 +00:00
|
|
|
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 7})
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/%s/pulls/%d/reviews/%d/comments", repo.OwnerName, repo.Name, pullIssue.Index, 10).
|
|
|
|
AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-05-02 00:20:51 +00:00
|
|
|
var reviewComments []*api.PullReviewComment
|
|
|
|
DecodeJSON(t, resp, &reviewComments)
|
|
|
|
assert.Len(t, reviewComments, 1)
|
2021-03-27 23:37:51 +00:00
|
|
|
assert.EqualValues(t, "Ghost", reviewComments[0].Poster.UserName)
|
2020-05-02 00:20:51 +00:00
|
|
|
assert.EqualValues(t, "a review from a deleted user", reviewComments[0].Body)
|
|
|
|
assert.EqualValues(t, comment.ID, reviewComments[0].ID)
|
|
|
|
assert.EqualValues(t, comment.UpdatedUnix, reviewComments[0].Updated.Unix())
|
2023-09-29 12:12:54 +00:00
|
|
|
assert.EqualValues(t, comment.HTMLURL(db.DefaultContext), reviewComments[0].HTMLURL)
|
2020-05-02 00:20:51 +00:00
|
|
|
|
|
|
|
// test CreatePullReview
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews", repo.OwnerName, repo.Name, pullIssue.Index), &api.CreatePullReviewOptions{
|
2020-05-02 00:20:51 +00:00
|
|
|
Body: "body1",
|
|
|
|
// Event: "" # will result in PENDING
|
2022-01-20 17:46:10 +00:00
|
|
|
Comments: []api.CreatePullReviewComment{
|
|
|
|
{
|
|
|
|
Path: "README.md",
|
|
|
|
Body: "first new line",
|
|
|
|
OldLineNum: 0,
|
|
|
|
NewLineNum: 1,
|
|
|
|
}, {
|
|
|
|
Path: "README.md",
|
|
|
|
Body: "first old line",
|
|
|
|
OldLineNum: 1,
|
|
|
|
NewLineNum: 0,
|
|
|
|
}, {
|
|
|
|
Path: "iso-8859-1.txt",
|
|
|
|
Body: "this line contains a non-utf-8 character",
|
|
|
|
OldLineNum: 0,
|
|
|
|
NewLineNum: 1,
|
|
|
|
},
|
2020-05-02 00:20:51 +00:00
|
|
|
},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-05-02 00:20:51 +00:00
|
|
|
DecodeJSON(t, resp, &review)
|
|
|
|
assert.EqualValues(t, 6, review.ID)
|
|
|
|
assert.EqualValues(t, "PENDING", review.State)
|
2020-06-18 14:07:09 +00:00
|
|
|
assert.EqualValues(t, 3, review.CodeCommentsCount)
|
2020-05-02 00:20:51 +00:00
|
|
|
|
|
|
|
// test SubmitPullReview
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d", repo.OwnerName, repo.Name, pullIssue.Index, review.ID), &api.SubmitPullReviewOptions{
|
2020-05-02 00:20:51 +00:00
|
|
|
Event: "APPROVED",
|
|
|
|
Body: "just two nits",
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-05-02 00:20:51 +00:00
|
|
|
DecodeJSON(t, resp, &review)
|
|
|
|
assert.EqualValues(t, 6, review.ID)
|
|
|
|
assert.EqualValues(t, "APPROVED", review.State)
|
2020-06-18 14:07:09 +00:00
|
|
|
assert.EqualValues(t, 3, review.CodeCommentsCount)
|
2020-05-02 00:20:51 +00:00
|
|
|
|
2021-02-11 17:32:25 +00:00
|
|
|
// test dismiss review
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d/dismissals", repo.OwnerName, repo.Name, pullIssue.Index, review.ID), &api.DismissPullReviewOptions{
|
2021-02-11 17:32:25 +00:00
|
|
|
Message: "test",
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2021-02-11 17:32:25 +00:00
|
|
|
DecodeJSON(t, resp, &review)
|
|
|
|
assert.EqualValues(t, 6, review.ID)
|
2021-06-07 05:27:09 +00:00
|
|
|
assert.True(t, review.Dismissed)
|
2021-02-11 17:32:25 +00:00
|
|
|
|
|
|
|
// test dismiss review
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequest(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews/%d/undismissals", repo.OwnerName, repo.Name, pullIssue.Index, review.ID)).
|
|
|
|
AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2021-02-11 17:32:25 +00:00
|
|
|
DecodeJSON(t, resp, &review)
|
|
|
|
assert.EqualValues(t, 6, review.ID)
|
2021-06-07 05:27:09 +00:00
|
|
|
assert.False(t, review.Dismissed)
|
2021-02-11 17:32:25 +00:00
|
|
|
|
2020-05-02 00:20:51 +00:00
|
|
|
// test DeletePullReview
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews", repo.OwnerName, repo.Name, pullIssue.Index), &api.CreatePullReviewOptions{
|
2020-05-02 00:20:51 +00:00
|
|
|
Body: "just a comment",
|
|
|
|
Event: "COMMENT",
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-05-02 00:20:51 +00:00
|
|
|
DecodeJSON(t, resp, &review)
|
|
|
|
assert.EqualValues(t, "COMMENT", review.State)
|
|
|
|
assert.EqualValues(t, 0, review.CodeCommentsCount)
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestf(t, http.MethodDelete, "/api/v1/repos/%s/%s/pulls/%d/reviews/%d", repo.OwnerName, repo.Name, pullIssue.Index, review.ID).
|
|
|
|
AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
2021-06-24 22:05:51 +00:00
|
|
|
// test CreatePullReview Comment without body but with comments
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews", repo.OwnerName, repo.Name, pullIssue.Index), &api.CreatePullReviewOptions{
|
2021-06-24 22:05:51 +00:00
|
|
|
// Body: "",
|
|
|
|
Event: "COMMENT",
|
2022-01-20 17:46:10 +00:00
|
|
|
Comments: []api.CreatePullReviewComment{
|
|
|
|
{
|
|
|
|
Path: "README.md",
|
|
|
|
Body: "first new line",
|
|
|
|
OldLineNum: 0,
|
|
|
|
NewLineNum: 1,
|
|
|
|
}, {
|
|
|
|
Path: "README.md",
|
|
|
|
Body: "first old line",
|
|
|
|
OldLineNum: 1,
|
|
|
|
NewLineNum: 0,
|
|
|
|
},
|
2021-06-24 22:05:51 +00:00
|
|
|
},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2021-06-24 22:05:51 +00:00
|
|
|
var commentReview api.PullReview
|
|
|
|
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2021-06-24 22:05:51 +00:00
|
|
|
DecodeJSON(t, resp, &commentReview)
|
|
|
|
assert.EqualValues(t, "COMMENT", commentReview.State)
|
|
|
|
assert.EqualValues(t, 2, commentReview.CodeCommentsCount)
|
2023-04-22 21:56:27 +00:00
|
|
|
assert.Empty(t, commentReview.Body)
|
|
|
|
assert.False(t, commentReview.Dismissed)
|
2021-06-24 22:05:51 +00:00
|
|
|
|
|
|
|
// test CreatePullReview Comment with body but without comments
|
|
|
|
commentBody := "This is a body of the comment."
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews", repo.OwnerName, repo.Name, pullIssue.Index), &api.CreatePullReviewOptions{
|
2021-06-24 22:05:51 +00:00
|
|
|
Body: commentBody,
|
|
|
|
Event: "COMMENT",
|
|
|
|
Comments: []api.CreatePullReviewComment{},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2021-06-24 22:05:51 +00:00
|
|
|
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2021-06-24 22:05:51 +00:00
|
|
|
DecodeJSON(t, resp, &commentReview)
|
|
|
|
assert.EqualValues(t, "COMMENT", commentReview.State)
|
|
|
|
assert.EqualValues(t, 0, commentReview.CodeCommentsCount)
|
|
|
|
assert.EqualValues(t, commentBody, commentReview.Body)
|
2023-04-22 21:56:27 +00:00
|
|
|
assert.False(t, commentReview.Dismissed)
|
2021-06-24 22:05:51 +00:00
|
|
|
|
|
|
|
// test CreatePullReview Comment without body and no comments
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews", repo.OwnerName, repo.Name, pullIssue.Index), &api.CreatePullReviewOptions{
|
2021-06-24 22:05:51 +00:00
|
|
|
Body: "",
|
|
|
|
Event: "COMMENT",
|
|
|
|
Comments: []api.CreatePullReviewComment{},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusUnprocessableEntity)
|
2023-07-04 18:36:08 +00:00
|
|
|
errMap := make(map[string]any)
|
2021-06-24 22:05:51 +00:00
|
|
|
json.Unmarshal(resp.Body.Bytes(), &errMap)
|
|
|
|
assert.EqualValues(t, "review event COMMENT requires a body or a comment", errMap["message"].(string))
|
|
|
|
|
2020-10-20 18:18:25 +00:00
|
|
|
// test get review requests
|
|
|
|
// to make it simple, use same api with get review
|
2022-08-16 02:22:25 +00:00
|
|
|
pullIssue12 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 12})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, pullIssue12.LoadAttributes(db.DefaultContext))
|
2022-08-16 02:22:25 +00:00
|
|
|
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: pullIssue12.RepoID})
|
2020-10-20 18:18:25 +00:00
|
|
|
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestf(t, http.MethodGet, "/api/v1/repos/%s/%s/pulls/%d/reviews", repo3.OwnerName, repo3.Name, pullIssue12.Index).
|
|
|
|
AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
resp = MakeRequest(t, req, http.StatusOK)
|
2020-10-20 18:18:25 +00:00
|
|
|
DecodeJSON(t, resp, &reviews)
|
|
|
|
assert.EqualValues(t, 11, reviews[0].ID)
|
|
|
|
assert.EqualValues(t, "REQUEST_REVIEW", reviews[0].State)
|
|
|
|
assert.EqualValues(t, 0, reviews[0].CodeCommentsCount)
|
2021-06-07 05:27:09 +00:00
|
|
|
assert.False(t, reviews[0].Stale)
|
|
|
|
assert.True(t, reviews[0].Official)
|
2020-10-20 18:18:25 +00:00
|
|
|
assert.EqualValues(t, "test_team", reviews[0].ReviewerTeam.Name)
|
|
|
|
|
|
|
|
assert.EqualValues(t, 12, reviews[1].ID)
|
|
|
|
assert.EqualValues(t, "REQUEST_REVIEW", reviews[1].State)
|
|
|
|
assert.EqualValues(t, 0, reviews[0].CodeCommentsCount)
|
2021-06-07 05:27:09 +00:00
|
|
|
assert.False(t, reviews[1].Stale)
|
|
|
|
assert.True(t, reviews[1].Official)
|
2020-10-20 18:18:25 +00:00
|
|
|
assert.EqualValues(t, 1, reviews[1].Reviewer.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAPIPullReviewRequest(t *testing.T) {
|
2022-09-02 19:18:23 +00:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2022-08-16 02:22:25 +00:00
|
|
|
pullIssue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 3})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, pullIssue.LoadAttributes(db.DefaultContext))
|
2022-08-16 02:22:25 +00:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: pullIssue.RepoID})
|
2020-10-20 18:18:25 +00:00
|
|
|
|
|
|
|
// Test add Review Request
|
|
|
|
session := loginUser(t, "user2")
|
Redesign Scoped Access Tokens (#24767)
## Changes
- Adds the following high level access scopes, each with `read` and
`write` levels:
- `activitypub`
- `admin` (hidden if user is not a site admin)
- `misc`
- `notification`
- `organization`
- `package`
- `issue`
- `repository`
- `user`
- Adds new middleware function `tokenRequiresScopes()` in addition to
`reqToken()`
- `tokenRequiresScopes()` is used for each high-level api section
- _if_ a scoped token is present, checks that the required scope is
included based on the section and HTTP method
- `reqToken()` is used for individual routes
- checks that required authentication is present (but does not check
scope levels as this will already have been handled by
`tokenRequiresScopes()`
- Adds migration to convert old scoped access tokens to the new set of
scopes
- Updates the user interface for scope selection
### User interface example
<img width="903" alt="Screen Shot 2023-05-31 at 1 56 55 PM"
src="https://github.com/go-gitea/gitea/assets/23248839/654766ec-2143-4f59-9037-3b51600e32f3">
<img width="917" alt="Screen Shot 2023-05-31 at 1 56 43 PM"
src="https://github.com/go-gitea/gitea/assets/23248839/1ad64081-012c-4a73-b393-66b30352654c">
## tokenRequiresScopes Design Decision
- `tokenRequiresScopes()` was added to more reliably cover api routes.
For an incoming request, this function uses the given scope category
(say `AccessTokenScopeCategoryOrganization`) and the HTTP method (say
`DELETE`) and verifies that any scoped tokens in use include
`delete:organization`.
- `reqToken()` is used to enforce auth for individual routes that
require it. If a scoped token is not present for a request,
`tokenRequiresScopes()` will not return an error
## TODO
- [x] Alphabetize scope categories
- [x] Change 'public repos only' to a radio button (private vs public).
Also expand this to organizations
- [X] Disable token creation if no scopes selected. Alternatively, show
warning
- [x] `reqToken()` is missing from many `POST/DELETE` routes in the api.
`tokenRequiresScopes()` only checks that a given token has the correct
scope, `reqToken()` must be used to check that a token (or some other
auth) is present.
- _This should be addressed in this PR_
- [x] The migration should be reviewed very carefully in order to
minimize access changes to existing user tokens.
- _This should be addressed in this PR_
- [x] Link to api to swagger documentation, clarify what
read/write/delete levels correspond to
- [x] Review cases where more than one scope is needed as this directly
deviates from the api definition.
- _This should be addressed in this PR_
- For example:
```go
m.Group("/users/{username}/orgs", func() {
m.Get("", reqToken(), org.ListUserOrgs)
m.Get("/{org}/permissions", reqToken(), org.GetUserOrgsPermissions)
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser,
auth_model.AccessTokenScopeCategoryOrganization),
context_service.UserAssignmentAPI())
```
## Future improvements
- [ ] Add required scopes to swagger documentation
- [ ] Redesign `reqToken()` to be opt-out rather than opt-in
- [ ] Subdivide scopes like `repository`
- [ ] Once a token is created, if it has no scopes, we should display
text instead of an empty bullet point
- [ ] If the 'public repos only' option is selected, should read
categories be selected by default
Closes #24501
Closes #24799
Co-authored-by: Jonathan Tran <jon@allspice.io>
Co-authored-by: Kyle D <kdumontnu@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
2023-06-04 18:57:16 +00:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
|
2023-12-21 23:59:59 +00:00
|
|
|
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo.OwnerName, repo.Name, pullIssue.Index), &api.PullReviewRequestOptions{
|
2020-10-20 18:18:25 +00:00
|
|
|
Reviewers: []string{"user4@example.com", "user8"},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
|
|
|
// poster of pr can't be reviewer
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo.OwnerName, repo.Name, pullIssue.Index), &api.PullReviewRequestOptions{
|
2020-10-20 18:18:25 +00:00
|
|
|
Reviewers: []string{"user1"},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
|
|
|
// test user not exist
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo.OwnerName, repo.Name, pullIssue.Index), &api.PullReviewRequestOptions{
|
2020-10-20 18:18:25 +00:00
|
|
|
Reviewers: []string{"testOther"},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
|
|
|
// Test Remove Review Request
|
|
|
|
session2 := loginUser(t, "user4")
|
Redesign Scoped Access Tokens (#24767)
## Changes
- Adds the following high level access scopes, each with `read` and
`write` levels:
- `activitypub`
- `admin` (hidden if user is not a site admin)
- `misc`
- `notification`
- `organization`
- `package`
- `issue`
- `repository`
- `user`
- Adds new middleware function `tokenRequiresScopes()` in addition to
`reqToken()`
- `tokenRequiresScopes()` is used for each high-level api section
- _if_ a scoped token is present, checks that the required scope is
included based on the section and HTTP method
- `reqToken()` is used for individual routes
- checks that required authentication is present (but does not check
scope levels as this will already have been handled by
`tokenRequiresScopes()`
- Adds migration to convert old scoped access tokens to the new set of
scopes
- Updates the user interface for scope selection
### User interface example
<img width="903" alt="Screen Shot 2023-05-31 at 1 56 55 PM"
src="https://github.com/go-gitea/gitea/assets/23248839/654766ec-2143-4f59-9037-3b51600e32f3">
<img width="917" alt="Screen Shot 2023-05-31 at 1 56 43 PM"
src="https://github.com/go-gitea/gitea/assets/23248839/1ad64081-012c-4a73-b393-66b30352654c">
## tokenRequiresScopes Design Decision
- `tokenRequiresScopes()` was added to more reliably cover api routes.
For an incoming request, this function uses the given scope category
(say `AccessTokenScopeCategoryOrganization`) and the HTTP method (say
`DELETE`) and verifies that any scoped tokens in use include
`delete:organization`.
- `reqToken()` is used to enforce auth for individual routes that
require it. If a scoped token is not present for a request,
`tokenRequiresScopes()` will not return an error
## TODO
- [x] Alphabetize scope categories
- [x] Change 'public repos only' to a radio button (private vs public).
Also expand this to organizations
- [X] Disable token creation if no scopes selected. Alternatively, show
warning
- [x] `reqToken()` is missing from many `POST/DELETE` routes in the api.
`tokenRequiresScopes()` only checks that a given token has the correct
scope, `reqToken()` must be used to check that a token (or some other
auth) is present.
- _This should be addressed in this PR_
- [x] The migration should be reviewed very carefully in order to
minimize access changes to existing user tokens.
- _This should be addressed in this PR_
- [x] Link to api to swagger documentation, clarify what
read/write/delete levels correspond to
- [x] Review cases where more than one scope is needed as this directly
deviates from the api definition.
- _This should be addressed in this PR_
- For example:
```go
m.Group("/users/{username}/orgs", func() {
m.Get("", reqToken(), org.ListUserOrgs)
m.Get("/{org}/permissions", reqToken(), org.GetUserOrgsPermissions)
}, tokenRequiresScopes(auth_model.AccessTokenScopeCategoryUser,
auth_model.AccessTokenScopeCategoryOrganization),
context_service.UserAssignmentAPI())
```
## Future improvements
- [ ] Add required scopes to swagger documentation
- [ ] Redesign `reqToken()` to be opt-out rather than opt-in
- [ ] Subdivide scopes like `repository`
- [ ] Once a token is created, if it has no scopes, we should display
text instead of an empty bullet point
- [ ] If the 'public repos only' option is selected, should read
categories be selected by default
Closes #24501
Closes #24799
Co-authored-by: Jonathan Tran <jon@allspice.io>
Co-authored-by: Kyle D <kdumontnu@gmail.com>
Co-authored-by: silverwind <me@silverwind.io>
2023-06-04 18:57:16 +00:00
|
|
|
token2 := getTokenForLoggedInUser(t, session2, auth_model.AccessTokenScopeWriteRepository)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo.OwnerName, repo.Name, pullIssue.Index), &api.PullReviewRequestOptions{
|
2020-10-20 18:18:25 +00:00
|
|
|
Reviewers: []string{"user4"},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token2)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
|
|
|
// doer is not admin
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo.OwnerName, repo.Name, pullIssue.Index), &api.PullReviewRequestOptions{
|
2020-10-20 18:18:25 +00:00
|
|
|
Reviewers: []string{"user8"},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token2)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo.OwnerName, repo.Name, pullIssue.Index), &api.PullReviewRequestOptions{
|
2020-10-20 18:18:25 +00:00
|
|
|
Reviewers: []string{"user8"},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
2024-02-24 12:38:43 +00:00
|
|
|
// a collaborator can add/remove a review request
|
|
|
|
pullIssue21 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 21})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, pullIssue21.LoadAttributes(db.DefaultContext))
|
2024-02-24 12:38:43 +00:00
|
|
|
pull21Repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: pullIssue21.RepoID}) // repo60
|
|
|
|
user38Session := loginUser(t, "user38")
|
|
|
|
user38Token := getTokenForLoggedInUser(t, user38Session, auth_model.AccessTokenScopeWriteRepository)
|
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", pull21Repo.OwnerName, pull21Repo.Name, pullIssue21.Index), &api.PullReviewRequestOptions{
|
|
|
|
Reviewers: []string{"user4@example.com"},
|
|
|
|
}).AddTokenAuth(user38Token)
|
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
|
|
|
|
|
|
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", pull21Repo.OwnerName, pull21Repo.Name, pullIssue21.Index), &api.PullReviewRequestOptions{
|
|
|
|
Reviewers: []string{"user4@example.com"},
|
|
|
|
}).AddTokenAuth(user38Token)
|
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
|
|
|
|
|
|
|
// the poster of the PR can add/remove a review request
|
|
|
|
user39Session := loginUser(t, "user39")
|
|
|
|
user39Token := getTokenForLoggedInUser(t, user39Session, auth_model.AccessTokenScopeWriteRepository)
|
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", pull21Repo.OwnerName, pull21Repo.Name, pullIssue21.Index), &api.PullReviewRequestOptions{
|
|
|
|
Reviewers: []string{"user8"},
|
|
|
|
}).AddTokenAuth(user39Token)
|
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
|
|
|
|
|
|
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", pull21Repo.OwnerName, pull21Repo.Name, pullIssue21.Index), &api.PullReviewRequestOptions{
|
|
|
|
Reviewers: []string{"user8"},
|
|
|
|
}).AddTokenAuth(user39Token)
|
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
|
|
|
|
|
|
|
// user with read permission on pull requests unit can add/remove a review request
|
|
|
|
pullIssue22 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 22})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, pullIssue22.LoadAttributes(db.DefaultContext))
|
2024-02-24 12:38:43 +00:00
|
|
|
pull22Repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: pullIssue22.RepoID}) // repo61
|
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", pull22Repo.OwnerName, pull22Repo.Name, pullIssue22.Index), &api.PullReviewRequestOptions{
|
|
|
|
Reviewers: []string{"user38"},
|
|
|
|
}).AddTokenAuth(user39Token) // user39 is from a team with read permission on pull requests unit
|
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
|
|
|
|
|
|
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", pull22Repo.OwnerName, pull22Repo.Name, pullIssue22.Index), &api.PullReviewRequestOptions{
|
|
|
|
Reviewers: []string{"user38"},
|
|
|
|
}).AddTokenAuth(user39Token) // user39 is from a team with read permission on pull requests unit
|
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
|
|
|
|
2020-10-20 18:18:25 +00:00
|
|
|
// Test team review request
|
2022-08-16 02:22:25 +00:00
|
|
|
pullIssue12 := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 12})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, pullIssue12.LoadAttributes(db.DefaultContext))
|
2022-08-16 02:22:25 +00:00
|
|
|
repo3 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: pullIssue12.RepoID})
|
2020-10-20 18:18:25 +00:00
|
|
|
|
|
|
|
// Test add Team Review Request
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo3.OwnerName, repo3.Name, pullIssue12.Index), &api.PullReviewRequestOptions{
|
2020-10-20 18:18:25 +00:00
|
|
|
TeamReviewers: []string{"team1", "owners"},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
|
|
|
// Test add Team Review Request to not allowned
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo3.OwnerName, repo3.Name, pullIssue12.Index), &api.PullReviewRequestOptions{
|
2020-10-20 18:18:25 +00:00
|
|
|
TeamReviewers: []string{"test_team"},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusUnprocessableEntity)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
|
|
|
// Test add Team Review Request to not exist
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo3.OwnerName, repo3.Name, pullIssue12.Index), &api.PullReviewRequestOptions{
|
2020-10-20 18:18:25 +00:00
|
|
|
TeamReviewers: []string{"not_exist_team"},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
|
|
|
// Test Remove team Review Request
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo3.OwnerName, repo3.Name, pullIssue12.Index), &api.PullReviewRequestOptions{
|
2020-10-20 18:18:25 +00:00
|
|
|
TeamReviewers: []string{"team1"},
|
2023-12-21 23:59:59 +00:00
|
|
|
}).AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
|
|
|
// empty request test
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo3.OwnerName, repo3.Name, pullIssue12.Index), &api.PullReviewRequestOptions{}).
|
|
|
|
AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
2020-10-20 18:18:25 +00:00
|
|
|
|
2023-12-21 23:59:59 +00:00
|
|
|
req = NewRequestWithJSON(t, http.MethodDelete, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo3.OwnerName, repo3.Name, pullIssue12.Index), &api.PullReviewRequestOptions{}).
|
|
|
|
AddTokenAuth(token)
|
2022-12-02 03:39:42 +00:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
2020-05-02 00:20:51 +00:00
|
|
|
}
|
2024-02-19 13:42:18 +00:00
|
|
|
|
|
|
|
func TestAPIPullReviewStayDismissed(t *testing.T) {
|
|
|
|
// This test against issue https://github.com/go-gitea/gitea/issues/28542
|
|
|
|
// where old reviews surface after a review request got dismissed.
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
pullIssue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 3})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, pullIssue.LoadAttributes(db.DefaultContext))
|
2024-02-19 13:42:18 +00:00
|
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: pullIssue.RepoID})
|
|
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
session2 := loginUser(t, user2.LoginName)
|
|
|
|
token2 := getTokenForLoggedInUser(t, session2, auth_model.AccessTokenScopeWriteRepository)
|
|
|
|
user8 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 8})
|
|
|
|
session8 := loginUser(t, user8.LoginName)
|
|
|
|
token8 := getTokenForLoggedInUser(t, session8, auth_model.AccessTokenScopeWriteRepository)
|
|
|
|
|
|
|
|
// user2 request user8
|
|
|
|
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo.OwnerName, repo.Name, pullIssue.Index), &api.PullReviewRequestOptions{
|
|
|
|
Reviewers: []string{user8.LoginName},
|
|
|
|
}).AddTokenAuth(token2)
|
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
|
|
|
|
|
|
reviewsCountCheck(t,
|
|
|
|
"check we have only one review request",
|
|
|
|
pullIssue.ID, user8.ID, 0, 1, 1, false)
|
|
|
|
|
|
|
|
// user2 request user8 again, it is expected to be ignored
|
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo.OwnerName, repo.Name, pullIssue.Index), &api.PullReviewRequestOptions{
|
|
|
|
Reviewers: []string{user8.LoginName},
|
|
|
|
}).AddTokenAuth(token2)
|
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
|
|
|
|
|
|
reviewsCountCheck(t,
|
|
|
|
"check we have only one review request, even after re-request it again",
|
|
|
|
pullIssue.ID, user8.ID, 0, 1, 1, false)
|
|
|
|
|
|
|
|
// user8 reviews it as accept
|
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews", repo.OwnerName, repo.Name, pullIssue.Index), &api.CreatePullReviewOptions{
|
|
|
|
Event: "APPROVED",
|
|
|
|
Body: "lgtm",
|
|
|
|
}).AddTokenAuth(token8)
|
|
|
|
MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
reviewsCountCheck(t,
|
|
|
|
"check we have one valid approval",
|
|
|
|
pullIssue.ID, user8.ID, 0, 0, 1, true)
|
|
|
|
|
|
|
|
// emulate of auto-dismiss lgtm on a protected branch that where a pull just got an update
|
|
|
|
_, err := db.GetEngine(db.DefaultContext).Where("issue_id = ? AND reviewer_id = ?", pullIssue.ID, user8.ID).
|
|
|
|
Cols("dismissed").Update(&issues_model.Review{Dismissed: true})
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2024-02-19 13:42:18 +00:00
|
|
|
|
|
|
|
// user2 request user8 again
|
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/requested_reviewers", repo.OwnerName, repo.Name, pullIssue.Index), &api.PullReviewRequestOptions{
|
|
|
|
Reviewers: []string{user8.LoginName},
|
|
|
|
}).AddTokenAuth(token2)
|
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
|
|
|
|
|
|
reviewsCountCheck(t,
|
|
|
|
"check we have no valid approval and one review request",
|
|
|
|
pullIssue.ID, user8.ID, 1, 1, 2, false)
|
|
|
|
|
|
|
|
// user8 dismiss review
|
|
|
|
_, err = issue_service.ReviewRequest(db.DefaultContext, pullIssue, user8, user8, false)
|
2024-07-30 19:41:10 +00:00
|
|
|
require.NoError(t, err)
|
2024-02-19 13:42:18 +00:00
|
|
|
|
|
|
|
reviewsCountCheck(t,
|
|
|
|
"check new review request is now dismissed",
|
|
|
|
pullIssue.ID, user8.ID, 1, 0, 1, false)
|
|
|
|
|
|
|
|
// add a new valid approval
|
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews", repo.OwnerName, repo.Name, pullIssue.Index), &api.CreatePullReviewOptions{
|
|
|
|
Event: "APPROVED",
|
|
|
|
Body: "lgtm",
|
|
|
|
}).AddTokenAuth(token8)
|
|
|
|
MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
reviewsCountCheck(t,
|
|
|
|
"check that old reviews requests are deleted",
|
|
|
|
pullIssue.ID, user8.ID, 1, 0, 2, true)
|
|
|
|
|
|
|
|
// now add a change request witch should dismiss the approval
|
|
|
|
req = NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/reviews", repo.OwnerName, repo.Name, pullIssue.Index), &api.CreatePullReviewOptions{
|
|
|
|
Event: "REQUEST_CHANGES",
|
|
|
|
Body: "please change XYZ",
|
|
|
|
}).AddTokenAuth(token8)
|
|
|
|
MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
reviewsCountCheck(t,
|
|
|
|
"check that old reviews are dismissed",
|
|
|
|
pullIssue.ID, user8.ID, 2, 0, 3, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func reviewsCountCheck(t *testing.T, name string, issueID, reviewerID int64, expectedDismissed, expectedRequested, expectedTotal int, expectApproval bool) {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
unittest.AssertCountByCond(t, "review", builder.Eq{
|
|
|
|
"issue_id": issueID,
|
|
|
|
"reviewer_id": reviewerID,
|
|
|
|
"dismissed": true,
|
|
|
|
}, expectedDismissed)
|
|
|
|
|
|
|
|
unittest.AssertCountByCond(t, "review", builder.Eq{
|
|
|
|
"issue_id": issueID,
|
|
|
|
"reviewer_id": reviewerID,
|
|
|
|
}, expectedTotal)
|
|
|
|
|
|
|
|
unittest.AssertCountByCond(t, "review", builder.Eq{
|
|
|
|
"issue_id": issueID,
|
|
|
|
"reviewer_id": reviewerID,
|
|
|
|
"type": issues_model.ReviewTypeRequest,
|
|
|
|
}, expectedRequested)
|
|
|
|
|
|
|
|
approvalCount := 0
|
|
|
|
if expectApproval {
|
|
|
|
approvalCount = 1
|
|
|
|
}
|
|
|
|
unittest.AssertCountByCond(t, "review", builder.Eq{
|
|
|
|
"issue_id": issueID,
|
|
|
|
"reviewer_id": reviewerID,
|
|
|
|
"type": issues_model.ReviewTypeApprove,
|
|
|
|
"dismissed": false,
|
|
|
|
}, approvalCount)
|
|
|
|
})
|
|
|
|
}
|