package org.sonar.server.issue.ws;
import com.google.common.io.Resources;
-
-import org.sonar.api.server.ws.WebService.NewAction;
-import org.sonar.server.issue.IssueService;
-import org.sonar.api.utils.text.JsonWriter;
-import org.sonar.api.server.ws.WebService;
import org.sonar.api.server.ws.Request;
-import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.RequestHandler;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.server.ws.WebService.NewAction;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.server.issue.IssueService;
/**
* List issue tags matching a given query.
import org.sonar.server.user.UserSession;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
return index.terms(RuleNormalizer.RuleField.ALL_TAGS.field());
}
+ /**
+ * List tags matching a given criterion
+ */
+ public Set<String> listTags(@Nullable String query, int size) {
+ /** using combined ALL_TAGS field of ES until ES update that has multiTerms aggregation */
+ return index.terms(RuleNormalizer.RuleField.ALL_TAGS.field(), query, size);
+ }
+
public void update(RuleUpdate update) {
checkPermission();
ruleUpdater.update(update, UserSession.get());
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.global.GlobalBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
+import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.sonar.server.search.StickyFacetBuilder;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
}
public Set<String> terms(String fields) {
+ return terms(fields, null, Integer.MAX_VALUE);
+ }
+
+ public Set<String> terms(String fields, @Nullable String query, int size) {
Set<String> tags = new HashSet<String>();
String key = "_ref";
+ TermsBuilder terms = AggregationBuilders.terms(key)
+ .field(fields)
+ .size(size)
+ .minDocCount(1);
+ if (query != null) {
+ terms.include(".*" + query + ".*");
+ }
SearchRequestBuilder request = this.getClient()
.prepareSearch(this.getIndexName())
.setQuery(QueryBuilders.matchAllQuery())
- .addAggregation(AggregationBuilders.terms(key)
- .field(fields)
- .size(Integer.MAX_VALUE)
- .minDocCount(1));
+ .addAggregation(terms);
SearchResponse esResponse = request.get();
import org.sonar.api.server.ws.RequestHandler;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
+import org.sonar.api.server.ws.WebService.NewAction;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.server.rule.RuleService;
}
void define(WebService.NewController controller) {
- controller
+ NewAction action = controller
.createAction("tags")
- .setDescription("List all rule tags")
+ .setDescription("List rule tags")
.setSince("4.4")
.setHandler(this)
.setResponseExample(Resources.getResource(getClass(), "example-tags.json"));
+
+ action.createParam("q")
+ .setDescription("A pattern to match tags against")
+ .setExampleValue("misra");
+ action.createParam("ps")
+ .setDescription("The size of the list to return, 0 for all tags")
+ .setExampleValue("25")
+ .setDefaultValue("0");
}
@Override
public void handle(Request request, Response response) {
- Set<String> tags = service.listTags();
+ String query = request.param("q");
+ int pageSize = request.mandatoryParamAsInt("ps");
+ Set<String> tags = service.listTags(query, pageSize);
JsonWriter json = response.newJsonWriter().beginObject();
json.name("tags").beginArray();
for (String tag : tags) {
ruleDao.insert(session, rule);
RuleDto rule2 = RuleTesting.newXooX2()
- .setTags(ImmutableSet.of("java"))
+ .setTags(ImmutableSet.of("hello", "java"))
.setSystemTags(ImmutableSet.of("sys1"));
ruleDao.insert(session, rule2);
session.commit();
MockUserSession.set();
- WsTester.TestRequest request = tester.wsTester().newGetRequest(API_ENDPOINT, API_TAGS_METHOD);
- WsTester.Result result = request.execute();
-
- result.assertJson(this.getClass(), "get_tags.json", false);
+ tester.wsTester().newGetRequest(API_ENDPOINT, API_TAGS_METHOD).execute().assertJson(this.getClass(), "get_tags.json", false);
+ tester.wsTester().newGetRequest(API_ENDPOINT, API_TAGS_METHOD)
+ .setParam("ps", "1").execute().assertJson(this.getClass(), "get_tags_limited.json", false);
+ tester.wsTester().newGetRequest(API_ENDPOINT, API_TAGS_METHOD)
+ .setParam("q", "ll").execute().assertJson(this.getClass(), "get_tags_filtered.json", false);
}
@Test
--- /dev/null
+{"tags": ["hello"]}
\ No newline at end of file
--- /dev/null
+{"tags": ["hello"]}
\ No newline at end of file