forked from jenkinsci/bitbucket-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitbucketHookReceiver.java
More file actions
62 lines (48 loc) · 2.21 KB
/
BitbucketHookReceiver.java
File metadata and controls
62 lines (48 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.cloudbees.jenkins.plugins;
import hudson.Extension;
import hudson.model.UnprotectedRootAction;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONObject;
import org.apache.commons.io.IOUtils;
import org.kohsuke.stapler.StaplerRequest2;
/**
* @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
*/
@Extension
public class BitbucketHookReceiver extends BitbucketCrumbExclusion implements UnprotectedRootAction {
private final BitbucketPayloadProcessor payloadProcessor = new BitbucketPayloadProcessor();
public static final String BITBUCKET_HOOK_URL = "bitbucket-hook";
public String getIconFileName() {
return null;
}
public String getDisplayName() {
return null;
}
public String getUrlName() {
return BITBUCKET_HOOK_URL;
}
/**
* Bitbucket send <a href="https://confluence.atlassian.com/display/BITBUCKET/Write+brokers+(hooks)+for+Bitbucket">payload</a>
* as form-urlencoded <pre>payload=JSON</pre>
* @throws IOException
*/
public void doIndex(StaplerRequest2 req) throws IOException {
String body = IOUtils.toString(req.getInputStream());
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 not have configured correctly the WebHook on BitBucket with the last slash `http://<JENKINS-URL>/bitbucket-hook/` or a 'Test connection' invocation of the hook was triggered.");
}
}
private static final Logger LOGGER = Logger.getLogger(BitbucketHookReceiver.class.getName());
}