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");
}
Aggregations