Skip to content

Commit 20477a6

Browse files
lunnyzeripath
andauthored
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>
1 parent 34fb9d6 commit 20477a6

File tree

6 files changed

+94
-36
lines changed

6 files changed

+94
-36
lines changed

models/issue.go

-34
Original file line numberDiff line numberDiff line change
@@ -596,40 +596,6 @@ func (issue *Issue) ClearLabels(doer *User) (err error) {
596596
if err = sess.Commit(); err != nil {
597597
return fmt.Errorf("Commit: %v", err)
598598
}
599-
sess.Close()
600-
601-
if err = issue.LoadPoster(); err != nil {
602-
return fmt.Errorf("loadPoster: %v", err)
603-
}
604-
605-
mode, _ := AccessLevel(issue.Poster, issue.Repo)
606-
if issue.IsPull {
607-
err = issue.PullRequest.LoadIssue()
608-
if err != nil {
609-
log.Error("LoadIssue: %v", err)
610-
return
611-
}
612-
err = PrepareWebhooks(issue.Repo, HookEventPullRequest, &api.PullRequestPayload{
613-
Action: api.HookIssueLabelCleared,
614-
Index: issue.Index,
615-
PullRequest: issue.PullRequest.APIFormat(),
616-
Repository: issue.Repo.APIFormat(mode),
617-
Sender: doer.APIFormat(),
618-
})
619-
} else {
620-
err = PrepareWebhooks(issue.Repo, HookEventIssues, &api.IssuePayload{
621-
Action: api.HookIssueLabelCleared,
622-
Index: issue.Index,
623-
Issue: issue.APIFormat(),
624-
Repository: issue.Repo.APIFormat(mode),
625-
Sender: doer.APIFormat(),
626-
})
627-
}
628-
if err != nil {
629-
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
630-
} else {
631-
go HookQueue.Add(issue.RepoID)
632-
}
633599

634600
return nil
635601
}

modules/notification/notification.go

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/modules/notification/indexer"
1212
"code.gitea.io/gitea/modules/notification/mail"
1313
"code.gitea.io/gitea/modules/notification/ui"
14+
"code.gitea.io/gitea/modules/notification/webhook"
1415
)
1516

1617
var (
@@ -27,6 +28,7 @@ func init() {
2728
RegisterNotifier(ui.NewNotifier())
2829
RegisterNotifier(mail.NewNotifier())
2930
RegisterNotifier(indexer.NewNotifier())
31+
RegisterNotifier(webhook.NewNotifier())
3032
}
3133

3234
// NotifyCreateIssueComment notifies issue comment related message to notifiers
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 webhook
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/log"
10+
"code.gitea.io/gitea/modules/notification/base"
11+
api "code.gitea.io/gitea/modules/structs"
12+
)
13+
14+
type webhookNotifier struct {
15+
base.NullNotifier
16+
}
17+
18+
var (
19+
_ base.Notifier = &webhookNotifier{}
20+
)
21+
22+
// NewNotifier create a new webhookNotifier notifier
23+
func NewNotifier() base.Notifier {
24+
return &webhookNotifier{}
25+
}
26+
27+
func (m *webhookNotifier) NotifyIssueClearLabels(doer *models.User, issue *models.Issue) {
28+
if err := issue.LoadPoster(); err != nil {
29+
log.Error("loadPoster: %v", err)
30+
return
31+
}
32+
33+
if err := issue.LoadRepo(); err != nil {
34+
log.Error("LoadRepo: %v", err)
35+
return
36+
}
37+
38+
mode, _ := models.AccessLevel(issue.Poster, issue.Repo)
39+
var err error
40+
if issue.IsPull {
41+
if err = issue.LoadPullRequest(); err != nil {
42+
log.Error("LoadPullRequest: %v", err)
43+
return
44+
}
45+
46+
err = models.PrepareWebhooks(issue.Repo, models.HookEventPullRequest, &api.PullRequestPayload{
47+
Action: api.HookIssueLabelCleared,
48+
Index: issue.Index,
49+
PullRequest: issue.PullRequest.APIFormat(),
50+
Repository: issue.Repo.APIFormat(mode),
51+
Sender: doer.APIFormat(),
52+
})
53+
} else {
54+
err = models.PrepareWebhooks(issue.Repo, models.HookEventIssues, &api.IssuePayload{
55+
Action: api.HookIssueLabelCleared,
56+
Index: issue.Index,
57+
Issue: issue.APIFormat(),
58+
Repository: issue.Repo.APIFormat(mode),
59+
Sender: doer.APIFormat(),
60+
})
61+
}
62+
if err != nil {
63+
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
64+
} else {
65+
go models.HookQueue.Add(issue.RepoID)
66+
}
67+
}

routers/api/v1/repo/issue_label.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"code.gitea.io/gitea/models"
1010
"code.gitea.io/gitea/modules/context"
1111
api "code.gitea.io/gitea/modules/structs"
12+
issue_service "code.gitea.io/gitea/services/issue"
1213
)
1314

1415
// ListIssueLabels list all the labels of an issue
@@ -314,7 +315,7 @@ func ClearIssueLabels(ctx *context.APIContext) {
314315
return
315316
}
316317

317-
if err := issue.ClearLabels(ctx.User); err != nil {
318+
if err := issue_service.ClearLabels(issue, ctx.User); err != nil {
318319
ctx.Error(500, "ClearLabels", err)
319320
return
320321
}

routers/repo/issue_label.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"code.gitea.io/gitea/modules/base"
1111
"code.gitea.io/gitea/modules/context"
1212
"code.gitea.io/gitea/modules/log"
13+
issue_service "code.gitea.io/gitea/services/issue"
1314
)
1415

1516
const (
@@ -132,7 +133,7 @@ func UpdateIssueLabel(ctx *context.Context) {
132133
switch action := ctx.Query("action"); action {
133134
case "clear":
134135
for _, issue := range issues {
135-
if err := issue.ClearLabels(ctx.User); err != nil {
136+
if err := issue_service.ClearLabels(issue, ctx.User); err != nil {
136137
ctx.ServerError("ClearLabels", err)
137138
return
138139
}

services/issue/label.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/notification"
10+
)
11+
12+
// ClearLabels clears all of an issue's labels
13+
func ClearLabels(issue *models.Issue, doer *models.User) (err error) {
14+
if err = issue.ClearLabels(doer); err != nil {
15+
return
16+
}
17+
18+
notification.NotifyIssueClearLabels(doer, issue)
19+
20+
return nil
21+
}

0 commit comments

Comments
 (0)