Skip to content

Commit 730a7e0

Browse files
authored
Merge pull request #9 from kounoike/pr-new-hooks
New hooks
2 parents 2c8dbd7 + 6de44b1 commit 730a7e0

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/main/scala/gitbucket/notifications/service/NotificationsHook.scala

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import gitbucket.core.controller.Context
44
import gitbucket.core.model.{Account, Issue}
55
import gitbucket.core.service._
66
import RepositoryService.RepositoryInfo
7+
import gitbucket.core
8+
import gitbucket.core.model
79
import gitbucket.core.util.{LDAPUtil, Mailer}
810
import gitbucket.core.view.Markdown
911
import gitbucket.notifications.model.Profile._
@@ -12,7 +14,7 @@ import profile.blockingApi._
1214

1315
import scala.concurrent.Future
1416
import scala.concurrent.ExecutionContext.Implicits.global
15-
import scala.util.{Success, Failure}
17+
import scala.util.{Failure, Success}
1618

1719

1820
class AccountHook extends gitbucket.core.plugin.AccountHook {
@@ -59,7 +61,8 @@ class IssueHook extends gitbucket.core.plugin.IssueHook
5961
with IssuesService
6062
with LabelsService
6163
with PrioritiesService
62-
with MilestonesService {
64+
with MilestonesService
65+
with SystemSettingsService {
6366

6467
private val logger = LoggerFactory.getLogger(classOf[IssueHook])
6568

@@ -102,6 +105,24 @@ class IssueHook extends gitbucket.core.plugin.IssueHook
102105
sendAsync(issue, r, subject(issue, r), markdown)
103106
}
104107

108+
override def assigned(issue: Issue, r: RepositoryInfo, assigner: Option[String], assigned: Option[String], oldAssigned: Option[String])(implicit session: model.Profile.profile.api.Session, context: Context): Unit = {
109+
val assignerMessage = assigner.flatMap(getAccountByUserName(_)).map(a => s"${a.fullName}(@${a.userName})").getOrElse("unknown user")
110+
val assignedMessage = assigned.flatMap(getAccountByUserName(_)).map(a => s"${a.fullName}(@${a.userName})").getOrElse("not assigned")
111+
val oldAssignedMessage = oldAssigned.flatMap(getAccountByUserName(_, true)).map(a => s"${a.fullName}(@${a.userName})").getOrElse("not assigned")
112+
val markdown =
113+
s"""assigned from ${oldAssignedMessage} to ${assignedMessage} by ${assignerMessage}
114+
|""".stripMargin
115+
sendAsync(issue, r, subject(issue, r), markdown)
116+
}
117+
118+
override def closedByCommitComment(issue: Issue, r: RepositoryInfo, commitMessage: String, pusher: Account)(implicit session: core.model.Profile.profile.api.Session): Unit = {
119+
val settings = loadSystemSettings()
120+
val message = s"""|close #[${issue.issueId}](${s"${settings.baseUrl}/${r.owner}/${r.name}/issues/${issue.issueId}"})
121+
|
122+
|${commitMessage}""".stripMargin
123+
println(message)
124+
sendAsyncTextOnly(issue, r, subject(issue, r), message, pusher, settings)
125+
}
105126

106127
protected def subject(issue: Issue, r: RepositoryInfo): String = {
107128
s"[${r.owner}/${r.name}] ${issue.title} (#${issue.issueId})"
@@ -117,6 +138,21 @@ class IssueHook extends gitbucket.core.plugin.IssueHook
117138
enableLineBreaks = false
118139
)
119140

141+
protected def sendAsyncTextOnly(issue: Issue, repository: RepositoryInfo, subject: String, message: String, senderAccount: Account, settings: SystemSettingsService.SystemSettings)(implicit session:Session): Unit = {
142+
val recipients = getRecipients(issue, senderAccount)
143+
val mailer = new Mailer(settings)
144+
val f = Future {
145+
recipients.foreach { address =>
146+
mailer.send(address, subject, message, None, Some(senderAccount))
147+
}
148+
"Notifications Successful."
149+
}
150+
f.onComplete {
151+
case Success(s) => logger.debug(s)
152+
case Failure(t) => logger.error("Notifications Failed.", t)
153+
}
154+
}
155+
120156
protected def sendAsync(issue: Issue, repository: RepositoryInfo, subject: String, markdown: String)
121157
(implicit session: Session, context: Context): Unit = {
122158
val recipients = getRecipients(issue, context.loginAccount.get)

0 commit comments

Comments
 (0)