Java tutorial
/** * Copyright 2014 ArcBees Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.arcbees.vcs.github; import java.io.IOException; import java.util.Date; import org.apache.http.HttpHeaders; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.message.BasicHeader; import com.arcbees.vcs.AbstractVcsApi; import com.arcbees.vcs.github.model.GitHubComment; import com.arcbees.vcs.github.model.GitHubCommitStatus; import com.arcbees.vcs.github.model.GitHubCreateComment; import com.arcbees.vcs.github.model.GitHubPullRequests; import com.arcbees.vcs.github.util.GitHubPullRequestsTypeAdapter; import com.arcbees.vcs.model.Comment; import com.arcbees.vcs.model.CommitStatus; import com.arcbees.vcs.model.PullRequest; import com.arcbees.vcs.model.PullRequests; import com.arcbees.vcs.util.CommitStatusTypeAdapter; import com.arcbees.vcs.util.GsonDateTypeAdapter; import com.arcbees.vcs.util.HttpClientWrapper; import com.google.common.base.Charsets; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import jetbrains.buildServer.serverSide.SRunningBuild; public class GitHubApi extends AbstractVcsApi { private final HttpClientWrapper httpClient; private final Gson gson; private final GitHubApiPaths apiPaths; private final String repositoryOwner; private final String repositoryName; private final UsernamePasswordCredentials credentials; public GitHubApi(HttpClientWrapper httpClient, GitHubApiPaths apiPaths, String userName, String password, String repositoryOwner, String repositoryName) { this.httpClient = httpClient; this.apiPaths = apiPaths; this.repositoryOwner = repositoryOwner; this.repositoryName = repositoryName; this.credentials = new UsernamePasswordCredentials(userName, password); this.gson = new GsonBuilder().registerTypeAdapter(Date.class, new GsonDateTypeAdapter()) .registerTypeAdapter(GitHubPullRequests.class, new GitHubPullRequestsTypeAdapter()) .registerTypeAdapter(CommitStatus.class, new CommitStatusTypeAdapter()).create(); } @Override public PullRequests getOpenedPullRequests() throws IOException { String requestUrl = apiPaths.getOpenedPullRequests(repositoryOwner, repositoryName); HttpGet request = new HttpGet(requestUrl); return processResponse(httpClient, request, credentials, gson, GitHubPullRequests.class); } @Override public PullRequests getMergedPullRequests() throws IOException { String requestUrl = apiPaths.getMergedPullRequests(repositoryOwner, repositoryName); HttpGet request = new HttpGet(requestUrl); return processResponse(httpClient, request, credentials, gson, GitHubPullRequests.class); } @Override public PullRequest getPullRequestForBranch(final String branchName) throws IOException { PullRequest pullRequestForBranch = findPullRequestForBranch(branchName, getOpenedPullRequests()); if (pullRequestForBranch == null) { pullRequestForBranch = findPullRequestForBranch(branchName, getMergedPullRequests()); } return pullRequestForBranch; } @Override public void deleteComment(Integer pullRequestId, Long commentId) throws IOException { String requestUrl = apiPaths.deleteComment(repositoryOwner, repositoryName, pullRequestId, commentId); HttpDelete request = new HttpDelete(requestUrl); executeRequest(httpClient, request, credentials); } @Override public Comment postComment(Integer pullRequestId, String comment) throws IOException { String requestUrl = apiPaths.addComment(repositoryOwner, repositoryName, pullRequestId); HttpPost request = new HttpPost(requestUrl); request.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType())); request.setEntity( new ByteArrayEntity(gson.toJson(new GitHubCreateComment(comment)).getBytes(Charsets.UTF_8))); return processResponse(httpClient, request, credentials, gson, GitHubComment.class); } @Override public void updateStatus(String commitHash, String message, CommitStatus status, String targetUrl, SRunningBuild build) throws IOException { String requestUrl = apiPaths.updateStatus(repositoryOwner, repositoryName, commitHash); HttpPost request = new HttpPost(requestUrl); request.setHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType())); String entityAsJson = gson.toJson(new GitHubCommitStatus(status, message, targetUrl)); request.setEntity(new StringEntity(entityAsJson)); executeRequest(httpClient, request, credentials); } @Override public void approvePullRequest(Integer pullRequestId) throws IOException, UnsupportedOperationException { throw new UnsupportedOperationException(); } @Override public void deletePullRequestApproval(Integer pullRequestId) throws IOException, UnsupportedOperationException { throw new UnsupportedOperationException(); } }