Pre-Receive Hook not invoked when Bitbucket file editor is used

A simple pre-receive hook (blocking all commits+push) is not invoked when the Bitbucket built-in editor is used. Any change can be committed and is added to the respective ref/branch regardless of the pre-receive hook presence.

A push from a git CLI invokes the pre-receive hook and the push is rejected.

How to reproduce:

  1. Create app with simple <pre-receive-hook> that rejects everything
  2. Install app in BBS
  3. edit a file in a repository on ‘master’ branch with the BBS Editor
  4. Commit the file.

Expected result: commit rejected
Actual result: commit accpted

Hi @izymesdev,

May I ask what version of the Bitbucket Server API you are using? As of 6.0, <pre-receive-hook> has been deprecated. Instead you can use <repository-hook> and create a class that implements PreRepositoryHook. PreRepositoryHooks receive updates from both Git and UI events such as file edit, where as the old pre-receive hooks only received updates that happened through git push (and file editing comes through the REST API rather than through Git)

Hope that helps.

Regards,
Kristy

2 Likes

Hello Kristy
thanks for getting back to us.

We’re looking at Bitbucket 5.15.x and 5.16.x - I will have migrate to repository-hooks for 5.x as well. Do you know which is the earliest version of Bitbucket that introduced PreRepositoryHook

For Bitbucket 6x we have changed to PushToBranchHook implements PreRepositoryHook<RepositoryHookRequest> already. Web UI commits are rejected :+1:

Thanks for your help clarifying this issue.

PreRepositoryHook was first released in 5.0 so it should be available for you to switch to.

1 Like
public class PushToBranchHook implements PreRepositoryHook<RepositoryHookRequest> { ...

with atlassian-plugin.xml

  <pre-receive-hook key="workzone-pushToBranchHook" name="Push to Branch Hook" class="com.izymes.workzone.hook.PushToBranchHook">
    <description>Disables pushing to branches that are the source of an active pull request</description>
  </pre-receive-hook>

rejects the push from the web-ui editor
CloudApp

The new module type repository-hook

  <repository-hook key="workzone-pushToBranchHook" name="Push to Branch Hook" class="com.izymes.workzone.hook.PushToBranchHook">
    <description>Disables pushing to branches that are the source of an active pull request</description>
  </repository-hook>

does not seem to enforce the control
CloudApp

@khughes - any idea what’s missing? Thanks for your help!

Your xml and class definition looks fine. Perhaps there is something missing in your implementation of the hook? Are you only rejecting certain RepositoryHookTriggers? You will need to make sure your hook still rejects requests that are of type StandardRepositoryHookTrigger.FILE_EDIT.

adds a configuration item to the repository hooks configuration - which has to be enabled manually.

Is there a way to turn on/of this hook programmatically? The hook Workzone provides is configured via the workzone app. Is there any option to enable the <repository-hook> permanently and programmatically when installing the app?

The Workzone admin should not have to go to the hooks configuration outside workzone to enable the hook.


In fact if <pre-receive-hook> is replaced with <repository-hook> most clients would lose their workzone hooks configuration since it won’t be activated.

Yep! If you take a look at the ‘Global Hooks’ section of the docs you will see:

This can be achieved by adding the configurable="false" attribute to the repository-hook element in atlassian-plugin.xml

Global hooks are always triggered, and your plugin can decide whether or not to reject each request or not based on your own configuration.

1 Like