30
30
import edu .umd .cs .findbugs .annotations .NonNull ;
31
31
import hudson .Extension ;
32
32
import hudson .FilePath ;
33
+ import hudson .model .ItemGroup ;
33
34
import hudson .model .Result ;
34
35
import hudson .model .Run ;
35
36
import hudson .model .TaskListener ;
48
49
import jenkins .scm .api .SCMRevision ;
49
50
import jenkins .scm .api .SCMRevisionAction ;
50
51
import jenkins .scm .api .SCMSource ;
52
+ import jenkins .scm .api .SCMSourceOwner ;
53
+ import jenkins .scm .impl .NullSCMSource ;
51
54
import org .apache .commons .lang .StringUtils ;
52
55
import org .jenkinsci .plugins .displayurlapi .DisplayURLProvider ;
53
56
@@ -105,8 +108,8 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
105
108
@ NonNull BitbucketApi bitbucket , @ NonNull String key , @ NonNull String hash )
106
109
throws IOException , InterruptedException {
107
110
108
- final SCMSource s = SCMSource . SourceByItem . findSource (build . getParent () );
109
- if (!( s instanceof BitbucketSCMSource ) ) {
111
+ final BitbucketSCMSource source = findBitbucketSCMSource (build );
112
+ if (source == null ) {
110
113
return ;
111
114
}
112
115
@@ -135,7 +138,6 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
135
138
} else if (Result .UNSTABLE .equals (result )) {
136
139
statusDescription = StringUtils .defaultIfBlank (buildDescription , "This commit has test failures." );
137
140
138
- BitbucketSCMSource source = (BitbucketSCMSource ) s ;
139
141
BitbucketSCMSourceContext sourceContext = new BitbucketSCMSourceContext (null , SCMHeadObserver .none ())
140
142
.withTraits (source .getTraits ());
141
143
if (sourceContext .sendSuccessNotificationForUnstableBuild ()) {
@@ -160,12 +162,19 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
160
162
status = new BitbucketBuildStatus (hash , statusDescription , state , url , key , name );
161
163
new BitbucketChangesetCommentNotifier (bitbucket ).buildStatus (status );
162
164
if (result != null ) {
163
- listener .getLogger ().println ("[Bitbucket] Build result notified" );
165
+ listener .getLogger ().println ("[Bitbucket] Build result notified: " + status . getState () );
164
166
}
165
167
}
166
168
167
169
private static @ CheckForNull BitbucketSCMSource findBitbucketSCMSource (Run <?, ?> build ) {
168
170
SCMSource s = SCMSource .SourceByItem .findSource (build .getParent ());
171
+ if (s instanceof NullSCMSource ) {
172
+ // for instance PR merged on Bitbucket since the build has been started
173
+ ItemGroup <?> grandFather = build .getParent ().getParent ();
174
+ if (grandFather instanceof SCMSourceOwner ) {
175
+ s = ((SCMSourceOwner ) grandFather ).getSCMSources ().get (0 );
176
+ }
177
+ }
169
178
return s instanceof BitbucketSCMSource ? (BitbucketSCMSource ) s : null ;
170
179
}
171
180
0 commit comments