27
27
import com .cloudbees .jenkins .plugins .bitbucket .BitbucketMockApiFactory ;
28
28
import com .cloudbees .jenkins .plugins .bitbucket .BitbucketSCMSource ;
29
29
import com .cloudbees .jenkins .plugins .bitbucket .BranchSCMHead ;
30
+ import com .cloudbees .jenkins .plugins .bitbucket .ForkPullRequestDiscoveryTrait ;
31
+ import com .cloudbees .jenkins .plugins .bitbucket .ForkPullRequestDiscoveryTrait .TrustEveryone ;
32
+ import com .cloudbees .jenkins .plugins .bitbucket .PullRequestSCMHead ;
33
+ import com .cloudbees .jenkins .plugins .bitbucket .PullRequestSCMRevision ;
30
34
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketApi ;
31
35
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketBuildStatus ;
32
36
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketBuildStatus .Status ;
54
58
import jenkins .model .JenkinsLocationConfiguration ;
55
59
import jenkins .plugins .git .AbstractGitSCMSource .SCMRevisionImpl ;
56
60
import jenkins .scm .api .SCMHead ;
61
+ import jenkins .scm .api .SCMHeadOrigin .Fork ;
57
62
import jenkins .scm .api .SCMRevision ;
58
63
import jenkins .scm .api .SCMRevisionAction ;
64
+ import jenkins .scm .api .mixin .ChangeRequestCheckoutStrategy ;
59
65
import jenkins .scm .api .trait .SCMSourceTrait ;
66
+ import org .apache .commons .codec .digest .DigestUtils ;
60
67
import org .jenkinsci .plugins .workflow .job .WorkflowJob ;
61
68
import org .jenkinsci .plugins .workflow .job .WorkflowRun ;
62
69
import org .jenkinsci .plugins .workflow .multibranch .WorkflowMultiBranchProject ;
63
70
import org .junit .jupiter .api .Test ;
64
71
import org .junit .jupiter .params .ParameterizedTest ;
65
72
import org .junit .jupiter .params .provider .Arguments ;
66
73
import org .junit .jupiter .params .provider .MethodSource ;
74
+ import org .jvnet .hudson .test .Issue ;
67
75
import org .jvnet .hudson .test .JenkinsRule ;
68
76
import org .jvnet .hudson .test .junit .jupiter .WithJenkins ;
69
77
import org .mockito .ArgumentCaptor ;
@@ -107,8 +115,9 @@ void test_status_notification_for_given_build_result(UnaryOperator<BitbucketBuil
107
115
assertThat (captor .getValue ().getState ()).isEqualTo (expectedStatus .name ());
108
116
}
109
117
118
+ @ Issue ("JENKINS-72780" )
110
119
@ Test
111
- void test_status_notification_for_given_build_result (@ NonNull JenkinsRule r ) throws Exception {
120
+ void test_status_notification_name_when_UseReadableNotificationIds_is_true (@ NonNull JenkinsRule r ) throws Exception {
112
121
StreamBuildListener taskListener = new StreamBuildListener (System .out , StandardCharsets .UTF_8 );
113
122
URL jenkinsURL = new URL ("http://example.com:" + r .getURL ().getPort () + r .contextPath + "/" );
114
123
JenkinsLocationConfiguration .get ().setUrl (jenkinsURL .toString ());
@@ -133,6 +142,41 @@ void test_status_notification_for_given_build_result(@NonNull JenkinsRule r) thr
133
142
assertThat (captor .getValue ().getKey ()).isEqualTo ("P/BRANCH-JOB" );
134
143
}
135
144
145
+ @ Issue ("JENKINS-74970" )
146
+ @ Test
147
+ void test_status_notification_on_fork (@ NonNull JenkinsRule r ) throws Exception {
148
+ StreamBuildListener taskListener = new StreamBuildListener (System .out , StandardCharsets .UTF_8 );
149
+ URL jenkinsURL = new URL ("http://example.com:" + r .getURL ().getPort () + r .contextPath + "/" );
150
+ JenkinsLocationConfiguration .get ().setUrl (jenkinsURL .toString ());
151
+
152
+ String serverURL = "https://acme.bitbucket.org" ;
153
+
154
+ ForkPullRequestDiscoveryTrait trait = new ForkPullRequestDiscoveryTrait (2 , new TrustEveryone ());
155
+ BranchSCMHead targetHead = new BranchSCMHead ("master" );
156
+ PullRequestSCMHead scmHead = new PullRequestSCMHead ("name" , "repoOwner" , "repository1" , "feature1" , "1" , "title" , targetHead , new Fork ("repository1" ), ChangeRequestCheckoutStrategy .HEAD );
157
+ SCMRevisionImpl prRevision = new SCMRevisionImpl (scmHead , "cff417db" );
158
+ SCMRevisionImpl targetRevision = new SCMRevisionImpl (targetHead , "c341232342311" );
159
+ SCMRevision scmRevision = new PullRequestSCMRevision (scmHead , targetRevision , prRevision );
160
+ WorkflowRun build = prepareBuildForNotification (r , trait , serverURL , scmRevision );
161
+ doReturn (Result .SUCCESS ).when (build ).getResult ();
162
+
163
+ FilePath workspace = r .jenkins .getWorkspaceFor (build .getParent ());
164
+
165
+ BitbucketApi apiClient = mock (BitbucketServerAPIClient .class );
166
+ BitbucketMockApiFactory .add (serverURL , apiClient );
167
+
168
+ JobCheckoutListener listener = new JobCheckoutListener ();
169
+ listener .onCheckout (build , null , workspace , taskListener , null , SCMRevisionState .NONE );
170
+
171
+ ArgumentCaptor <BitbucketBuildStatus > captor = ArgumentCaptor .forClass (BitbucketBuildStatus .class );
172
+ verify (apiClient ).postBuildStatus (captor .capture ());
173
+ assertThat (captor .getValue ()).satisfies (status -> {
174
+ assertThat (status .getHash ()).isEqualTo (prRevision .getHash ());
175
+ assertThat (status .getKey ()).isEqualTo (DigestUtils .md5Hex ("p/branch-job" ));
176
+ assertThat (status .getRefname ()).isEqualTo ("refs/heads/" + scmHead .getBranchName ());
177
+ });
178
+ }
179
+
136
180
private static Stream <Arguments > buildStatusProvider () {
137
181
UnaryOperator <BitbucketBuildStatusNotificationsTrait > notifyAbortAsCancelled = t -> {
138
182
t .setSendStoppedNotificationForAbortBuild (true );
@@ -156,6 +200,12 @@ private static Stream<Arguments> buildStatusProvider() {
156
200
}
157
201
158
202
private WorkflowRun prepareBuildForNotification (@ NonNull JenkinsRule r , @ NonNull SCMSourceTrait trait , @ NonNull String serverURL ) throws Exception {
203
+ SCMHead scmHead = new BranchSCMHead ("master" );
204
+ SCMRevision scmRevision = new SCMRevisionImpl (scmHead , "c341232342311" );
205
+ return prepareBuildForNotification (r , trait , serverURL , scmRevision );
206
+ }
207
+
208
+ private WorkflowRun prepareBuildForNotification (@ NonNull JenkinsRule r , @ NonNull SCMSourceTrait trait , @ NonNull String serverURL , SCMRevision scmRevision ) throws Exception {
159
209
BitbucketSCMSource scmSource = new BitbucketSCMSource ("repoOwner" , "repository" );
160
210
scmSource .setServerUrl (serverURL );
161
211
scmSource .setTraits (List .of (trait ));
@@ -166,8 +216,6 @@ private WorkflowRun prepareBuildForNotification(@NonNull JenkinsRule r, @NonNull
166
216
167
217
WorkflowJob job = new WorkflowJob (project , "branch-job" );
168
218
169
- SCMHead scmHead = new BranchSCMHead ("master" );
170
- SCMRevision scmRevision = new SCMRevisionImpl (scmHead , "c341232342311" );
171
219
SCM scm = mock (SCM .class );
172
220
173
221
WorkflowRun build = mock (WorkflowRun .class );
@@ -178,7 +226,7 @@ private WorkflowRun prepareBuildForNotification(@NonNull JenkinsRule r, @NonNull
178
226
BranchProjectFactory <WorkflowJob , WorkflowRun > projectFactory = mock (BranchProjectFactory .class );
179
227
when (projectFactory .isProject (job )).thenReturn (true );
180
228
when (projectFactory .asProject (job )).thenReturn (job );
181
- Branch branch = new Branch (scmSource .getId (), scmHead , scm , Collections .emptyList ());
229
+ Branch branch = new Branch (scmSource .getId (), scmRevision . getHead () , scm , Collections .emptyList ());
182
230
when (projectFactory .getBranch (job )).thenReturn (branch );
183
231
project .setProjectFactory (projectFactory );
184
232
0 commit comments