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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * user Gitblit User com.gitblit.models.UserModel
  57. * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>
  58. * url Base url for Gitblit String
  59. * logger Logs messages to Gitblit org.slf4j.Logger
  60. * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger
  61. *
  62. * Accessing Gitblit Custom Fields:
  63. * def myCustomField = repository.customFields.myCustomField
  64. *
  65. */
  66. // Indicate we have started the script
  67. logger.info("sendmail hook triggered by ${user.username} for ${repository.name}")
  68. /*
  69. * Primitive email notification.
  70. * This requires the mail settings to be properly configured in Gitblit.
  71. */
  72. Repository r = gitblit.getRepository(repository.name)
  73. // reuse existing repository config settings, if available
  74. Config config = r.getConfig()
  75. def mailinglist = config.getString('hooks', null, 'mailinglist')
  76. def emailprefix = config.getString('hooks', null, 'emailprefix')
  77. // set default values
  78. def toAddresses = []
  79. if (emailprefix == null)
  80. emailprefix = '[Gitblit]'
  81. if (mailinglist != null) {
  82. def addrs = mailinglist.split(/(,|\s)/)
  83. toAddresses.addAll(addrs)
  84. }
  85. // add all mailing lists defined in gitblit.properties or web.xml
  86. toAddresses.addAll(gitblit.getStrings(Keys.mail.mailingLists))
  87. // add all team mailing lists
  88. def teams = gitblit.getRepositoryTeams(repository)
  89. for (team in teams) {
  90. TeamModel model = gitblit.getTeamModel(team)
  91. if (model.mailingLists) {
  92. toAddresses.addAll(model.mailingLists)
  93. }
  94. }
  95. // add all mailing lists for the repository
  96. toAddresses.addAll(repository.mailingLists)
  97. // define the summary and commit urls
  98. def repo = repository.name
  99. def summaryUrl
  100. def commitUrl
  101. if (gitblit.getBoolean(Keys.web.mountParameters, true)) {
  102. repo = repo.replace('/', gitblit.getString(Keys.web.forwardSlashCharacter, '/')).replace('/', '%2F')
  103. summaryUrl = url + "/summary/$repo"
  104. commitUrl = url + "/commit/$repo/"
  105. } else {
  106. summaryUrl = url + "/summary?r=$repo"
  107. commitUrl = url + "/commit?r=$repo&h="
  108. }
  109. // construct a simple text summary of the changes contained in the push
  110. def branchBreak = '>---------------------------------------------------------------\n'
  111. def commitBreak = '\n\n ----\n'
  112. def commitCount = 0
  113. def changes = ''
  114. SimpleDateFormat df = new SimpleDateFormat(gitblit.getString(Keys.web.datetimestampLongFormat, 'EEEE, MMMM d, yyyy h:mm a z'))
  115. def table = { "\n ${JGitUtils.getDisplayName(it.authorIdent)}\n ${df.format(JGitUtils.getCommitDate(it))}\n\n $it.shortMessage\n\n $commitUrl$it.id.name" }
  116. for (command in commands) {
  117. def ref = command.refName
  118. def refType = 'branch'
  119. if (ref.startsWith('refs/heads/')) {
  120. ref = command.refName.substring('refs/heads/'.length())
  121. } else if (ref.startsWith('refs/tags/')) {
  122. ref = command.refName.substring('refs/tags/'.length())
  123. refType = 'tag'
  124. }
  125. switch (command.type) {
  126. case ReceiveCommand.Type.CREATE:
  127. def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name).reverse()
  128. commitCount += commits.size()
  129. // new branch
  130. changes += "\n$branchBreak new $refType $ref created ($commits.size commits)\n$branchBreak"
  131. changes += commits.collect(table).join(commitBreak)
  132. changes += '\n'
  133. break
  134. case ReceiveCommand.Type.UPDATE:
  135. def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name).reverse()
  136. commitCount += commits.size()
  137. // fast-forward branch commits table
  138. changes += "\n$branchBreak $ref $refType updated ($commits.size commits)\n$branchBreak"
  139. changes += commits.collect(table).join(commitBreak)
  140. changes += '\n'
  141. break
  142. case ReceiveCommand.Type.UPDATE_NONFASTFORWARD:
  143. def commits = JGitUtils.getRevLog(r, command.oldId.name, command.newId.name).reverse()
  144. commitCount += commits.size()
  145. // non-fast-forward branch commits table
  146. changes += "\n$branchBreak $ref $refType updated [NON fast-forward] ($commits.size commits)\n$branchBreak"
  147. changes += commits.collect(table).join(commitBreak)
  148. changes += '\n'
  149. break
  150. case ReceiveCommand.Type.DELETE:
  151. // deleted branch/tag
  152. changes += "\n$branchBreak $ref $refType deleted\n$branchBreak"
  153. break
  154. default:
  155. break
  156. }
  157. }
  158. // close the repository reference
  159. r.close()
  160. // tell Gitblit to send the message (Gitblit filters duplicate addresses)
  161. gitblit.sendMail("$emailprefix $user.username pushed $commitCount commits => $repository.name", "$summaryUrl\n$changes", toAddresses)