Search in sources :

Example 1 with PullRequestId

use of io.pivotal.cla.egit.github.core.PullRequestId in project pivotal-cla by pivotalsoftware.

the class GitHubHooksControllerTests method markCommitStatusSuccessIndividualAcceptingIssueComments.

@Test
public void markCommitStatusSuccessIndividualAcceptingIssueComments() 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));
    when(gitHubApiMock.getShaForPullRequest(any(PullRequestId.class))).thenReturn("a6befb598a35c1c206e1bf7bbb3018f4403b9610");
    mockMvc.perform(hookRequest().header("X-GitHub-Event", GithubEvents.ISSUE_COMMENT).content(getPayload("issue_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());
}
Also used : User(io.pivotal.cla.data.User) AccessToken(io.pivotal.cla.data.AccessToken) PullRequestStatus(io.pivotal.cla.service.github.PullRequestStatus) PullRequestId(io.pivotal.cla.egit.github.core.PullRequestId) Test(org.junit.Test)

Example 2 with PullRequestId

use of io.pivotal.cla.egit.github.core.PullRequestId in project pivotal-cla by pivotalsoftware.

the class MylynGitHubApi method createOrUpdatePullRequestComment.

private void createOrUpdatePullRequestComment(PullRequestId pullRequestId, PullRequestStatus commitStatus, boolean hasSignedCla, boolean obviousFix, ContextCommitStatus status, List<Comment> comments, String claUserLogin) throws IOException {
    String claLinkMarkdown = String.format("[%s](%s)", CONTRIBUTOR_LICENSE_AGREEMENT, status.getUrl());
    String userMentionMarkdown = String.format("@%s", commitStatus.getGitHubUsername());
    IssueService issues = getIssueService();
    List<Comment> claUserComments = // 
    comments.stream().filter(// 
    comment -> comment.getUser().getLogin().equals(claUserLogin)).collect(Collectors.toList());
    if (hasSignedCla) {
        String body = String.format("%s %s %s!", userMentionMarkdown, THANK_YOU, claLinkMarkdown);
        if (claUserComments.stream().anyMatch(c -> c.getBody().contains(PLEASE_SIGN))) {
            if (claUserComments.stream().anyMatch(c -> c.getBody().contains(THANK_YOU))) {
                return;
            }
            issues.createComment(pullRequestId.getRepositoryId(), commitStatus.getPullRequestId(), body);
        }
    } else {
        String sync = String.format("\n\n[Click here](%s) %s.", commitStatus.getSyncUrl(), TO_MANUALLY_SYNCHRONIZE_THE_STATUS);
        String faq = String.format("\n\nSee the [FAQ](%s) for %s.", commitStatus.getFaqUrl(), FREQUENTLY_ASKED_QUESTIONS);
        String oldBody = String.format("%s %s %s!", userMentionMarkdown, PLEASE_SIGN, claLinkMarkdown);
        String body = String.format("%s%s%s", oldBody, sync, faq);
        if (obviousFix) {
            if (claUserComments.stream().anyMatch(c -> c.getBody().contains(PLEASE_SIGN)) && claUserComments.stream().noneMatch(comment -> comment.getBody().contains(THIS_PR_CONTAINS_AN_OBVIOUS_FIX))) {
                createObviousFixCommentIfNecessary(pullRequestId, userMentionMarkdown, issues, claUserComments);
            }
            return;
        }
        if (claUserComments.stream().anyMatch(c -> c.getBody().contains(FREQUENTLY_ASKED_QUESTIONS) && c.getBody().contains(TO_MANUALLY_SYNCHRONIZE_THE_STATUS))) {
            return;
        }
        Optional<Comment> oldComment = claUserComments.stream().filter(c -> c.getBody().trim().contains(PLEASE_SIGN)).findFirst();
        if (oldComment.isPresent()) {
            Comment toEdit = oldComment.get();
            toEdit.setBody(body);
            issues.editComment(pullRequestId.getRepositoryId(), toEdit);
        } else {
            issues.createComment(pullRequestId.getRepositoryId(), pullRequestId.getId(), body);
        }
    }
}
Also used : IssueService(org.eclipse.egit.github.core.service.IssueService) ClaOAuthConfig(io.pivotal.cla.config.ClaOAuthConfig) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) java.util(java.util) PullRequest(org.eclipse.egit.github.core.PullRequest) EmailService(io.pivotal.cla.egit.github.core.service.EmailService) RequestException(org.eclipse.egit.github.core.client.RequestException) SneakyThrows(lombok.SneakyThrows) EventsRepositoryHook(io.pivotal.cla.egit.github.core.EventsRepositoryHook) Autowired(org.springframework.beans.factory.annotation.Autowired) ContextCommitStatus(io.pivotal.cla.egit.github.core.ContextCommitStatus) WithPermissionsRepository(io.pivotal.cla.egit.github.core.WithPermissionsRepository) CommitStatus(org.eclipse.egit.github.core.CommitStatus) RepositoryService(org.eclipse.egit.github.core.service.RepositoryService) Matcher(java.util.regex.Matcher) Email(io.pivotal.cla.egit.github.core.Email) GitHubClient(org.eclipse.egit.github.core.client.GitHubClient) ContextCommitService(io.pivotal.cla.egit.github.core.service.ContextCommitService) UrlBuilder(io.pivotal.cla.mvc.util.UrlBuilder) RestTemplate(org.springframework.web.client.RestTemplate) MarkdownService(org.eclipse.egit.github.core.service.MarkdownService) WithPermissionsRepositoryService(io.pivotal.cla.egit.github.core.service.WithPermissionsRepositoryService) MigratePullRequestStatusRequest(io.pivotal.cla.service.MigratePullRequestStatusRequest) User(io.pivotal.cla.data.User) IOException(java.io.IOException) OrganizationService(org.eclipse.egit.github.core.service.OrganizationService) Collectors(java.util.stream.Collectors) RepositoryHook(org.eclipse.egit.github.core.RepositoryHook) Comment(org.eclipse.egit.github.core.Comment) HttpStatus(org.springframework.http.HttpStatus) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) PullRequestId(io.pivotal.cla.egit.github.core.PullRequestId) IssueService(org.eclipse.egit.github.core.service.IssueService) Component(org.springframework.stereotype.Component) RepositoryId(org.eclipse.egit.github.core.RepositoryId) OAuthClientCredentials(io.pivotal.cla.config.OAuthClientCredentials) Data(lombok.Data) CommitComment(org.eclipse.egit.github.core.CommitComment) PullRequestService(org.eclipse.egit.github.core.service.PullRequestService) ResponseEntity(org.springframework.http.ResponseEntity) Pattern(java.util.regex.Pattern) GithubEvents(io.pivotal.cla.egit.github.core.event.GithubEvents) StringUtils(org.springframework.util.StringUtils) Comment(org.eclipse.egit.github.core.Comment) CommitComment(org.eclipse.egit.github.core.CommitComment)

