Search in sources :

Example 6 with ClaPullRequestStatusRequest

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}";
}
Also used : ClaPullRequestStatusRequest(io.pivotal.cla.service.ClaPullRequestStatusRequest) PullRequest(org.eclipse.egit.github.core.PullRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ClaPullRequestStatusRequest (io.pivotal.cla.service.ClaPullRequestStatusRequest)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ContributorLicenseAgreement (io.pivotal.cla.data.ContributorLicenseAgreement)2 PullRequestStatus (io.pivotal.cla.service.github.PullRequestStatus)2 Date (java.util.Date)2 PullRequest (org.eclipse.egit.github.core.PullRequest)2 Gson (com.google.gson.Gson)1 CorporateSignature (io.pivotal.cla.data.CorporateSignature)1 IndividualSignature (io.pivotal.cla.data.IndividualSignature)1 RepositoryAware (io.pivotal.cla.egit.github.core.event.RepositoryAware)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 ModelAttribute (org.springframework.web.bind.annotation.ModelAttribute)1