Skip to content

Commit e20e512

Browse files
committed
[JENKINS-74913] Allow extension point in bitbucket source plugin to provide a implementation for web-hooks management
new interface to apply a webhook configuration to the Bitbucket
1 parent ec1d7f5 commit e20e512

File tree

49 files changed

+1241
-1066
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1241
-1066
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMNavigator.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.UserRoleInRepository;
3434
import com.cloudbees.jenkins.plugins.bitbucket.impl.avatars.BitbucketTeamAvatarMetadataAction;
3535
import com.cloudbees.jenkins.plugins.bitbucket.impl.endpoint.BitbucketCloudEndpoint;
36-
import com.cloudbees.jenkins.plugins.bitbucket.impl.endpoint.BitbucketServerEndpoint;
3736
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketApiUtils;
3837
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketCredentialsUtils;
3938
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.MirrorListSupplier;
@@ -407,17 +406,6 @@ public static FormValidation doCheckCredentialsId(@AncestorInPath SCMSourceOwner
407406
return BitbucketCredentialsUtils.checkCredentialsId(context, value, serverURL);
408407
}
409408

410-
@RequirePOST
411-
public static FormValidation doCheckMirrorId(@QueryParameter String value,
412-
@QueryParameter(fixEmpty = true, value = "serverUrl") String serverURL) {
413-
if (!value.isEmpty()) {
414-
if (BitbucketServerEndpoint.supportsMirror(serverURL)) {
415-
return FormValidation.error("Mirror is not supported by the choosen webhooks");
416-
}
417-
}
418-
return FormValidation.ok();
419-
}
420-
421409
@RequirePOST
422410
public ListBoxModel doFillCredentialsIdItems(@AncestorInPath SCMSourceOwner context,
423411
@QueryParameter(fixEmpty = true, value = "serverUrl", required = true) String serverURL) {

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import com.cloudbees.jenkins.plugins.bitbucket.hooks.HasPullRequests;
4545
import com.cloudbees.jenkins.plugins.bitbucket.impl.avatars.BitbucketRepoAvatarMetadataAction;
4646
import com.cloudbees.jenkins.plugins.bitbucket.impl.endpoint.BitbucketCloudEndpoint;
47-
import com.cloudbees.jenkins.plugins.bitbucket.impl.endpoint.BitbucketServerEndpoint;
4847
import com.cloudbees.jenkins.plugins.bitbucket.impl.extension.BitbucketEnvVarExtension;
4948
import com.cloudbees.jenkins.plugins.bitbucket.impl.extension.GitClientAuthenticatorExtension;
5049
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketApiUtils;
@@ -293,14 +292,6 @@ public void setServerUrl(@CheckForNull String serverURL) {
293292
}
294293
}
295294