Example 3 with PullRequestId

use of io.pivotal.cla.egit.github.core.PullRequestId in project pivotal-cla by pivotalsoftware.

the class MylynGitHubApi method createCommitStatusIfNecessary.

private ContextCommitStatus createCommitStatusIfNecessary(PullRequestId pullRequestId, PullRequestStatus commitStatus, boolean hasSignedCla, boolean obviousFix, GitHubClient client) {
    ContextCommitService commitService = new ContextCommitService(client);
    ContextCommitStatus status = new ContextCommitStatus();
    String description;
    if (obviousFix) {
        description = OBVIOUS_FIX_CLA_NOT_REQUIRED;
    } else if (hasSignedCla) {
        description = String.format("%s %s!", THANK_YOU, CONTRIBUTOR_LICENSE_AGREEMENT);
    } else {
        description = String.format("%s %s!", PLEASE_SIGN, CONTRIBUTOR_LICENSE_AGREEMENT);
    }
    status.setDescription(description);
    status.setState((hasSignedCla || obviousFix) ? CommitStatus.STATE_SUCCESS : CommitStatus.STATE_FAILURE);
    status.setContext("ci/pivotal-cla");
    status.setUrl(commitStatus.getUrl());
    status.setTargetUrl(status.getUrl());
    List<ContextCommitStatus> statuses = commitService.getContextStatuses(pullRequestId.getRepositoryId(), commitStatus.getSha());
    if (!statuses.stream().anyMatch(s -> matches(status, s))) {
        commitService.createStatus(pullRequestId.getRepositoryId(), commitStatus.getSha(), status);
    }
    return status;
}
Also used : ClaOAuthConfig(io.pivotal.cla.config.ClaOAuthConfig) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) java.util(java.util) PullRequest(org.eclipse.egit.github.core.PullRequest) EmailService(io.pivotal.cla.egit.github.core.service.EmailService) RequestException(org.eclipse.egit.github.core.client.RequestException) SneakyThrows(lombok.SneakyThrows) EventsRepositoryHook(io.pivotal.cla.egit.github.core.EventsRepositoryHook) Autowired(org.springframework.beans.factory.annotation.Autowired) ContextCommitStatus(io.pivotal.cla.egit.github.core.ContextCommitStatus) WithPermissionsRepository(io.pivotal.cla.egit.github.core.WithPermissionsRepository) CommitStatus(org.eclipse.egit.github.core.CommitStatus) RepositoryService(org.eclipse.egit.github.core.service.RepositoryService) Matcher(java.util.regex.Matcher) Email(io.pivotal.cla.egit.github.core.Email) GitHubClient(org.eclipse.egit.github.core.client.GitHubClient) ContextCommitService(io.pivotal.cla.egit.github.core.service.ContextCommitService) UrlBuilder(io.pivotal.cla.mvc.util.UrlBuilder) RestTemplate(org.springframework.web.client.RestTemplate) MarkdownService(org.eclipse.egit.github.core.service.MarkdownService) WithPermissionsRepositoryService(io.pivotal.cla.egit.github.core.service.WithPermissionsRepositoryService) MigratePullRequestStatusRequest(io.pivotal.cla.service.MigratePullRequestStatusRequest) User(io.pivotal.cla.data.User) IOException(java.io.IOException) OrganizationService(org.eclipse.egit.github.core.service.OrganizationService) Collectors(java.util.stream.Collectors) RepositoryHook(org.eclipse.egit.github.core.RepositoryHook) Comment(org.eclipse.egit.github.core.Comment) HttpStatus(org.springframework.http.HttpStatus) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) PullRequestId(io.pivotal.cla.egit.github.core.PullRequestId) IssueService(org.eclipse.egit.github.core.service.IssueService) Component(org.springframework.stereotype.Component) RepositoryId(org.eclipse.egit.github.core.RepositoryId) OAuthClientCredentials(io.pivotal.cla.config.OAuthClientCredentials) Data(lombok.Data) CommitComment(org.eclipse.egit.github.core.CommitComment) PullRequestService(org.eclipse.egit.github.core.service.PullRequestService) ResponseEntity(org.springframework.http.ResponseEntity) Pattern(java.util.regex.Pattern) GithubEvents(io.pivotal.cla.egit.github.core.event.GithubEvents) StringUtils(org.springframework.util.StringUtils) ContextCommitService(io.pivotal.cla.egit.github.core.service.ContextCommitService) ContextCommitStatus(io.pivotal.cla.egit.github.core.ContextCommitStatus)

