James Moger
2011-12-19 916e848bd77cc0a63f083ff48e5a2e4e788e1f51
groovy/sendemail.groovy
@@ -71,42 +71,80 @@
// 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"
      break
   default:
   case 'ex@mple.git':
      toAddresses.add 'dev-team@somewhere.com'
      toAddresses.add 'qa-team@somewhere.com'
      break
}
// get the create/update commits from the repository to build message content
def commits = []
for (ReceiveCommand command:commands) {
// 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.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.UPDATE:
      case ReceiveCommand.Type.CREATE:
         RevCommit commit = JGitUtils.getCommit(r, command.newId.name)
         commits.add(commit)
         def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name)
         commitCount += commits.size()
         // new branch commits table
         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 $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 $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 $ref\n\n"
         break
      default:
         break
   }
@@ -114,21 +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 a simple commits table
def table = commits.collect { it.id.name[0..8] + " " + it.authorIdent.name.padRight(20, " ") + it.shortMessage }.join("\n")
// create the message body
def msg = """${user.username} pushed ${commits.size} commits to ${repository.name}
${summaryUrl}
${table}"""
// tell Gitblit to send the message (Gitblit filters duplicate addresses)
gitblit.notifyUsers("${emailprefix} ${user.username} pushed ${commits.size} commits => ${repository.name}", msg, toAddresses)
gitblit.sendEmail("$emailprefix $user.username pushed $commitCount commits => $repository.name", "$summaryUrl\n\n$changes", toAddresses)