Skip to content

[JENKINS-74913] Allow extension point in bitbucket source plugin to provide a implementation for web-hooks management #1061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docs/USER_GUIDE.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,12 @@ System.setProperty("http.socket.timeout", "300") // 5 minutes
In case Bitbucket has been configured to expire OAuth2 tokens before 5 minutes, you can configure via a JVM property the release time of the cache where all obtained OAuth2 tokens are stored. This setting is to avoid requests with expired tokens that will produce HTTP 401 responses. link:https://support.atlassian.com/bitbucket-cloud/docs/use-oauth-on-bitbucket-cloud/[Bitbucket Cloud] access tokens expire in two hours.
To change this amount of time (default is 300 seconds), add the system property `bitbucket.oauth2.cache.timeout=60` on Jenkins startup.

=== Disable Branch Indexing on Empty changes
=== Enable Branch Indexing on Empty changes

By default, the plugin triggers *a full branch indexing* when a push event contains *empty* changes. This may happen on various scenario, mainly in Bitbucket Data Center, such as:
By default, the plugin does not triggers *a full branch indexing* when a push event contains *empty* changes. This may happen on various scenario, mainly in Bitbucket Data Center, such as:

* When manually merging remote **Open** pull requests. This particular scenario produces 2 events and cause duplicated builds.
* For a fork, when link:https://confluence.atlassian.com/bitbucketserver/keeping-forks-synchronized-776639961.html[Auto-Sync] is on and a branch cannot be synchronised
* A link:http://confluence.atlassian.com/bitbucketserver/event-payload-938025882.html#Eventpayload-Mirrorsynchronized[mirror:repo_synchronized] event with too many refs

