Skip to content

Commit ac6acce

Browse files
lunnylafriks
authored andcommitted
Move webhook codes from service to webhook notification (#8712)
* Move webhook codes from service to webhook notification * move deletecomment webhook to notifications * fix notification
1 parent f694bb4 commit ac6acce

File tree

5 files changed

+94
-83
lines changed

5 files changed

+94
-83
lines changed

modules/notification/indexer/indexer.go

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ func (r *indexerNotifier) NotifyUpdateComment(doer *models.User, c *models.Comme
7474

7575
func (r *indexerNotifier) NotifyDeleteComment(doer *models.User, comment *models.Comment) {
7676
if comment.Type == models.CommentTypeComment {
77+
if err := comment.LoadIssue(); err != nil {
78+
log.Error("LoadIssue: %v", err)
79+
return
80+
}
81+
7782
var found bool
7883
if comment.Issue.Comments != nil {
7984
for i := 0; i < len(comment.Issue.Comments); i++ {

modules/notification/webhook/webhook.go

+83
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,86 @@ func (m *webhookNotifier) NotifyIssueChangeContent(doer *models.User, issue *mod
315315
go models.HookQueue.Add(issue.RepoID)
316316
}
317317
}
318+
319+
func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
320+
if err := c.LoadPoster(); err != nil {
321+
log.Error("LoadPoster: %v", err)
322+
return
323+
}
324+
if err := c.LoadIssue(); err != nil {
325+
log.Error("LoadIssue: %v", err)
326+
return
327+
}
328+
329+
if err := c.Issue.LoadAttributes(); err != nil {
330+
log.Error("LoadAttributes: %v", err)
331+
return
332+
}
333+
334+
mode, _ := models.AccessLevel(doer, c.Issue.Repo)
335+
if err := models.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
336+
Action: api.HookIssueCommentEdited,
337+
Issue: c.Issue.APIFormat(),
338+
Comment: c.APIFormat(),
339+
Changes: &api.ChangesPayload{
340+
Body: &api.ChangesFromPayload{
341+
From: oldContent,
342+
},
343+
},
344+
Repository: c.Issue.Repo.APIFormat(mode),
345+
Sender: doer.APIFormat(),
346+
IsPull: c.Issue.IsPull,
347+
}); err != nil {
348+
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
349+
} else {
350+
go models.HookQueue.Add(c.Issue.Repo.ID)
351+
}
352+
}
353+
354+
func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
355+
issue *models.Issue, comment *models.Comment) {
356+
mode, _ := models.AccessLevel(doer, repo)
357+
if err := models.PrepareWebhooks(repo, models.HookEventIssueComment, &api.IssueCommentPayload{
358+
Action: api.HookIssueCommentCreated,
359+
Issue: issue.APIFormat(),
360+
Comment: comment.APIFormat(),
361+
Repository: repo.APIFormat(mode),
362+
Sender: doer.APIFormat(),
363+
IsPull: issue.IsPull,
364+
}); err != nil {
365+
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
366+
} else {
367+
go models.HookQueue.Add(repo.ID)
368+
}
369+
}
370+
371+
func (m *webhookNotifier) NotifyDeleteComment(doer *models.User, comment *models.Comment) {
372+
if err := comment.LoadPoster(); err != nil {
373+
log.Error("LoadPoster: %v", err)
374+
return
375+
}
376+
if err := comment.LoadIssue(); err != nil {
377+
log.Error("LoadIssue: %v", err)
378+
return
379+
}
380+
381+
if err := comment.Issue.LoadAttributes(); err != nil {
382+
log.Error("LoadAttributes: %v", err)
383+
return
384+
}
385+
386+
mode, _ := models.AccessLevel(doer, comment.Issue.Repo)
387+
388+
if err := models.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
389+
Action: api.HookIssueCommentDeleted,
390+
Issue: comment.Issue.APIFormat(),
391+
Comment: comment.APIFormat(),
392+
Repository: comment.Issue.Repo.APIFormat(mode),
393+
Sender: doer.APIFormat(),
394+
IsPull: comment.Issue.IsPull,
395+
}); err != nil {
396+
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
397+
} else {
398+
go models.HookQueue.Add(comment.Issue.Repo.ID)
399+
}
400+
}

routers/api/v1/repo/issue_comment.go

-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"code.gitea.io/gitea/models"
1212
"code.gitea.io/gitea/modules/context"
13-
"code.gitea.io/gitea/modules/notification"
1413
api "code.gitea.io/gitea/modules/structs"
1514
comment_service "code.gitea.io/gitea/services/comments"
1615
)
@@ -196,8 +195,6 @@ func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOpti
196195
return
197196
}
198197

199-
notification.NotifyCreateIssueComment(ctx.User, ctx.Repo.Repository, issue, comment)
200-
201198
ctx.JSON(201, comment.APIFormat())
202199
}
203200

@@ -305,8 +302,6 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
305302
return
306303
}
307304

