From 4225de309130aceee9cf5d7fd15f28dee6bba7b6 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Mon, 30 Mar 2015 17:01:26 +0200 Subject: [PATCH] Have manual rules repository appear in api/rules/repositories WS --- .../server/rule/ws/RepositoriesAction.java | 47 +++++++++++++++---- .../rule/ws/RepositoriesActionTest.java | 7 +-- .../RepositoriesActionTest/repositories.json | 5 ++ .../repositories_manual.json | 9 ++++ .../repositories_sonar.json | 19 ++++++++ 5 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_manual.json create mode 100644 server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_sonar.json diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RepositoriesAction.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RepositoriesAction.java index db05d1ef7dc..8a2adbbd562 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RepositoriesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RepositoriesAction.java @@ -22,6 +22,7 @@ package org.sonar.server.rule.ws; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.io.Resources; +import org.sonar.api.rule.RuleKey; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -57,30 +58,43 @@ public class RepositoriesAction implements RulesAction { int pageSize = request.mandatoryParamAsInt("ps"); JsonWriter json = response.newJsonWriter().beginObject().name("repositories").beginArray(); - for (Repository repo : listMatchingRepositories(query, languageKey, pageSize)) { - json.beginObject().prop("key", repo.key()).prop("name", repo.name()).prop(LANGUAGE, repo.language()).endObject(); + for (Repo repo : listMatchingRepositories(query, languageKey, pageSize)) { + json.beginObject().prop("key", repo.key).prop("name", repo.name).prop(LANGUAGE, repo.language).endObject(); } json.endArray().endObject().close(); } - private Collection listMatchingRepositories(@Nullable String query, @Nullable String languageKey, int pageSize) { + private Collection listMatchingRepositories(@Nullable String query, @Nullable String languageKey, int pageSize) { Pattern pattern = Pattern.compile(query == null ? MATCH_ALL : MATCH_ALL + query + MATCH_ALL, Pattern.CASE_INSENSITIVE); - SortedMap reposByName = Maps.newTreeMap(); - Collection repos = languageKey == null ? repositories.repositories() : repositories.repositoriesForLang(languageKey); + SortedMap reposByName = Maps.newTreeMap(); + Collection repos = listRepositories(languageKey); - for (Repository repo : repos) { - if (pattern.matcher(repo.key()).matches() || pattern.matcher(repo.name()).matches()) { - reposByName.put(repo.name() + " -- " + repo.language(), repo); + for (Repo repo : repos) { + if (pattern.matcher(repo.key).matches() || pattern.matcher(repo.name).matches()) { + reposByName.put(repo.name + " -- " + repo.language, repo); } } - List result = Lists.newArrayList(reposByName.values()); + + List result = Lists.newArrayList(reposByName.values()); if (pageSize > 0 && pageSize < result.size()) { result = result.subList(0, pageSize); } return result; } + private Collection listRepositories(String languageKey) { + List allRepos = Lists.newArrayList(); + Collection reposFromPlugins = languageKey == null ? repositories.repositories() : repositories.repositoriesForLang(languageKey); + for (Repository repo: reposFromPlugins) { + allRepos.add(new Repo(repo)); + } + if (languageKey == null) { + allRepos.add(new Repo(RuleKey.MANUAL_REPOSITORY_KEY, "Manual Rule", "None")); + } + return allRepos; + } + @Override public void define(WebService.NewController controller) { NewAction action = controller.createAction("repositories") @@ -100,4 +114,19 @@ public class RepositoriesAction implements RulesAction { .setDefaultValue("0"); } + private static final class Repo { + private String key; + private String name; + private String language; + + private Repo(String key, String name, String language) { + this.key = key; + this.name = name; + this.language = language; + } + + private Repo(Repository repo) { + this(repo.key(), repo.name(), repo.language()); + } + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java index 7f059beb9a9..8eec2710eea 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java @@ -74,9 +74,10 @@ public class RepositoriesActionTest { newRequest().setParam("language", "ws").execute().assertJson(this.getClass(), "repositories_ws.json"); newRequest().setParam("q", "common").execute().assertJson(this.getClass(), "repositories_common.json"); newRequest().setParam("q", "squid").execute().assertJson(this.getClass(), "repositories_squid.json"); - newRequest().setParam("q", "sonar").execute().assertJson(this.getClass(), "repositories.json"); - newRequest().setParam("ps", "2").execute().assertJson(this.getClass(), "repositories_limited.json"); - newRequest().setParam("ps", "3").execute().assertJson(this.getClass(), "repositories.json"); + newRequest().setParam("q", "sonar").execute().assertJson(this.getClass(), "repositories_sonar.json"); + newRequest().setParam("q", "manu").execute().assertJson(this.getClass(), "repositories_manual.json"); + newRequest().setParam("q", "sonar").setParam("ps", "2").execute().assertJson(this.getClass(), "repositories_limited.json"); + newRequest().setParam("ps", "4").execute().assertJson(this.getClass(), "repositories.json"); newRequest().setParam("ps", "100").execute().assertJson(this.getClass(), "repositories.json"); } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories.json index 3635fef8561..b99c5aa6fb1 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories.json @@ -1,5 +1,10 @@ { "repositories": [ + { + "key": "manual", + "name": "Manual Rule", + "language": "None" + }, { "key": "squid", "name": "SonarQube", diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_manual.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_manual.json new file mode 100644 index 00000000000..4b36d1278e0 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_manual.json @@ -0,0 +1,9 @@ +{ + "repositories": [ + { + "key": "manual", + "name": "Manual Rule", + "language": "None" + } + ] +} \ No newline at end of file diff --git a/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_sonar.json b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_sonar.json new file mode 100644 index 00000000000..3635fef8561 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_sonar.json @@ -0,0 +1,19 @@ +{ + "repositories": [ + { + "key": "squid", + "name": "SonarQube", + "language": "ws" + }, + { + "key": "xoo", + "name": "SonarQube", + "language": "xoo" + }, + { + "key": "common-ws", + "name": "SonarQube Common", + "language": "ws" + } + ] +} \ No newline at end of file -- 2.39.5