How to add and execute Javascript hooks during code merge in Bitbucket

Javascript hooks for Bitbucket server allows repository administrators to write their own hooks. This article explains how to add and execute Javascript hooks during code merge.

Instructions

  1. Click Repositories in Bitbucket and select the repository where you want to add a Javascript hook.
  2. Navigate to Repository Settings > JS Hooks.
  3. Click Add hook under Merge Checks.


  4. Enter the Script name, write the Javascript code under Script code section, and, click Save.

Let's consider a sample Javascript hook that verifies the branch before code merge only from the selected branch. Note that the hook executes on code merge and depends on the script.

For this example, after the Javascript hook is added successfully, it displays under the Merge Checks section as shown:

How the Javascript hook works

  1. Merge the code from branch1 to master.



  2. If you merge the code from other branches apart from the one mentioned in the script, it displays the following message:

Implement the Javascript hook in a stage environment before adding it in production.