Skip to content

Commit 0613883

Browse files
lunnytechknowlogick
authored andcommitted
Move create issue comment to comments package (#8212)
* move create issue comment to comments package * extract actions on update/delete comment from models to comment service * fix lint * fix lint
1 parent 3dd1cee commit 0613883

File tree

4 files changed

+117
-117
lines changed

4 files changed

+117
-117
lines changed

models/issue_comment.go

+6-111
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (c *Comment) loadIssue(e Engine) (err error) {
169169
}
170170

171171
func (c *Comment) loadPoster(e Engine) (err error) {
172-
if c.Poster != nil {
172+
if c.PosterID <= 0 || c.Poster != nil {
173173
return nil
174174
}
175175

@@ -338,21 +338,7 @@ func (c *Comment) LoadMilestone() error {
338338

339339
// LoadPoster loads comment poster
340340
func (c *Comment) LoadPoster() error {
341-
if c.PosterID <= 0 || c.Poster != nil {
342-
return nil
343-
}
344-
345-
var err error
346-
c.Poster, err = getUserByID(x, c.PosterID)
347-
if err != nil {
348-
if IsErrUserNotExist(err) {
349-
c.PosterID = -1
350-
c.Poster = NewGhostUser()
351-
} else {
352-
log.Error("getUserByID[%d]: %v", c.ID, err)
353-
}
354-
}
355-
return nil
341+
return c.loadPoster(x)
356342
}
357343

358344
// LoadAttachments loads attachments
@@ -440,7 +426,7 @@ func (c *Comment) checkInvalidation(doer *User, repo *git.Repository, branch str
440426
}
441427
if c.CommitSHA != "" && c.CommitSHA != commit.ID.String() {
442428
c.Invalidated = true
443-
return UpdateComment(doer, c, "")
429+
return UpdateComment(c, doer)
444430
}
445431
return nil
446432
}
@@ -811,35 +797,6 @@ func CreateComment(opts *CreateCommentOptions) (comment *Comment, err error) {
811797
return comment, nil
812798
}
813799

814-
// CreateIssueComment creates a plain issue comment.
815-
func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content string, attachments []string) (*Comment, error) {
816-
comment, err := CreateComment(&CreateCommentOptions{
817-
Type: CommentTypeComment,
818-
Doer: doer,
819-
Repo: repo,
820-
Issue: issue,
821-
Content: content,
822-
Attachments: attachments,
823-
})
824-
if err != nil {
825-
return nil, fmt.Errorf("CreateComment: %v", err)
826-
}
827-
828-
mode, _ := AccessLevel(doer, repo)
829-
if err = PrepareWebhooks(repo, HookEventIssueComment, &api.IssueCommentPayload{
830-
Action: api.HookIssueCommentCreated,
831-
Issue: issue.APIFormat(),
832-
Comment: comment.APIFormat(),
833-
Repository: repo.APIFormat(mode),
834-
Sender: doer.APIFormat(),
835-
}); err != nil {
836-
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
837-
} else {
838-
go HookQueue.Add(repo.ID)
839-
}
840-
return comment, nil
841-
}
842-
843800
// CreateRefComment creates a commit reference comment to issue.
844801
func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
845802
if len(commitSHA) == 0 {
@@ -928,7 +885,7 @@ func FindComments(opts FindCommentsOptions) ([]*Comment, error) {
928885
}
929886

930887
// UpdateComment updates information of comment.
931-
func UpdateComment(doer *User, c *Comment, oldContent string) error {
888+
func UpdateComment(c *Comment, doer *User) error {
932889
sess := x.NewSession()
933890
defer sess.Close()
934891
if err := sess.Begin(); err != nil {
@@ -950,38 +907,12 @@ func UpdateComment(doer *User, c *Comment, oldContent string) error {
950907
if err := sess.Commit(); err != nil {
951908
return fmt.Errorf("Commit: %v", err)
952909
}
953-
sess.Close()
954-
955-
if err := c.LoadPoster(); err != nil {
956-
return err
957-
}
958-
if err := c.Issue.LoadAttributes(); err != nil {
959-
return err
960-
}
961-
962-
mode, _ := AccessLevel(doer, c.Issue.Repo)
963-
if err := PrepareWebhooks(c.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
964-
Action: api.HookIssueCommentEdited,
965-
Issue: c.Issue.APIFormat(),
966-
Comment: c.APIFormat(),
967-
Changes: &api.ChangesPayload{
968-
Body: &api.ChangesFromPayload{
969-
From: oldContent,
970-
},
971-
},
972-
Repository: c.Issue.Repo.APIFormat(mode),
973-
Sender: doer.APIFormat(),
974-
}); err != nil {
975-
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
976-
} else {
977-
go HookQueue.Add(c.Issue.Repo.ID)
978-
}
979910

980911
return nil
981912
}
982913

983914
// DeleteComment deletes the comment
984-
func DeleteComment(doer *User, comment *Comment) error {
915+
func DeleteComment(comment *Comment, doer *User) error {
985916
sess := x.NewSession()
986917
defer sess.Close()
987918
if err := sess.Begin(); err != nil {
@@ -1007,43 +938,7 @@ func DeleteComment(doer *User, comment *Comment) error {
1007938
return err
1008939
}
1009940

1010-
if err := sess.Commit(); err != nil {
1011-
return err
1012-
}
1013-
sess.Close()
1014-
1015-
if err := comment.LoadPoster(); err != nil {
1016-
return err
1017-
}
1018-
if err := comment.LoadIssue(); err != nil {
1019-
return err
1020-
}
1021-
1022-
if err := comment.Issue.LoadAttributes(); err != nil {
1023-
return err
1024-
}
1025-
if err := comment.loadPoster(x); err != nil {
1026-
return err
1027-
}
1028-
if err := comment.neuterCrossReferences(x); err != nil {
1029-
return err
1030-
}
1031-
1032-
mode, _ := AccessLevel(doer, comment.Issue.Repo)
1033-
1034-
if err := PrepareWebhooks(comment.Issue.Repo, HookEventIssueComment, &api.IssueCommentPayload{
1035-
Action: api.HookIssueCommentDeleted,
1036-
Issue: comment.Issue.APIFormat(),
1037-
Comment: comment.APIFormat(),
1038-
Repository: comment.Issue.Repo.APIFormat(mode),
1039-
Sender: doer.APIFormat(),
1040-
}); err != nil {
1041-
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
1042-
} else {
1043-
go HookQueue.Add(comment.Issue.Repo.ID)
1044-
}
1045-
1046-
return nil
941+
return sess.Commit()
1047942
}
1048943

1049944
// CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS

routers/api/v1/repo/issue_comment.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/context"
1313
"code.gitea.io/gitea/modules/notification"
1414
api "code.gitea.io/gitea/modules/structs"
15+
comment_service "code.gitea.io/gitea/services/comments"
1516
)
1617

1718
// ListIssueComments list all the comments of an issue
@@ -189,7 +190,7 @@ func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOpti
189190
return
190191
}
191192

192-
comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, nil)
193+
comment, err := comment_service.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Body, nil)
193194
if err != nil {
194195
ctx.Error(500, "CreateIssueComment", err)
195196
return
@@ -299,7 +300,7 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
299300

300301
oldContent := comment.Content
301302
comment.Content = form.Body
302-
if err := models.UpdateComment(ctx.User, comment, oldContent); err != nil {
303+
if err := comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
303304
ctx.Error(500, "UpdateComment", err)
304305
return
305306
}
@@ -390,7 +391,7 @@ func deleteIssueComment(ctx *context.APIContext) {
390391
return
391392
}
392393

393-
if err = models.DeleteComment(ctx.User, comment); err != nil {
394+
if err = comment_service.DeleteComment(comment, ctx.User); err != nil {
394395
ctx.Error(500, "DeleteCommentByID", err)
395396
return
396397
}

routers/repo/issue.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"code.gitea.io/gitea/modules/setting"
2727
api "code.gitea.io/gitea/modules/structs"
2828
"code.gitea.io/gitea/modules/util"
29+
comment_service "code.gitea.io/gitea/services/comments"
2930
milestone_service "code.gitea.io/gitea/services/milestone"
3031

3132
"github.com/unknwon/com"
@@ -1299,7 +1300,7 @@ func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
12991300
return
13001301
}
13011302

1302-
comment, err := models.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments)
1303+
comment, err := comment_service.CreateIssueComment(ctx.User, ctx.Repo.Repository, issue, form.Content, attachments)
13031304
if err != nil {
13041305
ctx.ServerError("CreateIssueComment", err)
13051306
return
@@ -1339,7 +1340,7 @@ func UpdateCommentContent(ctx *context.Context) {
13391340
})
13401341
return
13411342
}
1342-
if err = models.UpdateComment(ctx.User, comment, oldContent); err != nil {
1343+
if err = comment_service.UpdateComment(comment, ctx.User, oldContent); err != nil {
13431344
ctx.ServerError("UpdateComment", err)
13441345
return
13451346
}
@@ -1372,7 +1373,7 @@ func DeleteComment(ctx *context.Context) {
13721373
return
13731374
}
13741375

1375-
if err = models.DeleteComment(ctx.User, comment); err != nil {
1376+
if err = models.DeleteComment(comment, ctx.User); err != nil {
13761377
ctx.ServerError("DeleteCommentByID", err)
13771378
return
13781379
}

services/comments/comments.go

+103
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,41 @@ import (
1111

1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/modules/git"
14+
"code.gitea.io/gitea/modules/log"
1415
"code.gitea.io/gitea/modules/setting"
16+
api "code.gitea.io/gitea/modules/structs"
1517
"code.gitea.io/gitea/services/gitdiff"
1618
)
1719

20+
// CreateIssueComment creates a plain issue comment.
21+
func CreateIssueComment(doer *models.User, repo *models.Repository, issue *models.Issue, content string, attachments []string) (*models.Comment, error) {
22+
comment, err := models.CreateComment(&models.CreateCommentOptions{
23+
Type: models.CommentTypeComment,
24+
Doer: doer,
25+
Repo: repo,
26+
Issue: issue,
27+
Content: content,
28+
Attachments: attachments,
29+
})
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
mode, _ := models.AccessLevel(doer, repo)
35+
if err = models.PrepareWebhooks(repo, models.HookEventIssueComment, &api.IssueCommentPayload{
36+
Action: api.HookIssueCommentCreated,
37+
Issue: issue.APIFormat(),
38+
Comment: comment.APIFormat(),
39+
Repository: repo.APIFormat(mode),
40+
Sender: doer.APIFormat(),
41+
}); err != nil {
42+
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
43+
} else {
44+
go models.HookQueue.Add(repo.ID)
45+
}
46+
return comment, nil
47+
}
48+
1849
// CreateCodeComment creates a plain code comment at the specified line / path
1950
func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) {
2051
var commitID, patch string
@@ -67,3 +98,75 @@ func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models
6798
Patch: patch,
6899
})
69100
}
101+
102+
// UpdateComment updates information of comment.
103+
func UpdateComment(c *models.Comment, doer *models.User, oldContent string) error {
104+
if err := models.UpdateComment(c, doer); err != nil {
105+
return err
106+
}
107+
108+
if err := c.LoadPoster(); err != nil {
109+
return err
110+
}
111+
if err := c.LoadIssue(); err != nil {
112+
return err
113+
}
114+
115+
if err := c.Issue.LoadAttributes(); err != nil {
116+
return err
117+
}
118+
119+
mode, _ := models.AccessLevel(doer, c.Issue.Repo)
120+
if err := models.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
121+
Action: api.HookIssueCommentEdited,
122+
Issue: c.Issue.APIFormat(),
123+
Comment: c.APIFormat(),
124+
Changes: &api.ChangesPayload{
125+
Body: &api.ChangesFromPayload{
126+
From: oldContent,
127+
},
128+
},
129+
Repository: c.Issue.Repo.APIFormat(mode),
130+
Sender: doer.APIFormat(),
131+
}); err != nil {
132+
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
133+
} else {
134+
go models.HookQueue.Add(c.Issue.Repo.ID)
135+
}
136+
137+
return nil
138+
}
139+
140+
// DeleteComment deletes the comment
141+
func DeleteComment(comment *models.Comment, doer *models.User) error {
142+
if err := models.DeleteComment(comment, doer); err != nil {
143+
return err
144+
}
145+
146+
if err := comment.LoadPoster(); err != nil {
147+
return err
148+
}
149+
if err := comment.LoadIssue(); err != nil {
150+
return err
151+
}
152+
153+
if err := comment.Issue.LoadAttributes(); err != nil {
154+
return err
155+
}
156+
157+
mode, _ := models.AccessLevel(doer, comment.Issue.Repo)
158+
159+
if err := models.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
160+
Action: api.HookIssueCommentDeleted,
161+
Issue: comment.Issue.APIFormat(),
162+
Comment: comment.APIFormat(),
163+
Repository: comment.Issue.Repo.APIFormat(mode),
164+
Sender: doer.APIFormat(),
165+
}); err != nil {
166+
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
167+
} else {
168+
go models.HookQueue.Add(comment.Issue.Repo.ID)
169+
}
170+
171+
return nil
172+
}

0 commit comments

Comments
 (0)