Skip to content

Commit ea619b3

Browse files
authored
Add notification interface and refactor UI notifications (#5085)
* add notification interface and refactor UI notifications * add missing methods on notification interface and notifiy only issue status really changed * implement NotifyPullRequestReview for ui notification
1 parent dd62ca7 commit ea619b3

File tree

11 files changed

+376
-42
lines changed

11 files changed

+376
-42
lines changed

models/issue.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ func (issue *Issue) GetPullRequest() (pr *PullRequest, err error) {
112112
}
113113

114114
pr, err = getPullRequestByIssueID(x, issue.ID)
115+
if err != nil {
116+
return nil, err
117+
}
118+
pr.Issue = issue
115119
return
116120
}
117121

models/review.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ func getCurrentReview(e Engine, reviewer *User, issue *Issue) (*Review, error) {
239239
if len(reviews) == 0 {
240240
return nil, ErrReviewNotExist{}
241241
}
242+
reviews[0].Reviewer = reviewer
243+
reviews[0].Issue = issue
242244
return reviews[0], nil
243245
}
244246

modules/notification/base/base.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2018 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 base
6+
7+
import (
8+
"code.gitea.io/git"
9+
"code.gitea.io/gitea/models"
10+
)
11+
12+
// Notifier defines an interface to notify receiver
13+
type Notifier interface {
14+
Run()
15+
16+
NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository)
17+
NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository)
18+
NotifyDeleteRepository(doer *models.User, repo *models.Repository)
19+
NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository)
20+
21+
NotifyNewIssue(*models.Issue)
22+
NotifyIssueChangeStatus(*models.User, *models.Issue, bool)
23+
NotifyIssueChangeMilestone(doer *models.User, issue *models.Issue)
24+
NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, removed bool)
25+
NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string)
26+
NotifyIssueClearLabels(doer *models.User, issue *models.Issue)
27+
NotifyIssueChangeTitle(doer *models.User, issue *models.Issue, oldTitle string)
28+
NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
29+
addedLabels []*models.Label, removedLabels []*models.Label)
30+
31+
NotifyNewPullRequest(*models.PullRequest)
32+
NotifyMergePullRequest(*models.PullRequest, *models.User, *git.Repository)
33+
NotifyPullRequestReview(*models.PullRequest, *models.Review, *models.Comment)
34+
35+
NotifyCreateIssueComment(*models.User, *models.Repository,
36+
*models.Issue, *models.Comment)
37+
NotifyUpdateComment(*models.User, *models.Comment, string)
38+
NotifyDeleteComment(*models.User, *models.Comment)
39+
40+
NotifyNewRelease(rel *models.Release)
41+
NotifyUpdateRelease(doer *models.User, rel *models.Release)
42+
NotifyDeleteRelease(doer *models.User, rel *models.Release)
43+
}

modules/notification/notification.go

Lines changed: 153 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,175 @@
1-
// Copyright 2016 The Gitea Authors. All rights reserved.
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
22
// Use of this source code is governed by a MIT-style
33
// license that can be found in the LICENSE file.
44

55
package notification
66

77
import (
8+
"code.gitea.io/git"
89
"code.gitea.io/gitea/models"
9-
"code.gitea.io/gitea/modules/log"
10+
"code.gitea.io/gitea/modules/notification/base"
11+
"code.gitea.io/gitea/modules/notification/ui"
1012
)
1113

