]> source.dussan.org Git - gitea.git/commitdiff
Move clearlabels from models to issue service (#8326)
authorLunny Xiao <xiaolunwen@gmail.com>
Tue, 15 Oct 2019 05:03:05 +0000 (13:03 +0800)
committerGitHub <noreply@github.com>
Tue, 15 Oct 2019 05:03:05 +0000 (13:03 +0800)
* move clearlabels from models to issue service

* improve code

* Apply suggestions from code review

Co-Authored-By: zeripath <art27@cantab.net>
models/issue.go
modules/notification/notification.go
modules/notification/webhook/webhook.go [new file with mode: 0644]
routers/api/v1/repo/issue_label.go
routers/repo/issue_label.go
services/issue/label.go [new file with mode: 0644]

index fc675a3ffbe2acdf58f102cd634076e70d376b91..90925f92f597906e8eef4a2e0a1317c8ce19bdea 100644 (file)
@@ -596,40 +596,6 @@ func (issue *Issue) ClearLabels(doer *User) (err error) {
        if err = sess.Commit(); err != nil {
                return fmt.Errorf("Commit: %v", err)
        }
-       sess.Close()
-
-       if err = issue.LoadPoster(); err != nil {
-               return fmt.Errorf("loadPoster: %v", err)
-       }
-
-       mode, _ := AccessLevel(issue.Poster, issue.Repo)
-       if issue.IsPull {
-               err = issue.PullRequest.LoadIssue()
-               if err != nil {
-                       log.Error("LoadIssue: %v", err)
-                       return
-               }
-               err = PrepareWebhooks(issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
-                       Action:      api.HookIssueLabelCleared,
-                       Index:       issue.Index,
-                       PullRequest: issue.PullRequest.APIFormat(),
-                       Repository:  issue.Repo.APIFormat(mode),
-                       Sender:      doer.APIFormat(),
-               })
-       } else {
-               err = PrepareWebhooks(issue.Repo, 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 HookQueue.Add(issue.RepoID)
-       }
 
        return nil
 }
index e0de88346d75f34579a96d9043284719cc07d3a9..06220ecb04019942222ce6d10298c40711a938e7 100644 (file)
@@ -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 (file)
index 0000000..33adfaa
--- /dev/null
@@ -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)
+       }
+}
index e66bc35e90b429d1da3fe4f104411b2fdd366f1d..7bd76c24c23cfa556fa900b7489802768068da67 100644 (file)
@@ -9,6 +9,7 @@ import (
        "code.gitea.io/gitea/models"
        "code.gitea.io/gitea/modules/context"
        api "code.gitea.io/gitea/modules/structs"
+       issue_service "code.gitea.io/gitea/services/issue"
 )
 
 // ListIssueLabels list all the labels of an issue
@@ -314,7 +315,7 @@ func ClearIssueLabels(ctx *context.APIContext) {
                return
        }
 
-       if err := issue.ClearLabels(ctx.User); err != nil {
+       if err := issue_service.ClearLabels(issue, ctx.User); err != nil {
                ctx.Error(500, "ClearLabels", err)
                return
        }
index 452a0a4c0c3ee41f554dfd9a08a2a60e30430695..53c37e2e9762fa95ee730f961b19fd09a7fb4a2b 100644 (file)
@@ -10,6 +10,7 @@ import (
        "code.gitea.io/gitea/modules/base"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/log"
+       issue_service "code.gitea.io/gitea/services/issue"
 )
 
 const (
@@ -132,7 +133,7 @@ func UpdateIssueLabel(ctx *context.Context) {
        switch action := ctx.Query("action"); action {
        case "clear":
                for _, issue := range issues {
-                       if err := issue.ClearLabels(ctx.User); err != nil {
+                       if err := issue_service.ClearLabels(issue, ctx.User); err != nil {
                                ctx.ServerError("ClearLabels", err)
                                return
                        }
diff --git a/services/issue/label.go b/services/issue/label.go
new file mode 100644 (file)
index 0000000..4af8c2b
--- /dev/null
@@ -0,0 +1,21 @@
+// 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 issue
+
+import (
+       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/modules/notification"
+)
+
+// ClearLabels clears all of an issue's labels
+func ClearLabels(issue *models.Issue, doer *models.User) (err error) {
+       if err = issue.ClearLabels(doer); err != nil {
+               return
+       }
+
+       notification.NotifyIssueClearLabels(doer, issue)
+
+       return nil
+}