]> source.dussan.org Git - gitea.git/commitdiff
Move webhook codes from service to webhook notification (#8712)
authorLunny Xiao <xiaolunwen@gmail.com>
Wed, 30 Oct 2019 10:02:46 +0000 (18:02 +0800)
committerLauris BH <lauris@nix.lv>
Wed, 30 Oct 2019 10:02:46 +0000 (12:02 +0200)
* Move webhook codes from service to webhook notification

* move deletecomment webhook to notifications

* fix notification

modules/notification/indexer/indexer.go
modules/notification/webhook/webhook.go
routers/api/v1/repo/issue_comment.go
routers/repo/issue.go
services/comments/comments.go

index 453eb0c295e0437fd543b20676d23d430d04fcbc..13baa76ac0afe0af34aa2d07c2fe10e896d29cba 100644 (file)
@@ -74,6 +74,11 @@ func (r *indexerNotifier) NotifyUpdateComment(doer *models.User, c *models.Comme
 
 func (r *indexerNotifier) NotifyDeleteComment(doer *models.User, comment *models.Comment) {
        if comment.Type == models.CommentTypeComment {
+               if err := comment.LoadIssue(); err != nil {
+                       log.Error("LoadIssue: %v", err)
+                       return
+               }
+
                var found bool
                if comment.Issue.Comments != nil {
                        for i := 0; i < len(comment.Issue.Comments); i++ {
index a2af152edda420cf4523545c1fb06b21c0b4394c..b4629ac56df577bce72fa9c44319a314bb28209e 100644 (file)
@@ -315,3 +315,86 @@ func (m *webhookNotifier) NotifyIssueChangeContent(doer *models.User, issue *mod
                go models.HookQueue.Add(issue.RepoID)
        }
 }
+
+func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
+       if err := c.LoadPoster(); err != nil {
+               log.Error("LoadPoster: %v", err)
+               return
+       }
+       if err := c.LoadIssue(); err != nil {
+               log.Error("LoadIssue: %v", err)
+               return
+       }
+
+       if err := c.Issue.LoadAttributes(); err != nil {
+               log.Error("LoadAttributes: %v", err)
+               return
+       }
+
+       mode, _ := models.AccessLevel(doer, c.Issue.Repo)
+       if err := models.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
+               Action:  api.HookIssueCommentEdited,
+               Issue:   c.Issue.APIFormat(),
+               Comment: c.APIFormat(),
+               Changes: &api.ChangesPayload{
+                       Body: &api.ChangesFromPayload{
+                               From: oldContent,
+                       },
+               },
+               Repository: c.Issue.Repo.APIFormat(mode),
+               Sender:     doer.APIFormat(),
+               IsPull:     c.Issue.IsPull,
+       }); err != nil {
+               log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
+       } else {
+               go models.HookQueue.Add(c.Issue.Repo.ID)
+       }
+}
+
+func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
+       issue *models.Issue, comment *models.Comment) {
+       mode, _ := models.AccessLevel(doer, repo)
+       if err := models.PrepareWebhooks(repo, models.HookEventIssueComment, &api.IssueCommentPayload{
+               Action:     api.HookIssueCommentCreated,
+               Issue:      issue.APIFormat(),
+               Comment:    comment.APIFormat(),
+               Repository: repo.APIFormat(mode),
+               Sender:     doer.APIFormat(),
+               IsPull:     issue.IsPull,
+       }); err != nil {
+               log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
+       } else {
+               go models.HookQueue.Add(repo.ID)
+       }
+}
+
+func (m *webhookNotifier) NotifyDeleteComment(doer *models.User, comment *models.Comment) {
+       if err := comment.LoadPoster(); err != nil {
+               log.Error("LoadPoster: %v", err)
+               return
+       }
+       if err := comment.LoadIssue(); err != nil {
+               log.Error("LoadIssue: %v", err)
+               return
+       }
+
+       if err := comment.Issue.LoadAttributes(); err != nil {
+               log.Error("LoadAttributes: %v", err)
+               return
+       }
+
+       mode, _ := models.AccessLevel(doer, comment.Issue.Repo)
+
+       if err := models.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
+               Action:     api.HookIssueCommentDeleted,
+               Issue:      comment.Issue.APIFormat(),
+               Comment:    comment.APIFormat(),
+               Repository: comment.Issue.Repo.APIFormat(mode),
+               Sender:     doer.APIFormat(),
+               IsPull:     comment.Issue.IsPull,
+       }); err != nil {
+               log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
+       } else {
+               go models.HookQueue.Add(comment.Issue.Repo.ID)
+       }
+}
index 60796031a5877321a6d2def1d2d2e699ed3ec689..3a5f6d24474f4df87c220b9df1f4f4c5f6f94900 100644 (file)
@@ -10,7 +10,6 @@ import (
 
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/modules/context"
-       "code.gitea.io/gitea/modules/notification"
        api "code.gitea.io/gitea/modules/structs"
        comment_service "code.gitea.io/gitea/services/comments"
 )
@@ -196,8 +195,6 @@ func CreateIssueComment(ctx *context.APIContext, form api.CreateIssueCommentOpti
                return
        }
 
-       notification.NotifyCreateIssueComment(ctx.User, ctx.Repo.Repository, issue, comment)
-
        ctx.JSON(201, comment.APIFormat())
 }
 
@@ -305,8 +302,6 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
                return
        }
 
