fix(pull): refuse review comments on locked pull requests (#39960) (#40084)

Backport https://github.com/go-gitea/gitea/pull/39960

Locking a pull request stops users without write access from commenting,
but the review paths never checked the lock, so any signed-in user could
still post reviews, code comments and replies on a locked pull request.

The cherry-pick conflicted on the `repo-diff.ts` imports, because
`release/v28` has no `DiffFileTreeLocale`.

Co-authored-by: Basavaraj S m <basavaraj@digiscrypt.com>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-10-30 19:36:51 +00:00
1 parent 40079c7c09
commit 51736a9706
10 files changed
+86 -52

No files matched your search

+29 -1
View File
@@ -4,6 +4,7 @@
package integration
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
@@ -12,6 +13,7 @@ import (
"testing"
"time"
auth_model "gitea.dev/models/auth"
"gitea.dev/models/db"
git_model "gitea.dev/models/git"
issues_model "gitea.dev/models/issues"
@@ -19,6 +21,7 @@ import (
"gitea.dev/models/unittest"
user_model "gitea.dev/models/user"
"gitea.dev/modules/git"
api "gitea.dev/modules/structs"
"gitea.dev/modules/test"
issue_service "gitea.dev/services/issue"
pull_service "gitea.dev/services/pull"
@@ -30,8 +33,13 @@ import (
"github.com/stretchr/testify/require"
)
func TestPullView_ReviewerMissed(t *testing.T) {
func TestPullReview(t *testing.T) {
defer tests.PrepareTestEnv(t)()
t.Run("ReviewerMissed", testPullReviewReviewerMissed)
t.Run("LockedPR", testPullReviewLockedPR)
}
func testPullReviewReviewerMissed(t *testing.T) {
session := loginUser(t, "user1")
req := NewRequest(t, "GET", "/pulls")
@@ -50,6 +58,26 @@ func TestPullView_ReviewerMissed(t *testing.T) {
assert.True(t, test.IsNormalPageCompleted(resp.Body.String()))
}
func testPullReviewLockedPR(t *testing.T) {
const reviewsURL = "/api/v1/repos/user2/repo1/pulls/3/reviews"
token := getUserToken(t, "user4", auth_model.AccessTokenScopeWriteRepository)
resp := MakeRequest(t, NewRequestWithJSON(t, "POST", reviewsURL, &api.CreatePullReviewOptions{Event: api.ReviewStatePending, Body: "test"}).AddTokenAuth(token), http.StatusOK)
pendingReview := DecodeJSON(t, resp, &api.PullReview{})
require.NoError(t, issues_model.UpdateIssueCols(t.Context(), &issues_model.Issue{ID: 3, IsLocked: true}, "is_locked"))
commentCount := unittest.GetCount(t, &issues_model.Comment{IssueID: 3})
reviewOptions := &api.CreatePullReviewOptions{Event: api.ReviewStateComment, Body: "test"}
session := loginUser(t, "user4")
testSubmitReview(t, session, "user2", "repo1", "3", "", "comment", http.StatusBadRequest)
session.MakeRequest(t, NewRequestWithValues(t, "POST", "/user2/repo1/pulls/3/files/reviews/comments", map[string]string{"content": "test"}), http.StatusBadRequest)
MakeRequest(t, NewRequestWithJSON(t, "POST", reviewsURL, reviewOptions).AddTokenAuth(token), http.StatusForbidden)
MakeRequest(t, NewRequestWithJSON(t, "POST", fmt.Sprintf("%s/%d", reviewsURL, pendingReview.ID), &api.SubmitPullReviewOptions{Event: api.ReviewStateComment, Body: "test"}).AddTokenAuth(token), http.StatusForbidden)
MakeRequest(t, NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/pulls/3/comments/7/replies", &api.CreatePullReviewCommentReplyOptions{Body: "test"}).AddTokenAuth(token), http.StatusForbidden)
unittest.AssertCount(t, &issues_model.Comment{IssueID: 3}, commentCount)
MakeRequest(t, NewRequestWithJSON(t, "POST", reviewsURL, reviewOptions).AddTokenAuth(getUserToken(t, "user2", auth_model.AccessTokenScopeWriteRepository)), http.StatusOK)
}
func TestPullView_CodeOwner(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})