Search in sources :

Example 11 with PullRequestStatus

use of io.pivotal.cla.service.github.PullRequestStatus in project pivotal-cla by pivotalsoftware.

the class GitHubHooksControllerTests method markCommitStatusSuccessIndividualAcceptingReviewComments.

@Test
public void markCommitStatusSuccessIndividualAcceptingReviewComments() throws Exception {
    User user = WithSigningUserFactory.create();
    when(mockUserRepo.findOne(anyString())).thenReturn(user);
    when(mockTokenRepo.findOne("rwinch/176_test")).thenReturn(new AccessToken("rwinch/176_test", "mock_access_token_value"));
    when(mockIndividualSignatureRepository.findSignaturesFor(any(), any(), anyString())).thenReturn(Arrays.asList(individualSignature));
    mockMvc.perform(hookRequest().header("X-GitHub-Event", GithubEvents.PULL_REQUEST_REVIEW_COMMENT).content(getPayload("pull_request_review_comment.json"))).andExpect(status().isOk());
    ArgumentCaptor<PullRequestStatus> statusCaptor = ArgumentCaptor.forClass(PullRequestStatus.class);
    verify(mockGitHub).save(statusCaptor.capture());
    PullRequestStatus status = statusCaptor.getValue();
    assertThat(status.getRepoId()).isEqualTo("rwinch/176_test");
    assertThat(status.getAccessToken()).isEqualTo("mock_access_token_value");
    assertThat(status.getPullRequestId()).isEqualTo(2);
    assertThat(status.getSha()).isEqualTo("a6befb598a35c1c206e1bf7bbb3018f4403b9610");
    assertThat(status.getUrl()).isEqualTo("http://localhost/sign/pivotal?repositoryId=rwinch/176_test&pullRequestId=2");
    assertThat(status.isSuccess()).isTrue();
    assertThat(status.getGitHubUsername()).isEqualTo(user.getGitHubLogin());
    assertThat(status.getPullRequestBody()).isNotEmpty();
}
Also used : User(io.pivotal.cla.data.User) AccessToken(io.pivotal.cla.data.AccessToken) PullRequestStatus(io.pivotal.cla.service.github.PullRequestStatus) Test(org.junit.Test)

Example 12 with PullRequestStatus

use of io.pivotal.cla.service.github.PullRequestStatus 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)

Example 13 with PullRequestStatus

use of io.pivotal.cla.service.github.PullRequestStatus in project pivotal-cla by pivotalsoftware.

the class ClaService method savePullRequestStatus.

public void savePullRequestStatus(ClaPullRequestStatusRequest request) {
    String claName = request.getClaName();
    PullRequestStatus commitStatus = request.getCommitStatus();
    String gitHubLogin = commitStatus.getGitHubUsername();
    if (commitStatus.getAccessToken() == null) {
        AccessToken accessToken = accessTokenRepository.findOne(commitStatus.getRepoId());
        if (accessToken == null) {
            return;
        }
        String token = accessToken.getToken();
        commitStatus.setAccessToken(token);
    }
    if (commitStatus.getSha() == null) {
        String sha = gitHub.getShaForPullRequest(commitStatus);
        if (sha == null) {
            return;
        }
        commitStatus.setSha(sha);
    }
    if (commitStatus.getSuccess() == null) {
        boolean hasSigned = hasSigned(gitHubLogin, claName);
        commitStatus.setSuccess(hasSigned);
    }
    gitHub.save(commitStatus);
}
Also used : AccessToken(io.pivotal.cla.data.AccessToken) PullRequestStatus(io.pivotal.cla.service.github.PullRequestStatus)

Aggregations

PullRequestStatus (io.pivotal.cla.service.github.PullRequestStatus)13 AccessToken (io.pivotal.cla.data.AccessToken)10 Test (org.junit.Test)9 User (io.pivotal.cla.data.User)6 WithSigningUser (io.pivotal.cla.security.WithSigningUser)2 ClaPullRequestStatusRequest (io.pivotal.cla.service.ClaPullRequestStatusRequest)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Gson (com.google.gson.Gson)1 PullRequestId (io.pivotal.cla.egit.github.core.PullRequestId)1 RepositoryAware (io.pivotal.cla.egit.github.core.event.RepositoryAware)1 WithAdminUser (io.pivotal.cla.security.WithAdminUser)1 ContributingUrlsResponse (io.pivotal.cla.service.github.ContributingUrlsResponse)1 CreatePullRequestHookRequest (io.pivotal.cla.service.github.CreatePullRequestHookRequest)1 SignCclaPage (io.pivotal.cla.webdriver.pages.SignCclaPage)1 SignClaPage (io.pivotal.cla.webdriver.pages.SignClaPage)1 SignIclaPage (io.pivotal.cla.webdriver.pages.SignIclaPage)1 AdminLinkClaPage (io.pivotal.cla.webdriver.pages.admin.AdminLinkClaPage)1 SneakyThrows (lombok.SneakyThrows)1 PullRequest (org.eclipse.egit.github.core.PullRequest)1 Repository (org.eclipse.egit.github.core.Repository)1