Skip to content

[JENKINS-75804] Webhook auto registration fail with PLUGIN implementa… #1079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 936.4.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ private void registerHook(BitbucketSCMSource source) throws IOException {
.withTraits(source.getTraits())
.webhookConfiguration();
if (existingHook == null) {
LOGGER.log(Level.INFO, "Registering hook for {0}/{1}", new Object[]{source.getRepoOwner(), source.getRepository()});
LOGGER.log(Level.WARNING, "Registering hook for {0}/{1}", new Object[]{source.getRepoOwner(), source.getRepository()});
bitbucket.registerCommitWebHook(hookConfig.getHook(source));
} else if (hookConfig.updateHook(existingHook, source)) {
LOGGER.log(Level.INFO, "Updating hook for {0}/{1}", new Object[]{source.getRepoOwner(), source.getRepository()});
LOGGER.log(Level.WARNING, "Updating hook for {0}/{1}", new Object[]{source.getRepoOwner(), source.getRepository()});
bitbucket.updateCommitWebHook(existingHook);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,15 @@
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicNameValuePair;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -675,17 +680,41 @@

@Override
public void updateCommitWebHook(BitbucketWebHook hook) throws IOException {
String payload = JsonParser.toString(hook);
switch (webhookImplementation) {
case PLUGIN:
// API documentation at https://help.moveworkforward.com/BPW/how-to-manage-configurations-using-post-webhooks-f#HowtomanageconfigurationsusingPostWebhooksforBitbucketAPIs?-UpdateapostwebhookbyID
putRequest(
UriTemplate
.fromTemplate(this.baseURL + WEBHOOK_REPOSITORY_CONFIG_PATH)
.set("owner", getUserCentricOwner())
.set("repo", repositoryName)
.set("id", hook.getUuid())
.expand(), JsonParser.toString(hook)
);
String path = UriTemplate
.fromTemplate(this.baseURL + WEBHOOK_REPOSITORY_CONFIG_PATH)
.set("owner", getUserCentricOwner())
.set("repo", repositoryName)
.set("id", hook.getUuid())
.expand();
logger.warning("Updating PLUGIN web hook sending " + payload);
HttpPut request = new HttpPut(path);
request.setAbsoluteRequestUri(true);
request.setEntity(new StringEntity(payload, ContentType.create("application/json", "UTF-8")));
try (ClassicHttpResponse response = executeMethod(request)) {
int statusCode = response.getCode();
if (statusCode == HttpStatus.SC_NOT_FOUND) {
String errorMessage = getResponseContent(response);
logger.warning("Error updating PLUGIN web hook");
throw new FileNotFoundException("Resource " + request.getRequestUri() + " not found: " + errorMessage);
}
if (statusCode == HttpStatus.SC_NO_CONTENT) {
EntityUtils.consumeQuietly(response.getEntity());
logger.warning("No response returned from PLUGIN rest call -> 204");
}
String content = getResponseContent(response);
if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
throw buildResponseException(response, content);
}
logger.warning("Response from PLUGIN rest call " + content);
} catch (FileNotFoundException | BitbucketRequestException e) {
throw e;
} catch (IOException e) {
throw new IOException("Communication error, requested URL: " + request, e);
}

Check warning on line 717 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 687-717 are not covered by tests
break;

case NATIVE:
Expand All @@ -695,7 +724,7 @@
.set("owner", getUserCentricOwner())
.set("repo", repositoryName)
.set("id", hook.getUuid())
.expand(), JsonParser.toString(hook)
.expand(), payload
);
break;

Expand Down
Loading