]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4286 remove the assignee when resolving as false-positive
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 24 May 2013 07:53:45 +0000 (09:53 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 24 May 2013 07:53:59 +0000 (09:53 +0200)
sonar-core/src/main/java/org/sonar/core/issue/workflow/Function.java
sonar-core/src/main/java/org/sonar/core/issue/workflow/FunctionExecutor.java
sonar-core/src/main/java/org/sonar/core/issue/workflow/IssueWorkflow.java
sonar-core/src/main/java/org/sonar/core/issue/workflow/SetAssignee.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/issue/workflow/IssueWorkflowTest.java
sonar-core/src/test/java/org/sonar/core/issue/workflow/SetAssigneeTest.java [new file with mode: 0644]

index 578a3df0b247d0dec09eff71748c68e48584a0a3..556d5ac962a38284189bacb886ef239cc74be6e2 100644 (file)
@@ -26,6 +26,7 @@ import javax.annotation.Nullable;
 interface Function {
   interface Context {
     Issue issue();
+    Context setAssignee(@Nullable String s);
     Context setResolution(@Nullable String s);
     Context setCloseDate(boolean b);
   }
index c2d974e3defd5c8839ffe7c5c3ea9be1748f39d8..22b22e21ef8a1b35f577c5cf06285dff19e578cf 100644 (file)
@@ -26,6 +26,8 @@ import org.sonar.core.issue.DefaultIssue;
 import org.sonar.core.issue.IssueChangeContext;
 import org.sonar.core.issue.IssueUpdater;
 
+import javax.annotation.Nullable;
+
 public class FunctionExecutor implements BatchComponent, ServerComponent {
 
   private final IssueUpdater updater;
@@ -60,7 +62,13 @@ public class FunctionExecutor implements BatchComponent, ServerComponent {
     }
 
     @Override
-    public Function.Context setResolution(String s) {
+    public Function.Context setAssignee(@Nullable String s) {
+      updater.assign(issue, s, changeContext);
+      return this;
+    }
+
+    @Override
+    public Function.Context setResolution(@Nullable String s) {
       updater.setResolution(issue, s, changeContext);
       return this;
     }
index ff308798a0c1eb0427bf0e452ff2c8196735d326..4b4780f41229ed5b53f34f0adfadfc81d50f9b88 100644 (file)
@@ -80,20 +80,22 @@ public class IssueWorkflow implements BatchComponent, ServerComponent, Startable
         .from(Issue.STATUS_CLOSED).to(Issue.STATUS_REOPENED)
         .functions(new SetResolution(null), new SetCloseDate(false))
         .build())
+
+      // resolve as false-positive
       .transition(Transition.builder(DefaultTransitions.FALSE_POSITIVE)
         .from(Issue.STATUS_OPEN).to(Issue.STATUS_RESOLVED)
         .conditions(new IsManual(false))
-        .functions(new SetResolution(Issue.RESOLUTION_FALSE_POSITIVE))
+        .functions(new SetResolution(Issue.RESOLUTION_FALSE_POSITIVE), SetAssignee.UNASSIGN)
         .build())
       .transition(Transition.builder(DefaultTransitions.FALSE_POSITIVE)
         .from(Issue.STATUS_REOPENED).to(Issue.STATUS_RESOLVED)
         .conditions(new IsManual(false))
-        .functions(new SetResolution(Issue.RESOLUTION_FALSE_POSITIVE))
+        .functions(new SetResolution(Issue.RESOLUTION_FALSE_POSITIVE), SetAssignee.UNASSIGN)
         .build())
       .transition(Transition.builder(DefaultTransitions.FALSE_POSITIVE)
         .from(Issue.STATUS_CONFIRMED).to(Issue.STATUS_RESOLVED)
         .conditions(new IsManual(false))
-        .functions(new SetResolution(Issue.RESOLUTION_FALSE_POSITIVE))
+        .functions(new SetResolution(Issue.RESOLUTION_FALSE_POSITIVE), SetAssignee.UNASSIGN)
         .build())
 
         // automatic transitions
diff --git a/sonar-core/src/main/java/org/sonar/core/issue/workflow/SetAssignee.java b/sonar-core/src/main/java/org/sonar/core/issue/workflow/SetAssignee.java
new file mode 100644 (file)
index 0000000..a9116a6
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2013 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.core.issue.workflow;
+
+import javax.annotation.Nullable;
+
+public class SetAssignee implements Function {
+  public static final SetAssignee UNASSIGN = new SetAssignee(null);
+
+  private final String assignee;
+
+  public SetAssignee(@Nullable String assignee) {
+    this.assignee = assignee;
+  }
+
+  @Override
+  public void execute(Context context) {
+    context.setAssignee(assignee);
+  }
+}
index 8d5e3bbeb22bc343b7a8a9552e3ad9f58737477d..ae976307c3ca5193010c65fd6860e45543cddf5e 100644 (file)
@@ -22,8 +22,11 @@ package org.sonar.core.issue.workflow;
 import com.google.common.base.Function;
 import com.google.common.collect.Collections2;
 import org.junit.Test;
+import org.sonar.api.issue.DefaultTransitions;
 import org.sonar.api.issue.Issue;
+import org.sonar.api.rule.RuleKey;
 import org.sonar.core.issue.DefaultIssue;
+import org.sonar.core.issue.DefaultIssueBuilder;
 import org.sonar.core.issue.IssueChangeContext;
 import org.sonar.core.issue.IssueUpdater;
 
@@ -191,7 +194,25 @@ public class IssueWorkflowTest {
     } catch (IllegalStateException e) {
       assertThat(e).hasMessage("Unknown status: xxx [issue=ABCDE]");
     }
+  }
+
+  @Test
+  public void should_flag_as_false_positive() throws Exception {
+    DefaultIssue issue = new DefaultIssue()
+      .setKey("ABCDE")
+      .setStatus(Issue.STATUS_OPEN)
+      .setRuleKey(RuleKey.of("squid", "AvoidCycle"))
+      .setAssignee("morgan")
+      .setReporter("simon");
+
+    workflow.start();
+    workflow.doTransition(issue, DefaultTransitions.FALSE_POSITIVE, IssueChangeContext.createScan(new Date()));
+
+    assertThat(issue.resolution()).isEqualTo(Issue.RESOLUTION_FALSE_POSITIVE);
+    assertThat(issue.status()).isEqualTo(Issue.STATUS_RESOLVED);
 
+    // should remove assignee
+    assertThat(issue.assignee()).isNull();
   }
 
   private Collection<String> keys(List<Transition> transitions) {
diff --git a/sonar-core/src/test/java/org/sonar/core/issue/workflow/SetAssigneeTest.java b/sonar-core/src/test/java/org/sonar/core/issue/workflow/SetAssigneeTest.java
new file mode 100644 (file)
index 0000000..943cfb4
--- /dev/null
@@ -0,0 +1,24 @@
+package org.sonar.core.issue.workflow;
+
+import org.junit.Test;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+public class SetAssigneeTest {
+  @Test
+  public void assign() throws Exception {
+    SetAssignee function = new SetAssignee("eric");
+    Function.Context context = mock(Function.Context.class);
+    function.execute(context);
+    verify(context, times(1)).setAssignee("eric");
+  }
+
+  @Test
+  public void unassign() throws Exception {
+    Function.Context context = mock(Function.Context.class);
+    SetAssignee.UNASSIGN.execute(context);
+    verify(context, times(1)).setAssignee(null);
+  }
+}