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.

blockpush.groovy 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 java.text.MessageFormat;
  17. import com.gitblit.GitBlit
  18. import com.gitblit.models.RepositoryModel
  19. import com.gitblit.models.UserModel
  20. import org.eclipse.jgit.transport.ReceiveCommand
  21. import org.eclipse.jgit.transport.ReceiveCommand.Result
  22. import org.slf4j.Logger
  23. import com.gitblit.utils.ClientLogger
  24. /**
  25. * Sample Gitblit Pre-Receive Hook: blockpush
  26. *
  27. * This script could and perhaps should be further developed to provide
  28. * a full repository-branch permissions system similar to gitolite or gitosis.
  29. *
  30. * The Pre-Receive hook is executed after an incoming push has been parsed,
  31. * validated, and objects have been written but BEFORE the refs are updated.
  32. * This is the appropriate point to block a push for some reason.
  33. *
  34. * This script is only executed when pushing to *Gitblit*, not to other Git
  35. * tooling you may be using.
  36. *
  37. * If this script is specified in *groovy.preReceiveScripts* of gitblit.properties
  38. * or web.xml then it will be executed by any repository when it receives a
  39. * push. If you choose to share your script then you may have to consider
  40. * tailoring control-flow based on repository access restrictions.
  41. *
  42. * Scripts may also be specified per-repository in the repository settings page.
  43. * Shared scripts will be excluded from this list of available scripts.
  44. *
  45. * This script is dynamically reloaded and it is executed within it's own
  46. * exception handler so it will not crash another script nor crash Gitblit.
  47. *
  48. * If you want this hook script to fail and abort all subsequent scripts in the
  49. * chain, "return false" at the appropriate failure points.
  50. *
  51. * Bound Variables:
  52. * gitblit Gitblit Server com.gitblit.GitBlit
  53. * repository Gitblit Repository com.gitblit.models.RepositoryModel
  54. * receivePack JGit Receive Pack org.eclipse.jgit.transport.ReceivePack
  55. * user Gitblit User com.gitblit.models.UserModel
  56. * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>
  57. * url Base url for Gitblit String
  58. * logger Logs messages to Gitblit org.slf4j.Logger
  59. * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger
  60. *
  61. * Accessing Gitblit Custom Fields:
  62. * def myCustomField = repository.customFields.myCustomField
  63. *
  64. */
  65. // Indicate we have started the script
  66. logger.info("blockpush hook triggered by ${user.username} for ${repository.name}: checking ${commands.size} commands")
  67. /*
  68. * Example rejection of pushes to the master branch of example.git
  69. */
  70. def blocked = false
  71. switch (repository.name) {
  72. case 'ex@mple.git':
  73. for (ReceiveCommand command : commands) {
  74. def updatedRef = command.refName
  75. if (updatedRef.equals('refs/heads/master')) {
  76. // to reject a command set it's result to anything other than Result.NOT_ATTEMPTED
  77. command.setResult(Result.REJECTED_OTHER_REASON, "You are not permitted to write to ${repository.name}:${updatedRef}")
  78. blocked = true
  79. }
  80. }
  81. break
  82. default:
  83. break
  84. }
  85. if (blocked) {
  86. // return false to break the push hook chain
  87. return false
  88. }