Pre-Receive with no configuration
Here is an example of a pre-receive hook that disables deleting of branches. Note that there isn't any configuration required; this hook only has to be enabled. The source is available for download from Bitbucket here.
src/main/resources/atlassian-plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<atlassian-plugin key="com.atlassian.bitbucket.server.bitbucket-docs" name="Bitbucket Server - Documentation" plugins-version="2">
<plugin-info>
<description>A simple repository plugin for rejecting branch deletion</description>
<vendor name="Atlassian" url="http://www.atlassian.com"/>
<version>4.7.1</version>
</plugin-info>
<repository-hook key="examplehook" name="Disable Branch Deletion" class="com.mycompany.example.plugin.myhook.MyRepositoryHook">
<description>Disables deletion of all branches in this repository.</description>
<icon>icons/example.png</icon>
</repository-hook>
</atlassian-plugin>
src/main/java/com/mycompany/example/plugin/myhook/MyRepositoryHook.java
package com.mycompany.example.plugin.myhook;
import com.atlassian.bitbucket.hook.*;
import com.atlassian.bitbucket.hook.repository.*;
import com.atlassian.bitbucket.repository.*;
import javax.annotation.Nonnull;
import java.util.Collection;
public class MyRepositoryHook implements PreReceiveRepositoryHook {
/**
* Disables deletion of branches
*/
@Override
public boolean onReceive(@Nonnull RepositoryHookContext context,
@Nonnull Collection<RefChange> refChanges,
@Nonnull HookResponse hookResponse) {
for (RefChange refChange : refChanges) {
if (refChange.getType() == RefChangeType.DELETE) {
hookResponse.err().println("The ref '" + refChange.getRefId() + "' cannot be deleted.");
return false;
}
}
return true;
}
}
The HookResponse class has both error and output writers, both of which will be displayed to the client.