use of io.pivotal.cla.service.ClaPullRequestStatusRequest in project pivotal-cla by pivotalsoftware.
the class SyncGitHubStatusController method sync.
@RequestMapping(value = "/sync/{claName}", method = RequestMethod.POST)
public String sync(@AuthenticationPrincipal User currentUser, @ModelAttribute ClaRequest claRequest, RedirectAttributes redirect) throws Exception {
Assert.hasText(claRequest.getRepositoryId(), "RepositoryId must not be empty");
Assert.isTrue(claRequest.getPullRequestId() != null && claRequest.getPullRequestId() > 0, "PullRequest Id must be greater 0");
String[] repositoryParts = claRequest.getRepositoryId().split("/");
if (repositoryParts.length != 2 || !StringUtils.hasText(repositoryParts[0]) || !StringUtils.hasText(repositoryParts[1])) {
throw new IllegalArgumentException("RepositoryId must be in format 'username/repository'");
}
Set<String> verifiedEmails = github.getVerifiedEmails(currentUser.getAccessToken());
if (!currentUser.getEmails().containsAll(verifiedEmails)) {
currentUser.setEmails(verifiedEmails);
users.save(currentUser);
}
Optional<PullRequest> optionalPullRequest = claService.findPullRequest(claRequest.getRepositoryId(), claRequest.getPullRequestId());
PullRequest pullRequest = optionalPullRequest.orElseThrow(() -> new IllegalArgumentException(String.format("Pull-request %s#%s does not exist", claRequest.getRepositoryId(), claRequest.getPullRequestId())));
ClaPullRequestStatusRequest updatePullRequest = claRequest.createUpdatePullRequestStatus(pullRequest.getUser().getLogin());
updatePullRequest.getCommitStatus().setPullRequestState(pullRequest.getState());
Set<String> associatedClaNames = claService.findAssociatedClaNames(claRequest.getRepositoryId());
if (!associatedClaNames.contains(claRequest.getClaName())) {
throw new IllegalArgumentException(String.format("Requested CLA '%s' is not linked to the repository", claRequest.getClaName()));
}
if (updatePullRequest != null) {
updatePullRequest.getCommitStatus().setAdmin(currentUser.isAdmin());
claService.savePullRequestStatus(updatePullRequest);
}
String repositoryOwner = repositoryParts[0];
String repositoryName = repositoryParts[1];
redirect.addAttribute("repositoryOwner", repositoryOwner);
redirect.addAttribute("repositoryName", repositoryName);
redirect.addAttribute("pullRequestId", claRequest.getPullRequestId());
return "redirect:https://github.com/{repositoryOwner}/{repositoryName}/pull/{pullRequestId}";
}
Aggregations