Skip to content

Commit 3a43abd

Browse files
committed
initial creation of gitbucket-announce-plugin
0 parents  commit 3a43abd

File tree

11 files changed

+215
-0
lines changed

11 files changed

+215
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
target/

README.MD

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# gitbucket-announce-plugin
2+
3+
This plugin enhances [takezoe/gitbucket](https://github.com/takezoe/gitbucket) by providing an announce mechanism.
4+
5+
## Features
6+
7+
### Global Announce
8+
9+
This feature allows to notify gitbucket users. It is available to `admin` via the `System Administration` menu of gitbucket.
10+
11+
The global announce, sends an email to all active users.
12+
13+
It is then possible, for example, to notify every user that an upgarde of the system is planned the next monday at midday.
14+
15+
## Usage
16+
17+
- Open a shell window at the root of the project, hit `sbt package` in the root directory of this repository
18+
- remove any copy of gitbucket-announce-plugin from GITBUCKET_HOME/plugins
19+
- Copy target/scala-2.11/gitbucket-announce-plugin_2.11-1.0.jar into GITBUCKET_HOME/plugins
20+
- Restart GitBucket
21+
22+
## Release Notes
23+
24+
### 1.0
25+
26+
- introduce gitbucket-announce-plugin
27+
- global announce by email in `System Administration` menu
28+
- depends on takezoe/gitbucket#861

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version = 0.13.5

project/build.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sbt._
2+
import Keys._
3+
import play.twirl.sbt.SbtTwirl
4+
import play.twirl.sbt.Import.TwirlKeys._
5+
6+
object MyBuild extends Build {
7+
8+
val Organization = "fr.brouillard.gitbucket"
9+
val Name = "gitbucket-announce-plugin"
10+
val Version = "1.0"
11+
val ScalaVersion = "2.11.6"
12+
13+
lazy val project = Project (
14+
"gitbucket-announce-plugin",
15+
file(".")
16+
)
17+
.settings(
18+
sourcesInBase := false,
19+
organization := Organization,
20+
name := Name,
21+
version := Version,
22+
scalaVersion := ScalaVersion,
23+
scalacOptions := Seq("-deprecation", "-language:postfixOps"),
24+
resolvers ++= Seq(
25+
"amateras-repo" at "http://amateras.sourceforge.jp/mvn/"
26+
),
27+
libraryDependencies ++= Seq(
28+
"gitbucket" % "gitbucket-assembly" % "3.5.0" % "provided",
29+
"com.typesafe.play" %% "twirl-compiler" % "1.0.4" % "provided",
30+
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided"
31+
),
32+
javacOptions in compile ++= Seq("-target", "7", "-source", "7")
33+
).enablePlugins(SbtTwirl)
34+
}

project/plugins.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
logLevel := Level.Warn
2+
3+
addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.0.2")

sbt-launch-0.13.5.jar

1.13 MB
Binary file not shown.

sbt.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
set SCRIPT_DIR=%~dp0
2+
java -Dsbt.log.noformat=true -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -Xmx512M -Xss2M -jar "%SCRIPT_DIR%\sbt-launch-0.13.5.jar" %*

sbt.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
java -Dsbt.log.noformat=true -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -Xmx512M -Xss2M -jar `dirname $0`/sbt-launch-0.13.5.jar "$@"

src/main/scala/Plugin.scala

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import javax.servlet.ServletContext
2+
3+
import fr.brouillard.gitbucket.announce.controller.AnnounceController
4+
import gitbucket.core.plugin.PluginRegistry
5+
import gitbucket.core.service.SystemSettingsService.SystemSettings
6+
import gitbucket.core.util.Version
7+
8+
class Plugin extends gitbucket.core.plugin.Plugin {
9+
override val pluginId: String = "announce"
10+
11+
override val pluginName: String = "Announce Plugin"
12+
13+
override val description: String = "Allows to handle announces for gitbucket"
14+
15+
override val versions: List[Version] = List(
16+
Version(1, 0)
17+
)
18+
19+
override def javaScripts(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(String, String)] = {
20+
// Add Snippet link to the header
21+
val path = settings.baseUrl.getOrElse(context.getContextPath)
22+
Seq(
23+
".*/admin/announce" -> s"""
24+
|$$('#system-admin-menu-container>li:last').after(
25+
| $$('<li class="active"><a href="${path}/admin/announce">Global Announce</a></li>')
26+
|);
27+
""".stripMargin,
28+
".*/admin/(?!announce).*" -> s"""
29+
|$$('#system-admin-menu-container>li:last').after(
30+
| $$('<li><a href="${path}/admin/announce">Global Announce</a></li>')
31+
|);
32+
""".stripMargin
33+
)
34+
}
35+
36+
override val controllers = Seq(
37+
"/admin/announce" -> new AnnounceController()
38+
)
39+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package fr.brouillard.gitbucket.announce.controller
2+
3+
import fr.brouillard.gitbucket.announce.html
4+
import gitbucket.core.controller.ControllerBase
5+
import gitbucket.core.service.{AccountService, SystemSettingsService}
6+
import gitbucket.core.servlet.Database
7+
import gitbucket.core.util.AdminAuthenticator
8+
import jp.sf.amateras.scalatra.forms._
9+
import org.apache.commons.mail.{DefaultAuthenticator, HtmlEmail}
10+
import org.pegdown.PegDownProcessor
11+
import org.slf4j.LoggerFactory
12+
13+
class AnnounceController extends AnnounceControllerBase
14+
with AdminAuthenticator
15+
16+
trait AnnounceControllerBase extends ControllerBase with AccountService {
17+
self: AdminAuthenticator =>
18+
19+
private val logger = LoggerFactory.getLogger(classOf[AnnounceController])
20+
21+
case class AnnounceForm(content: String, subject: String)
22+
23+
private val announceForm = mapping(
24+
"content" -> trim(label("Announce", text(required))),
25+
"subject" -> trim(label("Subject", text(required)))
26+
)(AnnounceForm.apply)
27+
28+
get("/admin/announce")(adminOnly {
29+
html.announce(flash.get("info"))
30+
})
31+
32+
post("/admin/announce", announceForm)(adminOnly { form =>
33+
flash += "info" -> "Announce has been sent."
34+
35+
if (logger.isDebugEnabled) {
36+
logger.debug("sending announce: {}", form.content)
37+
}
38+
39+
val systemSettings = new SystemSettingsService {}.loadSystemSettings
40+
if (systemSettings.notification && systemSettings.smtp.nonEmpty) {
41+
val email = new HtmlEmail
42+
val smtp = systemSettings.smtp.get
43+
44+
email.setHostName(smtp.host)
45+
email.setSmtpPort(smtp.port.get)
46+
smtp.user.foreach { user =>
47+
email.setAuthenticator(new DefaultAuthenticator(user, smtp.password.getOrElse("")))
48+
}
49+
smtp.ssl.foreach { ssl =>
50+
email.setSSLOnConnect(ssl)
51+
}
52+
smtp.fromAddress
53+
.map (_ -> smtp.fromName.orNull)
54+
.orElse (Some("[email protected]" -> context.loginAccount.get.userName))
55+
.foreach { case (address, name) =>
56+
email.setFrom(address, name)
57+
}
58+
email.setCharset("UTF-8")
59+
email.setSubject(form.subject)
60+
61+
email.setHtmlMsg(new PegDownProcessor().markdownToHtml(form.content))
62+
63+
logger.info("sending email: {}", form.content)
64+
val database = Database()
65+
database withSession { implicit session =>
66+
getAllUsers(false).filter(account => !account.isGroupAccount && account.mailAddress.nonEmpty).foreach(account => email.addBcc(account.mailAddress))
67+
}
68+
69+
email.send()
70+
}
71+
72+
redirect("/admin/announce")
73+
})
74+
}

0 commit comments

Comments
 (0)