diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTrait.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTrait.java new file mode 100644 index 000000000..deeab2063 --- /dev/null +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTrait.java @@ -0,0 +1,114 @@ +/* + * 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; + +import com.cloudbees.jenkins.plugins.bitbucket.hooks.WebhookConfiguration; +import edu.umd.cs.findbugs.annotations.NonNull; +import hudson.Extension; +import jenkins.scm.api.SCMSource; +import jenkins.scm.api.trait.SCMSourceContext; +import jenkins.scm.api.trait.SCMSourceTrait; +import jenkins.scm.api.trait.SCMSourceTraitDescriptor; +import org.kohsuke.stapler.DataBoundConstructor; + +/** + * A {@link SCMSourceTrait} for {@link BitbucketSCMSource} that sets the committersToIgnore + * setting in {@link WebhookConfiguration}. + * + * @since 2.4.5 + */ +public class WebhookConfigurationTrait extends SCMSourceTrait { + + /** + * The committers that should be ignored in the webhook. A comma separated string. + */ + @NonNull + private final String committersToIgnore; + + /** + * Constructor. + * + * @param committersToIgnore a string of comma separated Bitbucket usernames to ignore + */ + @DataBoundConstructor + public WebhookConfigurationTrait(@NonNull String committersToIgnore) { + this.committersToIgnore = committersToIgnore; + } + + /** + * @return the committers to ignore + */ + public String getCommittersToIgnore() { + return this.committersToIgnore; + } + + /** + * Gets the WebhookConfiguration to apply. + * + * @return the WebhookConfiguration to apply. + */ + @NonNull + public final WebhookConfiguration getWebhookConfiguration() { + return new WebhookConfiguration(this.committersToIgnore); + } + + /** + * {@inheritDoc} + */ + @Override + protected void decorateContext(SCMSourceContext context) { + ((BitbucketSCMSourceContext) context).webhookConfiguration(getWebhookConfiguration()); + } + + /** + * Our constructor. + */ + @Extension + public static class DescriptorImpl extends SCMSourceTraitDescriptor { + + /** + * {@inheritDoc} + */ + @Override + public String getDisplayName() { + return Messages.WebhookConfigurationTrait_displayName(); + } + + /** + * {@inheritDoc} + */ + @Override + public Class getContextClass() { + return BitbucketSCMSourceContext.class; + } + + /** + * {@inheritDoc} + */ + @Override + public Class getSourceClass() { + return BitbucketSCMSource.class; + } + } +} diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/WebhookConfiguration.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/WebhookConfiguration.java index d05291dd0..4bc716383 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/WebhookConfiguration.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/WebhookConfiguration.java @@ -84,6 +84,10 @@ public WebhookConfiguration(@CheckForNull final String committersToIgnore) { this.committersToIgnore = committersToIgnore; } + public String getCommittersToIgnore() { + return this.committersToIgnore; + } + boolean updateHook(BitbucketWebHook hook, BitbucketSCMSource owner) { if (hook instanceof BitbucketRepositoryHook) { if (!hook.getEvents().containsAll(CLOUD_EVENTS)) { diff --git a/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/Messages.properties b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/Messages.properties index 640da9227..76e8c4261 100644 --- a/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/Messages.properties +++ b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/Messages.properties @@ -36,6 +36,7 @@ SSHCheckoutTrait.useAgentKey=- use build agent''s key - WebhookRegistrationTrait.disableHook=Disable hook management WebhookRegistrationTrait.displayName=Override hook management WebhookRegistrationTrait.useItemHook=Use item credentials for hook management +WebhookConfigurationTrait.displayName=Set ignored committers PullRequestSCMHead.Pronoun=Pull Request BranchSCMHead.Pronoun=Branch BitBucketTagSCMHead.Pronoun=Tag diff --git a/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTrait/config.jelly b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTrait/config.jelly new file mode 100644 index 000000000..67cc8b924 --- /dev/null +++ b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTrait/config.jelly @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTrait/help.html b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTrait/help.html new file mode 100644 index 000000000..7b08c5800 --- /dev/null +++ b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTrait/help.html @@ -0,0 +1,9 @@ +
+

+ Sets the value for committersToIgnore in the Bitbucket Webhook. Value should be a comma separated string. +

+

+ committerToIgnore is used to prevent triggering Jenkins builds when commits by certain users + are made. +

+
diff --git a/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTraitTest.java b/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTraitTest.java new file mode 100644 index 000000000..d8c08e722 --- /dev/null +++ b/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/WebhookConfigurationTraitTest.java @@ -0,0 +1,41 @@ +package com.cloudbees.jenkins.plugins.bitbucket; + +import jenkins.scm.api.SCMHeadObserver; +import org.junit.ClassRule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +import static org.junit.Assert.assertEquals; + +public class WebhookConfigurationTraitTest { + @ClassRule + public static JenkinsRule j = new JenkinsRule(); + + /** + * Test the default empty value + * @throws Exception + */ + @Test + public void ignoredCommittersDefault() + throws Exception { + BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none()); + assertEquals(ctx.webhookConfiguration().getCommittersToIgnore(), null); + WebhookConfigurationTrait instance = new WebhookConfigurationTrait(""); + instance.decorateContext(ctx); + assertEquals(ctx.webhookConfiguration().getCommittersToIgnore(), ""); + } + + /** + * Test a set value + * @throws Exception + */ + @Test + public void ignoredCommittersWithValue() + throws Exception { + BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none()); + assertEquals(ctx.webhookConfiguration().getCommittersToIgnore(), null); + WebhookConfigurationTrait instance = new WebhookConfigurationTrait("jenkins"); + instance.decorateContext(ctx); + assertEquals(ctx.webhookConfiguration().getCommittersToIgnore(), "jenkins"); + } +}