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());
}
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);
}
}
}
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;
}
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);
}
Aggregations