From a13e10b240e4c9bfd53687950902d414c8ab6f62 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 29 Nov 2012 15:52:14 +0100 Subject: [PATCH] SONAR-812 Add the possibility to filter resources by language in the Sonar Web Service API --- .../app/controllers/api/resources_controller.rb | 5 +++++ .../org/sonar/wsclient/services/ResourceQuery.java | 11 +++++++++++ .../sonar/wsclient/services/ResourceQueryTest.java | 11 +++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/resources_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/resources_controller.rb index 2c53126195d..3251d9b9d80 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/resources_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/resources_controller.rb @@ -221,6 +221,11 @@ class Api::ResourcesController < Api::ApiController snapshots_conditions << 'projects.qualifier in (:qualifiers)' end + if params['language'] + snapshots_conditions << 'projects.language in (:language)' + snapshots_values[:language]=params['language'].split(',') + end + snapshots_including_resource=Snapshot.find(:all, :conditions => [snapshots_conditions.join(' AND '), snapshots_values], :include => 'project') # ---------- APPLY SECURITY - remove unauthorized resources - only if no selected resource diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceQuery.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceQuery.java index 5b8cb0ad89c..c4a8dc02c1d 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceQuery.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/services/ResourceQuery.java @@ -33,6 +33,7 @@ public class ResourceQuery extends Query { private String[] rules; private String[] ruleSeverities; private String[] characteristicKeys; + private String[] languages; private String model; private boolean excludeRules = true; private boolean excludeRuleSeverities = true; @@ -129,6 +130,15 @@ public class ResourceQuery extends Query { return this; } + public String[] getLanguages() { + return languages; + } + + public ResourceQuery setLanguages(String... languages) { + this.languages = languages; + return this; + } + /** * @deprecated since 2.5 See http://jira.codehaus.org/browse/SONAR-2007 */ @@ -266,6 +276,7 @@ public class ResourceQuery extends Query { appendUrlParameter(url, "includetrends", includeTrends); appendUrlParameter(url, "model", model); appendUrlParameter(url, "characteristics", characteristicKeys); + appendUrlParameter(url, "languages", languages); appendUrlParameter(url, "verbose", verbose); return url.toString(); } diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ResourceQueryTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ResourceQueryTest.java index 5d68774d960..6817a60acb4 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ResourceQueryTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/services/ResourceQueryTest.java @@ -19,12 +19,12 @@ */ package org.sonar.wsclient.services; +import org.junit.Test; + import static org.hamcrest.Matchers.is; import static org.hamcrest.core.IsNull.nullValue; import static org.junit.Assert.assertThat; -import org.junit.Test; - public class ResourceQueryTest extends QueryTestCase { @Test @@ -35,6 +35,13 @@ public class ResourceQueryTest extends QueryTestCase { assertThat(query.isVerbose(), is(false)); } + @Test + public void resourceByLanguages() { + ResourceQuery query = new ResourceQuery("org.foo:bar").setLanguages("java,php"); + assertThat(query.getUrl(), is("/api/resources?resource=org.foo%3Abar&languages=java%2Cphp&verbose=false&")); + assertThat(query.getResourceKeyOrId(), is("org.foo:bar")); + } + @Test public void measures() { ResourceQuery query = new ResourceQuery(); -- 2.39.5