GitHubPRDescriptionEvent extends GitHubPREvent { @Override public GitHubPRCause check(@Nonnull GitHubPRDecisionContext prDecisionContext) throws IOException { TaskListener listener = prDecisionContext.getListener(); GitHubPRPullRequest localPR = prDecisionContext.getLocalPR(); GHPullRequest remotePR = prDecisionContext.getRemotePR(); String fullName = getFullName(prDecisionContext); final PrintStream logger = listener.getLogger(); GitHubPRCause cause = null; String pullRequestBody = remotePR.getBody().trim(); LOG.debug("Job: '{}', trigger event: '{}', body for test: '{}'", fullName, DISPLAY_NAME, escapeJava(pullRequestBody) ); if (StringUtils.isNotBlank(pullRequestBody)) { HashSet<String> skipBuildPhrases = new HashSet<>(Arrays.asList(getSkipMsg().split("[\\r\\n]+"))); skipBuildPhrases.remove(""); LOG.debug("Job: '{}', trigger event: '{}', skipBuildPhrases: '{}'", fullName, DISPLAY_NAME, skipBuildPhrases ); for (String skipBuildPhrase : skipBuildPhrases) { skipBuildPhrase = skipBuildPhrase.trim(); Pattern skipBuildPhrasePattern = Pattern.compile(skipBuildPhrase); if (skipBuildPhrasePattern.matcher(pullRequestBody).matches()) { LOG.info("Job: '{}', trigger event: '{}', PR body '{}' matches skipBuildPhrase '{}'.", fullName, DISPLAY_NAME, escapeJava(pullRequestBody), skipBuildPhrase ); logger.println(DISPLAY_NAME + ": Pull request description contains " + skipBuildPhrase + ", skipping"); cause = prDecisionContext.newCause("Pull request description contains " + skipBuildPhrase + ", skipping", true); break; } else { LOG.trace("Job: '{}', trigger event: '{}', phrase: '{}' didn't match to '{}'", fullName, DISPLAY_NAME, skipBuildPhrase, escapeJava(pullRequestBody) ); } } } return cause; } @DataBoundConstructor  GitHubPRDescriptionEvent(String skipMsg); @Override GitHubPRCause check(@Nonnull GitHubPRDecisionContext prDecisionContext); String getSkipMsg();  }
@Test public void skipDescriptionExist() throws IOException { commonExpectations(); causeCreationExpectations(); when(listener.getLogger()).thenReturn(logger); when(issue.getCreatedAt()).thenReturn(new Date()); when(remotePr.getBody()).thenReturn("must skip ci body"); GitHubPRCause cause = new GitHubPRDescriptionEvent(".*[skip ci].*") .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withRemotePR(remotePr) .withListener(listener) .build() ); assertNotNull(cause); assertThat(cause.isSkip(), is(true)); }
@Test public void skipDescriptionNotExist() throws IOException { commonExpectations(); causeCreationExpectations(); when(listener.getLogger()).thenReturn(logger); when(issue.getCreatedAt()).thenReturn(new Date()); when(remotePr.getBody()).thenReturn("unmatched comment"); GitHubPRCause cause = new GitHubPRDescriptionEvent(".*skip ci.*") .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withRemotePR(remotePr) .withListener(listener) .build() ); assertThat(cause, nullValue()); }