GitHubPRCommentEvent extends GitHubPREvent { @Override public GitHubPRCause check(@Nonnull GitHubPRDecisionContext prDecisionContext) { final TaskListener listener = prDecisionContext.getListener(); final PrintStream llog = listener.getLogger(); final GitHubPRPullRequest localPR = prDecisionContext.getLocalPR(); final GHPullRequest remotePR = prDecisionContext.getRemotePR(); final GitHubPRUserRestriction prUserRestriction = prDecisionContext.getPrUserRestriction(); GitHubPRCause cause = null; try { for (GHIssueComment issueComment : remotePR.getComments()) { if (isNull(localPR) || isNull(localPR.getLastCommentCreatedAt()) || localPR.getLastCommentCreatedAt().compareTo(issueComment.getCreatedAt()) < 0) { llog.printf("%s: state has changed (new comment found - '%s')%n", DISPLAY_NAME, issueComment.getBody()); cause = checkComment(prDecisionContext, issueComment, prUserRestriction, listener); if (nonNull(cause)) { break; } } } } catch (Exception e) { LOG.warn("Couldn't obtain comments: {}", e); listener.error("Couldn't obtain comments", e); } if (isNull(cause)) { LOG.debug("No matching comments found for {}", remotePR.getNumber()); llog.println("No matching comments found for " + remotePR.getNumber()); } return cause; } @DataBoundConstructor  GitHubPRCommentEvent(String comment); String getComment(); @Override GitHubPRCause check(@Nonnull GitHubPRDecisionContext prDecisionContext);  }
@Test public void testNullLocalComment() throws IOException { when(listener.getLogger()).thenReturn(logger); when(issue.getCreatedAt()).thenReturn(new Date()); when(comment.getBody()).thenReturn("body"); final ArrayList<GHIssueComment> ghIssueComments = new ArrayList<>(); ghIssueComments.add(comment); when(remotePr.getComments()).thenReturn(ghIssueComments); GitHubPRCause cause = new GitHubPRCommentEvent("Comment") .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withRemotePR(remotePr) .withListener(listener) .build() ); assertNull(cause); }
@Test public void testNullLocalCommentRemoteMatch() throws IOException { commonExpectations(emptySet()); causeCreationExpectations(); final String body = "test foo, bar tags please."; when(issue.getCreatedAt()).thenReturn(new Date()); when(comment.getCreatedAt()).thenReturn(new Date()); when(comment.getBody()).thenReturn(body); final ArrayList<GHIssueComment> ghIssueComments = new ArrayList<>(); ghIssueComments.add(comment); when(remotePr.getComments()).thenReturn(ghIssueComments); GitHubPRCause cause = new GitHubPRCommentEvent("test ([A-Za-z0-9 ,!]+) tags please.") .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withLocalPR(localPR) .withRemotePR(remotePr) .withListener(listener) .build() ); assertThat(cause.getCommentBody(), is(body)); assertThat(cause.getCommentBodyMatch(), is("foo, bar")); assertNotNull(cause); }
@Test public void firstCommentMatchSecondDont() throws IOException { commonExpectations(emptySet()); causeCreationExpectations(); when(issue.getCreatedAt()).thenReturn(new Date()); final String body = "test foo, bar tags please."; when(comment.getBody()).thenReturn(body); when(comment.getCreatedAt()).thenReturn(new Date()); final String body2 = "no matching in second comment"; when(comment2.getBody()).thenReturn(body2); when(comment2.getCreatedAt()).thenReturn(new Date()); final ArrayList<GHIssueComment> ghIssueComments = new ArrayList<>(); ghIssueComments.add(comment); ghIssueComments.add(comment2); when(remotePr.getComments()).thenReturn(ghIssueComments); GitHubPRCause cause = new GitHubPRCommentEvent("test ([A-Za-z0-9 ,!]+) tags please.") .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withLocalPR(localPR) .withRemotePR(remotePr) .withListener(listener) .build() ); assertThat(cause, notNullValue()); assertThat(cause.getCommentBody(), is(body)); assertThat(cause.getCommentBodyMatch(), is("foo, bar")); }
@Test public void testNoComments() throws IOException { when(remotePr.getComments()).thenReturn(emptyList()); when(remotePr.getNumber()).thenReturn(14); when(listener.getLogger()).thenReturn(logger); GitHubPRCause cause = new GitHubPRCommentEvent("Comment") .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withLocalPR(localPR) .withRemotePR(remotePr) .withListener(listener) .build() ); assertNull(cause); }
@Test public void testNullLocalPR() throws IOException { commonExpectations(emptySet()); causeCreationExpectations(); final String body = "test foo, bar tags please."; when(issue.getCreatedAt()).thenReturn(new Date()); when(comment.getCreatedAt()).thenReturn(new Date()); when(comment.getBody()).thenReturn(body); final ArrayList<GHIssueComment> ghIssueComments = new ArrayList<>(); ghIssueComments.add(comment); when(remotePr.getComments()).thenReturn(ghIssueComments); GitHubPRCause cause = new GitHubPRCommentEvent("test ([A-Za-z0-9 ,!]+) tags please.") .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withRemotePR(remotePr) .withListener(listener) .build() ); assertThat(cause.getCommentBody(), is(body)); assertThat(cause.getCommentBodyMatch(), is("foo, bar")); assertNotNull(cause); }