]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12717 store ruleId padded with 0s to fix sorting on this field
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 2 Dec 2019 10:15:14 +0000 (11:15 +0100)
committerSonarTech <sonartech@sonarsource.com>
Mon, 13 Jan 2020 19:46:25 +0000 (20:46 +0100)
server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueDoc.java
server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueIndex.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchAction.java

index 8680eb6a2f49928b7931fe11f7dfaf5b8933dc99..88e8254667aabbbab6753c9db2f24c064c53a039 100644 (file)
@@ -25,9 +25,11 @@ import java.util.Date;
 import java.util.Map;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
+import org.apache.commons.lang.StringUtils;
 import org.sonar.api.rule.Severity;
 import org.sonar.api.rules.RuleType;
 import org.sonar.api.utils.Duration;
+import org.sonar.db.rule.RuleDefinitionDto;
 import org.sonar.server.es.BaseDoc;
 import org.sonar.server.permission.index.AuthorizationDoc;
 import org.sonar.server.security.SecurityStandards;
@@ -79,7 +81,8 @@ public class IssueDoc extends BaseDoc {
   }
 
   public Integer ruleId() {
-    return getField(IssueIndexDefinition.FIELD_ISSUE_RULE_ID);
+    String field = getField(IssueIndexDefinition.FIELD_ISSUE_RULE_ID);
+    return Integer.valueOf(field);
   }
 
   public String language() {
@@ -190,10 +193,20 @@ public class IssueDoc extends BaseDoc {
   }
 
   public IssueDoc setRuleId(Integer s) {
-    setField(IssueIndexDefinition.FIELD_ISSUE_RULE_ID, s);
+    // leftpad with 0 to have correct sorting on this fields (other 10 becomes lower than 9)
+    String str = formatRuleId(s);
+    setField(IssueIndexDefinition.FIELD_ISSUE_RULE_ID, str);
     return this;
   }
 
+  public static String formatRuleId(RuleDefinitionDto dto) {
+    return formatRuleId(dto.getId());
+  }
+
+  private static String formatRuleId(int s) {
+    return StringUtils.leftPad(String.valueOf(s), 10, "0");
+  }
+
   public IssueDoc setLanguage(@Nullable String s) {
     setField(IssueIndexDefinition.FIELD_ISSUE_LANGUAGE, s);
     return this;
index 58ca0dccbfca2cfb1462553589490ecb0d29d792..9a2e700f34132ae17ae60976e999616bfed3b3df 100644 (file)
@@ -72,7 +72,6 @@ import org.sonar.api.rules.RuleType;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.stream.MoreCollectors;
-import org.sonar.db.rule.RuleDefinitionDto;
 import org.sonar.server.es.BaseDoc;
 import org.sonar.server.es.EsClient;
 import org.sonar.server.es.EsUtils;
@@ -385,7 +384,7 @@ public class IssueIndex {
     filters.put(FIELD_ISSUE_AUTHOR_LOGIN, createTermsFilter(FIELD_ISSUE_AUTHOR_LOGIN, query.authors()));
     filters.put(FIELD_ISSUE_RULE_ID, createTermsFilter(
       FIELD_ISSUE_RULE_ID,
-      query.rules().stream().map(RuleDefinitionDto::getId).collect(toList())));
+      query.rules().stream().map(IssueDoc::formatRuleId).collect(toList())));
     filters.put(FIELD_ISSUE_STATUS, createTermsFilter(FIELD_ISSUE_STATUS, query.statuses()));
     filters.put(FIELD_ISSUE_ORGANIZATION_UUID, createTermFilter(FIELD_ISSUE_ORGANIZATION_UUID, query.organizationUuid()));
     filters.put(FIELD_ISSUE_OWASP_TOP_10, createTermsFilter(FIELD_ISSUE_OWASP_TOP_10, query.owaspTop10()));
@@ -629,7 +628,7 @@ public class IssueIndex {
       addSimpleStickyFacetIfNeeded(options, stickyFacetBuilder, esSearch, DIRECTORIES, query.directories().toArray());
       addSimpleStickyFacetIfNeeded(options, stickyFacetBuilder, esSearch, FILE_UUIDS, query.fileUuids().toArray());
       addSimpleStickyFacetIfNeeded(options, stickyFacetBuilder, esSearch, LANGUAGES, query.languages().toArray());
-      addSimpleStickyFacetIfNeeded(options, stickyFacetBuilder, esSearch, RULES, query.rules().stream().map(RuleDefinitionDto::getId).toArray());
+      addSimpleStickyFacetIfNeeded(options, stickyFacetBuilder, esSearch, RULES, query.rules().stream().map(IssueDoc::formatRuleId).toArray());
       addSimpleStickyFacetIfNeeded(options, stickyFacetBuilder, esSearch, AUTHORS, query.authors().toArray());
       addSimpleStickyFacetIfNeeded(options, stickyFacetBuilder, esSearch, AUTHOR, query.authors().toArray());
       addSimpleStickyFacetIfNeeded(options, stickyFacetBuilder, esSearch, TAGS, query.tags().toArray());
index 7c7c4746d5d94245aeed321cd5c73f429379c4bd..b2942ef5deb7919701afd22a042bdb431cfc687c 100644 (file)
@@ -52,6 +52,7 @@ import org.sonar.db.user.UserDto;
 import org.sonar.server.es.Facets;
 import org.sonar.server.es.SearchOptions;
 import org.sonar.server.issue.SearchRequest;
+import org.sonar.server.issue.index.IssueDoc;
 import org.sonar.server.issue.index.IssueIndex;
 import org.sonar.server.issue.index.IssueQuery;
 import org.sonar.server.issue.index.IssueQueryFactory;
@@ -481,7 +482,7 @@ public class SearchAction implements IssuesWsAction, Startable {
     }
     addMandatoryValuesToFacet(facets, PARAM_ASSIGNEES, assignees);
     addMandatoryValuesToFacet(facets, FACET_ASSIGNED_TO_ME, singletonList(userSession.getUuid()));
-    addMandatoryValuesToFacet(facets, PARAM_RULES, query.rules().stream().map(r -> Integer.toString(r.getId())).collect(toList()));
+    addMandatoryValuesToFacet(facets, PARAM_RULES, query.rules().stream().map(IssueDoc::formatRuleId).collect(toList()));
     addMandatoryValuesToFacet(facets, PARAM_LANGUAGES, request.getLanguages());
     addMandatoryValuesToFacet(facets, PARAM_TAGS, request.getTags());
     addMandatoryValuesToFacet(facets, PARAM_TYPES, RuleType.names());