Skip to content

Commit a281d68

Browse files
redfivejetersen
authored andcommitted
JENKINS-47867 Add WebhookConfigurationTrait (#194)
1 parent eeb7c0a commit a281d68

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2017, CloudBees, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.cloudbees.jenkins.plugins.bitbucket;
25+
26+
import com.cloudbees.jenkins.plugins.bitbucket.hooks.WebhookConfiguration;
27+
import edu.umd.cs.findbugs.annotations.NonNull;
28+
import hudson.Extension;
29+
import jenkins.scm.api.SCMSource;
30+
import jenkins.scm.api.trait.SCMSourceContext;
31+
import jenkins.scm.api.trait.SCMSourceTrait;
32+
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
33+
import org.kohsuke.stapler.DataBoundConstructor;
34+
35+
/**
36+
* A {@link SCMSourceTrait} for {@link BitbucketSCMSource} that sets the committersToIgnore
37+
* setting in {@link WebhookConfiguration}.
38+
*
39+
* @since 2.4.5
40+
*/
41+
public class WebhookConfigurationTrait extends SCMSourceTrait {
42+
43+
/**
44+
* The committers that should be ignored in the webhook. A comma separated string.
45+
*/
46+
@NonNull
47+
private final String committersToIgnore;
48+
49+
/**
50+
* Constructor.
51+
*
52+
* @param committersToIgnore a string of comma separated Bitbucket usernames to ignore
53+
*/
54+
@DataBoundConstructor
55+
public WebhookConfigurationTrait(@NonNull String committersToIgnore) {
56+
this.committersToIgnore = committersToIgnore;
57+
}
58+
59+
/**
60+
* @return the committers to ignore
61+
*/
62+
public String getCommittersToIgnore() {
63+
return this.committersToIgnore;
64+
}
65+
66+
/**
67+
* Gets the WebhookConfiguration to apply.
68+
*
69+
* @return the WebhookConfiguration to apply.
70+
*/
71+
@NonNull
72+
public final WebhookConfiguration getWebhookConfiguration() {
73+
return new WebhookConfiguration(this.committersToIgnore);
74+
}
75+
76+
/**
77+
* {@inheritDoc}
78+
*/
79+
@Override
80+
protected void decorateContext(SCMSourceContext<?, ?> context) {
81+
((BitbucketSCMSourceContext) context).webhookConfiguration(getWebhookConfiguration());
82+
}
83+
84+
/**
85+
* Our constructor.
86+
*/
87+
@Extension
88+
public static class DescriptorImpl extends SCMSourceTraitDescriptor {
89+
90+
/**
91+
* {@inheritDoc}
92+
*/
93+
@Override
94+
public String getDisplayName() {
95+
return Messages.WebhookConfigurationTrait_displayName();
96+
}
97+
98+
/**
99+
* {@inheritDoc}
100+
*/
101+
@Override
102+
public Class<? extends SCMSourceContext> getContextClass() {
103+
return BitbucketSCMSourceContext.class;
104+
}
105+
106+
/**
107+
* {@inheritDoc}
108+
*/
109+
@Override
110+
public Class<? extends SCMSource> getSourceClass() {
111+
return BitbucketSCMSource.class;
112+
}
113+
}
114+
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/WebhookConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public WebhookConfiguration(@CheckForNull final String committersToIgnore) {
8484
this.committersToIgnore = committersToIgnore;
8585
}
8686

87+
public String getCommittersToIgnore() {
88+
return this.committersToIgnore;
89+
}
90+
8791
boolean updateHook(BitbucketWebHook hook, BitbucketSCMSource owner) {
8892
if (hook instanceof BitbucketRepositoryHook) {
8993
if (!hook.getEvents().containsAll(CLOUD_EVENTS)) {

src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/Messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ SSHCheckoutTrait.useAgentKey=- use build agent''s key -
3636
WebhookRegistrationTrait.disableHook=Disable hook management
3737
WebhookRegistrationTrait.displayName=Override hook management
3838
WebhookRegistrationTrait.useItemHook=Use item credentials for hook management
39+
WebhookConfigurationTrait.displayName=Set ignored committers
3940
PullRequestSCMHead.Pronoun=Pull Request
4041
BranchSCMHead.Pronoun=Branch
4142
BitBucketTagSCMHead.Pronoun=Tag
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?jelly escape-by-default='true'?>
2+
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
3+
<f:entry title="${%Ignore committers}" field="committersToIgnore">
4+
<f:textbox/>
5+
</f:entry>
6+
</j:jelly>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div>
2+
<p>
3+
Sets the value for committersToIgnore in the Bitbucket Webhook. Value should be a comma separated string.
4+
</p>
5+
<p>
6+
committerToIgnore is used to prevent triggering Jenkins builds when commits by certain users
7+
are made.
8+
</p>
9+
</div>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.cloudbees.jenkins.plugins.bitbucket;
2+
3+
import jenkins.scm.api.SCMHeadObserver;
4+
import org.junit.ClassRule;
5+
import org.junit.Test;
6+
import org.jvnet.hudson.test.JenkinsRule;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class WebhookConfigurationTraitTest {
11+
@ClassRule
12+
public static JenkinsRule j = new JenkinsRule();
13+
14+
/**
15+
* Test the default empty value
16+
* @throws Exception
17+
*/
18+
@Test
19+
public void ignoredCommittersDefault()
20+
throws Exception {
21+
BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none());
22+
assertEquals(ctx.webhookConfiguration().getCommittersToIgnore(), null);
23+
WebhookConfigurationTrait instance = new WebhookConfigurationTrait("");
24+
instance.decorateContext(ctx);
25+
assertEquals(ctx.webhookConfiguration().getCommittersToIgnore(), "");
26+
}
27+
28+
/**
29+
* Test a set value
30+
* @throws Exception
31+
*/
32+
@Test
33+
public void ignoredCommittersWithValue()
34+
throws Exception {
35+
BitbucketSCMSourceContext ctx = new BitbucketSCMSourceContext(null, SCMHeadObserver.none());
36+
assertEquals(ctx.webhookConfiguration().getCommittersToIgnore(), null);
37+
WebhookConfigurationTrait instance = new WebhookConfigurationTrait("jenkins");
38+
instance.decorateContext(ctx);
39+
assertEquals(ctx.webhookConfiguration().getCommittersToIgnore(), "jenkins");
40+
}
41+
}

0 commit comments

Comments
 (0)