Search in sources :

Example 1 with CreatePullRequestHookRequest

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

the class AdminLinkClaTests method linkClaRepositories.

@Test
@SuppressWarnings("unchecked")
public void linkClaRepositories() throws Exception {
    AccessToken token = new AccessToken(AccessToken.CLA_ACCESS_TOKEN_ID, "linkClaValidationRepositories_access_token_abc123");
    when(mockTokenRepo.findOne(AccessToken.CLA_ACCESS_TOKEN_ID)).thenReturn(token);
    when(mockGitHub.getContributingUrls(anyList())).thenReturn(new ContributingUrlsResponse());
    User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    AdminLinkClaPage link = AdminLinkClaPage.to(getDriver());
    link = link.link("test/this", cla.getName(), AdminLinkClaPage.class);
    link.assertRepositories().hasNoErrors();
    link.assertClaName().hasNoErrors();
    ArgumentCaptor<CreatePullRequestHookRequest> requestCaptor = ArgumentCaptor.forClass(CreatePullRequestHookRequest.class);
    verify(mockGitHub).createPullRequestHooks(requestCaptor.capture());
    CreatePullRequestHookRequest request = requestCaptor.getValue();
    assertThat(request.getAccessToken()).isEqualTo(user.getAccessToken());
    assertThat(request.getRepositoryIds()).containsOnly("test/this");
    assertThat(request.getGitHubEventUrl()).isEqualTo("http://localhost/github/hooks/pull_request/" + cla.getName());
    assertThat(request.getSecret()).isEqualTo(token.getToken());
    assertThat(driver.getPageSource()).doesNotContain(token.getToken());
    ArgumentCaptor<AccessToken> tokenCaptor = ArgumentCaptor.forClass(AccessToken.class);
    verify(mockTokenRepo).save(tokenCaptor.capture());
    AccessToken savedToken = tokenCaptor.getValue();
    assertThat(savedToken.getId()).isEqualTo("test/this");
    assertThat(savedToken.getToken()).isEqualTo(user.getAccessToken());
    PullRequestStatus expectedStatus = new PullRequestStatus();
    expectedStatus.setAccessToken("access-token-123");
    expectedStatus.setGitHubUsername("username");
    expectedStatus.setPullRequestId(1);
    expectedStatus.setRepoId("repo");
    expectedStatus.setSha("12345678");
    expectedStatus.setUrl("https://cla.pivotal.io/sign/pivotal");
    when(mockGitHub.createUpdatePullRequestStatuses(any())).thenReturn(Arrays.asList(expectedStatus));
    link = link.migrate();
    ArgumentCaptor<PullRequestStatus> statusCaptor = ArgumentCaptor.forClass(PullRequestStatus.class);
    verify(mockGitHub).save(statusCaptor.capture());
    PullRequestStatus status = statusCaptor.getValue();
    assertThat(status.getAccessToken()).isEqualTo(expectedStatus.getAccessToken());
    assertThat(status.getGitHubUsername()).isEqualTo(expectedStatus.getGitHubUsername());
    assertThat(status.getPullRequestId()).isEqualTo(expectedStatus.getPullRequestId());
    assertThat(status.getRepoId()).isEqualTo(expectedStatus.getRepoId());
    assertThat(status.getSha()).isEqualTo(expectedStatus.getSha());
    assertThat(status.getSuccess()).isFalse();
    assertThat(status.getUrl()).isEqualTo(expectedStatus.getUrl());
}
Also used : WithSigningUser(io.pivotal.cla.security.WithSigningUser) User(io.pivotal.cla.data.User) WithAdminUser(io.pivotal.cla.security.WithAdminUser) CreatePullRequestHookRequest(io.pivotal.cla.service.github.CreatePullRequestHookRequest) AccessToken(io.pivotal.cla.data.AccessToken) ContributingUrlsResponse(io.pivotal.cla.service.github.ContributingUrlsResponse) PullRequestStatus(io.pivotal.cla.service.github.PullRequestStatus) AdminLinkClaPage(io.pivotal.cla.webdriver.pages.admin.AdminLinkClaPage) Test(org.junit.Test)