-       notification.NotifyUpdateComment(ctx.User, comment, oldContent)
-
        ctx.JSON(200, comment.APIFormat())
 }
 
@@ -396,7 +391,5 @@ func deleteIssueComment(ctx *context.APIContext) {
                return
        }
 
-       notification.NotifyDeleteComment(ctx.User, comment)
-
        ctx.Status(204)
 }
index ac405a1c29e2c1b14a91ddafb91df74892184ba4..04c718d5b95b4fe85ffc63acc2407e7256aa9370 100644 (file)
@@ -1324,8 +1324,6 @@ func NewComment(ctx *context.Context, form auth.CreateCommentForm) {
                return
        }
 
-       notification.NotifyCreateIssueComment(ctx.User, ctx.Repo.Repository, issue, comment)
-
        log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
 }
 
@@ -1375,8 +1373,6 @@ func UpdateCommentContent(ctx *context.Context) {
                ctx.ServerError("UpdateAttachments", err)
        }
 
-       notification.NotifyUpdateComment(ctx.User, comment, oldContent)
-
        ctx.JSON(200, map[string]interface{}{
                "content":     string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
                "attachments": attachmentsHTML(ctx, comment.Attachments),
@@ -1404,13 +1400,11 @@ func DeleteComment(ctx *context.Context) {
                return
        }
 
-       if err = models.DeleteComment(comment, ctx.User); err != nil {
+       if err = comment_service.DeleteComment(comment, ctx.User); err != nil {
                ctx.ServerError("DeleteCommentByID", err)
                return
        }
 
-       notification.NotifyDeleteComment(ctx.User, comment)
-
        ctx.Status(200)
 }
 
index 010c0aaac7b982ae6a8163de9ad2d44085e41a67..1ae5e2743fe7fbfdb48f92767168aeb19d26da0e 100644 (file)
@@ -11,9 +11,8 @@ import (
 
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/modules/git"
-       "code.gitea.io/gitea/modules/log"
+       "code.gitea.io/gitea/modules/notification"
        "code.gitea.io/gitea/modules/setting"
-       api "code.gitea.io/gitea/modules/structs"
        "code.gitea.io/gitea/services/gitdiff"
 )
 
@@ -31,19 +30,8 @@ func CreateIssueComment(doer *models.User, repo *models.Repository, issue *model
                return nil, err
        }
 
-       mode, _ := models.AccessLevel(doer, repo)
-       if err = models.PrepareWebhooks(repo, models.HookEventIssueComment, &api.IssueCommentPayload{
-               Action:     api.HookIssueCommentCreated,
-               Issue:      issue.APIFormat(),
-               Comment:    comment.APIFormat(),
-               Repository: repo.APIFormat(mode),
-               Sender:     doer.APIFormat(),
-               IsPull:     issue.IsPull,
-       }); err != nil {
-               log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
-       } else {
-               go models.HookQueue.Add(repo.ID)
-       }
+       notification.NotifyCreateIssueComment(doer, repo, issue, comment)
+
        return comment, nil
 }
 
@@ -106,35 +94,7 @@ func UpdateComment(c *models.Comment, doer *models.User, oldContent string) erro
                return err
        }
 
-       if err := c.LoadPoster(); err != nil {
-               return err
-       }
-       if err := c.LoadIssue(); err != nil {
-               return err
-       }
-
-       if err := c.Issue.LoadAttributes(); err != nil {
-               return err
-       }
-
-       mode, _ := models.AccessLevel(doer, c.Issue.Repo)
-       if err := models.PrepareWebhooks(c.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
-               Action:  api.HookIssueCommentEdited,
-               Issue:   c.Issue.APIFormat(),
-               Comment: c.APIFormat(),
-               Changes: &api.ChangesPayload{
-                       Body: &api.ChangesFromPayload{
-                               From: oldContent,
-                       },
-               },
-               Repository: c.Issue.Repo.APIFormat(mode),
-               Sender:     doer.APIFormat(),
-               IsPull:     c.Issue.IsPull,
-       }); err != nil {
-               log.Error("PrepareWebhooks [comment_id: %d]: %v", c.ID, err)
-       } else {
-               go models.HookQueue.Add(c.Issue.Repo.ID)
-       }
+       notification.NotifyUpdateComment(doer, c, oldContent)
 
        return nil
 }
@@ -145,31 +105,7 @@ func DeleteComment(comment *models.Comment, doer *models.User) error {
                return err
        }
 
-       if err := comment.LoadPoster(); err != nil {
-               return err
-       }
-       if err := comment.LoadIssue(); err != nil {
-               return err
-       }
-
-       if err := comment.Issue.LoadAttributes(); err != nil {
-               return err
-       }
-
-       mode, _ := models.AccessLevel(doer, comment.Issue.Repo)
-
-       if err := models.PrepareWebhooks(comment.Issue.Repo, models.HookEventIssueComment, &api.IssueCommentPayload{
-               Action:     api.HookIssueCommentDeleted,
-               Issue:      comment.Issue.APIFormat(),
-               Comment:    comment.APIFormat(),
-               Repository: comment.Issue.Repo.APIFormat(mode),
-               Sender:     doer.APIFormat(),
-               IsPull:     comment.Issue.IsPull,
-       }); err != nil {
-               log.Error("PrepareWebhooks [comment_id: %d]: %v", comment.ID, err)
-       } else {
-               go models.HookQueue.Add(comment.Issue.Repo.ID)
-       }
+       notification.NotifyDeleteComment(doer, comment)
 
        return nil
 }