308-
notification.NotifyUpdateComment(ctx.User, comment, oldContent)
309-
310305
ctx.JSON(200, comment.APIFormat())
311306
}
312307

@@ -396,7 +391,5 @@ func deleteIssueComment(ctx *context.APIContext) {
396391
return
397392
}
398393

399-
notification.NotifyDeleteComment(ctx.User, comment)
400-
401394
ctx.Status(204)
402395
}

routers/repo/issue.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -1324,8 +1324,6 @@ func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
13241324
return
13251325
}
13261326

1327-
notification.NotifyCreateIssueComment(ctx.User, ctx.Repo.Repository, issue, comment)
1328-
13291327
log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
13301328
}
13311329

@@ -1375,8 +1373,6 @@ func UpdateCommentContent(ctx *context.Context) {
13751373
ctx.ServerError("UpdateAttachments", err)
13761374
}
13771375

1378-
notification.NotifyUpdateComment(ctx.User, comment, oldContent)
1379-
13801376
ctx.JSON(200, map[string]interface{}{
13811377
"content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
13821378
"attachments": attachmentsHTML(ctx, comment.Attachments),
@@ -1404,13 +1400,11 @@ func DeleteComment(ctx *context.Context) {
14041400
return
14051401
}
14061402

1407-
if err = models.DeleteComment(comment, ctx.User); err != nil {
1403+
if err = comment_service.DeleteComment(comment, ctx.User); err != nil {
14081404
ctx.ServerError("DeleteCommentByID", err)
14091405
return
14101406
}
14111407

1412-
notification.NotifyDeleteComment(ctx.User, comment)
1413-
14141408
ctx.Status(200)
14151409
}
14161410

services/comments/comments.go

+5-69
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import (
1111

1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/modules/git"
14-
"code.gitea.io/gitea/modules/log"
14+
"code.gitea.io/gitea/modules/notification"
1515
"code.gitea.io/gitea/modules/setting"
16-
api "code.gitea.io/gitea/modules/structs"
1716
"code.gitea.io/gitea/services/gitdiff"
1817
)
1918

@@ -31,19 +30,8 @@ func CreateIssueComment(doer *models.User, repo *models.Repository, issue *model
3130
return nil, err
3231
}
3332

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-
IsPull: issue.IsPull,
42-
}); err != nil {
43-
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
44-
} else {
45-
go models.HookQueue.Add(repo.ID)
46-
}
33+
notification.NotifyCreateIssueComment(doer, repo, issue, comment)
34+
4735
return comment, nil
4836
}
4937

@@ -106,35 +94,7 @@ func UpdateComment(c *models.Comment, doer *models.User, oldContent string) erro
10694
return err
10795
}
10896

109-
if err := c.LoadPoster(); err != nil {
110-
return err
111-
}
112-
if err := c.LoadIssue(); err != nil {
113-
return err
114-
}
115-
116-
if err := c.Issue.LoadAttributes(); err != nil {
117-
return err
118-
}
119-
120-
mode, _ := models.AccessLevel(doer, c.Issue.Repo)
121-
if err := models.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
122-
Action: api.HookIssueCommentEdited,
123-
Issue: c.Issue.APIFormat(),
124-
Comment: c.APIFormat(),
125-
Changes: &api.ChangesPayload{
126-
Body: &api.ChangesFromPayload{
127-
From: oldContent,
128-
},
129-
},
130-
Repository: c.Issue.Repo.APIFormat(mode),
131-
Sender: doer.APIFormat(),
132-
IsPull: c.Issue.IsPull,
133-
}); err != nil {
134-
log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
135-
} else {
136-
go models.HookQueue.Add(c.Issue.Repo.ID)
137-
}
97+
notification.NotifyUpdateComment(doer, c, oldContent)
13898

13999
return nil
140100
}
@@ -145,31 +105,7 @@ func DeleteComment(comment *models.Comment, doer *models.User) error {
145105
return err
146106
}
147107

148-
if err := comment.LoadPoster(); err != nil {
149-
return err
150-
}
151-
if err := comment.LoadIssue(); err != nil {
152-
return err
153-
}
154-
155-
if err := comment.Issue.LoadAttributes(); err != nil {
156-
return err
157-
}
158-
159-
mode, _ := models.AccessLevel(doer, comment.Issue.Repo)
160-
161-
if err := models.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
162-
Action: api.HookIssueCommentDeleted,
163-
Issue: comment.Issue.APIFormat(),
164-
Comment: comment.APIFormat(),
165-
Repository: comment.Issue.Repo.APIFormat(mode),
166-
Sender: doer.APIFormat(),
167-
IsPull: comment.Issue.IsPull,
168-
}); err != nil {
169-
log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
170-
} else {
171-
go models.HookQueue.Add(comment.Issue.Repo.ID)
172-
}
108+
notification.NotifyDeleteComment(doer, comment)
173109

174110
return nil
175111
}

0 commit comments

Comments
 (0)