Create Git Commit Hook

Roman Wolf
Contributor
April 22, 2022

Hi Community,

We are using BitBucket cloud service to host all our Git repositories.

I would like to enforce some rule on each commit for some repositories. Technically I want to apply a Git commit hook on server side to valid any incoming push and allow/reject it by a bunch of checks.

On BitBucket cloud I can only find the configuration of Webhooks. From the documentation I was not able to clarify my following questions:

  • The Webhook sends an HTTP POST request to remote server, is the response evaluated and has it impact on the incoming Git push? Or is it fire and forget?
  • Is it possible to achive commit hooks by using the Webhook functionality?

 

Thanks a lot, Roman

 

2 answers

1 accepted

1 vote
Answer accepted
Roman Wolf
Contributor
April 24, 2022

I think I found the answer (https://community.atlassian.com/t5/Bitbucket-questions/Does-Bitbucket-Cloud-support-git-pre-receive-hooks/qaq-p/950235):

Git pre-commit hook is currently support in BitBucket DataCenter, but NOT in BitBucket Cloud.

BitBucket WebHooks are NOT an equivalent to that.

There is a ticket pending (might need more votes): https://jira.atlassian.com/browse/BCLOUD-10471

 

Regards, Roman

Ulrich Kuhnhardt [IzymesCo]
Atlassian Partner
April 26, 2022

Everyone reading `this` please vote on https://jira.atlassian.com/browse/BCLOUD-10471

0 votes
laura.devita
Contributor
April 22, 2022

Hi Roman,

You can enable a webhook so every time you commit you can create a pull request, and manage a review of the code with that.

 You can make a Bitbucket REST API call (via curl or postman) to enable/disable specific hooks programmatically.

1. GET your repo hook keys via /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/settings/hooks

2. PUT request for hook to be enabled  (DELETE disables the hook) via /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/settings/hooks/{hookKey}/enabled

Best Regards

Laura

Roman Wolf
Contributor
April 24, 2022

Hi @laura.devita

thanks for you quick reply. Creating a PR on each commit is not exactly what I am looking for.

Actually I try to find a way on server side (BitBucket) to prevent a commit being  stored in BitBucket Git repository.

This can be useful for a couple reason. Mainly when you want to apply a quality gate that needs to be passed before a commit is accepted. E.g. developer should follow a certain code format rule or the commit message must contain some required information.

Apply this on all developer systems is a high effort and can of course be bypassed by the developers easily.

So it is a better approach to ensure those things on Git server side by what is called a commit hook.

It is also possible to apply these quality gates later on (thru a central CI instance) but it is much more charme to apply some check already when the commit is send and reject it if checks are failing.

Regards, Roman

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events