diff options
Diffstat (limited to 'src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java')
-rw-r--r-- | src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java | 62 |
1 files changed, 56 insertions, 6 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java index c8f1818..e635f65 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -16,6 +16,9 @@ import java.util.logging.Logger; import jenkins.model.Jenkins; import hudson.ProxyConfiguration; +import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.httpclient.params.HttpMethodParams; +import org.apache.commons.httpclient.util.EncodingUtil; /** * Created by nishio @@ -24,6 +27,7 @@ public class ApiClient { private static final Logger logger = Logger.getLogger(ApiClient.class.getName()); private static final String V1_API_BASE_URL = "https://bitbucket.org/api/1.0/repositories/"; private static final String V2_API_BASE_URL = "https://bitbucket.org/api/2.0/repositories/"; + private static final String COMPUTED_KEY_FORMAT = "%s-%s"; private String owner; private String repositoryName; private Credentials credentials; @@ -55,28 +59,53 @@ public class ApiClient { } return Collections.EMPTY_LIST; } + + public String getName() { + return this.name; + } + + private String computeAPIKey(String keyExPart) { + return String.format(COMPUTED_KEY_FORMAT, this.key, keyExPart); + } + + public String buildStatusKey(String bsKey) { + return this.computeAPIKey(bsKey); + } - public boolean hasBuildStatus(String owner, String repositoryName, String revision) { - String url = v2(owner, repositoryName, "/commit/" + revision + "/statuses/build/" + this.key); + public boolean hasBuildStatus(String owner, String repositoryName, String revision, String keyEx) { + String url = v2(owner, repositoryName, "/commit/" + revision + "/statuses/build/" + this.computeAPIKey(keyEx)); return get(url).contains("\"state\""); } - public void setBuildStatus(String owner, String repositoryName, String revision, BuildState state, String buildUrl, String comment) { + public void setBuildStatus(String owner, String repositoryName, String revision, BuildState state, String buildUrl, String comment, String keyEx) { String url = v2(owner, repositoryName, "/commit/" + revision + "/statuses/build"); + String computedKey = this.computeAPIKey(keyEx); NameValuePair[] data = new NameValuePair[]{ new NameValuePair("description", comment), - new NameValuePair("key", this.key), + new NameValuePair("key", computedKey), new NameValuePair("name", this.name), new NameValuePair("state", state.toString()), new NameValuePair("url", buildUrl), }; - logger.info("POST state " + state + " to " + url); - post(url, data); + logger.log(Level.INFO, "POST state {0} to {1} with key {2} with response {3}", new Object[]{ + state, url, computedKey, post(url, data)} + ); } public void deletePullRequestApproval(String pullRequestId) { delete(v2("/pullrequests/" + pullRequestId + "/approve")); } + + public void deletePullRequestComment(String pullRequestId, String commentId) { + delete(v1("/pullrequests/" + pullRequestId + "/comments/" + commentId)); + } + + public void updatePullRequestComment(String pullRequestId, String content, String commentId) { + NameValuePair[] data = new NameValuePair[] { + new NameValuePair("content", content), + }; + put(v1("/pullrequests/" + pullRequestId + "/comments/" + commentId), data); + } public Pullrequest.Participant postPullRequestApproval(String pullRequestId) { try { @@ -87,6 +116,19 @@ public class ApiClient { } return null; } + + public Pullrequest.Comment postPullRequestComment(String pullRequestId, String content) { + NameValuePair[] data = new NameValuePair[] { + new NameValuePair("content", content), + }; + try { + return parse(post(v1("/pullrequests/" + pullRequestId + "/comments"), data), new TypeReference<Pullrequest.Comment>() {}); + } catch(Exception e) { + logger.log(Level.WARNING, "Invalid pull request comment response.", e); + e.printStackTrace(); + } + return null; + } private HttpClient getHttpClient() { HttpClient client = new HttpClient(); @@ -128,12 +170,20 @@ public class ApiClient { private String post(String path, NameValuePair[] data) { PostMethod req = new PostMethod(path); req.setRequestBody(data); + req.getParams().setContentCharset("utf-8"); return send(req); } private void delete(String path) { send(new DeleteMethod(path)); } + + private void put(String path, NameValuePair[] data) { + PutMethod req = new PutMethod(path); + req.setRequestBody(EncodingUtil.formUrlEncode(data, "utf-8")); + req.getParams().setContentCharset("utf-8"); + send(req); + } private String send(HttpMethodBase req) { HttpClient client = getHttpClient(); |