GitHubPRLabelAddedEvent extends GitHubPREvent { @Override public GitHubPRCause check(@Nonnull GitHubPRDecisionContext prDecisionContext) throws IOException { TaskListener listener = prDecisionContext.getListener(); GitHubPRPullRequest localPR = prDecisionContext.getLocalPR(); GHPullRequest remotePR = prDecisionContext.getRemotePR(); if (remotePR.getState().equals(GHIssueState.CLOSED)) { return null; } if (isNull(label)) { LOG.error("Label is null. Bad configured event: {}", getDescriptor().getDisplayName()); throw new IllegalStateException("Label is null. Bad configured event: " + getDescriptor().getDisplayName()); } if (localPR != null && localPR.getLabels().containsAll(label.getLabelsSet())) { return null; } GitHubPRCause cause = null; Collection<GHLabel> labels = remotePR.getRepository().getIssue(remotePR.getNumber()).getLabels(); Set<String> existingLabels = new HashSet<String>(); for (GHLabel curLabel : labels) { existingLabels.add(curLabel.getName()); } if (existingLabels.containsAll(label.getLabelsSet())) { final PrintStream logger = listener.getLogger(); logger.println(DISPLAY_NAME + ": state has changed (" + label.getLabelsSet() + " labels were added"); cause = prDecisionContext.newCause(label.getLabelsSet() + " labels were added", false); } return cause; } @DataBoundConstructor  GitHubPRLabelAddedEvent(GitHubPRLabel label); GitHubPRLabel getLabel(); @Override GitHubPRCause check(@Nonnull GitHubPRDecisionContext prDecisionContext);  }
@Test public void secondOfThreeLabelsWasAdded() 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 GitHubPRLabelAddedEvent(labels) .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withRemotePR(remotePr) .withListener(listener) .withLocalPR(localPR) .build() ); assertNull(cause); }
@Test @Ignore public void thirdOfThreeLabelsWasAdded() 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); causeCreationExpectations(); GitHubPRCause cause = new GitHubPRLabelAddedEvent(labels) .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withRemotePR(remotePr) .withListener(listener) .withLocalPR(localPR) .build() ); assertThat(cause.getLabels(), equalTo(localLabels)); }
@Test public void allLabelsAlreadyExist() 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); GitHubPRLabelAddedEvent instance = new GitHubPRLabelAddedEvent(labels); GitHubPRCause cause = instance.check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withRemotePR(remotePr) .withListener(listener) .withLocalPR(localPR) .build() ); ; assertNull(cause); }
@Test public void noLabelsWasRemoved() throws IOException { Set<String> localLabels = new HashSet<>(asList(MERGE, REVIEWED, LOCALLY_TESTED)); 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 GitHubPRLabelAddedEvent(labels) .check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withRemotePR(remotePr) .withListener(listener) .withLocalPR(localPR) .build() ); ; assertNull(cause); }