Contents
- src/main/resources/atlassian-plugin.xml
- src/main/resources/META-INF/spring/atlassian-plugins-component-imports.xml
- src/main/java/com/atlassian/bitbucket/repository/hook/ref/ProtectRefHook.java
- src/main/java/com/atlassian/bitbucket/repository/hook/ref/RefValidator.java
- src/main/resources/static/simple.soy
Pre-Receive with configuration
Here is an example of a pre-receive hook that rejects changes to a specified branch or tag. The source is available for download from Bitbucket here.
src/main/resources/atlassian-plugin.xml
1<atlassian-plugin key="com.atlassian.bitbucket.server.bitbucket-docs" name="Bitbucket Server - Documentation" plugins-version="2">2<plugin-info>3<description>Provides a Hook for Bitbucket Server, which allows pushes to be denied</description>4<vendor name="Atlassian" url="http://www.atlassian.com"/>5<version>4.7.1</version>6</plugin-info>78<repository-hook key="disableRefChanges" name="Disable Ref Changes" class="com.atlassian.bitbucket.repository.hook.ref.ProtectRefHook">9<description>Disables changes to a specified reference (branch or tag) in this repository</description>10<icon>icons/example.png</icon>11<config-form name="Ref Hook Config" key="refHook-config">12<view>com.atlassian.bitbucket.repository.hook.ref.formContents</view>13<directory location="/static/"/>14</config-form>15<!-- Validators can be declared separately -->16<validator>com.atlassian.bitbucket.repository.hook.ref.RefValidator</validator>17</repository-hook>1819</atlassian-plugin>
src/main/resources/META-INF/spring/atlassian-plugins-component-imports.xml
12<beans xmlns="http://www.springframework.org/schema/beans"3xmlns:osgi="http://www.springframework.org/schema/osgi"4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"5xsi:schemaLocation="http://www.springframework.org/schema/beans6http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7http://www.springframework.org/schema/osgi
8http://www.springframework.org/schema/osgi/spring-osgi.xsd">910<osgi:reference id="refService" interface="com.atlassian.bitbucket.repository.RefService" />1112</beans>
src/main/java/com/atlassian/bitbucket/repository/hook/ref/ProtectRefHook.java
1package com.atlassian.bitbucket.repository.hook.ref;23import com.atlassian.bitbucket.hook.HookResponse;4import com.atlassian.bitbucket.hook.repository.PreReceiveRepositoryHook;5import com.atlassian.bitbucket.hook.repository.RepositoryHookContext;6import com.atlassian.bitbucket.repository.Ref;7import com.atlassian.bitbucket.repository.RefChange;8import com.atlassian.bitbucket.repository.RefService;910import javax.annotation.Nonnull;11import java.util.Collection;1213public class ProtectRefHook implements PreReceiveRepositoryHook {1415private final RefService refService;1617public ProtectRefHook(RefService refService) {18this.refService = refService;19}202122/**
23* Disables changes to a ref
24*/
25
26public boolean onReceive( RepositoryHookContext context,27Collection<RefChange> refChanges,28HookResponse hookResponse) {29String refId = context.getSettings().getString("ref-id");30Ref found = refService.resolveRef(context.getRepository(), refId);31for (RefChange refChange : refChanges) {32if (refChange.getRefId().equals(found.getId())) {33hookResponse.err().println("The ref '" + refChange.getRefId() + "' cannot be altered.");34return false;35}36}37return true;38}39}
src/main/java/com/atlassian/bitbucket/repository/hook/ref/RefValidator.java
1package com.atlassian.bitbucket.repository.hook.ref;23import com.atlassian.bitbucket.repository.Ref;4import com.atlassian.bitbucket.repository.RefService;5import com.atlassian.bitbucket.repository.Repository;6import com.atlassian.bitbucket.setting.RepositorySettingsValidator;7import com.atlassian.bitbucket.setting.Settings;8import com.atlassian.bitbucket.setting.SettingsValidationErrors;9import org.apache.commons.lang3.StringUtils;1011import javax.annotation.Nonnull;1213public class RefValidator implements RepositorySettingsValidator {1415private final RefService refService;1617public RefValidator(RefService refService) {18this.refService = refService;19}2021
22public void validate( Settings settings, SettingsValidationErrors settingsValidationErrors, Repository repository) {23String refId = settings.getString("ref-id", null);24if (StringUtils.isEmpty(refId)) {25settingsValidationErrors.addFieldError("ref-id", "The ref id must be specified");26}else {
27Ref ref = refService.resolveRef(repository, refId);28if (ref == null) {29settingsValidationErrors.addFieldError("ref-id", "Failed to find the ref");30}31}32}33}
src/main/resources/static/simple.soy
1{namespace com.atlassian.bitbucket.repository.hook.ref}23/**4* @param config5* @param? errors6*/7{template .formContents}8{call bitbucket.component.branchSelector.field}9{param id: 'ref-id' /}10{param initialValue: $config['ref-id'] /}11{param labelText: 'Reference' /}12{param descriptionText: 'Any pushes with changes to this reference (branch or tag) will be rejected' /}13{param errorTexts: $errors ? $errors['ref-id'] : null/}14{param showTags: true/}15{/call}
16{/template}
The HookResponse class has both error and output writers, both of which will be displayed to the client.