From 5391d5ab0907b936e0cd07a8a87cdabecc55e3b6 Mon Sep 17 00:00:00 2001 From: Nikolas Falco Date: Fri, 21 Nov 2025 19:25:27 +0100 Subject: [PATCH] Do not remove webhook from repository if discard old branch/tag trait is configured, new commit in the target repository will trigger the item creation without trigger a "Scan Organization Folder Now" or "Scan Multiproject Now" --- .../hooks/WebhookAutoRegisterListener.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/WebhookAutoRegisterListener.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/WebhookAutoRegisterListener.java index 9f2360dcd..476b88f50 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/WebhookAutoRegisterListener.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/WebhookAutoRegisterListener.java @@ -36,6 +36,8 @@ import com.cloudbees.jenkins.plugins.bitbucket.api.webhook.BitbucketWebhookConfiguration; import com.cloudbees.jenkins.plugins.bitbucket.api.webhook.BitbucketWebhookManager; import com.cloudbees.jenkins.plugins.bitbucket.impl.util.URLUtils; +import com.cloudbees.jenkins.plugins.bitbucket.trait.DiscardOldBranchTrait; +import com.cloudbees.jenkins.plugins.bitbucket.trait.DiscardOldTagTrait; import com.cloudbees.jenkins.plugins.bitbucket.util.BitbucketCredentialsUtils; import com.cloudbees.plugins.credentials.common.StandardCredentials; import edu.umd.cs.findbugs.annotations.CheckForNull; @@ -214,6 +216,12 @@ private void removeHooks(SCMSourceOwner owner) throws IOException { if (endpoint == null) { continue; } + if (keepWebhook(source)) { + logger.log(Level.INFO, "Not removing hook for {0}/{1} because of DiscardOldBranch trait, new commit will recrete project without the need trigger a repository scan", + new Object[] { source.getRepoOwner(), source.getRepository() }); + continue; + } + BitbucketApi client = getClientBySource(source, endpoint); if (client != null) { try (BitbucketAuthenticatedClient webhookClient = client.adapt(BitbucketAuthenticatedClient.class)) { @@ -242,6 +250,11 @@ private void removeHooks(SCMSourceOwner owner) throws IOException { } } + private boolean keepWebhook(BitbucketSCMSource source) { + return source.getTraits().stream() + .anyMatch(trait -> trait instanceof DiscardOldBranchTrait || trait instanceof DiscardOldTagTrait); + } + @CheckForNull private BitbucketApi getClientBySource(@NonNull BitbucketSCMSource source, @NonNull BitbucketEndpoint endpoint) { switch (getRegistration(source)) {