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;
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")
.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());
+ }
+ }
}
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");
}