Search in sources :

Example 1 with ContextCommitStatus

use of io.pivotal.cla.egit.github.core.ContextCommitStatus 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 2 with ContextCommitStatus

use of io.pivotal.cla.egit.github.core.ContextCommitStatus 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 3 with ContextCommitStatus

use of io.pivotal.cla.egit.github.core.ContextCommitStatus 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

ContextCommitStatus (io.pivotal.cla.egit.github.core.ContextCommitStatus)3 PullRequestId (io.pivotal.cla.egit.github.core.PullRequestId)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 User (io.pivotal.cla.data.User)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