Skip to content

Commit 46c3273

Browse files
committed
Move webhook codes from service to webhook notification
1 parent f694bb4 commit 46c3273

File tree

4 files changed

+56
-50
lines changed

4 files changed

+56
-50
lines changed

modules/notification/webhook/webhook.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,55 @@ 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+
}

routers/api/v1/repo/issue_comment.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,6 @@ func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOpti
196196
return
197197
}
198198

199-
notification.NotifyCreateIssueComment(ctx.User, ctx.Repo.Repository, issue, comment)
200-
201199
ctx.JSON(201, comment.APIFormat())
202200
}
203201

@@ -305,8 +303,6 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
305303
return
306304
}
307305

308-
notification.NotifyUpdateComment(ctx.User, comment, oldContent)
309-
310306
ctx.JSON(200, comment.APIFormat())
311307
}
312308

routers/repo/issue.go

Lines changed: 0 additions & 4 deletions
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),

services/comments/comments.go

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/modules/git"
1414
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/notification"
1516
"code.gitea.io/gitea/modules/setting"
1617
api "code.gitea.io/gitea/modules/structs"
1718
"code.gitea.io/gitea/services/gitdiff"
@@ -31,19 +32,8 @@ func CreateIssueComment(doer *models.User, repo *models.Repository, issue *model
3132
return nil, err
3233
}
3334

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-
}
35+
notification.NotifyCreateIssueComment(doer, repo, issue, comment)
36+
4737
return comment, nil
4838
}
4939

@@ -106,35 +96,7 @@ func UpdateComment(c *models.Comment, doer *models.User, oldContent string) erro
10696
return err
10797
}
10898

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-
}
99+
notification.NotifyUpdateComment(doer, c, oldContent)
138100

139101
return nil
140102
}

0 commit comments

Comments
 (0)