Skip to content

Commit f751c61

Browse files
jglickbitwiseman
andauthored
Only add FirstCheckoutCompletedInvisibleAction if this build is using BitbucketSCMSource (#404)
* Only add FirstCheckoutCompletedInvisibleAction if this build is using BitbucketSCMSource * Added test to verify that FirstCheckoutCompletedInvisibleAction is not added to unrelated builds * Update BitbucketBuildStatusNotificationsTest.java The IDE says this cast isn't needed, but it is. * Fix imports * Fix import order Co-authored-by: Liam Newman <[email protected]>
1 parent 18e20c3 commit f751c61

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,19 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
147147
}
148148
}
149149

150-
private static void sendNotifications(Run<?, ?> build, TaskListener listener)
150+
private static @CheckForNull BitbucketSCMSource findBitbucketSCMSource(Run<?, ?> build) {
151+
SCMSource s = SCMSource.SourceByItem.findSource(build.getParent());
152+
return s instanceof BitbucketSCMSource ? (BitbucketSCMSource) s : null;
153+
}
154+
155+
private static void sendNotifications(BitbucketSCMSource source, Run<?, ?> build, TaskListener listener)
151156
throws IOException, InterruptedException {
152-
final SCMSource s = SCMSource.SourceByItem.findSource(build.getParent());
153-
if (!(s instanceof BitbucketSCMSource)) {
154-
return;
155-
}
156-
BitbucketSCMSource source = (BitbucketSCMSource) s;
157157
BitbucketSCMSourceContext sourceContext = new BitbucketSCMSourceContext(null,
158158
SCMHeadObserver.none()).withTraits(source.getTraits());
159159
if (sourceContext.notificationsDisabled()) {
160160
return;
161161
}
162-
SCMRevision r = SCMRevisionAction.getRevision(s, build);
162+
SCMRevision r = SCMRevisionAction.getRevision(source, build);
163163
if (r == null) {
164164
return;
165165
}
@@ -230,6 +230,10 @@ public static class JobCheckOutListener extends SCMListener {
230230
@Override
231231
public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListener listener, File changelogFile,
232232
SCMRevisionState pollingBaseline) throws Exception {
233+
BitbucketSCMSource source = findBitbucketSCMSource(build);
234+
if (source == null) {
235+
return;
236+
}
233237

234238
boolean hasCompletedCheckoutBefore =
235239
build.getAction(FirstCheckoutCompletedInvisibleAction.class) != null;
@@ -238,7 +242,7 @@ public void onCheckout(Run<?, ?> build, SCM scm, FilePath workspace, TaskListene
238242
build.addAction(new FirstCheckoutCompletedInvisibleAction());
239243

240244
try {
241-
sendNotifications(build, listener);
245+
sendNotifications(source, build, listener);
242246
} catch (IOException | InterruptedException e) {
243247
e.printStackTrace(listener.error("Could not send notifications"));
244248
}
@@ -254,8 +258,13 @@ public static class JobCompletedListener extends RunListener<Run<?, ?>> {
254258

255259
@Override
256260
public void onCompleted(Run<?, ?> build, TaskListener listener) {
261+
BitbucketSCMSource source = findBitbucketSCMSource(build);
262+
if (source == null) {
263+
return;
264+
}
265+
257266
try {
258-
sendNotifications(build, listener);
267+
sendNotifications(source, build, listener);
259268
} catch (IOException | InterruptedException e) {
260269
e.printStackTrace(listener.error("Could not send notifications"));
261270
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2020 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+
25+
package com.cloudbees.jenkins.plugins.bitbucket;
26+
27+
import hudson.model.Action;
28+
import hudson.model.FreeStyleBuild;
29+
import hudson.model.FreeStyleProject;
30+
import java.util.List;
31+
import org.junit.Rule;
32+
import org.junit.Test;
33+
import org.jvnet.hudson.test.JenkinsRule;
34+
import org.jvnet.hudson.test.SingleFileSCM;
35+
36+
import static org.hamcrest.MatcherAssert.assertThat;
37+
import static org.hamcrest.Matchers.hasItem;
38+
import static org.hamcrest.Matchers.instanceOf;
39+
import static org.hamcrest.Matchers.not;
40+
41+
public class BitbucketBuildStatusNotificationsTest {
42+
43+
@Rule
44+
public JenkinsRule r = new JenkinsRule();
45+
46+
// TODO test of effectiveness of FirstCheckoutCompletedInvisibleAction (#130)
47+
48+
@Test
49+
public void noInappropriateFirstCheckoutCompletedInvisibleAction() throws Exception {
50+
FreeStyleProject p = r.createFreeStyleProject();
51+
p.setScm(new SingleFileSCM("file", "contents"));
52+
FreeStyleBuild b = r.buildAndAssertSuccess(p);
53+
assertThat((List<Action>)b.getAllActions(), not(hasItem(instanceOf(FirstCheckoutCompletedInvisibleAction.class))));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)