28
28
import com .cloudbees .jenkins .plugins .bitbucket .api .endpoint .BitbucketEndpoint ;
29
29
import com .cloudbees .jenkins .plugins .bitbucket .api .endpoint .BitbucketEndpointProvider ;
30
30
import com .cloudbees .jenkins .plugins .bitbucket .client .repository .BitbucketCloudHook ;
31
+ import com .cloudbees .jenkins .plugins .bitbucket .endpoints .AbstractBitbucketEndpoint ;
31
32
import com .cloudbees .jenkins .plugins .bitbucket .endpoints .BitbucketServerEndpoint ;
32
33
import com .cloudbees .jenkins .plugins .bitbucket .impl .util .BitbucketApiUtils ;
33
34
import com .cloudbees .jenkins .plugins .bitbucket .server .client .repository .BitbucketPluginWebhook ;
@@ -121,30 +122,59 @@ public String getCommittersToIgnore() {
121
122
boolean updateHook (BitbucketWebHook hook , BitbucketSCMSource owner ) {
122
123
boolean updated = false ;
123
124
125
+ final String serverURL = owner .getServerUrl ();
126
+ final String rootURL = getEndpointJenkinsRootURL (serverURL );
124
127
final String signatureSecret = getSecret (owner .getServerUrl ());
125
128
126
129
if (hook instanceof BitbucketCloudHook cloudHook ) {
130
+ String url = getCloudWebhookURL (serverURL , rootURL );
131
+ if (!Objects .equal (hook .getUrl (), url )) {
132
+ cloudHook .setUrl (url );
133
+ updated = true ;
134
+ }
135
+
127
136
if (!hook .getEvents ().containsAll (CLOUD_EVENTS )) {
128
137
Set <String > events = new TreeSet <>(hook .getEvents ());
129
138
events .addAll (CLOUD_EVENTS );
130
139
cloudHook .setEvents (new ArrayList <>(events ));
131
140
updated = true ;
132
141
}
142
+
133
143
if (!Objects .equal (hook .getSecret (), signatureSecret )) {
134
144
cloudHook .setSecret (signatureSecret );
135
145
updated = true ;
136
146
}
137
- } else if (hook instanceof BitbucketPluginWebhook serverHook ) {
138
- String hookCommittersToIgnore = Util .fixEmptyAndTrim (serverHook .getCommittersToIgnore ());
147
+ } else if (hook instanceof BitbucketPluginWebhook pluginHook ) {
148
+ String hookCommittersToIgnore = Util .fixEmptyAndTrim (pluginHook .getCommittersToIgnore ());
139
149
String thisCommittersToIgnore = Util .fixEmptyAndTrim (committersToIgnore );
140
150
if (!Objects .equal (thisCommittersToIgnore , hookCommittersToIgnore )) {
141
- serverHook .setCommittersToIgnore (thisCommittersToIgnore );
151
+ pluginHook .setCommittersToIgnore (thisCommittersToIgnore );
152
+ updated = true ;
153
+ }
154
+
155
+ String url = getServerWebhookURL (serverURL , rootURL );
156
+ if (!url .equals (pluginHook .getUrl ())) {
157
+ pluginHook .setUrl (url );
158
+ updated = true ;
159
+ }
160
+
161
+ if (!pluginHook .isActive ()) {
162
+ pluginHook .setActive (true );
142
163
updated = true ;
143
164
}
144
- } else if (hook instanceof BitbucketServerWebhook serverHook ) {
145
- String serverURL = owner .getServerUrl ();
146
- String url = getServerWebhookURL (serverURL , owner .getEndpointJenkinsRootURL ());
147
165
166
+ List <String > events = pluginHook .getEvents ();
167
+ if (events == null ) {
168
+ pluginHook .setEvents (getNativeServerEvents (serverURL ));
169
+ updated = true ;
170
+ } else if (!events .containsAll (getNativeServerEvents (serverURL ))) {
171
+ Set <String > newEvents = new TreeSet <>(events );
172
+ newEvents .addAll (getNativeServerEvents (serverURL ));
173
+ pluginHook .setEvents (new ArrayList <>(newEvents ));
174
+ updated = true ;
175
+ }
176
+ } else if (hook instanceof BitbucketServerWebhook serverHook ) {
177
+ String url = getServerWebhookURL (serverURL , rootURL );
148
178
if (!url .equals (serverHook .getUrl ())) {
149
179
serverHook .setUrl (url );
150
180
updated = true ;
@@ -170,28 +200,34 @@ boolean updateHook(BitbucketWebHook hook, BitbucketSCMSource owner) {
170
200
return updated ;
171
201
}
172
202
203
+ @ NonNull
204
+ private String getEndpointJenkinsRootURL (@ NonNull String serverURL ) {
205
+ return AbstractBitbucketEndpoint .getEndpointJenkinsRootUrl (serverURL );
206
+ }
207
+
208
+ @ NonNull
173
209
public BitbucketWebHook getHook (BitbucketSCMSource owner ) {
174
- final String serverUrl = owner .getServerUrl ();
175
- final String rootUrl = owner . getEndpointJenkinsRootURL ();
210
+ final String serverURL = owner .getServerUrl ();
211
+ final String rootURL = getEndpointJenkinsRootURL (serverURL );
176
212
final String signatureSecret = getSecret (owner .getServerUrl ());
177
213
178
- if (BitbucketApiUtils .isCloud (serverUrl )) {
214
+ if (BitbucketApiUtils .isCloud (serverURL )) {
179
215
BitbucketCloudHook hook = new BitbucketCloudHook ();
180
216
hook .setEvents (CLOUD_EVENTS );
181
217
hook .setActive (true );
182
218
hook .setDescription (description );
183
- hook .setUrl (rootUrl + BitbucketSCMSourcePushHookReceiver . FULL_PATH );
219
+ hook .setUrl (getCloudWebhookURL ( serverURL , rootURL ) );
184
220
hook .setSecret (signatureSecret );
185
221
return hook ;
186
222
}
187
223
188
- switch (BitbucketServerEndpoint .findWebhookImplementation (serverUrl )) {
224
+ switch (BitbucketServerEndpoint .findWebhookImplementation (serverURL )) {
189
225
case NATIVE : {
190
226
BitbucketServerWebhook hook = new BitbucketServerWebhook ();
191
227
hook .setActive (true );
192
228
hook .setDescription (description );
193
- hook .setEvents (getNativeServerEvents (serverUrl ));
194
- hook .setUrl (getServerWebhookURL (serverUrl , rootUrl ));
229
+ hook .setEvents (getNativeServerEvents (serverURL ));
230
+ hook .setUrl (getServerWebhookURL (serverURL , rootURL ));
195
231
hook .setSecret (signatureSecret );
196
232
return hook ;
197
233
}
@@ -201,7 +237,7 @@ public BitbucketWebHook getHook(BitbucketSCMSource owner) {
201
237
BitbucketPluginWebhook hook = new BitbucketPluginWebhook ();
202
238
hook .setActive (true );
203
239
hook .setDescription (description );
204
- hook .setUrl (getServerWebhookURL (serverUrl , rootUrl ));
240
+ hook .setUrl (getServerWebhookURL (serverURL , rootURL ));
205
241
hook .setCommittersToIgnore (committersToIgnore );
206
242
return hook ;
207
243
}
@@ -256,6 +292,13 @@ private static List<String> getNativeServerEvents(String serverUrl) {
256
292
return NATIVE_SERVER_EVENTS_v7 ;
257
293
}
258
294
295
+ private static String getCloudWebhookURL (String serverURL , String rootURL ) {
296
+ return UriTemplate .buildFromTemplate (rootURL )
297
+ .template (BitbucketSCMSourcePushHookReceiver .FULL_PATH )
298
+ .build ()
299
+ .expand ();
300
+ }
301
+
259
302
private static String getServerWebhookURL (String serverURL , String rootURL ) {
260
303
return UriTemplate .buildFromTemplate (rootURL )
261
304
.template (BitbucketSCMSourcePushHookReceiver .FULL_PATH )
0 commit comments