From 198fa1ef2317f9c532cc9f40c8d54010195e8cbe Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Mon, 19 Dec 2011 08:51:30 -0500
Subject: [PATCH] Groovy script tweaks

---
 groovy/jenkins.groovy   |    4 +-
 groovy/blockpush.groovy |    4 +-
 groovy/sendemail.groovy |   64 ++++++++++++++++----------------
 3 files changed, 36 insertions(+), 36 deletions(-)

diff --git a/groovy/blockpush.groovy b/groovy/blockpush.groovy
index eac7d3f..a4cede3 100644
--- a/groovy/blockpush.groovy
+++ b/groovy/blockpush.groovy
@@ -67,10 +67,10 @@
  */
 def blocked = false
 switch (repository.name) {
-	case "ex@mple.git":
+	case 'ex@mple.git':
 		for (ReceiveCommand command : commands) {
 			def updatedRef = command.refName
-			if (updatedRef.equals("refs/heads/master")) {
+			if (updatedRef.equals('refs/heads/master')) {
 				// to reject a command set it's result to anything other than Result.NOT_ATTEMPTED
 				command.setResult(Result.REJECTED_OTHER_REASON, "You are not permitted to write to ${repository.name}:${updatedRef}")
 				blocked = true
diff --git a/groovy/jenkins.groovy b/groovy/jenkins.groovy
index df4e588..443e51d 100644
--- a/groovy/jenkins.groovy
+++ b/groovy/jenkins.groovy
@@ -62,10 +62,10 @@
 
 // define your jenkins url here or set groovy.jenkinsServer in 
 // gitblit.properties or web.xml
-def jenkinsUrl = gitblit.getString("groovy.jenkinsServer", "http://yourserver/jenkins")
+def jenkinsUrl = gitblit.getString('groovy.jenkinsServer', 'http://yourserver/jenkins')
 
 // define the trigger url
-def triggerUrl = jenkinsUrl + "/git/notifyCommit?url=" + url + "/git/" + repository.name
+def triggerUrl = jenkinsUrl + "/git/notifyCommit?url=$url/git/$repository.name"
 
 // trigger the build
 new URL(triggerUrl).getContent()
diff --git a/groovy/sendemail.groovy b/groovy/sendemail.groovy
index 471ede8..f90d7ff 100644
--- a/groovy/sendemail.groovy
+++ b/groovy/sendemail.groovy
@@ -71,16 +71,16 @@
 
 // reuse some existing repository config settings, if available
 Config config = r.getConfig()
-def mailinglist = config.getString("hooks", null, "mailinglist")
-def emailprefix = config.getString("hooks", null, "emailprefix")
+def mailinglist = config.getString('hooks', null, 'mailinglist')
+def emailprefix = config.getString('hooks', null, 'emailprefix')
 
 // set default values
 def toAddresses = []
 if (emailprefix == null)
-	emailprefix = "[Gitblit]"
+emailprefix = '"[Gitblit]'
 
 if (mailinglist != null) {
-	def addrs = mailinglist.split("(,|\\s)")
+	def addrs = mailinglist.split('(,|\\s)')
 	toAddresses.addAll(addrs)
 }
 
@@ -92,45 +92,57 @@
 
 // special custom cases
 switch(repository.name) {
-	case "ex@mple.git":
-		toAddresses.add "dev-team@somewhere.com"
-		toAddresses.add "qa-team@somewhere.com"
+	case 'ex@mple.git':
+		toAddresses.add 'dev-team@somewhere.com'
+		toAddresses.add 'qa-team@somewhere.com'
 		break
+}
+
+// define the summary and commit urls
+def summaryUrl
+def commitUrl
+if (gitblit.getBoolean(Keys.web.mountParameters, true)) {
+	summaryUrl = url + '/summary/' + repository.name.replace('/', gitblit.getString(Keys.web.forwardSlashCharacter, '/'))
+	commitUrl = url + '/commit/'
+} else {
+	summaryUrl = url + '/summary?r=' + repository.name
+	commitUrl = url + '/commit?h='
 }
 
 // construct a simple text summary of the changes contained in the push
 def commitCount = 0
-def changes = ""
-def table = { it.id.name[0..8] + " " + it.authorIdent.name.padRight(20, " ") + it.shortMessage }
+def changes = ''
+def table = { it.authorIdent.name.padRight(25, ' ') + it.shortMessage + "\n$commitUrl" + it.id.name }
 for (command in commands) {
+	def ref = command.refName.substring('refs/heads/'.length())
 	switch (command.type) {
 		case ReceiveCommand.Type.CREATE:
 			def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name)
 			commitCount += commits.size()
 			// new branch commits table
-			changes += "created ${command.refName}\n\n"
-			changes += commits.collect(table).join("\n")
-			changes += "\n"
+			changes += "created $ref ($commits.size commits)\n\n"
+			changes += commits.collect(table).join('\n\n')
+			changes += '\n'
 			break
 		case ReceiveCommand.Type.UPDATE:
 			def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name)
 			commitCount += commits.size()
 			// fast-forward branch commits table
-			changes += "updated ${command.refName}\n\n"
-			changes += commits.collect(table).join("\n")
-			changes += "\n"
+			changes += "updated $ref ($commits.size commits)\n\n"
+			changes += commits.collect(table).join('\n\n')
+			changes += '\n'
 			break
 		case ReceiveCommand.Type.UPDATE_NONFASTFORWARD:
 			def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name)
 			commitCount += commits.size()
 			// non-fast-forward branch commits table
-			changes += "updated ${command.refName} (NON fast-forward)\n\n"
-			changes += commits.collect(table).join("\n")
-			changes += "\n"
+			changes += "updated $ref [NON fast-forward] ($commits.size commits)\n\n"
+			changes += commits.collect(table).join('\n\n')
+			changes += '\n'
 			break
 		case ReceiveCommand.Type.DELETE:
 			// deleted branch
-			changes += "deleted ${command.refName}\n\n"
+			changes += "deleted $ref\n\n"
 			break
 		default:
 			break
@@ -139,17 +151,5 @@
 // close the repository reference
 r.close()
 
-// build a link to the summary page, either mounted or parameterized
-def summaryUrl
-if (gitblit.getBoolean(Keys.web.mountParameters, true))
-	summaryUrl = url + "/summary/" + repository.name.replace("/", gitblit.getString(Keys.web.forwardSlashCharacter, "/"))
-else
-	summaryUrl = url + "/summary?r=" + repository.name
-
-// create the message body
-def msg = """${summaryUrl}
-
-${changes}"""
-
 // tell Gitblit to send the message (Gitblit filters duplicate addresses)
-gitblit.sendEmail("${emailprefix} ${user.username} pushed ${commitCount} commits => ${repository.name}", msg, toAddresses)
\ No newline at end of file
+gitblit.sendEmail("$emailprefix $user.username pushed $commitCount commits => $repository.name", "$summaryUrl\n\n$changes", toAddresses)
\ No newline at end of file

--
Gitblit v1.9.1