Example 2 with CreatePullRequestHookRequest

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

the class AdminLinkClaController method linkCla.

@RequestMapping(value = "/admin/cla/link", method = RequestMethod.POST)
public String linkCla(@AuthenticationPrincipal User user, HttpServletRequest request, Map<String, Object> model, @Valid LinkClaForm linkClaForm, BindingResult result, RedirectAttributes attrs) throws Exception {
    if (result.hasErrors()) {
        model.put("licenses", findPrimaryClas());
        model.put("accessTokensUrl", ACCESS_TOKENS_URL);
        model.put("repositories", gitHub.findRepositoryNamesWithAdminPermission(user.getAccessToken()));
        return "admin/cla/link";
    }
    AccessToken accessToken = tokenRepo.findOne(AccessToken.CLA_ACCESS_TOKEN_ID);
    UrlBuilder pullRequestUrlBldr = UrlBuilder.fromRequest(request);
    UrlBuilder signClaUrlBldr = UrlBuilder.fromRequest(request);
    String urlEncodedClaName = URLEncoder.encode(linkClaForm.getClaName(), "UTF-8");
    String pullRequestHookUrl = pullRequestUrlBldr.path(UrlBuilder.pullRequestHookCallbackPath(linkClaForm.getClaName())).build();
    String signClaUrl = signClaUrlBldr.path("/sign/" + urlEncodedClaName).build();
    CreatePullRequestHookRequest createPullRequest = new CreatePullRequestHookRequest();
    createPullRequest.setAccessToken(user.getAccessToken());
    List<String> repositoryIds = linkClaForm.getRepositories();
    createPullRequest.setRepositoryIds(repositoryIds);
    createPullRequest.setGitHubEventUrl(pullRequestHookUrl);
    createPullRequest.setSecret(accessToken.getToken());
    ContributingUrlsResponse contributingUrls = gitHub.getContributingUrls(repositoryIds);
    List<String> hookUrls = gitHub.createPullRequestHooks(createPullRequest);
    for (String repositoryId : repositoryIds) {
        AccessToken token = new AccessToken();
        token.setId(repositoryId);
        token.setToken(user.getAccessToken());
        tokenRepo.save(token);
    }
    UpdatePullRequestStatusesForm updatePullRequestStatusesForm = new UpdatePullRequestStatusesForm();
    updatePullRequestStatusesForm.setClaName(linkClaForm.getClaName());
    updatePullRequestStatusesForm.setRepositories(repositoryIds);
    attrs.addFlashAttribute("signClaUrl", signClaUrl);
    attrs.addFlashAttribute("hookUrls", hookUrls);
    attrs.addFlashAttribute("success", true);
    attrs.addFlashAttribute("editContributingAdocUrls", contributingUrls.getAsciidoc());
    attrs.addFlashAttribute("editContributingMdUrls", contributingUrls.getMarkdown());
    attrs.addFlashAttribute("updatePullRequestStatusesForm", updatePullRequestStatusesForm);
    return "redirect:/admin/cla/link";
}
Also used : CreatePullRequestHookRequest(io.pivotal.cla.service.github.CreatePullRequestHookRequest) AccessToken(io.pivotal.cla.data.AccessToken) ContributingUrlsResponse(io.pivotal.cla.service.github.ContributingUrlsResponse) UrlBuilder(io.pivotal.cla.mvc.util.UrlBuilder) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

AccessToken (io.pivotal.cla.data.AccessToken)2 ContributingUrlsResponse (io.pivotal.cla.service.github.ContributingUrlsResponse)2 CreatePullRequestHookRequest (io.pivotal.cla.service.github.CreatePullRequestHookRequest)2 User (io.pivotal.cla.data.User)1 UrlBuilder (io.pivotal.cla.mvc.util.UrlBuilder)1 WithAdminUser (io.pivotal.cla.security.WithAdminUser)1 WithSigningUser (io.pivotal.cla.security.WithSigningUser)1 PullRequestStatus (io.pivotal.cla.service.github.PullRequestStatus)1 AdminLinkClaPage (io.pivotal.cla.webdriver.pages.admin.AdminLinkClaPage)1 Test (org.junit.Test)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1