From f9d31d2f119a66fb401c4a68b6f43290bce398ef Mon Sep 17 00:00:00 2001 From: Giteabot Date: Tue, 10 Nov 2026 15:55:38 +0000 Subject: [PATCH] fix(api): lock and unlock issues as the signed-in user, not the repository owner (#40291) (#40298) Backport #40291 by @lunny `PUT/DELETE /repos/{owner}/{repo}/issues/{index}/lock` passed `ctx.ContextUser` as the doer of `LockIssue` / `UnlockIssue`. In the repo routes that is the owner of the repository, so on a repository of an organization the lock and unlock timeline comments were created by the organization instead of the user who did it. The web handler already uses the signed-in user; this uses `ctx.Doer` as well. Adds a subtest `DoerOnOrganizationRepo` to the existing `TestAPILockIssue`: a member of an organization locks and unlocks an issue of an organization repository, and the comments must be posted by that member. Found while testing audit logging of the issue lock events downstream. _Authored by Codet (Claude)._ Co-authored-by: Lunny Xiao --- routers/api/v1/repo/issue_lock.go | 4 ++-- tests/integration/api_issue_lock_test.go | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/issue_lock.go b/routers/api/v1/repo/issue_lock.go index f0ac1847b08..39ec3e72ede 100644 --- a/routers/api/v1/repo/issue_lock.go +++ b/routers/api/v1/repo/issue_lock.go @@ -63,7 +63,7 @@ func LockIssue(ctx *context.APIContext) { if !issue.IsLocked { opt := &issues_model.IssueLockOptions{ - Doer: ctx.ContextUser, + Doer: ctx.Doer, Issue: issue, Reason: reason, } @@ -125,7 +125,7 @@ func UnlockIssue(ctx *context.APIContext) { if issue.IsLocked { opt := &issues_model.IssueLockOptions{ - Doer: ctx.ContextUser, + Doer: ctx.Doer, Issue: issue, } diff --git a/tests/integration/api_issue_lock_test.go b/tests/integration/api_issue_lock_test.go index 86f7b0eab23..21c04935dd0 100644 --- a/tests/integration/api_issue_lock_test.go +++ b/tests/integration/api_issue_lock_test.go @@ -17,6 +17,7 @@ import ( "gitea.dev/tests" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestAPILockIssue(t *testing.T) { @@ -71,4 +72,26 @@ func TestAPILockIssue(t *testing.T) { req = NewRequest(t, "DELETE", urlStr).AddTokenAuth(token34) MakeRequest(t, req, http.StatusForbidden) }) + + t.Run("DoerOnOrganizationRepo", func(t *testing.T) { + // the lock and unlock comments are posted by the signed-in user, not by the organization owning the repository + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 6}) + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID}) + org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) + require.True(t, org.IsOrganization()) + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"}) // an owner of the organization + session := loginUser(t, doer.Name) + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteIssue, auth_model.AccessTokenScopeWriteRepository) + urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/lock", org.Name, repo.Name, issue.Index) + + req := NewRequestWithJSON(t, "PUT", urlStr, api.LockIssueOption{Reason: "Spam"}).AddTokenAuth(token) + MakeRequest(t, req, http.StatusNoContent) + lockComment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: issue.ID, Type: issues_model.CommentTypeLock}) + assert.Equal(t, doer.ID, lockComment.PosterID) + + req = NewRequest(t, "DELETE", urlStr).AddTokenAuth(token) + MakeRequest(t, req, http.StatusNoContent) + unlockComment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{IssueID: issue.ID, Type: issues_model.CommentTypeUnlock}) + assert.Equal(t, doer.ID, unlockComment.PosterID) + }) }