]> source.dussan.org Git - gitea.git/commitdiff
Extract actions on new issue from models to services (#8217)
authorLunny Xiao <xiaolunwen@gmail.com>
Mon, 30 Sep 2019 13:50:44 +0000 (21:50 +0800)
committerGitHub <noreply@github.com>
Mon, 30 Sep 2019 13:50:44 +0000 (21:50 +0800)
* extract actions on new issue from models to services

* improve code

* rename services/issues to services/issue

models/issue.go
routers/api/v1/repo/issue.go
routers/repo/issue.go
services/issue/issue.go [new file with mode: 0644]

index 77712c0fec72ea8a62c38bfbdd41f1d14b7ee23a..da98fac7df3671990e612b10c09f5b61b136a3ab 100644 (file)
@@ -1225,31 +1225,6 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, assigneeIDs []in
        }
        sess.Close()
 
-       if err = NotifyWatchers(&Action{
-               ActUserID: issue.Poster.ID,
-               ActUser:   issue.Poster,
-               OpType:    ActionCreateIssue,
-               Content:   fmt.Sprintf("%d|%s", issue.Index, issue.Title),
-               RepoID:    repo.ID,
-               Repo:      repo,
-               IsPrivate: repo.IsPrivate,
-       }); err != nil {
-               log.Error("NotifyWatchers: %v", err)
-       }
-
-       mode, _ := AccessLevel(issue.Poster, issue.Repo)
-       if err = PrepareWebhooks(repo, HookEventIssues, &api.IssuePayload{
-               Action:     api.HookIssueOpened,
-               Index:      issue.Index,
-               Issue:      issue.APIFormat(),
-               Repository: repo.APIFormat(mode),
-               Sender:     issue.Poster.APIFormat(),
-       }); err != nil {
-               log.Error("PrepareWebhooks: %v", err)
-       } else {
-               go HookQueue.Add(issue.RepoID)
-       }
-
        return nil
 }
 
index 6f502a5abd30336ba5e439a262b5d247a5661d51..aab167bc6816c3f2ef789bf1dfa7f05e2606e6f9 100644 (file)
@@ -19,6 +19,7 @@ import (
        api "code.gitea.io/gitea/modules/structs"
        "code.gitea.io/gitea/modules/timeutil"
        "code.gitea.io/gitea/modules/util"
+       issue_service "code.gitea.io/gitea/services/issue"
        milestone_service "code.gitea.io/gitea/services/milestone"
 )
 
@@ -217,7 +218,7 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
                form.Labels = make([]int64, 0)
        }
 
-       if err := models.NewIssue(ctx.Repo.Repository, issue, form.Labels, assigneeIDs, nil); err != nil {
+       if err := issue_service.NewIssue(ctx.Repo.Repository, issue, form.Labels, assigneeIDs, nil); err != nil {
                if models.IsErrUserDoesNotHaveAccessToRepo(err) {
                        ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
                        return
index 1be0b408a6a003fc9bafaab9d5e623342fcc7881..e4ef3b1dbb03391d06d4374b1b001feaffa65809 100644 (file)
@@ -27,6 +27,7 @@ import (
        api "code.gitea.io/gitea/modules/structs"
        "code.gitea.io/gitea/modules/util"
        comment_service "code.gitea.io/gitea/services/comments"
+       issue_service "code.gitea.io/gitea/services/issue"
        milestone_service "code.gitea.io/gitea/services/milestone"
 
        "github.com/unknwon/com"
@@ -571,7 +572,7 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
                Content:     form.Content,
                Ref:         form.Ref,
        }
-       if err := models.NewIssue(repo, issue, labelIDs, assigneeIDs, attachments); err != nil {
+       if err := issue_service.NewIssue(repo, issue, labelIDs, assigneeIDs, attachments); err != nil {
                if models.IsErrUserDoesNotHaveAccessToRepo(err) {
                        ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error())
                        return
diff --git a/services/issue/issue.go b/services/issue/issue.go
new file mode 100644 (file)
index 0000000..5afdfc9
--- /dev/null
@@ -0,0 +1,47 @@
+// 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 (
+       "fmt"
+
+       "code.gitea.io/gitea/models"
+       "code.gitea.io/gitea/modules/log"
+       api "code.gitea.io/gitea/modules/structs"
+)
+
+// NewIssue creates new issue with labels for repository.
+func NewIssue(repo *models.Repository, issue *models.Issue, labelIDs []int64, assigneeIDs []int64, uuids []string) error {
+       if err := models.NewIssue(repo, issue, labelIDs, assigneeIDs, uuids); err != nil {
+               return err
+       }
+
+       if err := models.NotifyWatchers(&models.Action{
+               ActUserID: issue.Poster.ID,
+               ActUser:   issue.Poster,
+               OpType:    models.ActionCreateIssue,
+               Content:   fmt.Sprintf("%d|%s", issue.Index, issue.Title),
+               RepoID:    repo.ID,
+               Repo:      repo,
+               IsPrivate: repo.IsPrivate,
+       }); err != nil {
+               log.Error("NotifyWatchers: %v", err)
+       }
+
+       mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
+       if err := models.PrepareWebhooks(repo, models.HookEventIssues, &api.IssuePayload{
+               Action:     api.HookIssueOpened,
+               Index:      issue.Index,
+               Issue:      issue.APIFormat(),
+               Repository: repo.APIFormat(mode),
+               Sender:     issue.Poster.APIFormat(),
+       }); err != nil {
+               log.Error("PrepareWebhooks: %v", err)
+       } else {
+               go models.HookQueue.Add(issue.RepoID)
+       }
+
+       return nil
+}