From 6a2d2fa24390116d31ae2507c0a93d423f690b7b Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Tue, 12 Sep 2023 09:04:46 +0200 Subject: [PATCH] [GITEA] enable system users for comment.LoadPoster System users (Ghost, ActionsUser, etc) have a negative id and may be the author of a comment, either because it was created by a now deleted user or via an action using a transient token. The GetPossibleUserByID function has special cases related to system users and will not fail if given a negative id. Refs: https://codeberg.org/forgejo/forgejo/issues/1425 (cherry picked from commit 97667e06b384d834a04eaa05e8f91563481709b1) (cherry picked from commit 8ef73a09c9cb08e536aff7df8fc715c8d48c7c4f) (cherry picked from commit fa8a00d26420ef56497203a5c607b3d336feb813) (cherry picked from commit 2ada2074b56f3cd981dbacac0a7b1033ff486ffd) (cherry picked from commit f9a59b940a2ab2c9abac7c346da24b22d15017f1) (cherry picked from commit cd82834043c14a679cc55eba980c27549b2f87e5) (cherry picked from commit aeb9a2c2b8e874856bd1f287cb1f059327551cd4) (cherry picked from commit 0bfac925c23b9c780b685b594ebec330a7f93b73) (cherry picked from commit bb78cdcec0915239143bfef1c824eca920574ab7) --- models/issues/comment.go | 2 +- tests/integration/api_comment_test.go | 37 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index 5847fe6e0c..d07787faba 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -350,7 +350,7 @@ func (c *Comment) AfterLoad(session *xorm.Session) { // LoadPoster loads comment poster func (c *Comment) LoadPoster(ctx context.Context) (err error) { - if c.PosterID <= 0 || c.Poster != nil { + if c.Poster != nil { return nil } diff --git a/tests/integration/api_comment_test.go b/tests/integration/api_comment_test.go index cce65ab0dd..f97bbf4646 100644 --- a/tests/integration/api_comment_test.go +++ b/tests/integration/api_comment_test.go @@ -288,6 +288,43 @@ func TestAPIGetComment(t *testing.T) { assert.Equal(t, expect.Created.Unix(), apiComment.Created.Unix()) } +func TestAPIGetSystemUserComment(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{}) + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID}) + repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) + + for _, systemUser := range []*user_model.User{ + user_model.NewGhostUser(), + user_model.NewActionsUser(), + } { + body := fmt.Sprintf("Hello %s", systemUser.Name) + comment, err := issues_model.CreateComment(db.DefaultContext, &issues_model.CreateCommentOptions{ + Type: issues_model.CommentTypeComment, + Doer: systemUser, + Repo: repo, + Issue: issue, + Content: body, + }) + assert.NoError(t, err) + + req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID) + resp := MakeRequest(t, req, http.StatusOK) + + var apiComment api.Comment + DecodeJSON(t, resp, &apiComment) + + if assert.NotNil(t, apiComment.Poster) { + if assert.Equal(t, systemUser.ID, apiComment.Poster.ID) { + assert.NoError(t, comment.LoadPoster(db.DefaultContext)) + assert.Equal(t, systemUser.Name, apiComment.Poster.UserName) + } + } + assert.Equal(t, body, apiComment.Body) + } +} + func TestAPIEditComment(t *testing.T) { defer tests.PrepareTestEnv(t)() const newCommentBody = "This is the new comment body"