Skip to content

JENKINS-47867 Add WebhookConfigurationTrait #194

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

Merged
Merged
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
Original file line number Diff line number Diff line change
@@ -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<? extends SCMSourceContext> getContextClass() {
return BitbucketSCMSourceContext.class;
}

/**
* {@inheritDoc}
*/
@Override
public Class<? extends SCMSource> getSourceClass() {
return BitbucketSCMSource.class;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry title="${%Ignore committers}" field="committersToIgnore">
<f:textbox/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div>
<p>
Sets the value for committersToIgnore in the Bitbucket Webhook. Value should be a comma separated string.
</p>
<p>
committerToIgnore is used to prevent triggering Jenkins builds when commits by certain users
are made.
</p>
</div>
Original file line number Diff line number Diff line change
@@ -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");
}
}