Failing to get commit message from pre push git hook

git

#1

Hi,

I am trying to get the commit message during the git pre push hook stage, but it is not retrieving the message during a git push from the git client side. I am trying to use the message to verify whether a push is allowed in a specific repo, and also get the issue number out of the commit message to use for checking Jira.
I based it off the example in the documents, but even checking which user is doing the git push is not working. I would expect it to have returned the hook response to the git client, or at least an error message to the log but I am not getting anything.

I’m currently using version 4.14 with the enterprise trial, does anyone have a working example of a git pre push hook for 4.14? Where can I find what properties of the commit I can access using the hook?

Thanks.


#2

The output from rcextensions should be shown to the git client, example:

def _push_hook(*args, **kwargs):  
    from .helpers import extra_fields
    # fetch stored deploy url from repo extra fields
    repo_extra_fields = extra_fields.run(**kwargs)
    deploy_url = repo_extra_fields.get('deploy_endpoint_url', {}).get('field_value')

    msg = ''
    if deploy_url:
        my_deploy_function(deploy_url)
        last_commit = kwargs['commit_ids'][-1]
        msg = 'Commit {} was deployed to: {}'.format(last_commit, deploy_url)
    return HookResponse(0, msg)

This would result in this:


#3

So I have tried that, and I am not getting the output of this
msg = ‘Commit {} was deployed to: {}’.format(last_commit, deploy_url)
to display on the client end. I have also tried copying the above code verbatim but it does not work. Is there somewhere I can see a full list of the kwargs I can access? Other than commit id I would like to be able to see the commit message


#4

Did you restart rhodecode after changing the rcextensions? This is required to do. Also please check if you haven’t modify the git hooks by yourself, this would mean that the rcextensions aren’t executed.

Best to test this is to add something like raise Exception('test') to check if the push fails. If it doesn you can enter such url:

http://server.com/REPO_NAME/settings/advanced/hooks to re-install the hooks.

For getting the commit author/messages you’d need to extract this. There’s a helper in rcextensions for that.

from .helpers import extra_fields, extract_pre_commits
commit_list = extract_pre_commits.run(**kwargs)

for commit_data in commit_list:
    message = commit_data['message']

#5

Yes I restart rhodecode each time I make a change. What do you mean by modifying the git hooks by myself? Do I have to

In the .rccontrol/enterprise-1 folder, I have the rcextensions folder and the only file I am editing within there is the hooks.py file


#6

I was referring to pre-receive/post-receive git hooks. Inside the repository. If you enter: http://server.com/REPO_NAME/settings/advanced/hooks RhodeCode will check them and fix if they are modified.


#7

I was able to get raise Exception to be recognized after I restarted the service, but the example above does not work for responding to the git client with a message, I have tried copying the example verbatim.


#8

The example will not print anything if the deploy_url isn’t set via extra fields in the repository due to this condition:

You could simply test this by settings instead of msg = ‘’ to msg = ‘TEST MESSAGE FOR GIT HOOK’


#9

In the extra fields in the repo, can the keywords be anything? I made a “test_url” keyword there and used it within the example, but I did not get output on the client side.


#10

yes, they can be called anything, in the example, we used deploy_endpoint_url but ensure that it has a value.

Simples test would be:

def _push_hook(*args, **kwargs):  
    return HookResponse(0, 'TEST MESSAGE')

If that works and the extra_fields don’t we recommend upgrading to 4.15.X as there were some small fixes for that logic of rcextensions.
It’s very easy to upgrade and it should take you ~10minutes. Just make sure to get newer rcextensions in case it gives troubles now.


#11

Hi,

We’ve updated to 4.15.1, I am able to see some response to the hook changes when I do a git push when I try to push with “admin” or “root” but I tried the earlier example for the commit message and I am getting this error: