]> source.dussan.org Git - sonarqube.git/commitdiff
Have manual rules repository appear in api/rules/repositories WS 179/head
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 30 Mar 2015 15:01:26 +0000 (17:01 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 30 Mar 2015 15:01:26 +0000 (17:01 +0200)
server/sonar-server/src/main/java/org/sonar/server/rule/ws/RepositoriesAction.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/RepositoriesActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories.json
server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_manual.json [new file with mode: 0644]
server/sonar-server/src/test/resources/org/sonar/server/rule/ws/RepositoriesActionTest/repositories_sonar.json [new file with mode: 0644]

index db05d1ef7dc79c8880c87e3a509caaa852f8835b..8a2adbbd562f0e06b5611d73b7eb587e8ff0a5cb 100644 (file)
@@ -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<Repository> listMatchingRepositories(@Nullable String query, @Nullable String languageKey, int pageSize) {
+  private Collection<Repo> 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<String, Repository> reposByName = Maps.newTreeMap();
-    Collection<Repository> repos = languageKey == null ? repositories.repositories() : repositories.repositoriesForLang(languageKey);
+    SortedMap<String, Repo> reposByName = Maps.newTreeMap();
+    Collection<Repo> 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<Repository> result = Lists.newArrayList(reposByName.values());
+
+    List<Repo> result = Lists.newArrayList(reposByName.values());
     if (pageSize > 0 && pageSize < result.size()) {
       result = result.subList(0, pageSize);
     }
     return result;
   }
 
+  private Collection<Repo> listRepositories(String languageKey) {
+    List<Repo> allRepos = Lists.newArrayList();
+    Collection<Repository> 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());
+    }
+  }
 }
index 7f059beb9a90dba2784fb5183fe48b4ce86c872a..8eec2710eea748c0940c90a13c710277eebff2cd 100644 (file)
@@ -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");
   }
 
index 3635fef8561d04e26ad6e26f84bb67a6154b09ca..b99c5aa6fb17b3e797d7bee4b9447657b76bb866 100644 (file)
@@ -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 (file)
index 0000000..4b36d12
--- /dev/null
@@ -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 (file)
index 0000000..3635fef
--- /dev/null
@@ -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