Disable and Enable hooks in Bitbucket
Asked by Raphael Viana:

Hi, I'd like to enable and disable hooks, like: 'Reject Force Push', through script.  
Is it possible? Exists API to do that action?

Thanks!


Answers:
author: Ulrich Kuhnhardt _IzymesCo_
body: [@Raphael Viana](/t5/user/viewprofilepage/user-id/4625044)

You can make a [Bitbucket REST API call](https://docs.atlassian.com/bitbucket-server/rest/7.18.0/bitbucket-rest.html#idp382) (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

As a general practice for automating UI tasks, play the scenario in the UI and log the XHR data requests in the browser console. This gives you a good idea what to look for in the REST API spec.

Let me know if that approach works for you.

Best, Ulrich

[Izymes](https://marketplace.atlassian.com/vendors/1210584/izymes-pty-ltd) - *Remove boring from work*

comments: author: Raphael Viana
body: Hey [@Ulrich Kuhnhardt _IzymesCo_](/t5/user/viewprofilepage/user-id/4011392)   
thanks so much about your explanation.  
I got it and my automation works now.  
Thanks!


author: Syahrul
body: Hi [@Raphael Viana](/t5/user/viewprofilepage/user-id/4625044)  

Welcome to the community!  

Currently, Bitbucket doesn't support the Hooks feature. We do have an existing feature request on this, please **Vote \& Watch** the feature request so that you'll receive an update if we have any at:

* [BCLOUD-10471 - Git server-side "pre-receive" hook](https://jira.atlassian.com/browse/BCLOUD-10471)

Meantime, you may want to look into an alternate feature Bitbucket has which is [Branch permission](https://support.atlassian.com/bitbucket-cloud/docs/use-branch-permissions/) where you can enforce specific workflows and prevent errors like a new workspace member deleting the main branch or pushing to branch without creating pull request and approved.

Feel free to let me know if you have any additional questions.

Cheers,  
Syahrul

comments: author: Raphael Viana
body: Hey [@Syahrul](/t5/user/viewprofilepage/user-id/744536)  
I'd like to check that information.

![bitbucket_example.png](https://community.atlassian.com/t5/image/serverpage/image-id/168333i8904040C26867960/image-size/large?v=v2&px=999 "bitbucket_example.png")

In BitBuket server we can't enable or disable pre-receive hook with API or  
another script access, that action is only possible manually. Correct?


author: Syahrul
body: Hi [@Raphael Viana](/t5/user/viewprofilepage/user-id/4625044)  

Apologize for that as I thought you were asking about Bitbucket cloud. If you are looking into Bitbucket server, I believe this documentation could help you:

* [Using repository hooks](https://confluence.atlassian.com/bitbucketserver/using-repository-hooks-776639836.html)
* [How to create a simple hook in Bitbucket Server](https://confluence.atlassian.com/bitbucketserverkb/how-to-create-a-simple-hook-in-bitbucket-server-779171711.html)

If you still failing to enable or use your pre-hook I would suggest you raise a support ticket for further assistance.

I hope this helps.

Cheers,  
Syahrul


author: Raphael Viana
body: [@Syahrul](/t5/user/viewprofilepage/user-id/744536)   
It's possible to enable or disable the hooks using traditional API path as [@Ulrich Kuhnhardt _IzymesCo_](/t5/user/viewprofilepage/user-id/4011392) said.  
It's a great point to put in tutorial or any other content.  
As complementing: PUT verb enable hook and Delete disable, so by this way we can control hooks passing his key (obtained through GET to the same endpoint).  
Thanks.


Tags: bitbucket-server

server