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