Search in sources :

Example 1 with RepositoryAware

use of io.pivotal.cla.egit.github.core.event.RepositoryAware in project pivotal-cla by pivotalsoftware.

the class GitHubHooksController method pullRequest.

/**
 * @param request
 * @param body
 * @param cla
 * @param githubEvent
 * @return
 * @throws Exception
 */
@RequestMapping("/github/hooks/pull_request/{cla}")
public ResponseEntity<String> pullRequest(HttpServletRequest request, @RequestBody String body, @PathVariable String cla, @RequestHeader("X-GitHub-Event") String githubEvent) throws Exception {
    if (!ACCEPTED_EVENTS.contains(githubEvent)) {
        return ResponseEntity.badRequest().body(String.format("X-Github-Event: %s not acceptable", githubEvent));
    }
    Gson gson = GsonUtils.createGson();
    RepositoryAware payload = gson.fromJson(body, PAYLOAD_TYPES.get(githubEvent));
    PullRequest pullRequest = getPullRequest(payload);
    if (pullRequest == null) {
        return ResponseEntity.badRequest().body("Not related to a Pull request");
    }
    User senderUser = getSender(payload);
    if (senderUser.getLogin().equals(gitHubApi.getGitHubClaUserLogin())) {
        return ResponseEntity.ok("Skipping self-events");
    }
    User user = getPullRequestUser(payload);
    Repository repository = payload.getRepository();
    RepositoryId repoId = RepositoryId.createFromId(repository.getOwner().getLogin() + "/" + repository.getName());
    String sha = getPullRequestSha(repoId, pullRequest);
    String gitHubLogin = user.getLogin();
    PullRequestStatus status = new PullRequestStatus();
    status.setGitHubUsername(gitHubLogin);
    status.setPullRequestId(pullRequest.getNumber());
    status.setPullRequestBody(pullRequest.getBody());
    status.setRepoId(repoId.generateId());
    status.setSha(sha);
    String signUrl = UrlBuilder.signUrl().request(request).claName(cla).repositoryId(status.getRepoId()).pullRequestId(status.getPullRequestId()).build();
    status.setUrl(signUrl);
    status.setPullRequestState(pullRequest.getState());
    String syncUrl = UrlBuilder.createSyncUrl(request, cla, status.getRepoId(), status.getPullRequestId());
    status.setSyncUrl(syncUrl);
    String faqUrl = UrlBuilder.createAboutUrl(request);
    status.setFaqUrl(faqUrl);
    ClaPullRequestStatusRequest pullRequestStatusRequest = new ClaPullRequestStatusRequest();
    pullRequestStatusRequest.setClaName(cla);
    pullRequestStatusRequest.setCommitStatus(status);
    claService.savePullRequestStatus(pullRequestStatusRequest);
    return ResponseEntity.ok("SUCCESS");
}
Also used : Repository(org.eclipse.egit.github.core.Repository) User(org.eclipse.egit.github.core.User) ClaPullRequestStatusRequest(io.pivotal.cla.service.ClaPullRequestStatusRequest) PullRequest(org.eclipse.egit.github.core.PullRequest) Gson(com.google.gson.Gson) RepositoryAware(io.pivotal.cla.egit.github.core.event.RepositoryAware) PullRequestStatus(io.pivotal.cla.service.github.PullRequestStatus) RepositoryId(org.eclipse.egit.github.core.RepositoryId) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

Gson (com.google.gson.Gson)1 RepositoryAware (io.pivotal.cla.egit.github.core.event.RepositoryAware)1 ClaPullRequestStatusRequest (io.pivotal.cla.service.ClaPullRequestStatusRequest)1 PullRequestStatus (io.pivotal.cla.service.github.PullRequestStatus)1 PullRequest (org.eclipse.egit.github.core.PullRequest)1 Repository (org.eclipse.egit.github.core.Repository)1 RepositoryId (org.eclipse.egit.github.core.RepositoryId)1 User (org.eclipse.egit.github.core.User)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1