]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4865 Keep issue locations on existing issues 593/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 20 Oct 2015 13:11:16 +0000 (15:11 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 21 Oct 2015 15:40:05 +0000 (17:40 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/issue/IssueLifecycle.java
server/sonar-server/src/main/java/org/sonar/server/computation/issue/TrackerRawInputFactory.java
server/sonar-server/src/test/java/org/sonar/server/computation/issue/IssueLifecycleTest.java
sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java
sonar-core/src/main/java/org/sonar/core/issue/IssueUpdater.java
sonar-core/src/test/java/org/sonar/core/issue/IssueUpdaterTest.java

index 93cbffd32cdfa0f34179d054b34fb61e41db71e5..a7f71a02c03f99f2523eb289f7c57852b8d905e3 100644 (file)
@@ -88,6 +88,7 @@ public class IssueLifecycle {
 
     // fields coming from raw
     updater.setPastLine(raw, base.getLine());
+    updater.setPastLocations(raw, base.getLocations());
     updater.setPastMessage(raw, base.getMessage(), changeContext);
     updater.setPastEffortToFix(raw, base.effortToFix(), changeContext);
     updater.setPastTechnicalDebt(raw, base.debt(), changeContext);
index 2d670a756a49a7040f4f97fc7b2000a215a610f2..fca45414d4c71c524d731ea546343acbb9834e74 100644 (file)
@@ -19,9 +19,6 @@
  */
 package org.sonar.server.computation.issue;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
 import org.sonar.api.issue.Issue;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.utils.log.Loggers;
@@ -40,6 +37,10 @@ import org.sonar.server.computation.issue.commonrule.CommonRuleEngine;
 import org.sonar.server.computation.source.SourceLinesRepository;
 import org.sonar.server.rule.CommonRuleKeys;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
 import static com.google.common.collect.Lists.newArrayList;
 
 public class TrackerRawInputFactory {
@@ -172,7 +173,7 @@ public class TrackerRawInputFactory {
     }
   }
 
-  private DbCommons.TextRange.Builder convertTextRange(BatchReport.TextRange sourceRange) {
+  private static DbCommons.TextRange.Builder convertTextRange(BatchReport.TextRange sourceRange) {
     DbCommons.TextRange.Builder targetRange = DbCommons.TextRange.newBuilder();
     if (sourceRange.hasStartLine()) {
       targetRange.setStartLine(sourceRange.getStartLine());
index 80fbd779a4604b9899b46c2ebfc52e917e16a7fa..ee8ec5f039ae8ec3eb17d642e45aba28bd6e0e3c 100644 (file)
@@ -28,6 +28,8 @@ import org.sonar.core.issue.DefaultIssue;
 import org.sonar.core.issue.IssueChangeContext;
 import org.sonar.core.issue.IssueUpdater;
 import org.sonar.core.issue.workflow.IssueWorkflow;
+import org.sonar.db.protobuf.DbCommons;
+import org.sonar.db.protobuf.DbIssues;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.assertj.core.api.Assertions.assertThat;
@@ -97,6 +99,13 @@ public class IssueLifecycleTest {
       .setCreationDate(parseDate("2015-10-01"))
       .setUpdateDate(parseDate("2015-10-02"))
       .setCloseDate(parseDate("2015-10-03"));
+
+    DbIssues.Locations issueLocations = DbIssues.Locations.newBuilder()
+      .setTextRange(DbCommons.TextRange.newBuilder()
+        .setStartLine(10)
+        .setEndLine(12)
+        .build())
+      .build();
     DefaultIssue base = new DefaultIssue()
       .setKey("BASE_KEY")
       .setCreationDate(parseDate("2015-01-01"))
@@ -115,7 +124,8 @@ public class IssueLifecycleTest {
       .setMessage("message")
       .setEffortToFix(15d)
       .setDebt(Duration.create(15L))
-      .setManualSeverity(false);
+      .setManualSeverity(false)
+      .setLocations(issueLocations);
 
     when(debtCalculator.calculate(raw)).thenReturn(DEFAULT_DURATION);
 
@@ -140,6 +150,7 @@ public class IssueLifecycleTest {
     verify(updater).setPastLine(raw, 10);
     verify(updater).setPastMessage(raw, "message", issueChangeContext);
     verify(updater).setPastTechnicalDebt(raw, Duration.create(15L), issueChangeContext);
+    verify(updater).setPastLocations(raw, issueLocations);
   }
 
   @Test
index 46ac2fbad120f0a2f330cb143e5c852a63ec2a06..f9a4f4da016015643920b67202c945178bcb1373 100644 (file)
@@ -26,18 +26,6 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Maps;
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.commons.lang.builder.ToStringStyle;
@@ -49,6 +37,20 @@ import org.sonar.api.rule.Severity;
 import org.sonar.api.utils.Duration;
 import org.sonar.core.issue.tracking.Trackable;
 
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
 import static java.lang.String.format;
 
 public class DefaultIssue implements Issue, Trackable, org.sonar.api.ce.measure.Issue {
@@ -553,12 +555,14 @@ public class DefaultIssue implements Issue, Trackable, org.sonar.api.ce.measure.
     return this;
   }
 
+  @CheckForNull
   public <T> T getLocations() {
     return (T)locations;
   }
 
-  public void setLocations(Object locations) {
+  public DefaultIssue setLocations(@Nullable Object locations) {
     this.locations = locations;
+    return this;
   }
 
   @Override
index 851523cb5be290f815f01d841d226412c36cec4e..a7d0edb1803878b4a2d9b88e67654d544ff6a367 100644 (file)
@@ -123,6 +123,22 @@ public class IssueUpdater {
     return setLine(issue, currentLine);
   }
 
+  public boolean setLocations(DefaultIssue issue, @Nullable Object locations) {
+    if (!Objects.equal(locations, issue.getLocations())) {
+      issue.setLocations(locations);
+      issue.setChanged(true);
+      return true;
+    }
+    return false;
+  }
+
+  public boolean setPastLocations(DefaultIssue issue, @Nullable Object previousLocations) {
+    Object currentLocations = issue.getLocations();
+    issue.setLocations(previousLocations);
+    return setLocations(issue, currentLocations);
+
+  }
+
   public boolean setResolution(DefaultIssue issue, @Nullable String resolution, IssueChangeContext context) {
     if (!Objects.equal(resolution, issue.resolution())) {
       issue.setFieldChange(context, RESOLUTION, issue.resolution(), resolution);
@@ -302,4 +318,5 @@ public class IssueUpdater {
     return false;
   }
 
+
 }
index ab5954ac145f7b7be02e39756ae20c4ca295ad89..86f103885c0f1c4438ab27422864cb3ad73d9d54 100644 (file)
@@ -202,7 +202,7 @@ public class IssueUpdaterTest {
   }
 
   @Test
-  public void not_change_line() {
+  public void line_is_not_changed() {
     issue.setLine(123);
     boolean updated = updater.setLine(issue, 123);
     assertThat(updated).isFalse();
@@ -211,6 +211,36 @@ public class IssueUpdaterTest {
     assertThat(issue.mustSendNotifications()).isFalse();
   }
 
+  @Test
+  public void change_locations() {
+    issue.setLocations("[1-3]");
+    boolean updated = updater.setLocations(issue, "[1-4]");
+    assertThat(updated).isTrue();
+    assertThat(issue.getLocations()).isEqualTo("[1-4]");
+    assertThat(issue.currentChange()).isNull();
+    assertThat(issue.mustSendNotifications()).isFalse();
+  }
+
+  @Test
+  public void do_not_change_locations() {
+    issue.setLocations("[1-3]");
+    boolean updated = updater.setLocations(issue, "[1-3]");
+    assertThat(updated).isFalse();
+    assertThat(issue.getLocations()).isEqualTo("[1-3]");
+    assertThat(issue.currentChange()).isNull();
+    assertThat(issue.mustSendNotifications()).isFalse();
+  }
+
+  @Test
+  public void set_locations_for_the_first_time() {
+    issue.setLocations(null);
+    boolean updated = updater.setLocations(issue, "[1-4]");
+    assertThat(updated).isTrue();
+    assertThat(issue.getLocations()).isEqualTo("[1-4]");
+    assertThat(issue.currentChange()).isNull();
+    assertThat(issue.mustSendNotifications()).isFalse();
+  }
+
   @Test
   public void set_resolution() {
     boolean updated = updater.setResolution(issue, "OPEN", context);