]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6047 Add dirPath in Issues Index
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 21 Jan 2015 12:09:32 +0000 (13:09 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 21 Jan 2015 14:10:30 +0000 (15:10 +0100)
server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueDoc.java
server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java
server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueResultSetIterator.java
server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexerTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueResultSetIteratorTest.java
server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueIndexerTest/index.xml
server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueResultSetIteratorTest/extract_directory_path.xml [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueResultSetIteratorTest/shared.xml

index dbda8319ab71aa50f4a8176d04fd65800c502694..fd166c6b898e281e1217fd2e9ba54dd1e6cd410d 100644 (file)
@@ -53,10 +53,15 @@ public class IssueDoc extends BaseDoc implements Issue {
     return getField(IssueIndexDefinition.FIELD_ISSUE_COMPONENT_UUID);
   }
 
+  @CheckForNull
   public String moduleUuid() {
     return getField(IssueIndexDefinition.FIELD_ISSUE_MODULE_UUID);
   }
 
+  public String modulePath() {
+    return getField(IssueIndexDefinition.FIELD_ISSUE_MODULE_PATH);
+  }
+
   @Override
   public String projectKey() {
     throw new IllegalStateException("projectKey is not available on server side");
@@ -195,6 +200,11 @@ public class IssueDoc extends BaseDoc implements Issue {
     return getNullableField(IssueIndexDefinition.FIELD_ISSUE_FILE_PATH);
   }
 
+  @CheckForNull
+  public String directoryPath() {
+    return getNullableField(IssueIndexDefinition.FIELD_ISSUE_DIRECTORY_PATH);
+  }
+
   public IssueDoc setKey(@Nullable String s) {
     setField(IssueIndexDefinition.FIELD_ISSUE_KEY, s);
     return this;
@@ -311,6 +321,11 @@ public class IssueDoc extends BaseDoc implements Issue {
     return this;
   }
 
+  public IssueDoc setDirectoryPath(@Nullable String s) {
+    setField(IssueIndexDefinition.FIELD_ISSUE_DIRECTORY_PATH, s);
+    return this;
+  }
+
   public IssueDoc setModuleUuidPath(@Nullable String s) {
     setField(IssueIndexDefinition.FIELD_ISSUE_MODULE_PATH, s);
     return this;
index 8cf12c7b20f124ab008d92c80c146af19c6abdf1..a368fd142c959650b3d05dcfbe84a6905c44e228 100644 (file)
@@ -68,6 +68,7 @@ public class IssueIndexDefinition implements IndexDefinition {
   public static final String FIELD_ISSUE_MODULE_UUID = "module";
   public static final String FIELD_ISSUE_MODULE_PATH = "modulePath";
   public static final String FIELD_ISSUE_PROJECT_UUID = "project";
+  public static final String FIELD_ISSUE_DIRECTORY_PATH = "dirPath";
   public static final String FIELD_ISSUE_REPORTER = "reporter";
   public static final String FIELD_ISSUE_RESOLUTION = "resolution";
   public static final String FIELD_ISSUE_RULE_KEY = "ruleKey";
@@ -129,6 +130,7 @@ public class IssueIndexDefinition implements IndexDefinition {
     issueMapping.stringFieldBuilder(FIELD_ISSUE_MODULE_UUID).build();
     issueMapping.createUuidPathField(FIELD_ISSUE_MODULE_PATH);
     issueMapping.stringFieldBuilder(FIELD_ISSUE_PROJECT_UUID).enableSorting().build();
+    issueMapping.stringFieldBuilder(FIELD_ISSUE_DIRECTORY_PATH).build();
     issueMapping.stringFieldBuilder(FIELD_ISSUE_REPORTER).build();
     issueMapping.stringFieldBuilder(FIELD_ISSUE_RESOLUTION).build();
     issueMapping.stringFieldBuilder(FIELD_ISSUE_RULE_KEY).build();
index c0e36fc19b783de3850db03cf46d5d689d5f3b3c..072639ef48f6420cab7b8f788da8936c6dc2a49d 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.issue.index;
 
+import com.google.common.base.CharMatcher;
 import com.google.common.base.Splitter;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Maps;
@@ -28,6 +29,9 @@ import org.sonar.server.db.DbClient;
 import org.sonar.server.db.ResultSetIterator;
 import org.sonar.server.db.migrations.SqlUtil;
 
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -134,8 +138,22 @@ class IssueResultSetIterator extends ResultSetIterator<IssueDoc> {
     doc.setModuleUuid(rs.getString(23));
     doc.setModuleUuidPath(rs.getString(24));
     doc.setFilePath(rs.getString(25));
+    doc.setDirectoryPath(extractDirPath(doc.filePath()));
     String tags = rs.getString(26);
     doc.setTags(ImmutableList.copyOf(TAGS_SPLITTER.split(tags == null ? "" : tags)));
     return doc;
   }
+
+  @CheckForNull
+  private static String extractDirPath(@Nullable String filePath) {
+    if (filePath != null) {
+      StringBuilder dirPath = new StringBuilder("/");
+      int lastSlashIndex = CharMatcher.anyOf("/").lastIndexIn(filePath);
+      if (lastSlashIndex > 0) {
+        dirPath.append(filePath.substring(0, lastSlashIndex));
+      }
+      return dirPath.toString();
+    }
+    return null;
+  }
 }
index 22ca688cf2443be71826c602ba89ddea42027a2d..ce16b9fa94ddfb1cd21625a245376e5ad7ed3743 100644 (file)
@@ -62,6 +62,9 @@ public class IssueIndexerTest {
     IssueDoc doc = docs.get(0);
     assertThat(doc.projectUuid()).isEqualTo("THE_PROJECT");
     assertThat(doc.componentUuid()).isEqualTo("THE_FILE");
+    assertThat(doc.moduleUuid()).isEqualTo("THE_PROJECT");
+    assertThat(doc.modulePath()).isEqualTo(".THE_PROJECT.");
+    assertThat(doc.directoryPath()).isEqualTo("/src/main/java");
     assertThat(doc.severity()).isEqualTo("BLOCKER");
     assertThat(doc.ruleKey()).isEqualTo(RuleKey.of("squid", "AvoidCycles"));
 
index ca05d7c35d2d5c01ea1d2bf23bb954d62634b29f..bb540e9548d500700ac9adfd7fb8445d5fcb8090 100644 (file)
@@ -75,6 +75,7 @@ public class IssueResultSetIteratorTest {
     assertThat(issue.assignee()).isEqualTo("guy1");
     assertThat(issue.componentUuid()).isEqualTo("FILE1");
     assertThat(issue.projectUuid()).isEqualTo("PROJECT1");
+    assertThat(issue.filePath()).isEqualTo("src/main/java/Action.java");
     assertThat(issue.tags()).containsOnly("tag1", "tag2", "tag3");
     assertThat(issue.debt().toMinutes()).isGreaterThan(0L);
 
@@ -83,9 +84,33 @@ public class IssueResultSetIteratorTest {
     assertThat(issue.assignee()).isEqualTo("guy2");
     assertThat(issue.componentUuid()).isEqualTo("FILE1");
     assertThat(issue.projectUuid()).isEqualTo("PROJECT1");
+    assertThat(issue.filePath()).isEqualTo("src/main/java/Action.java");
     assertThat(issue.tags()).isEmpty();
     assertThat(issue.debt().toMinutes()).isGreaterThan(0L);
+  }
+
+  @Test
+  public void extract_directory_path() throws Exception {
+    dbTester.prepareDbUnit(getClass(), "extract_directory_path.xml");
+    IssueResultSetIterator it = IssueResultSetIterator.create(client, connection, 0L);
+    Map<String, IssueDoc> issuesByKey = Maps.uniqueIndex(it, new Function<IssueDoc, String>() {
+      @Override
+      public String apply(IssueDoc issue) {
+        return issue.key();
+      }
+    });
+    it.close();
+
+    assertThat(issuesByKey).hasSize(3);
+
+    // File in sub directoy
+    assertThat(issuesByKey.get("ABC").directoryPath()).isEqualTo("/src/main/java");
+
+    // File in root directoy
+    assertThat(issuesByKey.get("DEF").directoryPath()).isEqualTo("/");
 
+    // Project
+    assertThat(issuesByKey.get("EFG").directoryPath()).isNull();
   }
 
   @Test
index 1e07da6e627d4455a34d3be2f58baa928226f3d1..791b02efb2b5a386848a98d7507ca0631f0710b5 100644 (file)
@@ -3,9 +3,9 @@
          plugin_config_key="[null]" plugin_name="squid"/>
 
   <projects id="10" scope="PRJ" qualifier="TRK" kee="the_project" name="TheProject"
-            uuid="THE_PROJECT" module_uuid="[null]" module_uuid_path="."/>
+            uuid="THE_PROJECT" module_uuid="[null]" module_uuid_path="." path="[null]"/>
   <projects id="11" scope="FIL" qualifier="FIL" kee="abcde" name="TheFile"
-            uuid="THE_FILE" module_uuid="THE_PROJECT" module_uuid_path="."/>
+            uuid="THE_FILE" module_uuid="THE_PROJECT" module_uuid_path=".THE_PROJECT." path="src/main/java/TheFile.java"/>
 
   <issues id="1"
           kee="ABCDE"
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueResultSetIteratorTest/extract_directory_path.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueResultSetIteratorTest/extract_directory_path.xml
new file mode 100644 (file)
index 0000000..47fc260
--- /dev/null
@@ -0,0 +1,90 @@
+<dataset>
+  <rules tags="[null]" system_tags="[null]" id="200" name="Avoid Cycles" plugin_rule_key="AvoidCycles"
+         plugin_config_key="[null]" plugin_name="squid"/>
+
+  <projects id="10" scope="PRJ" qualifier="TRK" kee="struts" name="Struts" uuid="PROJECT1" path="[null]"/>
+  <projects id="100" scope="FIL" qualifier="CLA" kee="struts:Action" name="Action" uuid="FILE1" path="src/main/java/Action.java"/>
+  <projects id="101" scope="FIL" qualifier="CLA" kee="pom" name="pom.xml" uuid="ROOT_FILE1" path="pom.xml"/>
+
+  <issues
+      id="1"
+      kee="ABC"
+      resolution="FIXED"
+      status="RESOLVED"
+      severity="BLOCKER"
+      manual_severity="[false]"
+      assignee="guy1"
+      author_login="guy2"
+      checksum="FFFFF"
+      effort_to_fix="[null]"
+      technical_debt="10"
+      message="[null]"
+      line="444"
+      component_id="100"
+      root_component_id="10"
+      rule_id="200"
+      reporter="[null]"
+      issue_attributes="JIRA=http://jira.com"
+      action_plan_key="PLAN1"
+      tags="tag1,tag2,tag3"
+      created_at="1400000000000"
+      updated_at="1400000000000"
+      issue_creation_date="2005-05-12 00:00:00.0"
+      issue_update_date="2013-01-01 00:00:00.0"
+      issue_close_date="[null]"/>
+
+  <issues
+      id="2"
+      kee="DEF"
+      resolution="[null]"
+      status="OPEN"
+      severity="MAJOR"
+      manual_severity="[false]"
+      assignee="guy2"
+      author_login="[null]"
+      checksum="FFFFF"
+      effort_to_fix="[null]"
+      technical_debt="15"
+      message="[null]"
+      line="444"
+      component_id="101"
+      root_component_id="10"
+      rule_id="200"
+      reporter="[null]"
+      issue_attributes="JIRA=http://jira.com"
+      action_plan_key="PLAN2"
+      tags="[null]"
+      created_at="1400000000000"
+      updated_at="1450000000000"
+      issue_creation_date="2005-05-12 00:00:00.0"
+      issue_update_date="2013-05-18 00:00:00.0"
+      issue_close_date="[null]"/>
+
+  <issues
+      id="3"
+      kee="EFG"
+      resolution="[null]"
+      status="OPEN"
+      severity="MAJOR"
+      manual_severity="[false]"
+      assignee="guy2"
+      author_login="[null]"
+      checksum="FFFFF"
+      effort_to_fix="[null]"
+      technical_debt="15"
+      message="[null]"
+      line="444"
+      component_id="10"
+      root_component_id="10"
+      rule_id="200"
+      reporter="[null]"
+      issue_attributes="JIRA=http://jira.com"
+      action_plan_key="PLAN2"
+      tags="[null]"
+      created_at="1400000000000"
+      updated_at="1450000000000"
+      issue_creation_date="2005-05-12 00:00:00.0"
+      issue_update_date="2013-05-18 00:00:00.0"
+      issue_close_date="[null]"/>
+
+</dataset>
index ae9dbb951e6837c732a5c70726f01ecdbffdbd57..d6f9ba9209815aff2a981babb45182ea6b76be90 100644 (file)
@@ -2,8 +2,8 @@
   <rules tags="[null]" system_tags="[null]" id="200" name="Avoid Cycles" plugin_rule_key="AvoidCycles"
          plugin_config_key="[null]" plugin_name="squid"/>
 
-  <projects id="10" scope="PRJ" qualifier="TRK" kee="struts" name="Struts" uuid="PROJECT1"/>
-  <projects id="100" scope="FIL" qualifier="CLA" kee="struts:Action" name="Action" uuid="FILE1"/>
+  <projects id="10" scope="PRJ" qualifier="TRK" kee="struts" name="Struts" uuid="PROJECT1" path="[null]"/>
+  <projects id="100" scope="FIL" qualifier="CLA" kee="struts:Action" name="Action" uuid="FILE1" path="src/main/java/Action.java"/>
 
   <issues
     id="1"