Example 4 with PullRequestId

use of io.pivotal.cla.egit.github.core.PullRequestId in project pivotal-cla by pivotalsoftware.

the class MylynGitHubApi method save.

@SneakyThrows
public void save(PullRequestStatus commitStatus) {
    String repoId = commitStatus.getRepoId();
    String accessToken = commitStatus.getAccessToken();
    if (accessToken == null) {
        return;
    }
    PullRequestId pullRequestId = PullRequestId.of(RepositoryId.createFromId(repoId), commitStatus.getPullRequestId());
    boolean hasSignedCla = commitStatus.isSuccess();
    GitHubClient client = createClient(accessToken);
    String claUserLogin = getGitHubClaUserLogin();
    List<Comment> comments = getComments(pullRequestId, getIssueService());
    boolean obviousFix = isObviousFix(pullRequestId, comments, claUserLogin, commitStatus.getPullRequestBody());
    ContextCommitStatus status = createCommitStatusIfNecessary(pullRequestId, commitStatus, hasSignedCla, obviousFix, client);
    createOrUpdatePullRequestComment(pullRequestId, commitStatus, hasSignedCla, obviousFix, status, comments, claUserLogin);
}
Also used : Comment(org.eclipse.egit.github.core.Comment) CommitComment(org.eclipse.egit.github.core.CommitComment) GitHubClient(org.eclipse.egit.github.core.client.GitHubClient) PullRequestId(io.pivotal.cla.egit.github.core.PullRequestId) ContextCommitStatus(io.pivotal.cla.egit.github.core.ContextCommitStatus) SneakyThrows(lombok.SneakyThrows)

Aggregations

PullRequestId (io.pivotal.cla.egit.github.core.PullRequestId)4 User (io.pivotal.cla.data.User)3 ContextCommitStatus (io.pivotal.cla.egit.github.core.ContextCommitStatus)3 SneakyThrows (lombok.SneakyThrows)3 Comment (org.eclipse.egit.github.core.Comment)3 CommitComment (org.eclipse.egit.github.core.CommitComment)3 GitHubClient (org.eclipse.egit.github.core.client.GitHubClient)3 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 ClaOAuthConfig (io.pivotal.cla.config.ClaOAuthConfig)2 OAuthClientCredentials (io.pivotal.cla.config.OAuthClientCredentials)2 Email (io.pivotal.cla.egit.github.core.Email)2 EventsRepositoryHook (io.pivotal.cla.egit.github.core.EventsRepositoryHook)2 WithPermissionsRepository (io.pivotal.cla.egit.github.core.WithPermissionsRepository)2 GithubEvents (io.pivotal.cla.egit.github.core.event.GithubEvents)2 ContextCommitService (io.pivotal.cla.egit.github.core.service.ContextCommitService)2 EmailService (io.pivotal.cla.egit.github.core.service.EmailService)2 WithPermissionsRepositoryService (io.pivotal.cla.egit.github.core.service.WithPermissionsRepositoryService)2 UrlBuilder (io.pivotal.cla.mvc.util.UrlBuilder)2 MigratePullRequestStatusRequest (io.pivotal.cla.service.MigratePullRequestStatusRequest)2 IOException (java.io.IOException)2