This behaviour can be disabled by adding the system property `bitbucket.hooks.processor.scanOnEmptyChanges=false` on Jenkins startup.
This behaviour can be enabled by adding the system property `bitbucket.hooks.processor.scanOnEmptyChanges=true` on Jenkins startup.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-lang3-api</artifactId>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>commons-collections4-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>jackson2-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.cloudbees.jenkins.plugins.bitbucket.api.PullRequestBranchType;
import com.cloudbees.jenkins.plugins.bitbucket.api.endpoint.BitbucketEndpoint;
import com.cloudbees.jenkins.plugins.bitbucket.api.endpoint.BitbucketEndpointProvider;
import com.cloudbees.jenkins.plugins.bitbucket.impl.endpoint.BitbucketCloudEndpoint;
import com.cloudbees.jenkins.plugins.bitbucket.impl.endpoint.BitbucketServerEndpoint;
import com.cloudbees.jenkins.plugins.bitbucket.impl.extension.FallbackToOtherRepositoryGitSCMExtension;
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketApiUtils;
Expand Down Expand Up @@ -113,7 +114,7 @@ public BitbucketGitSCMBuilder(@NonNull BitbucketSCMSource scmSource, @NonNull SC

String serverURL = scmSource.getServerUrl();
BitbucketEndpoint endpoint = BitbucketEndpointProvider.lookupEndpoint(serverURL)
.orElse(new BitbucketServerEndpoint(null, serverURL, false, null, false, null));
.orElse(BitbucketApiUtils.isCloud(serverURL) ? new BitbucketCloudEndpoint() : new BitbucketServerEndpoint("tmp", serverURL));

String repositoryURL = endpoint.getRepositoryURL(scmSource.getRepoOwner(), scmSource.getRepository());
if (BitbucketApiUtils.isCloud(endpoint.getServerURL())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketCredentialsUtils;
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.MirrorListSupplier;
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.URLUtils;
import com.cloudbees.jenkins.plugins.bitbucket.server.BitbucketServerWebhookImplementation;
import com.cloudbees.jenkins.plugins.bitbucket.trait.BranchDiscoveryTrait;
import com.cloudbees.jenkins.plugins.bitbucket.trait.ForkPullRequestDiscoveryTrait;
import com.cloudbees.jenkins.plugins.bitbucket.trait.OriginPullRequestDiscoveryTrait;
Expand Down Expand Up @@ -412,10 +411,8 @@ public static FormValidation doCheckCredentialsId(@AncestorInPath SCMSourceOwner
public static FormValidation doCheckMirrorId(@QueryParameter String value,
@QueryParameter(fixEmpty = true, value = "serverUrl") String serverURL) {
if (!value.isEmpty()) {
BitbucketServerWebhookImplementation webhookImplementation =
BitbucketServerEndpoint.findWebhookImplementation(serverURL);
if (webhookImplementation == BitbucketServerWebhookImplementation.PLUGIN) {
return FormValidation.error("Mirror can only be used with native webhooks");
if (BitbucketServerEndpoint.supportsMirror(serverURL)) {
return FormValidation.error("Mirror is not supported by the choosen webhooks");
}
}
return FormValidation.ok();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.DateUtils;
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.MirrorListSupplier;
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.URLUtils;
import com.cloudbees.jenkins.plugins.bitbucket.server.BitbucketServerWebhookImplementation;
import com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerAPIClient;
import com.cloudbees.jenkins.plugins.bitbucket.server.client.repository.BitbucketServerRepository;
import com.cloudbees.jenkins.plugins.bitbucket.trait.BranchDiscoveryTrait;
Expand Down Expand Up @@ -1073,9 +1072,7 @@ public static FormValidation doCheckServerUrl(@AncestorInPath SCMSourceOwner con
public static FormValidation doCheckMirrorId(@QueryParameter String value,
@QueryParameter(fixEmpty = true, value = "serverUrl") String serverURL) {
if (!value.isEmpty()) {
BitbucketServerWebhookImplementation webhookImplementation =
BitbucketServerEndpoint.findWebhookImplementation(serverURL);
if (webhookImplementation == BitbucketServerWebhookImplementation.PLUGIN) {
if (BitbucketServerEndpoint.supportsMirror(serverURL)) {
return FormValidation.error("Mirror can only be used with native webhooks");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.cloudbees.jenkins.plugins.bitbucket.api;

public class BitbucketException extends RuntimeException {
private static final long serialVersionUID = 1L;

public BitbucketException(String message, Throwable cause) {
super(message, cause);
Expand All @@ -33,6 +34,4 @@ public BitbucketException(String message) {
super(message);
}

private static final long serialVersionUID = 1L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,86 +23,12 @@
*/
package com.cloudbees.jenkins.plugins.bitbucket.api.endpoint;

import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketCredentialsUtils;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.security.ACL;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.net.MalformedURLException;
import java.net.URL;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.interceptor.RequirePOST;

/**
* {@link Descriptor} for {@link BitbucketEndpoint}s.
*
* @since 936.4.0
*/
public class BitbucketEndpointDescriptor extends Descriptor<BitbucketEndpoint> {
/**
* Stapler form completion.
*
* @param credentialsId selected credentials.
* @param serverURL the server URL.
* @return the available credentials.
*/
@RequirePOST
public ListBoxModel doFillCredentialsIdItems(@QueryParameter(fixEmpty = true) String credentialsId,
@QueryParameter(value = "serverUrl", fixEmpty = true) String serverURL) {
Jenkins jenkins = checkPermission();
return BitbucketCredentialsUtils.listCredentials(jenkins, serverURL, credentialsId);
}

private static Jenkins checkPermission() {
Jenkins jenkins = Jenkins.get();
jenkins.checkPermission(Jenkins.MANAGE);
return jenkins;
}

/**
* Stapler form completion.
*
* @param hookSignatureCredentialsId selected hook signature credentials.
* @param serverURL the server URL.
* @return the available credentials.
*/
@RequirePOST
public ListBoxModel doFillHookSignatureCredentialsIdItems(@QueryParameter(fixEmpty = true) String hookSignatureCredentialsId,
@QueryParameter(value = "serverUrl", fixEmpty = true) String serverURL) {
Jenkins jenkins = checkPermission();
StandardListBoxModel result = new StandardListBoxModel();
result.includeMatchingAs(ACL.SYSTEM2,
jenkins,
StringCredentials.class,
URIRequirementBuilder.fromUri(serverURL).build(),
CredentialsMatchers.always());
if (hookSignatureCredentialsId != null) {
result.includeCurrentValue(hookSignatureCredentialsId);
}
return result;
}

@Restricted(NoExternalUse.class)
@RequirePOST
public static FormValidation doCheckBitbucketJenkinsRootUrl(@QueryParameter String value) {
checkPermission();
String url = Util.fixEmptyAndTrim(value);
if (url == null) {
return FormValidation.ok();
}
try {
new URL(url);
} catch (MalformedURLException e) {
return FormValidation.error("Invalid URL: " + e.getMessage());
}
return FormValidation.ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static BitbucketEndpoint registerEndpoint(@NonNull String name, @NonNull
if (BitbucketApiUtils.isCloud(serverURL)) {
endpoint = new BitbucketCloudEndpoint();
} else {
endpoint = new BitbucketServerEndpoint(name, serverURL, false, null, false, null);
endpoint = new BitbucketServerEndpoint(name, serverURL);
}
if (endpointCustomiser != null) {
endpoint = endpointCustomiser.apply(endpoint);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License
*
* Copyright (c) 2016-2018, Yieldlab AG
* Copyright (c) 2025, Falco Nikolas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,23 +21,31 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.jenkins.plugins.bitbucket.hooks;
package com.cloudbees.jenkins.plugins.bitbucket.api.webhook;

import hudson.RestrictedSince;
import java.util.logging.Level;
import java.util.logging.Logger;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.Describable;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.accmod.restrictions.Beta;

@Restricted(NoExternalUse.class)
@RestrictedSince("933.3.0")
public class NativeServerPingHookProcessor extends HookProcessor {
@Restricted(Beta.class)
public interface BitbucketWebhook extends Describable<BitbucketWebhook> {

private static final Logger LOGGER = Logger.getLogger(NativeServerPingHookProcessor.class.getName());
/**
* Name to use to describe the hook implementation.
*
* @return the name to use for the implementation
*/
@CheckForNull
String getDisplayName();

@Override
public void process(HookEventType hookEvent, String payload, BitbucketType instanceType, String origin) {
LOGGER.log(Level.INFO, "Received webhook ping event from {0}", origin);
}
/**
* The hook implementation identifier.
*
* @return the unique identifier for this implementation
*/
@NonNull
String getId();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* The MIT License
*
* Copyright (c) 2017, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.jenkins.plugins.bitbucket.api.webhook;

import hudson.model.Descriptor;

/**
* {@link Descriptor} for {@link BitbucketWebhook}s.
*
* @since 936.4.0
*/
public abstract class BitbucketWebhookDescriptor extends Descriptor<BitbucketWebhook> {
public abstract boolean isApplicable(String serverURL);
}
Loading
Loading