23
23
*/
24
24
package com .cloudbees .jenkins .plugins .bitbucket ;
25
25
26
+ import com .cloudbees .jenkins .plugins .bitbucket .BranchDiscoveryTrait .ExcludeOriginPRBranchesSCMHeadFilter ;
26
27
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketApi ;
27
28
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketBuildStatus ;
28
29
import com .cloudbees .jenkins .plugins .bitbucket .client .BitbucketCloudApiClient ;
@@ -97,7 +98,7 @@ static String checkURL(@NonNull String url, BitbucketApi bitbucket) {
97
98
}
98
99
99
100
private static void createStatus (@ NonNull Run <?, ?> build , @ NonNull TaskListener listener ,
100
- @ NonNull BitbucketApi bitbucket , @ NonNull String hash )
101
+ @ NonNull BitbucketApi bitbucket , @ NonNull String key , @ NonNull String hash )
101
102
throws IOException , InterruptedException {
102
103
103
104
String url ;
@@ -112,7 +113,6 @@ private static void createStatus(@NonNull Run<?, ?> build, @NonNull TaskListener
112
113
return ;
113
114
}
114
115
115
- String key = build .getParent ().getFullName (); // use the job full name as the key for the status
116
116
String name = build .getFullDisplayName (); // use the build number as the display name of the status
117
117
BitbucketBuildStatus status ;
118
118
Result result = build .getResult ();
@@ -153,24 +153,36 @@ private static void sendNotifications(Run<?, ?> build, TaskListener listener)
153
153
return ;
154
154
}
155
155
BitbucketSCMSource source = (BitbucketSCMSource ) s ;
156
- if ( new BitbucketSCMSourceContext (null , SCMHeadObserver . none ())
157
- . withTraits (source .getTraits ())
158
- .notificationsDisabled ()) {
156
+ BitbucketSCMSourceContext sourceContext = new BitbucketSCMSourceContext (null ,
157
+ SCMHeadObserver . none ()). withTraits (source .getTraits ());
158
+ if ( sourceContext .notificationsDisabled ()) {
159
159
return ;
160
160
}
161
161
SCMRevision r = SCMRevisionAction .getRevision (s , build );
162
+ if (r == null ) {
163
+ return ;
164
+ }
162
165
String hash = getHash (r );
163
166
if (hash == null ) {
164
167
return ;
165
168
}
169
+ boolean shareBuildKeyBetweenBranchAndPR = sourceContext
170
+ .filters ().stream ()
171
+ .anyMatch (filter -> filter instanceof ExcludeOriginPRBranchesSCMHeadFilter );
172
+
173
+ String key ;
174
+ BitbucketApi bitbucket ;
166
175
if (r instanceof PullRequestSCMRevision ) {
167
176
listener .getLogger ().println ("[Bitbucket] Notifying pull request build result" );
168
- createStatus (build , listener , source .buildBitbucketClient ((PullRequestSCMHead ) r .getHead ()), hash );
169
-
177
+ PullRequestSCMHead head = (PullRequestSCMHead ) r .getHead ();
178
+ key = getBuildKey (build , head .getOriginName (), shareBuildKeyBetweenBranchAndPR );
179
+ bitbucket = source .buildBitbucketClient (head );
170
180
} else {
171
181
listener .getLogger ().println ("[Bitbucket] Notifying commit build result" );
172
- createStatus (build , listener , source .buildBitbucketClient (), hash );
182
+ key = getBuildKey (build , r .getHead ().getName (), shareBuildKeyBetweenBranchAndPR );
183
+ bitbucket = source .buildBitbucketClient ();
173
184
}
185
+ createStatus (build , listener , bitbucket , key , hash );
174
186
}
175
187
176
188
@ CheckForNull
@@ -189,6 +201,25 @@ private static String getHash(@CheckForNull SCMRevision revision) {
189
201
return null ;
190
202
}
191
203
204
+ private static String getBuildKey (@ NonNull Run <?, ?> build , String branch ,
205
+ boolean shareBuildKeyBetweenBranchAndPR ) {
206
+
207
+ // When the ExcludeOriginPRBranchesSCMHeadFilter filter is active, we want the
208
+ // build status key to be the same between the branch project and the PR project.
209
+ // This is to avoid having two build statuses when a branch goes into PR and
210
+ // it was already built at least once as a branch.
211
+ // So the key we use is the branch name.
212
+ String key ;
213
+ if (shareBuildKeyBetweenBranchAndPR ) {
214
+ String folderName = build .getParent ().getParent ().getFullName ();
215
+ key = String .format ("%s/%s" , folderName , branch );
216
+ } else {
217
+ key = build .getParent ().getFullName (); // use the job full name as the key for the status
218
+ }
219
+
220
+ return key ;
221
+ }
222
+
192
223
/**
193
224
* Sends notifications to Bitbucket on Checkout (for the "In Progress" Status).
194
225
*/
0 commit comments