From a50c4afdde37845b94b3545029ed9aac5796fdf7 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Mon, 19 Dec 2011 09:15:08 -0500
Subject: [PATCH] More sendemail.groovy fixes

---
 groovy/sendemail.groovy |   68 ++++++++++++++++++----------------
 1 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/groovy/sendemail.groovy b/groovy/sendemail.groovy
index 29c5e5a..69e2b93 100644
--- a/groovy/sendemail.groovy
+++ b/groovy/sendemail.groovy
@@ -71,63 +71,79 @@
 
 // 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)
 }
 
 // add all mailing lists defined in gitblit.properties or web.xml
 toAddresses.addAll(gitblit.getStrings(Keys.mail.mailingLists))
 
+// add all mail recipients for the repository
+toAddresses.addAll(repository.mailRecipients)
+
 // 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 repo = repository.name.replace('/', gitblit.getString(Keys.web.forwardSlashCharacter, '/'))
+def summaryUrl
+def commitUrl
+if (gitblit.getBoolean(Keys.web.mountParameters, true)) {	
+	summaryUrl = url + "/summary/$repo"
+	commitUrl = url + "/commit/$repo/"
+} else {
+	summaryUrl = url + "/summary?r=$repo"
+	commitUrl = url + "/commit?r=$repo&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
@@ -136,17 +152,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.notifyUsers("${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