Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

blockpush.groovy 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. * user Gitblit User com.gitblit.models.UserModel
  55. * commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand>
  56. * url Base url for Gitblit String
  57. * logger Logs messages to Gitblit org.slf4j.Logger
  58. * clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger
  59. *
  60. * Accessing Gitblit Custom Fields:
  61. * def myCustomField = repository.customFields.myCustomField
  62. *
  63. */
  64. // Indicate we have started the script
  65. logger.info("blockpush hook triggered by ${user.username} for ${repository.name}: checking ${commands.size} commands")
  66. /*
  67. * Example rejection of pushes to the master branch of example.git
  68. */
  69. def blocked = false
  70. switch (repository.name) {
  71. case 'ex@mple.git':
  72. for (ReceiveCommand command : commands) {
  73. def updatedRef = command.refName
  74. if (updatedRef.equals('refs/heads/master')) {
  75. // to reject a command set it's result to anything other than Result.NOT_ATTEMPTED
  76. command.setResult(Result.REJECTED_OTHER_REASON, "You are not permitted to write to ${repository.name}:${updatedRef}")
  77. blocked = true
  78. }
  79. }
  80. break
  81. default:
  82. break
  83. }
  84. if (blocked) {
  85. // return false to break the push hook chain
  86. return false
  87. }