summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-10-15 13:03:05 +0800
committerGitHub <noreply@github.com>2019-10-15 13:03:05 +0800
commit20477a69ea123a7800ebf94bfd2225eb9ae90e8f (patch)
tree0e01914d69bd5dbda358c7c5ce90c375e0f29be0 /modules
parent34fb9d68a5567423ddde736ff42f9780f4048366 (diff)
downloadgitea-20477a69ea123a7800ebf94bfd2225eb9ae90e8f.tar.gz
gitea-20477a69ea123a7800ebf94bfd2225eb9ae90e8f.zip
Move clearlabels from models to issue service (#8326)
* move clearlabels from models to issue service * improve code * Apply suggestions from code review Co-Authored-By: zeripath <art27@cantab.net>
Diffstat (limited to 'modules')
-rw-r--r--modules/notification/notification.go2
-rw-r--r--modules/notification/webhook/webhook.go67
2 files changed, 69 insertions, 0 deletions
diff --git a/modules/notification/notification.go b/modules/notification/notification.go
index e0de88346d..06220ecb04 100644
--- a/modules/notification/notification.go
+++ b/modules/notification/notification.go
@@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/modules/notification/indexer"
"code.gitea.io/gitea/modules/notification/mail"
"code.gitea.io/gitea/modules/notification/ui"
+ "code.gitea.io/gitea/modules/notification/webhook"
)
var (
@@ -27,6 +28,7 @@ func init() {
RegisterNotifier(ui.NewNotifier())
RegisterNotifier(mail.NewNotifier())
RegisterNotifier(indexer.NewNotifier())
+ RegisterNotifier(webhook.NewNotifier())
}
// NotifyCreateIssueComment notifies issue comment related message to notifiers
diff --git a/modules/notification/webhook/webhook.go b/modules/notification/webhook/webhook.go
new file mode 100644
index 0000000000..33adfaa739
--- /dev/null
+++ b/modules/notification/webhook/webhook.go
@@ -0,0 +1,67 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package webhook
+
+import (
+ "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/notification/base"
+ api "code.gitea.io/gitea/modules/structs"
+)
+
+type webhookNotifier struct {
+ base.NullNotifier
+}
+
+var (
+ _ base.Notifier = &webhookNotifier{}
+)
+
+// NewNotifier create a new webhookNotifier notifier
+func NewNotifier() base.Notifier {
+ return &webhookNotifier{}
+}
+
+func (m *webhookNotifier) NotifyIssueClearLabels(doer *models.User, issue *models.Issue) {
+ if err := issue.LoadPoster(); err != nil {
+ log.Error("loadPoster: %v", err)
+ return
+ }
+
+ if err := issue.LoadRepo(); err != nil {
+ log.Error("LoadRepo: %v", err)
+ return
+ }
+
+ mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
+ var err error
+ if issue.IsPull {
+ if err = issue.LoadPullRequest(); err != nil {
+ log.Error("LoadPullRequest: %v", err)
+ return
+ }
+
+ err = models.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
+ Action: api.HookIssueLabelCleared,
+ Index: issue.Index,
+ PullRequest: issue.PullRequest.APIFormat(),
+ Repository: issue.Repo.APIFormat(mode),
+ Sender: doer.APIFormat(),
+ })
+ } else {
+ err = models.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
+ Action: api.HookIssueLabelCleared,
+ Index: issue.Index,
+ Issue: issue.APIFormat(),
+ Repository: issue.Repo.APIFormat(mode),
+ Sender: doer.APIFormat(),
+ })
+ }
+ if err != nil {
+ log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
+ } else {
+ go models.HookQueue.Add(issue.RepoID)
+ }
+}