296-
@Restricted(NoExternalUse.class)
297-
@Deprecated(since = "936.4.0", forRemoval = true)
298-
// expose if needed in BitbucketEndpointProvider, normally could be get from endpoint if not customized
299-
@NonNull
300-
public String getEndpointJenkinsRootURL() {
301-
return BitbucketEndpointProvider.lookupEndpointJenkinsRootURL(serverUrl);
302-
}
303-
304295
@Override
305296
@NonNull
306297
public List<SCMSourceTrait> getTraits() {
@@ -1070,17 +1061,6 @@ public static FormValidation doCheckServerUrl(@AncestorInPath SCMSourceOwner con
10701061
return FormValidation.ok();
10711062
}
10721063

1073-
@RequirePOST
1074-
public static FormValidation doCheckMirrorId(@QueryParameter String value,
1075-
@QueryParameter(fixEmpty = true, value = "serverUrl") String serverURL) {
1076-
if (!value.isEmpty()) {
1077-
if (BitbucketServerEndpoint.supportsMirror(serverURL)) {
1078-
return FormValidation.error("Mirror can only be used with native webhooks");
1079-
}
1080-
}
1081-
return FormValidation.ok();
1082-
}
1083-
10841064
public boolean isServerUrlSelectable() {
10851065
return !BitbucketEndpointProvider.all().isEmpty();
10861066
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSourceContext.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
*/
2424
package com.cloudbees.jenkins.plugins.bitbucket;
2525

26-
import com.cloudbees.jenkins.plugins.bitbucket.hooks.WebhookConfiguration;
2726
import edu.umd.cs.findbugs.annotations.CheckForNull;
2827
import edu.umd.cs.findbugs.annotations.NonNull;
2928
import hudson.model.TaskListener;
@@ -76,11 +75,6 @@ public class BitbucketSCMSourceContext extends SCMSourceContext<BitbucketSCMSour
7675
*/
7776
@NonNull
7877
private WebhookRegistration webhookRegistration = WebhookRegistration.SYSTEM;
79-
/**
80-
* The {@link WebhookConfiguration} to use in this context.
81-
*/
82-
@NonNull
83-
private WebhookConfiguration webhookConfiguration = new WebhookConfiguration();
8478

8579
/**
8680
* {@code true} if notifications should be disabled in this context.
@@ -202,16 +196,6 @@ public final WebhookRegistration webhookRegistration() {
202196
return webhookRegistration;
203197
}
204198

205-
/**
206-
* Returns the {@link WebhookConfiguration} configuration.
207-
*
208-
* @return the {@link WebhookConfiguration} configuration.
209-
*/
210-
@NonNull
211-
public final WebhookConfiguration webhookConfiguration() {
212-
return webhookConfiguration;
213-
}
214-
215199
/**
216200
* Returns {@code true} if notifications should be disabled.
217201
*
@@ -341,18 +325,6 @@ public final BitbucketSCMSourceContext withForkPRStrategies(
341325
return this;
342326
}
343327

344-
/**
345-
* Defines the {@link WebhookRegistration} mode to use in this context.
346-
*
347-
* @param configuration the webhook configuration.
348-
* @return {@code this} for method chaining.
349-
*/
350-
@NonNull
351-
public final BitbucketSCMSourceContext webhookConfiguration(WebhookConfiguration configuration) {
352-
webhookConfiguration = configuration;
353-
return this;
354-
}
355-
356328
/**
357329
* Defines the {@link WebhookRegistration} mode to use in this context.
358330
*

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketApi.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
package com.cloudbees.jenkins.plugins.bitbucket.api;
2525

26-
import com.cloudbees.jenkins.plugins.bitbucket.api.webhook.BitbucketWebhook;
26+
import com.cloudbees.jenkins.plugins.bitbucket.api.webhook.BitbucketWebhookConfiguration;
2727
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.UserRoleInRepository;
2828
import com.cloudbees.jenkins.plugins.bitbucket.filesystem.BitbucketSCMFile;
2929
import edu.umd.cs.findbugs.annotations.CheckForNull;
@@ -193,7 +193,7 @@ boolean checkPathExists(@NonNull String branchOrHash, @NonNull String path)
193193
*
194194
* @param hook the webhook object
195195
* @throws IOException if there was a network communications error.
196-
* @deprecated Use the {@link BitbucketWebhook} implementation to gather
196+
* @deprecated Use the {@link BitbucketWebhookConfiguration} implementation to gather
197197
* information about webhook.
198198
*/
199199
@Deprecated(since = "937.0.0", forRemoval = true)
@@ -205,7 +205,7 @@ default void registerCommitWebHook(@NonNull BitbucketWebHook hook) throws IOExce
205205
*
206206
* @param hook the webhook object
207207
* @throws IOException if there was a network communications error.
208-
* @deprecated Use the {@link BitbucketWebhook} implementation to gather
208+
* @deprecated Use the {@link BitbucketWebhookConfiguration} implementation to gather
209209
* information about webhook.
210210
*/
211211
@Deprecated(since = "937.0.0", forRemoval = true)
@@ -217,7 +217,7 @@ default void updateCommitWebHook(@NonNull BitbucketWebHook hook) throws IOExcept
217217
*
218218
* @param hook the webhook object
219219
* @throws IOException if there was a network communications error.
220-
* @deprecated Use the {@link BitbucketWebhook} implementation to gather
220+
* @deprecated Use the {@link BitbucketWebhookConfiguration} implementation to gather
221221
* information about webhook.
222222
*/
223223
@Deprecated(since = "937.0.0", forRemoval = true)
@@ -229,7 +229,7 @@ default void removeCommitWebHook(@NonNull BitbucketWebHook hook) throws IOExcept
229229
*
230230
* @return the list of webhooks registered in the repository.
231231
* @throws IOException if there was a network communications error.
232-
* @deprecated Use the {@link BitbucketWebhook} implementation to gather
232+
* @deprecated Use the {@link BitbucketWebhookConfiguration} implementation to gather
233233
* information about webhook.
234234
*/
235235
@Deprecated(since = "937.0.0", forRemoval = true)

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/endpoint/BitbucketEndpoint.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
package com.cloudbees.jenkins.plugins.bitbucket.api.endpoint;
2525

2626
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource;
27-
import com.cloudbees.jenkins.plugins.bitbucket.api.webhook.BitbucketWebhook;
27+
import com.cloudbees.jenkins.plugins.bitbucket.api.webhook.BitbucketWebhookConfiguration;
2828
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketCredentialsUtils;
2929
import com.cloudbees.plugins.credentials.common.StandardCredentials;
3030
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
@@ -80,11 +80,11 @@ public interface BitbucketEndpoint extends Describable<BitbucketEndpoint> {
8080
/**
8181
* Returns the webhook implementation that this endpoint is using to manage the incoming payload.
8282
*
83-
* @return the {@link BitbucketWebhook} implementation selected for this endpoint.
83+
* @return the {@link BitbucketWebhookConfiguration} implementation selected for this endpoint.
8484
* @since 937.0.0
8585
*/
8686
@NonNull
87-
BitbucketWebhook getWebhook();
87+
BitbucketWebhookConfiguration getWebhook();
8888

8989
/**
9090
* Returns {@code true} if and only if Jenkins is supposed to auto-manage

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/endpoint/BitbucketEndpointProvider.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*/
2424
package com.cloudbees.jenkins.plugins.bitbucket.api.endpoint;
2525

26-
import com.cloudbees.jenkins.plugins.bitbucket.api.webhook.BitbucketWebhook;
26+
import com.cloudbees.jenkins.plugins.bitbucket.api.webhook.BitbucketWebhookConfiguration;
2727
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketEndpointConfiguration;
2828
import com.cloudbees.jenkins.plugins.bitbucket.impl.endpoint.BitbucketCloudEndpoint;
2929
import com.cloudbees.jenkins.plugins.bitbucket.impl.endpoint.BitbucketServerEndpoint;
@@ -104,14 +104,15 @@ public static <T extends BitbucketEndpoint> Optional<T> lookupEndpoint(@CheckFor
104104
*
105105
* @param serverURL the server url to check.
106106
* @return the verbatim setting provided by endpoint configuration
107-
* @deprecated This is a value specific for of {@link BitbucketWebhook}
107+
* @deprecated This value depends on the underline {@link BitbucketWebhookConfiguration} configured for the endpoint.
108108
*/
109109
@Deprecated(since = "937.0.0", forRemoval = true)
110110
@NonNull
111111
public static String lookupEndpointJenkinsRootURL(@CheckForNull String serverURL) {
112-
return lookupEndpoint(serverURL)
112+
String jenkinsURL = lookupEndpoint(serverURL)
113113
.map(BitbucketEndpoint::getEndpointJenkinsRootURL)
114-
.orElse(Util.ensureEndsWith(URLUtils.normalizeURL(Util.fixEmptyAndTrim(DisplayURLProvider.get().getRoot())), "/"));
114+
.orElse(URLUtils.normalizeURL(Util.fixEmptyAndTrim(DisplayURLProvider.get().getRoot())));
115+
return Util.ensureEndsWith(jenkinsURL, "/");
115116
}
116117

117118
/**

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/webhook/BitbucketWebhookClient.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import edu.umd.cs.findbugs.annotations.NonNull;
55
import java.io.IOException;
66

7-
public interface BitbucketWebhookClient {
7+
public interface BitbucketWebhookClient extends AutoCloseable {
88

99
String post(@NonNull String path, @CheckForNull String payload) throws IOException ;
1010

@@ -14,4 +14,7 @@ public interface BitbucketWebhookClient {
1414

1515
@NonNull
1616
String get(@NonNull String path) throws IOException ;
17+
18+
@Override
19+
void close() throws IOException;
1720
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/webhook/BitbucketWebhook.java renamed to src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/webhook/BitbucketWebhookConfiguration.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@
2323
*/
2424
package com.cloudbees.jenkins.plugins.bitbucket.api.webhook;
2525

26-
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource;
27-
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketWebHook;
2826
import com.cloudbees.jenkins.plugins.bitbucket.api.endpoint.BitbucketEndpoint;
29-
import com.cloudbees.jenkins.plugins.bitbucket.hooks.WebhookConfiguration;
3027
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
3128
import edu.umd.cs.findbugs.annotations.CheckForNull;
3229
import edu.umd.cs.findbugs.annotations.NonNull;
3330
import hudson.model.Describable;
34-
import java.util.Collection;
3531
import jenkins.model.Jenkins;
3632

3733
/**
@@ -40,7 +36,7 @@
4036
*
4137
* @since 937.0.0
4238
*/
43-
public interface BitbucketWebhook extends Describable<BitbucketWebhook> {
39+
public interface BitbucketWebhookConfiguration extends Describable<BitbucketWebhookConfiguration> {
4440

4541
/**
4642
* Name to use to describe the hook implementation.
@@ -77,6 +73,25 @@ public interface BitbucketWebhook extends Describable<BitbucketWebhook> {
7773
@CheckForNull
7874
String getCredentialsId();
7975

76+
/**
77+
* A custom Jenkins root URL to be used by the webhook implementation as
78+
* part of the receiver callback endpoint.
79+
* <p>
80+
* The returned value must ensure that ends with a slash {@code /}
81+
*
82+
* @return the verbatim setting provided by endpoint configuration
83+
*/
84+
@CheckForNull
85+
String getEndpointJenkinsRootURL();
86+
87+
/**
88+
* Returns the implementation that is in charge to apply this configuration to the Bitbucket.
89+
* @param <T> the specific BitbucketWebhookIntegration subtype.
90+
* @return a new instance of the integration, never return a singleton instance.
91+
* FIXME think if returns class and in case instantiate via reflection and apply this configuration to ensure NO singleton.
92+
*/
93+
<T extends BitbucketWebhookIntegration> T getIntegration();
94+
8095
/**
8196
* @see Describable#getDescriptor()
8297
*/
@@ -85,15 +100,4 @@ default BitbucketWebhookDescriptor getDescriptor() {
85100
return (BitbucketWebhookDescriptor) Jenkins.get().getDescriptorOrDie(getClass());
86101
}
87102

88-
@NonNull
89-
<T extends BitbucketWebHook> Collection<T> retrieveHooks(@NonNull String serverURL, @NonNull BitbucketWebhookClient client);
90-
<T extends BitbucketWebHook> void registerHook(@NonNull T payload, @NonNull BitbucketWebhookClient client);
91-
<T extends BitbucketWebHook> void updateHook(@NonNull T payload, @NonNull BitbucketWebhookClient client);
92-
void removeHook(@NonNull BitbucketWebHook payload, @NonNull BitbucketWebhookClient client);
93-
94-
@NonNull
95-
<T extends BitbucketWebHook> T buildPayload(@NonNull WebhookConfiguration hookConfig, @NonNull BitbucketSCMSource source);
96-
<T extends BitbucketWebHook> boolean shouldUpdate(@NonNull T existing, @NonNull T newOne);
97-
98-
99103
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/webhook/BitbucketWebhookDescriptor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
import hudson.model.Descriptor;
2929

3030
/**
31-
* {@link Descriptor} for {@link BitbucketWebhook}s.
31+
* {@link Descriptor} for {@link BitbucketWebhookConfiguration}s.
3232
*
3333
* @since 937.0.0
3434
*/
35-
public abstract class BitbucketWebhookDescriptor extends Descriptor<BitbucketWebhook> {
35+
public abstract class BitbucketWebhookDescriptor extends Descriptor<BitbucketWebhookConfiguration> {
3636

3737
/**
3838
* Returns if this implementation can supports and can be installed by the
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.cloudbees.jenkins.plugins.bitbucket.api.webhook;
2+
3+
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketWebHook;
4+
import edu.umd.cs.findbugs.annotations.NonNull;
5+
import hudson.ExtensionPoint;
6+
import java.io.IOException;
7+
import java.util.Collection;
8+
import jenkins.scm.api.trait.SCMSourceTrait;
9+
10+
public interface BitbucketWebhookIntegration extends ExtensionPoint {
11+
12+
void setRepositoryOwner(@NonNull String repositoryOwner);
13+
void setRepositoryName(@NonNull String repositoryName);
14+
void setServerURL(@NonNull String serverURL);
15+
void setCallbackURL(@NonNull String callbackURL);
16+
17+
Collection<Class<? extends SCMSourceTrait>> supportedTraits();
18+
void apply(SCMSourceTrait trait);
19+
20+
Collection<BitbucketWebHook> retrieve(@NonNull BitbucketWebhookClient client) throws IOException;
21+
void register(@NonNull BitbucketWebhookClient client) throws IOException;
22+
void remove(@NonNull BitbucketWebHook payload, @NonNull BitbucketWebhookClient client) throws IOException;
23+
}

0 commit comments

Comments
 (0)