diff options
author | S.Nishio <nishio@densan-labs.net> | 2015-09-06 14:28:48 +0900 |
---|---|---|
committer | S.Nishio <nishio@densan-labs.net> | 2015-09-06 14:28:48 +0900 |
commit | d002e0b758c1907c2d4963a2b9e556177e05ab03 (patch) | |
tree | d5c5c8edf32b08f3e4b3ab90bbe4dacd56bb81e2 /src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java | |
parent | 636cb7acb982f351f36005cbb71292e1c92ff52a (diff) | |
parent | 733e87f9ce3bd72eab48de2e55b4bb59644c9237 (diff) | |
download | bbprb-d002e0b758c1907c2d4963a2b9e556177e05ab03.tar.gz |
Merge pull request #58 from b-studios/refactor
Refactorings
Diffstat (limited to 'src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java')
-rw-r--r-- | src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java new file mode 100644 index 0000000..713a061 --- /dev/null +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/bitbucket/ApiClient.java @@ -0,0 +1,157 @@ +package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket; + +import org.apache.commons.httpclient.*; +import org.apache.commons.httpclient.auth.AuthScope; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.DeleteMethod; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.type.TypeReference; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Collections; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; + +import jenkins.model.Jenkins; +import hudson.ProxyConfiguration; + +/** + * Created by nishio + */ +public class ApiClient { + private static final Logger logger = Logger.getLogger(ApiClient.class.getName()); + private static final String BITBUCKET_HOST = "bitbucket.org"; + 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 String owner; + private String repositoryName; + private Credentials credentials; + + public ApiClient(String username, String password, String owner, String repositoryName) { + this.credentials = new UsernamePasswordCredentials(username, password); + this.owner = owner; + this.repositoryName = repositoryName; + } + + public List<Pullrequest> getPullRequests() { + try { + return parse(get(v2("/pullrequests/")), Pullrequest.Response.class).getPullrequests(); + } catch(Exception e) { + logger.log(Level.WARNING, "invalid pull request response.", e); + } + return Collections.EMPTY_LIST; + } + + public List<Pullrequest.Comment> getPullRequestComments(String commentOwnerName, String commentRepositoryName, String pullRequestId) { + try { + return parse(get(v1("/pullrequests/" + pullRequestId + "/comments")), new TypeReference<List<Pullrequest.Comment>>() {}); + } catch(Exception e) { + logger.log(Level.WARNING, "invalid pull request response.", e); + } + return Collections.EMPTY_LIST; + } + + public void deletePullRequestComment(String pullRequestId, String commentId) { + String path = V1_API_BASE_URL + this.owner + "/" + this.repositoryName + "/pullrequests/" + pullRequestId + "/comments/" + commentId; + //https://bitbucket.org/api/1.0/repositories/{accountname}/{repo_slug}/pullrequests/{pull_request_id}/comments/{comment_id} + delete(path); + } + + + public Pullrequest.Comment postPullRequestComment(String pullRequestId, String comment) { + try { + String response = post( + v1("/pullrequests/" + pullRequestId + "/comments"), + new NameValuePair[]{ new NameValuePair("content", comment) }); + return parse(response, Pullrequest.Comment.class); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public void deletePullRequestApproval(String pullRequestId) { + delete(v2("/pullrequests/" + pullRequestId + "/approve")); + } + + public Pullrequest.Participant postPullRequestApproval(String pullRequestId) { + try { + return parse(post(v2("/pullrequests/" + pullRequestId + "/approve"), + new NameValuePair[]{}), Pullrequest.Participant.class); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + private HttpClient getHttpClient() { + HttpClient client = new HttpClient(); + if (Jenkins.getInstance() == null) return client; + + ProxyConfiguration proxy = Jenkins.getInstance().proxy; + if (proxy == null) return client; + + logger.info("Jenkins proxy: " + proxy.name + ":" + proxy.port); + client.getHostConfiguration().setProxy(proxy.name, proxy.port); + String username = proxy.getUserName(); + String password = proxy.getPassword(); + + // Consider it to be passed if username specified. Sufficient? + if (username != null && !"".equals(username.trim())) { + logger.info("Using proxy authentication (user=" + username + ")"); + client.getState().setProxyCredentials(AuthScope.ANY, + new UsernamePasswordCredentials(username, password)); + } + return client; + } + + private String v1(String url) { + return V1_API_BASE_URL + this.owner + "/" + this.repositoryName + url; + } + + private String v2(String url) { + return V2_API_BASE_URL + this.owner + "/" + this.repositoryName + url; + } + + private String get(String path) { + return send(new GetMethod(path)); + } + + private String post(String path, NameValuePair[] data) { + PostMethod req = new PostMethod(path); + req.setRequestBody(data); + return send(req); + } + + private void delete(String path) { + send(new DeleteMethod(path)); + } + + private String send(HttpMethodBase req) { + HttpClient client = getHttpClient(); + client.getState().setCredentials(AuthScope.ANY, credentials); + client.getParams().setAuthenticationPreemptive(true); + try { + client.executeMethod(req); + return req.getResponseBodyAsString(); + } catch (HttpException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + private <R> R parse(String response, Class<R> cls) throws IOException { + return new ObjectMapper().readValue(response, cls); + } + private <R> R parse(String response, TypeReference<R> ref) throws IOException { + return new ObjectMapper().readValue(response, ref); + } +} |