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,40 +122,64 @@ 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 ) {
127
- if (!hook .getEvents ().containsAll (CLOUD_EVENTS )) {
128
- Set <String > events = new TreeSet <>(hook .getEvents ());
129
- events .addAll (CLOUD_EVENTS );
130
- cloudHook .setEvents (new ArrayList <>(events ));
130
+ String url = getCloudWebhookURL (serverURL , rootURL );
131
+ if (!Objects .equal (hook .getUrl (), url )) {
132
+ cloudHook .setUrl (url );
131
133
updated = true ;
132
134
}
135
+
136
+ List <String > events = hook .getEvents ();
137
+ if (!events .containsAll (CLOUD_EVENTS )) {
138
+ Set <String > newEvents = new TreeSet <>(events );
139
+ newEvents .addAll (CLOUD_EVENTS );
140
+ cloudHook .setEvents (new ArrayList <>(newEvents ));
141
+ updated = true ;
142
+ }
143
+
133
144
if (!Objects .equal (hook .getSecret (), signatureSecret )) {
134
145
cloudHook .setSecret (signatureSecret );
135
146
updated = true ;
136
147
}
137
- } else if (hook instanceof BitbucketPluginWebhook serverHook ) {
138
- String hookCommittersToIgnore = Util .fixEmptyAndTrim (serverHook .getCommittersToIgnore ());
148
+ } else if (hook instanceof BitbucketPluginWebhook pluginHook ) {
149
+ String hookCommittersToIgnore = Util .fixEmptyAndTrim (pluginHook .getCommittersToIgnore ());
139
150
String thisCommittersToIgnore = Util .fixEmptyAndTrim (committersToIgnore );
140
151
if (!Objects .equal (thisCommittersToIgnore , hookCommittersToIgnore )) {
141
- serverHook .setCommittersToIgnore (thisCommittersToIgnore );
152
+ pluginHook .setCommittersToIgnore (thisCommittersToIgnore );
153
+ updated = true ;
154
+ }
155
+
156
+ String url = getServerWebhookURL (serverURL , rootURL );
157
+ if (!url .equals (pluginHook .getUrl ())) {
158
+ pluginHook .setUrl (url );
159
+ updated = true ;
160
+ }
161
+
162
+ if (!pluginHook .isActive ()) {
163
+ pluginHook .setActive (true );
142
164
updated = true ;
143
165
}
144
- } else if (hook instanceof BitbucketServerWebhook serverHook ) {
145
- String serverURL = owner .getServerUrl ();
146
- String url = getServerWebhookURL (serverURL , owner .getEndpointJenkinsRootURL ());
147
166
167
+ List <String > events = pluginHook .getEvents ();
168
+ if (!events .containsAll (getNativeServerEvents (serverURL ))) {
169
+ Set <String > newEvents = new TreeSet <>(events );
170
+ newEvents .addAll (getNativeServerEvents (serverURL ));
171
+ pluginHook .setEvents (new ArrayList <>(newEvents ));
172
+ updated = true ;
173
+ }
174
+ } else if (hook instanceof BitbucketServerWebhook serverHook ) {
175
+ String url = getServerWebhookURL (serverURL , rootURL );
148
176
if (!url .equals (serverHook .getUrl ())) {
149
177
serverHook .setUrl (url );
150
178
updated = true ;
151
179
}
152
180
153
181
List <String > events = serverHook .getEvents ();
154
- if (events == null ) {
155
- serverHook .setEvents (getNativeServerEvents (serverURL ));
156
- updated = true ;
157
- } else if (!events .containsAll (getNativeServerEvents (serverURL ))) {
182
+ if (!events .containsAll (getNativeServerEvents (serverURL ))) {
158
183
Set <String > newEvents = new TreeSet <>(events );
159
184
newEvents .addAll (getNativeServerEvents (serverURL ));
160
185
serverHook .setEvents (new ArrayList <>(newEvents ));
@@ -170,28 +195,34 @@ boolean updateHook(BitbucketWebHook hook, BitbucketSCMSource owner) {
170
195
return updated ;
171
196
}
172
197
198
+ @ NonNull
199
+ private String getEndpointJenkinsRootURL (@ NonNull String serverURL ) {
200
+ return AbstractBitbucketEndpoint .getEndpointJenkinsRootUrl (serverURL );
201
+ }
202
+
203
+ @ NonNull
173
204
public BitbucketWebHook getHook (BitbucketSCMSource owner ) {
174
- final String serverUrl = owner .getServerUrl ();
175
- final String rootUrl = owner . getEndpointJenkinsRootURL ();
205
+ final String serverURL = owner .getServerUrl ();
206
+ final String rootURL = getEndpointJenkinsRootURL (serverURL );
176
207
final String signatureSecret = getSecret (owner .getServerUrl ());
177
208
178
- if (BitbucketApiUtils .isCloud (serverUrl )) {
209
+ if (BitbucketApiUtils .isCloud (serverURL )) {
179
210
BitbucketCloudHook hook = new BitbucketCloudHook ();
180
211
hook .setEvents (CLOUD_EVENTS );
181
212
hook .setActive (true );
182
213
hook .setDescription (description );
183
- hook .setUrl (rootUrl + BitbucketSCMSourcePushHookReceiver . FULL_PATH );
214
+ hook .setUrl (getCloudWebhookURL ( serverURL , rootURL ) );
184
215
hook .setSecret (signatureSecret );
185
216
return hook ;
186
217
}
187
218
188
- switch (BitbucketServerEndpoint .findWebhookImplementation (serverUrl )) {
219
+ switch (BitbucketServerEndpoint .findWebhookImplementation (serverURL )) {
189
220
case NATIVE : {
190
221
BitbucketServerWebhook hook = new BitbucketServerWebhook ();
191
222
hook .setActive (true );
192
223
hook .setDescription (description );
193
- hook .setEvents (getNativeServerEvents (serverUrl ));
194
- hook .setUrl (getServerWebhookURL (serverUrl , rootUrl ));
224
+ hook .setEvents (getNativeServerEvents (serverURL ));
225
+ hook .setUrl (getServerWebhookURL (serverURL , rootURL ));
195
226
hook .setSecret (signatureSecret );
196
227
return hook ;
197
228
}
@@ -201,7 +232,7 @@ public BitbucketWebHook getHook(BitbucketSCMSource owner) {
201
232
BitbucketPluginWebhook hook = new BitbucketPluginWebhook ();
202
233
hook .setActive (true );
203
234
hook .setDescription (description );
204
- hook .setUrl (getServerWebhookURL (serverUrl , rootUrl ));
235
+ hook .setUrl (getServerWebhookURL (serverURL , rootURL ));
205
236
hook .setCommittersToIgnore (committersToIgnore );
206
237
return hook ;
207
238
}
@@ -256,6 +287,13 @@ private static List<String> getNativeServerEvents(String serverUrl) {
256
287
return NATIVE_SERVER_EVENTS_v7 ;
257
288
}
258
289
290
+ private static String getCloudWebhookURL (String serverURL , String rootURL ) {
291
+ return UriTemplate .buildFromTemplate (rootURL )
292
+ .template (BitbucketSCMSourcePushHookReceiver .FULL_PATH )
293
+ .build ()
294
+ .expand ();
295
+ }
296
+
259
297
private static String getServerWebhookURL (String serverURL , String rootURL ) {
260
298
return UriTemplate .buildFromTemplate (rootURL )
261
299
.template (BitbucketSCMSourcePushHookReceiver .FULL_PATH )
0 commit comments