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

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