From 5eedd7e06021b78f49522f3d296bc9cd3939c415 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 29 Nov 2016 22:10:51 +0100 Subject: [PATCH] SONAR-8437 fix listing of issue tags and authors --- .../org/sonar/server/issue/index/IssueIndex.java | 14 +++++++------- .../sonar/server/issue/IssueServiceMediumTest.java | 13 ++++++++++++- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java index 54a4123c4f6..318461fa5e1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/index/IssueIndex.java @@ -82,6 +82,7 @@ import org.sonar.server.user.UserSession; import org.sonar.server.view.index.ViewIndexDefinition; import static com.google.common.collect.Lists.newArrayList; +import static java.lang.String.format; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.existsQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; @@ -210,7 +211,7 @@ public class IssueIndex extends BaseIndex { public IssueDoc getByKey(String key) { IssueDoc value = getNullableByKey(key); if (value == null) { - throw new NotFoundException(String.format("Issue with key '%s' does not exist", key)); + throw new NotFoundException(format("Issue with key '%s' does not exist", key)); } return value; } @@ -622,16 +623,15 @@ public class IssueIndex extends BaseIndex { .size(maxNumberOfTags) .order(Terms.Order.term(true)) .minDocCount(1L); - if (textQuery != null) { - issueTags.include(String.format(SUBSTRING_MATCH_REGEXP, textQuery)); - } TermsBuilder ruleTags = AggregationBuilders.terms(tagsOnRulesSubAggregation) .field(RuleIndexDefinition.FIELD_RULE_ALL_TAGS) .size(maxNumberOfTags) .order(Terms.Order.term(true)) .minDocCount(1L); if (textQuery != null) { - ruleTags.include(String.format(SUBSTRING_MATCH_REGEXP, textQuery)); + String escapedTextQuery = escapeSpecialRegexChars(textQuery); + issueTags.include(format(SUBSTRING_MATCH_REGEXP, escapedTextQuery)); + ruleTags.include(format(SUBSTRING_MATCH_REGEXP, escapedTextQuery)); } SearchResponse searchResponse = requestBuilder.addAggregation(topAggreg.subAggregation(issueTags).subAggregation(ruleTags)).get(); @@ -670,7 +670,7 @@ public class IssueIndex extends BaseIndex { .order(termsOrder) .minDocCount(1L); if (textQuery != null) { - aggreg.include(String.format(SUBSTRING_MATCH_REGEXP, textQuery)); + aggreg.include(format(SUBSTRING_MATCH_REGEXP, escapeSpecialRegexChars(textQuery))); } SearchResponse searchResponse = requestBuilder.addAggregation(aggreg).get(); @@ -704,7 +704,7 @@ public class IssueIndex extends BaseIndex { filter.must(termsQuery(IssueIndexDefinition.FIELD_ISSUE_COMPONENT_UUID, component.uuid())); break; default: - throw new IllegalStateException(String.format("Component of scope '%s' is not allowed", component.scope())); + throw new IllegalStateException(format("Component of scope '%s' is not allowed", component.scope())); } SearchRequestBuilder requestBuilder = getClient() diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java index f0b605833d7..ff940104b98 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/IssueServiceMediumTest.java @@ -245,6 +245,7 @@ public class IssueServiceMediumTest { assertThat(service.listTags("sys", 5)).containsOnly("systag1", "systag2"); assertThat(service.listTags(null, 1)).containsOnly("bug"); assertThat(service.listTags(null, Integer.MAX_VALUE)).containsOnly("convention", "java8", "bug", "systag1", "systag2", "tag1", "tag2"); + assertThat(service.listTags("invalidRegexp[", 5)).isEmpty(); } @Test @@ -303,7 +304,7 @@ public class IssueServiceMediumTest { } @Test - public void list_authors() { + public void test_listAuthors() { RuleDto rule = newRule(); ComponentDto project = newProject(); ComponentDto file = newFile(project); @@ -319,6 +320,16 @@ public class IssueServiceMediumTest { assertThat(service.listAuthors(null, Integer.MAX_VALUE)).containsExactly("anakin@skywalker.name", "luke.skywalker", "luke@skywalker.name"); } + @Test + public void listAuthors_escapes_regexp_special_characters() { + saveIssue(IssueTesting.newDto(newRule(), newFile(newProject()), newProject()).setAuthorLogin("name++")); + + assertThat(service.listAuthors("invalidRegexp[", 5)).isEmpty(); + assertThat(service.listAuthors("nam+", 5)).isEmpty(); + assertThat(service.listAuthors("name+", 5)).containsExactly("name++"); + assertThat(service.listAuthors(".*", 5)).isEmpty(); + } + private RuleDto newRule() { return newRule(RuleTesting.newXooX1()); } -- 2.39.5