Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to control order of pre-receive hooks?

Deleted user July 25, 2016

We are running Stash 3.7.  We protect the master branch in all our repositories using branch permissions.  We are also using standard Git hooks (we use the update hook) to enforce various conditions such as commit message formats, user names with email address known to Stash, ...  We validate that all branch names begin with a Rally ID (story id or defect id).  Our branch permissions allow any authenticated user to push a branch that is named US*  or DE*.

If a user tries to push a branch that does not meet this requirement, they get an error message from the pre-receive hook that enforces branch permissions:

 

remote: You have insufficient permissions to update 'refs/heads/invalid-branch-name'
remote: Check your branch permissions with the project administrator
To http://stash.host:8080/scm/ips/ips-atms
! [remote rejected] master -> invalid-branch-name (pre-receive hook declined)
error: failed to push some refs to 'http://stash.chotel.com:8080/scm/ips/ips-atms'

This message, while clear enough to the experienced developer, results in several emails asking for access to Stash.  We implemented a Git update hook that checks the branch name and issues a very explicit message:

 

remote: -----
remote: branch-name-verifier: (refs/heads/invalid-branch-name) branch name is NOT valid
remote: Branch names must include a Rally ID prefix followed by a dash or underscore
remote: followed by a brief description of the branch.
remote: Examples:
remote: US0000-update-command-parser
remote: DE0000_new-button
remote: TS0000_what-this-test-does
remote:
remote: Your branch name "invalid-branch-name" does not match this pattern.

Unfortunately, the message is only issued if the user has permissions to push code to any branch (such as administrators, and users authorized to merge branches to master).   From our experience, the order of hook execution is pre-receive hooks followed by Git hooks.

Given that we want control of the error message issued for unacceptable branch names, what is the best approach?

Is there a file we can drop our custom message into?  would be nice to provide our own message for branch permissions rejections.

If not, can we inject our Git update hook into the pre-receive chain of processing?  If so, how?

If not, and if we write our own pre-receive hook in Java, how do we guarantee that it will be fired BEFORE branch permissions are checked?

Michael

1 answer

0 votes
Josh Wheeler
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 26, 2016

Maybe you could write your own pre-receive hook that will take care of both the validity of branch names and branch permissions...

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events