Failure to enable repository hook and to change its settings

The simplified code below, when running in BitBucket 4.11.2, finds a repository hook, configures it and enables

private RepositoryHookService rhService;
private void configureJenkinsHook(Repository repository, String ciUrl) {
    RepositoryHook hook = rhService.getByKey(repository, KEY);
    if (hook != null) {
        Map<String, Object> mapSettings = new HashMap<>();
        mapSettings.put(CLONE_TYPE, "ssh");
        mapSettings.put(IGNORE_CERTS, Boolean.TRUE);
        mapSettings.put(JENKINS_BASE, ciUrl);

        Settings settings = rhService.createSettingsBuilder().addAll(mapSettings).build();

        if (!hook.isEnabled()) {
            rhService.enable(repository, KEY, settings);
        } else {
            rhService.setSettings(repository, KEY, settings);
        }
    }
}

However when it runs in 5.3.1, no exceptions are thrown, but the hook does not get enabled. If it’s already enabled, its settings do not change. Seems enable() and setSettings() do not have any effect.

According to 5.3.1 RepositoryHookService API, the used methods are deprecated with removal in 6.0. Did their functionality already change?

PS: it’s been tested also in 5.1.5, where RepositoryHookService API is not changed yet - it fails to enable the hook and to change its settings too. More details are in the replies below

I added logging of the hook state at the end of the method. According to atlassian-bitbucket.log the hook gets enabled. The same is confirmed by atlassian-bitbucket-augit.log. However it’s not reflected in UI and the hook does not fire.

private static final Logger LOGGER = LoggerFactory.getLogger(Temp.class);
private RepositoryHookService rhService;

private void configureJenkinsHook(Repository repository, String ciUrl) {
    RepositoryHook hook = rhService.getByKey(repository, KEY);
    if (hook != null) {

        LOGGER.error("before: Repository hook {}#enabled: {}", KEY, hook.isEnabled());
        LOGGER.error("before: Repository hook {}#configured: {}", KEY, hook.isConfigured());
        
        Map<String, Object> mapSettings = new HashMap<>();
        mapSettings.put(CLONE_TYPE, "ssh");
        mapSettings.put(IGNORE_CERTS, Boolean.TRUE);
        mapSettings.put(JENKINS_BASE, ciUrl);

        Settings settings = rhService.createSettingsBuilder().addAll(mapSettings).build();

        if (!hook.isEnabled()) {
            hook = rhService.enable(repository, KEY, settings);
        } else {
            rhService.setSettings(repository, KEY, settings);
        }

        LOGGER.error("after: Repository hook {}#enabled: {}", KEY, hook.isEnabled());
        LOGGER.error("after: Repository hook {}#configured: {}", KEY, hook.isConfigured());

    }
}

Content of atlassian-bitcket.log file

o.e.c.b.h.EpoReceiveRepositoryHook before: Repository hook com.nerdwin15.stash-stash-webhook-jenkins:jenkinsPostReceiveHook#enabled: false
o.e.c.b.h.EpoReceiveRepositoryHook before: Repository hook com.nerdwin15.stash-stash-webhook-jenkins:jenkinsPostReceiveHook#configured: false
c.a.s.i.e.TransactionAwareEventPublisher Deferring publishing for RepositoryHookEnabledEvent until AFTER_COMMIT
c.a.s.i.e.TransactionAwareEventPublisher Deferring publishing for RepositoryHookSettingsChangedEvent until AFTER_COMMIT
o.e.c.b.h.EpoReceiveRepositoryHook after: Repository hook com.nerdwin15.stash-stash-webhook-jenkins:jenkinsPostReceiveHook#enabled: true
o.e.c.b.h.EpoReceiveRepositoryHook after: Repository hook com.nerdwin15.stash-stash-webhook-jenkins:jenkinsPostReceiveHook#configured: true
c.a.s.i.e.TransactionAwareEventPublisher Publishing RepositoryHookEnabledEvent after commit
c.a.s.i.e.TransactionAwareEventPublisher Publishing RepositoryHookSettingsChangedEvent after commit

Hi @sylvie, @bturner, I’m sorry for addressing you here directly, but I stuck and do not hear any suggestions in the forum.

Do you know if there is a way in BitBucket 5+ to configure and to enable repository hook within code of another repository hook? It works just fine in 4.11 (and my plugin is written against 4.11.2 version of APIs), but plugin to fails to “commit” changes in 5.3.1.

It also fails to enable repository hook (and to change its settings) in 5.1.5, where API of RepositoryHookService is not changed yet

I will very appreciate if you can lit any light on this.

Hi @viktor,

In the RepositoryService API, there’s a suggestion to use:

@Deprecated @Nonnull RepositoryHook	disable(Repository repository, String hookKey)
This method is deprecated. in 5.2 for removal in 6.0. Use disable(DisableRepositoryHookRequest) instead.
@Deprecated @Nonnull RepositoryHook	enable(Repository repository, String hookKey, Settings settings)
This method is deprecated. in 5.2 for removal in 6.0. Use enable(EnableRepositoryHookRequest) instead.

Have you tried these methods?

Thanks!

Cheers,
Anne Calantog

Hi @acalantog, thanks a lot for taking a look at the problem!

Frankly speaking, I hope “deprecated” does not mean “broken” :wink: and therefore the same code should work with BitBucket versions from 4.7.1 until 6.0. Otherwise it forces developers to release different versions of the plugins for every major version of BB… This might be acceptable, but still this is not what “deprecated” means.

However new methods are introduced in 5.2, but the old methods (not deprecated yet) stop working in 5.0 and 5.1. What should be done for these versions? Let’s try to getting this functionality working in 5.1.6

With kind regards,
Viktor

Hi @viktor,

Thank you for reporting this issue to us. A bug is already raised in relation to this issue. BSERV-10193, kindly vote, watch or comment on it.

Cheers,
Anne

1 Like