From: Julien Lancelot Date: Wed, 21 Jan 2015 12:09:32 +0000 (+0100) Subject: SONAR-6047 Add dirPath in Issues Index X-Git-Tag: latest-silver-master-#65~150 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=45e68e75931ccef49f458e8e473c75330675f685;p=sonarqube.git SONAR-6047 Add dirPath in Issues Index --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueDoc.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueDoc.java index dbda8319ab7..fd166c6b898 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueDoc.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueDoc.java @@ -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; diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java index 8cf12c7b20f..a368fd142c9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndexDefinition.java @@ -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(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueResultSetIterator.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueResultSetIterator.java index c0e36fc19b7..072639ef48f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueResultSetIterator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueResultSetIterator.java @@ -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 { 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; + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexerTest.java index 22ca688cf24..ce16b9fa94d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueIndexerTest.java @@ -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")); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueResultSetIteratorTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueResultSetIteratorTest.java index ca05d7c35d2..bb540e9548d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueResultSetIteratorTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueResultSetIteratorTest.java @@ -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 issuesByKey = Maps.uniqueIndex(it, new Function() { + @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 diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueIndexerTest/index.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueIndexerTest/index.xml index 1e07da6e627..791b02efb2b 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueIndexerTest/index.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueIndexerTest/index.xml @@ -3,9 +3,9 @@ plugin_config_key="[null]" plugin_name="squid"/> + uuid="THE_PROJECT" module_uuid="[null]" module_uuid_path="." path="[null]"/> + uuid="THE_FILE" module_uuid="THE_PROJECT" module_uuid_path=".THE_PROJECT." path="src/main/java/TheFile.java"/> + + + + + + + + + + + + + diff --git a/server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueResultSetIteratorTest/shared.xml b/server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueResultSetIteratorTest/shared.xml index ae9dbb951e6..d6f9ba92098 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueResultSetIteratorTest/shared.xml +++ b/server/sonar-server/src/test/resources/org/sonar/server/issue/index/IssueResultSetIteratorTest/shared.xml @@ -2,8 +2,8 @@ - - + +