Browse Source

Move webhook codes from service to webhook notification (#8712)

* Move webhook codes from service to webhook notification

* move deletecomment webhook to notifications

* fix notification
tags/v1.11.0-rc1
Lunny Xiao 4 years ago
parent
commit
ac6accef09

+ 5
- 0
modules/notification/indexer/indexer.go View File



func (r *indexerNotifier) NotifyDeleteComment(doer *models.User, comment *models.Comment) { func (r *indexerNotifier) NotifyDeleteComment(doer *models.User, comment *models.Comment) {
if comment.Type == models.CommentTypeComment { if comment.Type == models.CommentTypeComment {
if err := comment.LoadIssue(); err != nil {
log.Error("LoadIssue: %v", err)
return
}

var found bool var found bool
if comment.Issue.Comments != nil { if comment.Issue.Comments != nil {
for i := 0; i < len(comment.Issue.Comments); i++ { for i := 0; i < len(comment.Issue.Comments); i++ {

+ 83
- 0
modules/notification/webhook/webhook.go View File

go models.HookQueue.Add(issue.RepoID) 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)
}
}

+ 0
- 7
routers/api/v1/repo/issue_comment.go View File



"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/notification"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
comment_service "code.gitea.io/gitea/services/comments" comment_service "code.gitea.io/gitea/services/comments"
) )
return return
} }


notification.NotifyCreateIssueComment(ctx.User, ctx.Repo.Repository, issue, comment)

ctx.JSON(201, comment.APIFormat()) ctx.JSON(201, comment.APIFormat())
} }


return return
} }


notification.NotifyUpdateComment(ctx.User, comment, oldContent)

ctx.JSON(200, comment.APIFormat()) ctx.JSON(200, comment.APIFormat())
} }


return return
} }


notification.NotifyDeleteComment(ctx.User, comment)

ctx.Status(204) ctx.Status(204)
} }

+ 1
- 7
routers/repo/issue.go View File

return 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) log.Trace("Comment created: %d/%d/%d", ctx.Repo.Repository.ID, issue.ID, comment.ID)
} }


ctx.ServerError("UpdateAttachments", err) ctx.ServerError("UpdateAttachments", err)
} }


notification.NotifyUpdateComment(ctx.User, comment, oldContent)

ctx.JSON(200, map[string]interface{}{ ctx.JSON(200, map[string]interface{}{
"content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())), "content": string(markdown.Render([]byte(comment.Content), ctx.Query("context"), ctx.Repo.Repository.ComposeMetas())),
"attachments": attachmentsHTML(ctx, comment.Attachments), "attachments": attachmentsHTML(ctx, comment.Attachments),
return return
} }


if err = models.DeleteComment(comment, ctx.User); err != nil {
if err = comment_service.DeleteComment(comment, ctx.User); err != nil {
ctx.ServerError("DeleteCommentByID", err) ctx.ServerError("DeleteCommentByID", err)
return return
} }


notification.NotifyDeleteComment(ctx.User, comment)

ctx.Status(200) ctx.Status(200)
} }



+ 5
- 69
services/comments/comments.go View File



"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/gitdiff" "code.gitea.io/gitea/services/gitdiff"
) )


return nil, err 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 return comment, nil
} }


return err 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 return nil
} }
return err 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 return nil
} }

Loading…
Cancel
Save