Skip to content

Commit

Permalink
Merge pull request #19 from fbelzunc/JENKINS-29096
Browse files Browse the repository at this point in the history
[FIXED JENKINS-29096] Advice users when they don't use the right hook url
  • Loading branch information
fbelzunc committed Oct 10, 2015
2 parents 2f45c5f + 81473e8 commit e20b8b3
Showing 1 changed file with 16 additions and 9 deletions.
Expand Up @@ -5,6 +5,7 @@

import java.io.IOException;
import java.net.URLDecoder;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.sf.json.JSONObject;
Expand All @@ -19,6 +20,7 @@
public class BitbucketHookReceiver implements UnprotectedRootAction {

private final BitbucketPayloadProcessor payloadProcessor = new BitbucketPayloadProcessor();
private final String BITBUCKET_HOOK_URL = "bitbucket-hook";

public String getIconFileName() {
return null;
Expand All @@ -29,7 +31,7 @@ public String getDisplayName() {
}

public String getUrlName() {
return "bitbucket-hook";
return BITBUCKET_HOOK_URL;
}

/**
Expand All @@ -39,16 +41,21 @@ public String getUrlName() {
*/
public void doIndex(StaplerRequest req) throws IOException {
String body = IOUtils.toString(req.getInputStream());
String contentType = req.getContentType();
if (contentType != null && contentType.startsWith("application/x-www-form-urlencoded")) {
body = URLDecoder.decode(body);
if (!body.isEmpty() && req.getRequestURI().contains("/" + BITBUCKET_HOOK_URL + "/")) {
String contentType = req.getContentType();
if (contentType != null && contentType.startsWith("application/x-www-form-urlencoded")) {
body = URLDecoder.decode(body);
}
if (body.startsWith("payload=")) body = body.substring(8);

LOGGER.log(Level.FINE, "Received commit hook notification : {0}", body);
JSONObject payload = JSONObject.fromObject(body);

payloadProcessor.processPayload(payload, req);
} else {
LOGGER.log(Level.WARNING, "The Jenkins job cannot be triggered. You might no have configured correctly the WebHook on BitBucket with the last slash `http://<JENKINS-URL>/bitbucket-hook/`");
}
if (body.startsWith("payload=")) body = body.substring(8);

LOGGER.fine("Received commit hook notification : " + body);
JSONObject payload = JSONObject.fromObject(body);

payloadProcessor.processPayload(payload, req);
}

private static final Logger LOGGER = Logger.getLogger(BitbucketHookReceiver.class.getName());
Expand Down

0 comments on commit e20b8b3

Please sign in to comment.