@@ -2,12 +2,18 @@ package gitbucket.notifications.service
2
2
3
3
import gitbucket .core .controller .Context
4
4
import gitbucket .core .model .{Account , Issue }
5
- import gitbucket .core .service ._ , RepositoryService .RepositoryInfo
6
- import gitbucket .core .util .{LDAPUtil , Notifier }
5
+ import gitbucket .core .service ._
6
+ import RepositoryService .RepositoryInfo
7
+ import gitbucket .core .util .{LDAPUtil , Mailer }
7
8
import gitbucket .core .view .Markdown
8
9
import gitbucket .notifications .model .Profile ._
10
+ import org .slf4j .LoggerFactory
9
11
import profile .blockingApi ._
10
12
13
+ import scala .concurrent .Future
14
+ import scala .concurrent .ExecutionContext .Implicits .global
15
+ import scala .util .{Success , Failure }
16
+
11
17
12
18
class AccountHook extends gitbucket.core.plugin.AccountHook {
13
19
@@ -52,106 +58,124 @@ class IssueHook extends gitbucket.core.plugin.IssueHook
52
58
with AccountService
53
59
with IssuesService {
54
60
55
- override def created (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
56
- Notifier ().toNotify(
57
- subject(issue, r),
58
- message(issue.content getOrElse " " , r)(content => s """
59
- | $content<br/>
60
- |--<br/>
61
- |<a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" }">View it on GitBucket</a>
62
- """ .stripMargin)
63
- )(recipients(issue))
61
+ private val logger = LoggerFactory .getLogger(classOf [IssueHook ])
62
+
63
+ override def created (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
64
+ val markdown =
65
+ s """ | ${issue.content getOrElse " " }
66
+ |
67
+ |----
68
+ |[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
69
+ | """ .stripMargin
70
+
71
+ sendAsync(issue, r, subject(issue, r), markdown)
64
72
}
65
73
66
- override def addedComment (commentId : Int , content : String , issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
67
- Notifier ().toNotify(
68
- subject(issue, r),
69
- message(content, r)(content => s """
70
- | $content<br/>
71
- |--<br/>
72
- |<a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}#comment- $commentId" }">View it on GitBucket</a>
73
- """ .stripMargin)
74
- )(recipients(issue))
74
+ override def addedComment (commentId : Int , content : String , issue : Issue , r : RepositoryInfo )
75
+ (implicit session : Session , context : Context ): Unit = {
76
+ val markdown =
77
+ s """ | ${content}
78
+ |
79
+ |----
80
+ |[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}#comment- $commentId" })
81
+ | """ .stripMargin
82
+
83
+ sendAsync(issue, r, subject(issue, r), markdown)
75
84
}
76
85
77
- override def closed (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
78
- Notifier ().toNotify(
79
- subject(issue, r),
80
- message(" close" , r)(content => s """
81
- | $content <a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" }"># ${issue.issueId}</a>
82
- """ .stripMargin)
83
- )(recipients(issue))
86
+ override def closed (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
87
+ val markdown =
88
+ s """ |close #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
89
+ | """ .stripMargin
90
+
91
+ sendAsync(issue, r, subject(issue, r), markdown)
84
92
}
85
93
86
- override def reopened (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
87
- Notifier ().toNotify(
88
- subject(issue, r),
89
- message(" reopen" , r)(content => s """
90
- | $content <a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" }"># ${issue.issueId}</a>
91
- """ .stripMargin)
92
- )(recipients(issue))
94
+ override def reopened (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
95
+ val markdown =
96
+ s """ |reopen #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/issues/ ${issue.issueId}" })
97
+ | """ .stripMargin
98
+
99
+ sendAsync(issue, r, subject(issue, r), markdown)
93
100
}
94
101
95
102
96
- protected def subject (issue : Issue , r : RepositoryInfo ): String = s " [ ${r.owner}/ ${r.name}] ${issue.title} (# ${issue.issueId}) "
103
+ protected def subject (issue : Issue , r : RepositoryInfo ): String = {
104
+ s " [ ${r.owner}/ ${r.name}] ${issue.title} (# ${issue.issueId}) "
105
+ }
97
106
98
- protected def message ( content : String , r : RepositoryInfo )( msg : String => String )(implicit context : Context ): String =
99
- msg( Markdown .toHtml(
100
- markdown = content ,
107
+ protected def toHtml ( markdown : String , r : RepositoryInfo )(implicit context : Context ): String =
108
+ Markdown .toHtml(
109
+ markdown = markdown ,
101
110
repository = r,
102
111
enableWikiLink = false ,
103
112
enableRefsLink = true ,
104
113
enableAnchor = false ,
105
114
enableLineBreaks = false
106
- ))
107
-
108
- protected val recipients : Issue => Account => Session => Seq [String ] = {
109
- issue => loginAccount => implicit session =>
110
- getNotificationUsers(issue)
111
- .withFilter ( _ != loginAccount.userName ) // the operation in person is excluded
112
- .flatMap (
113
- getAccountByUserName(_)
114
- .filterNot (_.isGroupAccount)
115
- .filterNot (LDAPUtil .isDummyMailAddress)
116
- .filterNot (isDisableEmailNotification)
117
- .map (_.mailAddress)
118
- )
115
+ )
116
+
117
+ protected def sendAsync (issue : Issue , repository : RepositoryInfo , subject : String , markdown : String )
118
+ (implicit session : Session , context : Context ): Unit = {
119
+ val recipients = getRecipients(issue, context.loginAccount.get)
120
+ val mailer = new Mailer (context.settings)
121
+ val f = Future {
122
+ recipients.foreach { address =>
123
+ mailer.send(address, subject, markdown, Some (toHtml(markdown, repository)), context.loginAccount)
124
+ }
125
+ " Notifications Successful."
126
+ }
127
+ f.onComplete {
128
+ case Success (s) => logger.debug(s)
129
+ case Failure (t) => logger.error(" Notifications Failed." , t)
130
+ }
131
+ }
132
+
133
+ protected def getRecipients (issue : Issue , loginAccount : Account )(implicit session : Session ): Seq [String ] = {
134
+ getNotificationUsers(issue)
135
+ .withFilter ( _ != loginAccount.userName ) // the operation in person is excluded
136
+ .flatMap (
137
+ getAccountByUserName(_)
138
+ .filterNot (_.isGroupAccount)
139
+ .filterNot (LDAPUtil .isDummyMailAddress)
140
+ .filterNot (isDisableEmailNotification)
141
+ .map (_.mailAddress)
142
+ )
119
143
}
120
144
121
145
}
122
146
123
147
class PullRequestHook extends IssueHook with gitbucket.core.plugin.PullRequestHook {
124
148
125
- override def created (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
126
- val url = s " ${context.baseUrl} / ${r.owner} / ${r.name} /pull/ ${issue.issueId} "
127
- Notifier ().toNotify(
128
- subject(issue, r),
129
- message(issue.content getOrElse " " , r)(content => s """
130
- | $content <hr/>
131
- |View, comment on, or merge it at:<br/>
132
- |<a href=" $url "> $url </a>
133
- """ .stripMargin)
134
- )(recipients (issue) )
149
+ override def created (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
150
+ val markdown =
151
+ s """ | ${issue.content getOrElse " " }
152
+ |
153
+ |----
154
+ |View, comment on, or merge it at:
155
+ | ${context.baseUrl} / ${r.owner} / ${r.name} /pull/ ${issue.issueId}
156
+ | """ .stripMargin
157
+
158
+ sendAsync(issue, r, subject (issue, r), markdown )
135
159
}
136
160
137
- override def addedComment (commentId : Int , content : String , issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
138
- Notifier ().toNotify(
139
- subject(issue, r),
140
- message(content, r)(content => s """
141
- | $content<br/>
142
- |--<br/>
143
- |<a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}#comment- $commentId" }">View it on GitBucket</a>
144
- """ .stripMargin)
145
- )(recipients(issue))
161
+ override def addedComment (commentId : Int , content : String , issue : Issue , r : RepositoryInfo )
162
+ (implicit session : Session , context : Context ): Unit = {
163
+ val markdown =
164
+ s """ | $content
165
+ |
166
+ |----
167
+ |[View it on GitBucket]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}#comment- $commentId" })
168
+ | """ .stripMargin
169
+
170
+ sendAsync(issue, r, subject(issue, r), markdown)
146
171
}
147
172
148
- override def merged (issue : Issue , r : RepositoryInfo )(implicit context : Context ): Unit = {
149
- Notifier ().toNotify(
150
- subject(issue, r),
151
- message(" merge" , r)(content => s """
152
- | $content <a href=" ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}" }"># ${issue.issueId}</a>
153
- """ .stripMargin)
154
- )(recipients(issue))
173
+ override def merged (issue : Issue , r : RepositoryInfo )(implicit session : Session , context : Context ): Unit = {
174
+ val markdown =
175
+ s """ |merge #[ ${issue.issueId}]( ${s " ${context.baseUrl}/ ${r.owner}/ ${r.name}/pull/ ${issue.issueId}" })
176
+ | """ .stripMargin
177
+
178
+ sendAsync(issue, r, subject(issue, r), markdown)
155
179
}
156
180
157
181
}
0 commit comments