You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sendmail.groovy 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import com.gitblit.GitBlit
  17. import com.gitblit.Keys
  18. import com.gitblit.models.RepositoryModel
  19. import com.gitblit.models.TeamModel
  20. import com.gitblit.models.UserModel
  21. import com.gitblit.utils.JGitUtils
  22. import java.text.SimpleDateFormat
  23. import org.eclipse.jgit.lib.Repository
  24. import org.eclipse.jgit.lib.Config
  25. import org.eclipse.jgit.revwalk.RevCommit
  26. import org.eclipse.jgit.transport.ReceiveCommand
  27. import org.eclipse.jgit.transport.ReceiveCommand.Result
  28. import org.slf4j.Logger
  29. /**
  30. * Sample Gitblit Post-Receive Hook: sendmail
  31. *
  32. * The Post-Receive hook is executed AFTER the pushed commits have been applied
  33. * to the Git repository. This is the appropriate point to trigger an
  34. * integration build or to send a notification.
  35. *
  36. * This script is only executed when pushing to *Gitblit*, not to other Git
  37. * tooling you may be using.
  38. *
  39. * If this script is specified in *groovy.postReceiveScripts* of gitblit.properties
  40. * or web.xml then it will be executed by any repository when it receives a
  41. * push. If you choose to share your script then you may have to consider
  42. * tailoring control-flow based on repository access restrictions.
  43. *
  44. * Scripts may also be specified per-repository in the repository settings page.
  45. * Shared scripts will be excluded from this list of available scripts.
  46. *
  47. * This script is dynamically reloaded and it is executed within it's own
  48. * exception handler so it will not crash another script nor crash Gitblit.
  49. *
  50. * If you want this hook script to fail and abort all subsequent scripts in the
  51. * chain, "return false" at the appropriate failure points.
  52. *
  53. * Bound Variables:
  54. * gitblit Gitblit Server com.gitblit.GitBlit
  55. * repository Gitblit Repository com.gitblit.models.RepositoryModel
  56. * receivePack JGit Receive Pack org.eclipse.jgit.transport.ReceivePack
  57. * user Gitblit User com.gitblit.models.UserModel
  58. * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>
  59. * url Base url for Gitblit String
  60. * logger Logs messages to Gitblit org.slf4j.Logger
  61. * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger
  62. *
  63. * Accessing Gitblit Custom Fields:
  64. * def myCustomField = repository.customFields.myCustomField
  65. *
  66. */
  67. // Indicate we have started the script
  68. logger.info("sendmail hook triggered by ${user.username} for ${repository.name}")
  69. /*
  70. * Primitive email notification.
  71. * This requires the mail settings to be properly configured in Gitblit.
  72. */
  73. Repository r = gitblit.getRepository(repository.name)
  74. // reuse existing repository config settings, if available
  75. Config config = r.getConfig()
  76. def mailinglist = config.getString('hooks', null, 'mailinglist')
  77. def emailprefix = config.getString('hooks', null, 'emailprefix')
  78. // set default values
  79. def toAddresses = []
  80. if (emailprefix == null)
  81. emailprefix = '[Gitblit]'
  82. if (mailinglist != null) {
  83. def addrs = mailinglist.split(/(,|\s)/)
  84. toAddresses.addAll(addrs)
  85. }
  86. // add all mailing lists defined in gitblit.properties or web.xml
  87. toAddresses.addAll(gitblit.getStrings(Keys.mail.mailingLists))
  88. // add all team mailing lists
  89. def teams = gitblit.getRepositoryTeams(repository)
  90. for (team in teams) {
  91. TeamModel model = gitblit.getTeamModel(team)
  92. if (model.mailingLists) {
  93. toAddresses.addAll(model.mailingLists)
  94. }
  95. }
  96. // add all mailing lists for the repository
  97. toAddresses.addAll(repository.mailingLists)
  98. // define the summary and commit urls
  99. def repo = repository.name
  100. def summaryUrl
  101. def commitUrl
  102. if (gitblit.getBoolean(Keys.web.mountParameters, true)) {
  103. repo = repo.replace('/', gitblit.getString(Keys.web.forwardSlashCharacter, '/')).replace('/', '%2F')
  104. summaryUrl = url + "/summary/$repo"
  105. commitUrl = url + "/commit/$repo/"
  106. } else {
  107. summaryUrl = url + "/summary?r=$repo"
  108. commitUrl = url + "/commit?r=$repo&h="
  109. }
  110. // construct a simple text summary of the changes contained in the push
  111. def branchBreak = '>---------------------------------------------------------------\n'
  112. def commitBreak = '\n\n ----\n'
  113. def commitCount = 0
  114. def changes = ''
  115. SimpleDateFormat df = new SimpleDateFormat(gitblit.getString(Keys.web.datetimestampLongFormat, 'EEEE, MMMM d, yyyy h:mm a z'))
  116. def table = { "\n ${JGitUtils.getDisplayName(it.authorIdent)}\n ${df.format(JGitUtils.getCommitDate(it))}\n\n $it.shortMessage\n\n $commitUrl$it.id.name" }
  117. for (command in commands) {
  118. def ref = command.refName
  119. def refType = 'branch'
  120. if (ref.startsWith('refs/heads/')) {
  121. ref = command.refName.substring('refs/heads/'.length())
  122. } else if (ref.startsWith('refs/tags/')) {
  123. ref = command.refName.substring('refs/tags/'.length())
  124. refType = 'tag'
  125. }
  126. switch (command.type) {
  127. case ReceiveCommand.Type.CREATE:
  128. def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name).reverse()
  129. commitCount += commits.size()
  130. // new branch
  131. changes += "\n$branchBreak new $refType $ref created ($commits.size commits)\n$branchBreak"
  132. changes += commits.collect(table).join(commitBreak)
  133. changes += '\n'
  134. break
  135. case ReceiveCommand.Type.UPDATE:
  136. def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name).reverse()
  137. commitCount += commits.size()
  138. // fast-forward branch commits table
  139. changes += "\n$branchBreak $ref $refType updated ($commits.size commits)\n$branchBreak"
  140. changes += commits.collect(table).join(commitBreak)
  141. changes += '\n'
  142. break
  143. case ReceiveCommand.Type.UPDATE_NONFASTFORWARD:
  144. def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name).reverse()
  145. commitCount += commits.size()
  146. // non-fast-forward branch commits table
  147. changes += "\n$branchBreak $ref $refType updated [NON fast-forward] ($commits.size commits)\n$branchBreak"
  148. changes += commits.collect(table).join(commitBreak)
  149. changes += '\n'
  150. break
  151. case ReceiveCommand.Type.DELETE:
  152. // deleted branch/tag
  153. changes += "\n$branchBreak $ref $refType deleted\n$branchBreak"
  154. break
  155. default:
  156. break
  157. }
  158. }
  159. // close the repository reference
  160. r.close()
  161. // tell Gitblit to send the message (Gitblit filters duplicate addresses)
  162. gitblit.sendMail("$emailprefix $user.username pushed $commitCount commits => $repository.name", "$summaryUrl\n$changes", toAddresses)