Skip to content

Commit e611300

Browse files
authored
Extract actions on new issue from models to services (#8217)
* extract actions on new issue from models to services * improve code * rename services/issues to services/issue
1 parent a5992d1 commit e611300

File tree

4 files changed

+51
-27
lines changed

4 files changed

+51
-27
lines changed

models/issue.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,31 +1225,6 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, assigneeIDs []in
12251225
}
12261226
sess.Close()
12271227

1228-
if err = NotifyWatchers(&Action{
1229-
ActUserID: issue.Poster.ID,
1230-
ActUser: issue.Poster,
1231-
OpType: ActionCreateIssue,
1232-
Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
1233-
RepoID: repo.ID,
1234-
Repo: repo,
1235-
IsPrivate: repo.IsPrivate,
1236-
}); err != nil {
1237-
log.Error("NotifyWatchers: %v", err)
1238-
}
1239-
1240-
mode, _ := AccessLevel(issue.Poster, issue.Repo)
1241-
if err = PrepareWebhooks(repo, HookEventIssues, &api.IssuePayload{
1242-
Action: api.HookIssueOpened,
1243-
Index: issue.Index,
1244-
Issue: issue.APIFormat(),
1245-
Repository: repo.APIFormat(mode),
1246-
Sender: issue.Poster.APIFormat(),
1247-
}); err != nil {
1248-
log.Error("PrepareWebhooks: %v", err)
1249-
} else {
1250-
go HookQueue.Add(issue.RepoID)
1251-
}
1252-
12531228
return nil
12541229
}
12551230

routers/api/v1/repo/issue.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
api "code.gitea.io/gitea/modules/structs"
2020
"code.gitea.io/gitea/modules/timeutil"
2121
"code.gitea.io/gitea/modules/util"
22+
issue_service "code.gitea.io/gitea/services/issue"
2223
milestone_service "code.gitea.io/gitea/services/milestone"
2324
)
2425

@@ -217,7 +218,7 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
217218
form.Labels = make([]int64, 0)
218219
}
219220

220-
if err := models.NewIssue(ctx.Repo.Repository, issue, form.Labels, assigneeIDs, nil); err != nil {
221+
if err := issue_service.NewIssue(ctx.Repo.Repository, issue, form.Labels, assigneeIDs, nil); err != nil {
221222
if models.IsErrUserDoesNotHaveAccessToRepo(err) {
222223
ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
223224
return

routers/repo/issue.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
api "code.gitea.io/gitea/modules/structs"
2828
"code.gitea.io/gitea/modules/util"
2929
comment_service "code.gitea.io/gitea/services/comments"
30+
issue_service "code.gitea.io/gitea/services/issue"
3031
milestone_service "code.gitea.io/gitea/services/milestone"
3132

3233
"github.com/unknwon/com"
@@ -571,7 +572,7 @@ func NewIssuePost(ctx *context.Context, form auth.CreateIssueForm) {
571572
Content: form.Content,
572573
Ref: form.Ref,
573574
}
574-
if err := models.NewIssue(repo, issue, labelIDs, assigneeIDs, attachments); err != nil {
575+
if err := issue_service.NewIssue(repo, issue, labelIDs, assigneeIDs, attachments); err != nil {
575576
if models.IsErrUserDoesNotHaveAccessToRepo(err) {
576577
ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error())
577578
return

services/issue/issue.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package issue
6+
7+
import (
8+
"fmt"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/log"
12+
api "code.gitea.io/gitea/modules/structs"
13+
)
14+
15+
// NewIssue creates new issue with labels for repository.
16+
func NewIssue(repo *models.Repository, issue *models.Issue, labelIDs []int64, assigneeIDs []int64, uuids []string) error {
17+
if err := models.NewIssue(repo, issue, labelIDs, assigneeIDs, uuids); err != nil {
18+
return err
19+
}
20+
21+
if err := models.NotifyWatchers(&models.Action{
22+
ActUserID: issue.Poster.ID,
23+
ActUser: issue.Poster,
24+
OpType: models.ActionCreateIssue,
25+
Content: fmt.Sprintf("%d|%s", issue.Index, issue.Title),
26+
RepoID: repo.ID,
27+
Repo: repo,
28+
IsPrivate: repo.IsPrivate,
29+
}); err != nil {
30+
log.Error("NotifyWatchers: %v", err)
31+
}
32+
33+
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
34+
if err := models.PrepareWebhooks(repo, models.HookEventIssues, &api.IssuePayload{
35+
Action: api.HookIssueOpened,
36+
Index: issue.Index,
37+
Issue: issue.APIFormat(),
38+
Repository: repo.APIFormat(mode),
39+
Sender: issue.Poster.APIFormat(),
40+
}); err != nil {
41+
log.Error("PrepareWebhooks: %v", err)
42+
} else {
43+
go models.HookQueue.Add(issue.RepoID)
44+
}
45+
46+
return nil
47+
}

0 commit comments

Comments
 (0)