GitHubPRLabelRemovedEvent 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(localPR)) { return null; } boolean hasLocal = false; for (String l : localPR.getLabels()) { for (String checkedLabel : label.getLabelsSet()) { if (checkedLabel.equals(l)) { hasLocal = true; break; } } if (hasLocal) { break; } } GitHubPRCause cause = null; boolean hasRemote = false; Collection<GHLabel> labels = remotePR.getRepository().getIssue(remotePR.getNumber()).getLabels(); for (GHLabel remoteLabel : labels) { for (String checkedLabel : label.getLabelsSet()) { if (checkedLabel.equals(remoteLabel.getName())) { hasRemote = true; break; } } if (hasRemote) { break; } } if (hasLocal && !hasRemote) { final PrintStream logger = listener.getLogger(); logger.println(DISPLAY_NAME + ": state has changed (" + label.getLabelsSet() + " labels were removed)"); cause = prDecisionContext.newCause(label.getLabelsSet() + " labels were removed", false); } return cause; } @DataBoundConstructor  GitHubPRLabelRemovedEvent(GitHubPRLabel label); @Override GitHubPRCause check(@Nonnull GitHubPRDecisionContext prDecisionContext); GitHubPRLabel getLabel();  }
@Test public void twoOfThreeLabelsWasRemoved() throws IOException { Set<String> localLabels = new HashSet<>(); localLabels.add(TESTS_FAILURE); List<GHLabel> remoteLabels = new ArrayList<>(); remoteLabels.add(label); commonExpectations(localLabels); when(issue.getLabels()).thenReturn(remoteLabels); when(label.getName()).thenReturn(TESTS_FAILURE); GitHubPRLabelRemovedEvent instance = new GitHubPRLabelRemovedEvent(labels); GitHubPRCause cause = instance.check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withLocalPR(localPR) .withRemotePR(remotePr) .withListener(listener) .build() ); Assert.assertNull(cause); }
@Test public void threeOfThreeLabelsWasRemoved() throws IOException { Set<String> localLabels = new HashSet<>(); localLabels.add(TESTS_FAILURE); commonExpectations(localLabels); when(issue.getLabels()) .thenReturn(new ArrayList<>()); causeCreationExpectations(); GitHubPRLabelRemovedEvent instance = new GitHubPRLabelRemovedEvent(labels); GitHubPRCause cause = instance.check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withLocalPR(localPR) .withRemotePR(remotePr) .withListener(listener) .build() ); Assert.assertEquals("[tests failure, not reviewed, not ready for merge] labels were removed", cause.getReason()); }
@Test public void noLabelsWasRemoved() throws IOException { Set<String> localLabels = new HashSet<>(); localLabels.add(NOT_READY_FOR_MERGE); localLabels.add(NOT_REVIEWED); localLabels.add(TESTS_FAILURE); List<GHLabel> remoteLabels = new ArrayList<>(); for (int i = 0; i < localLabels.size(); i++) { remoteLabels.add(label); } commonExpectations(localLabels); when(issue.getLabels()).thenReturn(remoteLabels); when(label.getName()).thenReturn(TESTS_FAILURE); when(label.getName()).thenReturn(NOT_READY_FOR_MERGE); when(label.getName()).thenReturn(NOT_REVIEWED); GitHubPRLabelRemovedEvent instance = new GitHubPRLabelRemovedEvent(labels); GitHubPRCause cause = instance.check(newGitHubPRDecisionContext() .withPrTrigger(trigger) .withLocalPR(localPR) .withRemotePR(remotePr) .withListener(listener) .build() ); Assert.assertNull(cause); }