Skip to content

Commit c074de4

Browse files
committed
[JENKINS-75804] Webhook auto registration fail with PLUGIN implementation (#1071)
Add logs everywhere
1 parent 511b1ff commit c074de4

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/WebhookAutoRegisterListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ private void registerHook(BitbucketSCMSource source) throws IOException {
179179
.withTraits(source.getTraits())
180180
.webhookConfiguration();
181181
if (existingHook == null) {
182-
LOGGER.log(Level.INFO, "Registering hook for {0}/{1}", new Object[]{source.getRepoOwner(), source.getRepository()});
182+
LOGGER.log(Level.WARNING, "Registering hook for {0}/{1}", new Object[]{source.getRepoOwner(), source.getRepository()});
183183
bitbucket.registerCommitWebHook(hookConfig.getHook(source));
184184
} else if (hookConfig.updateHook(existingHook, source)) {
185-
LOGGER.log(Level.INFO, "Updating hook for {0}/{1}", new Object[]{source.getRepoOwner(), source.getRepository()});
185+
LOGGER.log(Level.WARNING, "Updating hook for {0}/{1}", new Object[]{source.getRepoOwner(), source.getRepository()});
186186
bitbucket.updateCommitWebHook(existingHook);
187187
}
188188
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,15 @@
9191
import org.apache.commons.codec.digest.DigestUtils;
9292
import org.apache.commons.io.IOUtils;
9393
import org.apache.commons.lang3.StringUtils;
94+
import org.apache.hc.client5.http.classic.methods.HttpPut;
9495
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
9596
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
97+
import org.apache.hc.core5.http.ClassicHttpResponse;
98+
import org.apache.hc.core5.http.ContentType;
9699
import org.apache.hc.core5.http.HttpHost;
97100
import org.apache.hc.core5.http.HttpStatus;
101+
import org.apache.hc.core5.http.io.entity.EntityUtils;
102+
import org.apache.hc.core5.http.io.entity.StringEntity;
98103
import org.apache.hc.core5.http.message.BasicNameValuePair;
99104

100105
import static java.util.Objects.requireNonNull;
@@ -675,17 +680,41 @@ public void registerCommitWebHook(BitbucketWebHook hook) throws IOException {
675680

676681
@Override
677682
public void updateCommitWebHook(BitbucketWebHook hook) throws IOException {
683+
String payload = JsonParser.toString(hook);
678684
switch (webhookImplementation) {
679685
case PLUGIN:
680686
// API documentation at https://help.moveworkforward.com/BPW/how-to-manage-configurations-using-post-webhooks-f#HowtomanageconfigurationsusingPostWebhooksforBitbucketAPIs?-UpdateapostwebhookbyID
681-
putRequest(
682-
UriTemplate
683-
.fromTemplate(this.baseURL + WEBHOOK_REPOSITORY_CONFIG_PATH)
684-
.set("owner", getUserCentricOwner())
685-
.set("repo", repositoryName)
686-
.set("id", hook.getUuid())
687-
.expand(), JsonParser.toString(hook)
688-
);
687+
String path = UriTemplate
688+
.fromTemplate(this.baseURL + WEBHOOK_REPOSITORY_CONFIG_PATH)
689+
.set("owner", getUserCentricOwner())
690+
.set("repo", repositoryName)
691+
.set("id", hook.getUuid())
692+
.expand();
693+
logger.warning("Updating PLUGIN web hook sending " + payload);
694+
HttpPut request = new HttpPut(path);
695+
request.setAbsoluteRequestUri(true);
696+
request.setEntity(new StringEntity(payload, ContentType.create("application/json", "UTF-8")));
697+
try (ClassicHttpResponse response = executeMethod(request)) {
698+
int statusCode = response.getCode();
699+
if (statusCode == HttpStatus.SC_NOT_FOUND) {
700+
String errorMessage = getResponseContent(response);
701+
logger.warning("Error updating PLUGIN web hook");
702+
throw new FileNotFoundException("Resource " + request.getRequestUri() + " not found: " + errorMessage);
703+
}
704+
if (statusCode == HttpStatus.SC_NO_CONTENT) {
705+
EntityUtils.consumeQuietly(response.getEntity());
706+
logger.warning("No response returned from PLUGIN rest call -> 204");
707+
}
708+
String content = getResponseContent(response);
709+
if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
710+
throw buildResponseException(response, content);
711+
}
712+
logger.warning("Response from PLUGIN rest call " + content);
713+
} catch (FileNotFoundException | BitbucketRequestException e) {
714+
throw e;
715+
} catch (IOException e) {
716+
throw new IOException("Communication error, requested URL: " + request, e);
717+
}
689718
break;
690719

691720
case NATIVE:
@@ -695,7 +724,7 @@ public void updateCommitWebHook(BitbucketWebHook hook) throws IOException {
695724
.set("owner", getUserCentricOwner())
696725
.set("repo", repositoryName)
697726
.set("id", hook.getUuid())
698-
.expand(), JsonParser.toString(hook)
727+
.expand(), payload
699728
);
700729
break;
701730

0 commit comments

Comments
 (0)