Skip to content

Commit 789c794

Browse files
committed
find SCMSource even if NullSCM now associated to build
1 parent 3817449 commit 789c794

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import edu.umd.cs.findbugs.annotations.NonNull;
3131
import hudson.Extension;
3232
import hudson.FilePath;
33+
import hudson.model.ItemGroup;
3334
import hudson.model.Result;
3435
import hudson.model.Run;
3536
import hudson.model.TaskListener;
@@ -48,6 +49,9 @@
4849
import jenkins.scm.api.SCMRevision;
4950
import jenkins.scm.api.SCMRevisionAction;
5051
import jenkins.scm.api.SCMSource;
52+
import jenkins.scm.api.SCMSourceOwner;
53+
import jenkins.scm.impl.NullSCMSource;
54+
5155
import org.apache.commons.lang.StringUtils;
5256
import org.jenkinsci.plugins.displayurlapi.DisplayURLProvider;
5357

@@ -105,8 +109,8 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
105109
@NonNull BitbucketApi bitbucket, @NonNull String key, @NonNull String hash)
106110
throws IOException, InterruptedException {
107111

108-
final SCMSource s = SCMSource.SourceByItem.findSource(build.getParent());
109-
if (!(s instanceof BitbucketSCMSource)) {
112+
final BitbucketSCMSource source = findBitbucketSCMSource(build);
113+
if (source == null) {
110114
return;
111115
}
112116

@@ -135,7 +139,6 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
135139
} else if (Result.UNSTABLE.equals(result)) {
136140
statusDescription = StringUtils.defaultIfBlank(buildDescription, "This commit has test failures.");
137141

138-
BitbucketSCMSource source = (BitbucketSCMSource) s;
139142
BitbucketSCMSourceContext sourceContext = new BitbucketSCMSourceContext(null, SCMHeadObserver.none())
140143
.withTraits(source.getTraits());
141144
if (sourceContext.sendSuccessNotificationForUnstableBuild()) {
@@ -160,12 +163,19 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
160163
status = new BitbucketBuildStatus(hash, statusDescription, state, url, key, name);
161164
new BitbucketChangesetCommentNotifier(bitbucket).buildStatus(status);
162165
if (result != null) {
163-
listener.getLogger().println("[Bitbucket] Build result notified");
166+
listener.getLogger().println("[Bitbucket] Build result notified: " + status.getState());
164167
}
165168
}
166169

167170
private static @CheckForNull BitbucketSCMSource findBitbucketSCMSource(Run<?, ?> build) {
168171
SCMSource s = SCMSource.SourceByItem.findSource(build.getParent());
172+
if (s instanceof NullSCMSource) {
173+
// for instance PR merged on Bitbucket since the build has been started
174+
ItemGroup<?> grandFather = build.getParent().getParent();
175+
if (grandFather instanceof SCMSourceOwner) {
176+
s = ((SCMSourceOwner) grandFather).getSCMSources().get(0);
177+
}
178+
}
169179
return s instanceof BitbucketSCMSource ? (BitbucketSCMSource) s : null;
170180
}
171181

0 commit comments

Comments
 (0)