GitHubPRLabelNotExistsEvent extends GitHubPREvent { @Override public GitHubPRCause check(@Nonnull GitHubPRDecisionContext prDecisionContext) throws IOException { TaskListener listener = prDecisionContext.getListener(); GHPullRequest remotePR = prDecisionContext.getRemotePR(); if (remotePR.getState().equals(GHIssueState.CLOSED)) { return null; } GitHubPRCause cause = null; Collection<GHLabel> remoteLabels = remotePR.getRepository().getIssue(remotePR.getNumber()).getLabels(); Set<String> existingLabels = new HashSet<>(); for (GHLabel ghLabel : remoteLabels) { existingLabels.add(ghLabel.getName()); } existingLabels.retainAll(label.getLabelsSet()); if (existingLabels.isEmpty()) { final PrintStream logger = listener.getLogger(); LOG.debug("{}:{} not found", DISPLAY_NAME, label.getLabelsSet()); logger.println(DISPLAY_NAME + ": " + label.getLabelsSet() + " not found"); cause = prDecisionContext.newCause(label.getLabelsSet() + " labels not exist", isSkip()); } return cause; } @DataBoundConstructor  GitHubPRLabelNotExistsEvent(GitHubPRLabel label, boolean skip); @Override GitHubPRCause check(@Nonnull GitHubPRDecisionContext prDecisionContext); GitHubPRLabel getLabel(); boolean isSkip();  }
@Test public void secondOfThreeLabelsExists() throws IOException { Set<String> localLabels = new HashSet<>(Collections.singleton(LOCALLY_TESTED)); List<GHLabel> remoteLabels = asList(testLabel, reviewedLabel); commonExpectations(localLabels); when(issue.getLabels()).thenReturn(remoteLabels); when(testLabel.getName()).thenReturn(LOCALLY_TESTED); when(reviewedLabel.getName()).thenReturn(REVIEWED); GitHubPRCause cause = new GitHubPRLabelNotExistsEvent(labels, false) .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withLocalPR(localPR) .withRemotePR(remotePr) .withListener(listener) .build() ); assertNull(cause); }
@Test public void twoOfThreeLabelsExists() throws IOException { Set<String> localLabels = new HashSet<>(asList(LOCALLY_TESTED, MERGE)); List<GHLabel> remoteLabels = asList(testLabel, reviewedLabel, mergeLabel); commonExpectations(localLabels); when(issue.getLabels()).thenReturn(remoteLabels); when(testLabel.getName()).thenReturn(LOCALLY_TESTED); when(reviewedLabel.getName()).thenReturn(REVIEWED); when(mergeLabel.getName()).thenReturn(MERGE); GitHubPRCause cause = new GitHubPRLabelNotExistsEvent(labels, false) .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withLocalPR(localPR) .withRemotePR(remotePr) .withListener(listener) .build() ); assertNull(cause); }
@Test public void allLabelsExist() throws IOException { Set<String> localLabels = new HashSet<>(asList(LOCALLY_TESTED, MERGE, REVIEWED)); List<GHLabel> remoteLabels = asList(testLabel, reviewedLabel, mergeLabel); commonExpectations(localLabels); when(issue.getLabels()).thenReturn(remoteLabels); when(testLabel.getName()).thenReturn(LOCALLY_TESTED); when(reviewedLabel.getName()).thenReturn(REVIEWED); when(mergeLabel.getName()).thenReturn(MERGE); GitHubPRCause cause = new GitHubPRLabelNotExistsEvent(labels, false) .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withLocalPR(localPR) .withRemotePR(remotePr) .withListener(listener) .build() ); assertNull(cause); }
@Test public void allLabelsExistAndSkip() throws IOException { Set<String> localLabels = new HashSet<>(asList(LOCALLY_TESTED, MERGE, REVIEWED)); List<GHLabel> remoteLabels = asList(testLabel, reviewedLabel, mergeLabel); commonExpectations(localLabels); when(issue.getLabels()).thenReturn(remoteLabels); when(testLabel.getName()).thenReturn(LOCALLY_TESTED); when(reviewedLabel.getName()).thenReturn(REVIEWED); when(mergeLabel.getName()).thenReturn(MERGE); GitHubPRCause cause = new GitHubPRLabelNotExistsEvent(labels, true) .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withLocalPR(localPR) .withRemotePR(remotePr) .withListener(listener) .build() ); assertNull(cause); }
@Test public void someLabelExistsAndSkip() throws IOException { Set<String> localLabels = new HashSet<>(asList(LOCALLY_TESTED, REVIEWED)); List<GHLabel> remoteLabels = asList(reviewedLabel, mergeLabel); commonExpectations(localLabels); when(issue.getLabels()).thenReturn(remoteLabels); when(testLabel.getName()).thenReturn(LOCALLY_TESTED); when(reviewedLabel.getName()).thenReturn(REVIEWED); when(mergeLabel.getName()).thenReturn(MERGE); GitHubPRCause cause = new GitHubPRLabelNotExistsEvent(labels, true) .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withLocalPR(localPR) .withRemotePR(remotePr) .withListener(listener) .build() ); assertNull(cause); }