import java.time.Instant;
import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.util.Strings;
private final AnticipatedTransitionRepository anticipatedTransitionRepository;
- public TransitionIssuesToAnticipatedStatesVisitor(AnticipatedTransitionRepository anticipatedTransitionRepository, IssueLifecycle issueLifecycle, CeTaskMessages ceTaskMessages) {
+ public TransitionIssuesToAnticipatedStatesVisitor(AnticipatedTransitionRepository anticipatedTransitionRepository,
+ IssueLifecycle issueLifecycle, CeTaskMessages ceTaskMessages) {
this.anticipatedTransitionRepository = anticipatedTransitionRepository;
this.issueLifecycle = issueLifecycle;
this.ceTaskMessages = ceTaskMessages;
public void beforeComponent(Component component) {
if (FILE.equals(component.getType())) {
anticipatedTransitions = anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component);
+ } else {
+ anticipatedTransitions = Collections.emptyList();
}
}
String componentKey = componentKeyLength > MAX_LENGTH ? ("..." + issue.componentKey().substring(componentKeyLength - MAX_LENGTH, componentKeyLength)) : issue.componentKey();
return String.format(TRANSITION_ERROR_TEMPLATE.replace("{}", "%s"), issue.getLine(), componentKey, e.getMessage());
}
-
}
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
public class TransitionIssuesToAnticipatedStatesVisitorTest {
+
@Rule
public LogTester logTester = new LogTester();
private final IssueLifecycle issueLifecycle = mock(IssueLifecycle.class);
private final CeTaskMessages ceTaskMessages = mock(CeTaskMessages.class);
- private final TransitionIssuesToAnticipatedStatesVisitor underTest = new TransitionIssuesToAnticipatedStatesVisitor(anticipatedTransitionRepository, issueLifecycle, ceTaskMessages);
+ private final TransitionIssuesToAnticipatedStatesVisitor underTest =
+ new TransitionIssuesToAnticipatedStatesVisitor(anticipatedTransitionRepository, issueLifecycle, ceTaskMessages);
@Test
public void givenMatchingAnticipatedTransitions_transitionsShouldBeAppliedToIssues() {
Component component = getComponent(Component.Type.FILE);
- when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
+ when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component))
+ .thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
DefaultIssue issue = getDefaultIssue(1, "abcdefghi", "issue message");
Component component = getComponent(Component.Type.FILE);
String exceptionMessage = "Cannot apply transition";
- when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
+ when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component))
+ .thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
doThrow(new IllegalStateException(exceptionMessage)).when(issueLifecycle).doManualTransition(any(), any(), any());
DefaultIssue issue = getDefaultIssue(1, "abcdefghi", "issue message");
issue.setComponentKey(component.getKey());
@Test
public void givenMatchingAnticipatedTransitionsOnResolvedIssue_transitionsShouldNotBeAppliedToIssues() {
Component component = getComponent(Component.Type.FILE);
- when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
+ when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component))
+ .thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
DefaultIssue issue = getDefaultIssue(1, "abcdefghi", "issue message");
issue.setStatus(STATUS_RESOLVED);
@Test
public void givenMatchingAnticipatedTransitions_whenIssueIsNotNew_transitionsShouldNotBeAppliedToIssues() {
Component component = getComponent(Component.Type.FILE);
- when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
+ when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component))
+ .thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
DefaultIssue issue = getDefaultIssue(1, "abcdefghi", "issue message");
issue.setNew(false);
@Test
public void givenNonMatchingAnticipatedTransitions_transitionsAreNotAppliedToIssues() {
Component component = getComponent(Component.Type.FILE);
- when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
+ when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component))
+ .thenReturn(getAnticipatedTransitions("projectKey", "fileName"));
DefaultIssue issue = getDefaultIssue(2, "abcdefghf", "another issue message");
@Test
public void givenMatchingAnticipatedTransitionsWithEmptyComment_transitionsShouldBeAppliedToIssuesAndDefaultCommentApplied() {
Component component = getComponent(Component.Type.FILE);
- when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component)).thenReturn(getAnticipatedTransitionsWithEmptyComment("projectKey", "fileName"));
+ when(anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component))
+ .thenReturn(getAnticipatedTransitionsWithEmptyComment("projectKey", "fileName"));
DefaultIssue issue = getDefaultIssue(1, "abcdefghi", "issue message");
verifyNoInteractions(anticipatedTransitionRepository);
}
+ @Test
+ public void givenAProjecComponent_the_issue_is_not_affected() {
+ Component component = getComponent(PROJECT);
+ DefaultIssue issue = getDefaultIssue(1, "abcdefghi", "issue message");
+
+ underTest.beforeComponent(component);
+ underTest.onIssue(component, issue);
+ assertThat(issue.getAnticipatedTransitionUuid()).isEmpty();
+ assertThat(issue.isBeingClosed()).isFalse();
+ }
+
private Collection<AnticipatedTransition> getAnticipatedTransitions(String projecKey, String fileName) {
- return Stream.of(new AnticipatedTransition("atuuid", projecKey, "admin", RuleKey.parse("repo:id"), "issue message", fileName, 1, "abcdefghi", DefaultTransitions.ACCEPT, "doing the transition in an anticipated way")).toList();
+ return Stream.of(new AnticipatedTransition("atuuid", projecKey, "admin", RuleKey.parse("repo:id"), "issue message", fileName, 1,
+ "abcdefghi", DefaultTransitions.ACCEPT, "doing the transition in an anticipated way")).toList();
}
private Collection<AnticipatedTransition> getAnticipatedTransitionsWithEmptyComment(String projecKey, String fileName) {
- return Stream.of(new AnticipatedTransition("atuuid", projecKey, "admin", RuleKey.parse("repo:id"), "issue message", fileName, 1, "abcdefghi", DefaultTransitions.ACCEPT, null)).toList();
+ return Stream.of(new AnticipatedTransition("atuuid", projecKey, "admin", RuleKey.parse("repo:id"), "issue message", fileName, 1,
+ "abcdefghi", DefaultTransitions.ACCEPT, null)).toList();
}
private Component getComponent(Component.Type type) {