12-
type (
13-
notificationService struct {
14-
issueQueue chan issueNotificationOpts
14+
var (
15+
notifiers []base.Notifier
16+
)
17+
18+
// RegisterNotifier providers method to receive notify messages
19+
func RegisterNotifier(notifier base.Notifier) {
20+
go notifier.Run()
21+
notifiers = append(notifiers, notifier)
22+
}
23+
24+
func init() {
25+
RegisterNotifier(ui.NewNotifier())
26+
}
27+
28+
// NotifyCreateIssueComment notifies issue comment related message to notifiers
29+
func NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
30+
issue *models.Issue, comment *models.Comment) {
31+
for _, notifier := range notifiers {
32+
notifier.NotifyCreateIssueComment(doer, repo, issue, comment)
1533
}
34+
}
1635

17-
issueNotificationOpts struct {
18-
issue *models.Issue
19-
notificationAuthorID int64
36+
// NotifyNewIssue notifies new issue to notifiers
37+
func NotifyNewIssue(issue *models.Issue) {
38+
for _, notifier := range notifiers {
39+
notifier.NotifyNewIssue(issue)
2040
}
21-
)
41+
}
2242

23-
var (
24-
// Service is the notification service
25-
Service = &notificationService{
26-
issueQueue: make(chan issueNotificationOpts, 100),
43+
// NotifyIssueChangeStatus notifies close or reopen issue to notifiers
44+
func NotifyIssueChangeStatus(doer *models.User, issue *models.Issue, closeOrReopen bool) {
45+
for _, notifier := range notifiers {
46+
notifier.NotifyIssueChangeStatus(doer, issue, closeOrReopen)
2747
}
28-
)
48+
}
2949

30-
func init() {
31-
go Service.Run()
50+
// NotifyMergePullRequest notifies merge pull request to notifiers
51+
func NotifyMergePullRequest(pr *models.PullRequest, doer *models.User, baseGitRepo *git.Repository) {
52+
for _, notifier := range notifiers {
53+
notifier.NotifyMergePullRequest(pr, doer, baseGitRepo)
54+
}
55+
}
56+
57+
// NotifyNewPullRequest notifies new pull request to notifiers
58+
func NotifyNewPullRequest(pr *models.PullRequest) {
59+
for _, notifier := range notifiers {
60+
notifier.NotifyNewPullRequest(pr)
61+
}
62+
}
63+
64+
// NotifyPullRequestReview notifies new pull request review
65+
func NotifyPullRequestReview(pr *models.PullRequest, review *models.Review, comment *models.Comment) {
66+
for _, notifier := range notifiers {
67+
notifier.NotifyPullRequestReview(pr, review, comment)
68+
}
69+
}
70+
71+
// NotifyUpdateComment notifies update comment to notifiers
72+
func NotifyUpdateComment(doer *models.User, c *models.Comment, oldContent string) {
73+
for _, notifier := range notifiers {
74+
notifier.NotifyUpdateComment(doer, c, oldContent)
75+
}
76+
}
77+
78+
// NotifyDeleteComment notifies delete comment to notifiers
79+
func NotifyDeleteComment(doer *models.User, c *models.Comment) {
80+
for _, notifier := range notifiers {
81+
notifier.NotifyDeleteComment(doer, c)
82+
}
83+
}
84+
85+
// NotifyDeleteRepository notifies delete repository to notifiers
86+
func NotifyDeleteRepository(doer *models.User, repo *models.Repository) {
87+
for _, notifier := range notifiers {
88+
notifier.NotifyDeleteRepository(doer, repo)
89+
}
90+
}
91+
92+
// NotifyForkRepository notifies fork repository to notifiers
93+
func NotifyForkRepository(doer *models.User, oldRepo, repo *models.Repository) {
94+
for _, notifier := range notifiers {
95+
notifier.NotifyForkRepository(doer, oldRepo, repo)
96+
}
97+
}
98+
99+
// NotifyNewRelease notifies new release to notifiers
100+
func NotifyNewRelease(rel *models.Release) {
101+
for _, notifier := range notifiers {
102+
notifier.NotifyNewRelease(rel)
103+
}
104+
}
105+
106+
// NotifyUpdateRelease notifies update release to notifiers
107+
func NotifyUpdateRelease(doer *models.User, rel *models.Release) {
108+
for _, notifier := range notifiers {
109+
notifier.NotifyUpdateRelease(doer, rel)
110+
}
111+
}
112+
113+
// NotifyDeleteRelease notifies delete release to notifiers
114+
func NotifyDeleteRelease(doer *models.User, rel *models.Release) {
115+
for _, notifier := range notifiers {
116+
notifier.NotifyDeleteRelease(doer, rel)
117+
}
118+
}
119+
120+
// NotifyIssueChangeMilestone notifies change milestone to notifiers
121+
func NotifyIssueChangeMilestone(doer *models.User, issue *models.Issue) {
122+
for _, notifier := range notifiers {
123+
notifier.NotifyIssueChangeMilestone(doer, issue)
124+
}
125+
}
126+
127+
// NotifyIssueChangeContent notifies change content to notifiers
128+
func NotifyIssueChangeContent(doer *models.User, issue *models.Issue, oldContent string) {
129+
for _, notifier := range notifiers {
130+
notifier.NotifyIssueChangeContent(doer, issue, oldContent)
131+
}
132+
}
133+
134+
// NotifyIssueChangeAssignee notifies change content to notifiers
135+
func NotifyIssueChangeAssignee(doer *models.User, issue *models.Issue, removed bool) {
136+
for _, notifier := range notifiers {
137+
notifier.NotifyIssueChangeAssignee(doer, issue, removed)
138+
}
139+
}
140+
141+
// NotifyIssueClearLabels notifies clear labels to notifiers
142+
func NotifyIssueClearLabels(doer *models.User, issue *models.Issue) {
143+
for _, notifier := range notifiers {
144+
notifier.NotifyIssueClearLabels(doer, issue)
145+
}
146+
}
147+
148+
// NotifyIssueChangeTitle notifies change title to notifiers
149+
func NotifyIssueChangeTitle(doer *models.User, issue *models.Issue, oldTitle string) {
150+
for _, notifier := range notifiers {
151+
notifier.NotifyIssueChangeTitle(doer, issue, oldTitle)
152+
}
153+
}
154+
155+
// NotifyIssueChangeLabels notifies change labels to notifiers
156+
func NotifyIssueChangeLabels(doer *models.User, issue *models.Issue,
157+
addedLabels []*models.Label, removedLabels []*models.Label) {
158+
for _, notifier := range notifiers {
159+
notifier.NotifyIssueChangeLabels(doer, issue, addedLabels, removedLabels)
160+
}
32161
}
33162

34-
func (ns *notificationService) Run() {
35-
for {
36-
select {
37-
case opts := <-ns.issueQueue:
38-
if err := models.CreateOrUpdateIssueNotifications(opts.issue, opts.notificationAuthorID); err != nil {
39-
log.Error(4, "Was unable to create issue notification: %v", err)
40-
}
41-
}
163+
// NotifyCreateRepository notifies create repository to notifiers
164+
func NotifyCreateRepository(doer *models.User, u *models.User, repo *models.Repository) {
165+
for _, notifier := range notifiers {
166+
notifier.NotifyCreateRepository(doer, u, repo)
42167
}
43168
}
44169

45-
func (ns *notificationService) NotifyIssue(issue *models.Issue, notificationAuthorID int64) {
46-
ns.issueQueue <- issueNotificationOpts{
47-
issue,
48-
notificationAuthorID,
170+
// NotifyMigrateRepository notifies create repository to notifiers
171+
func NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
172+
for _, notifier := range notifiers {
173+
notifier.NotifyMigrateRepository(doer, u, repo)
49174
}
50175
}

0 commit comments

Comments
 (0)