Skip to content

Commit 8ef73a0

Browse files
committed
[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)
1 parent 74f70ca commit 8ef73a0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

models/issues/comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ func (c *Comment) AfterLoad(session *xorm.Session) {
342342

343343
// LoadPoster loads comment poster
344344
func (c *Comment) LoadPoster(ctx context.Context) (err error) {
345-
if c.PosterID <= 0 || c.Poster != nil {
345+
if c.Poster != nil {
346346
return nil
347347
}
348348

tests/integration/api_comment_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,43 @@ func TestAPIGetComment(t *testing.T) {
189189
assert.Equal(t, expect.Created.Unix(), apiComment.Created.Unix())
190190
}
191191

192+
func TestAPIGetSystemUserComment(t *testing.T) {
193+
defer tests.PrepareTestEnv(t)()
194+
195+
issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{})
196+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: issue.RepoID})
197+
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
198+
199+
for _, systemUser := range []*user_model.User{
200+
user_model.NewGhostUser(),
201+
user_model.NewActionsUser(),
202+
} {
203+
body := fmt.Sprintf("Hello %s", systemUser.Name)
204+
comment, err := issues_model.CreateComment(db.DefaultContext, &issues_model.CreateCommentOptions{
205+
Type: issues_model.CommentTypeComment,
206+
Doer: systemUser,
207+
Repo: repo,
208+
Issue: issue,
209+
Content: body,
210+
})
211+
assert.NoError(t, err)
212+
213+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/issues/comments/%d", repoOwner.Name, repo.Name, comment.ID)
214+
resp := MakeRequest(t, req, http.StatusOK)
215+
216+
var apiComment api.Comment
217+
DecodeJSON(t, resp, &apiComment)
218+
219+
if assert.NotNil(t, apiComment.Poster) {
220+
if assert.Equal(t, systemUser.ID, apiComment.Poster.ID) {
221+
assert.NoError(t, comment.LoadPoster(db.DefaultContext))
222+
assert.Equal(t, systemUser.Name, apiComment.Poster.UserName)
223+
}
224+
}
225+
assert.Equal(t, body, apiComment.Body)
226+
}
227+
}
228+
192229
func TestAPIEditComment(t *testing.T) {
193230
defer tests.PrepareTestEnv(t)()
194231
const newCommentBody = "This is the new comment body"

0 commit comments

Comments
 (0)