From 07d0c8f4bfd48c84d9dff39c1f69892e5a1dcc38 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 19 Jun 2013 08:28:09 +0200 Subject: [PATCH] SONAR-4383 Add WS to execute issue filter --- .../app/controllers/api/issues_controller.rb | 45 ++++++++++++------- .../org/sonar/wsclient/issue/IssueClient.java | 7 +++ .../issue/internal/DefaultIssueClient.java | 9 ++++ .../internal/DefaultIssueClientTest.java | 14 +++++- 4 files changed, 59 insertions(+), 16 deletions(-) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb index 7d21da4eb0d..4e3b01361c9 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/issues_controller.rb @@ -28,22 +28,20 @@ class Api::IssuesController < Api::ApiController # curl -v -u admin:admin 'http://localhost:9000/api/issues/search?statuses=OPEN,RESOLVED' # def search - results = Api.issues.find(params) - hash = { - :maxResultsReached => results.maxResultsReached, - :paging => paging_to_hash(results.paging), - :issues => results.issues.map { |issue| Issue.to_hash(issue) }, - :components => results.components.map { |component| component_to_hash(component) }, - :projects => results.projects.map { |project| component_to_hash(project) }, - :rules => results.rules.map { |rule| Rule.to_hash(rule) }, - :users => results.users.map { |user| User.to_hash(user) } - } - hash[:actionPlans] = results.actionPlans.map { |plan| ActionPlan.to_hash(plan) } if results.actionPlans.size>0 + render_result_issues(Api.issues.find(params)) + end - respond_to do |format| - format.json { render :json => jsonp(hash) } - format.xml { render :xml => hash.to_xml(:skip_types => true, :root => 'issues') } - end + # Load existing filter + # GET /api/issues/filter? + # + # -- Example + # curl -v -u admin:admin 'http://localhost:9000/api/issues/filter?filter=20' + # + # since 3.7 + # + def filter + require_parameters :filter + render_result_issues(Internal.issues.execute(params[:filter].to_i)) end # @@ -265,6 +263,23 @@ class Api::IssuesController < Api::ApiController end end + def render_result_issues(results) + hash = { + :maxResultsReached => results.maxResultsReached, + :paging => paging_to_hash(results.paging), + :issues => results.issues.map { |issue| Issue.to_hash(issue) }, + :components => results.components.map { |component| component_to_hash(component) }, + :projects => results.projects.map { |project| component_to_hash(project) }, + :rules => results.rules.map { |rule| Rule.to_hash(rule) }, + :users => results.users.map { |user| User.to_hash(user) } + } + hash[:actionPlans] = results.actionPlans.map { |plan| ActionPlan.to_hash(plan) } if results.actionPlans.size>0 + respond_to do |format| + format.json { render :json => jsonp(hash) } + format.xml { render :xml => hash.to_xml(:skip_types => true, :root => 'issues') } + end + end + def component_to_hash(component) hash = { :key => component.key, diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java index 1e1fd52df4f..96a7fb08294 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/IssueClient.java @@ -35,6 +35,13 @@ public interface IssueClient { */ Issues find(IssueQuery query); + /** + * Wrap the web service /api/issues/filter in order to execute issue filter. + * + * @since 3.7 + */ + Issues filter(String filterId); + /** * Assign an existing issue to a user. A null assignee removes the assignee. * diff --git a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/DefaultIssueClient.java b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/DefaultIssueClient.java index c4b2f326555..030b3ea2323 100644 --- a/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/DefaultIssueClient.java +++ b/sonar-ws-client/src/main/java/org/sonar/wsclient/issue/internal/DefaultIssueClient.java @@ -25,6 +25,7 @@ import org.sonar.wsclient.internal.HttpRequestFactory; import org.sonar.wsclient.issue.*; import javax.annotation.Nullable; + import java.util.List; import java.util.Map; @@ -43,11 +44,19 @@ public class DefaultIssueClient implements IssueClient { this.parser = new IssueJsonParser(); } + @Override public Issues find(IssueQuery query) { String json = requestFactory.get(SEARCH_URL, query.urlParams()); return parser.parseIssues(json); } + @Override + public Issues filter(String filterId) { + Map queryParams = EncodingUtils.toMap("filter", filterId); + String json = requestFactory.get("/api/issues/filter", queryParams); + return parser.parseIssues(json); + } + @Override public Issue create(NewIssue newIssue) { String json = requestFactory.post("/api/issues/create", newIssue.urlParams()); diff --git a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java index d262073e26e..6cb81463043 100644 --- a/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java +++ b/sonar-ws-client/src/test/java/org/sonar/wsclient/issue/internal/DefaultIssueClientTest.java @@ -26,7 +26,6 @@ import org.sonar.wsclient.MockHttpServerInterceptor; import org.sonar.wsclient.base.HttpException; import org.sonar.wsclient.internal.HttpRequestFactory; import org.sonar.wsclient.issue.*; -import org.sonar.wsclient.issue.internal.DefaultIssueClient; import java.util.List; @@ -67,6 +66,19 @@ public class DefaultIssueClientTest { } } + @Test + public void should_get_issue_filter() { + HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); + httpServer.doReturnBody("{\"issues\": [{\"key\": \"ABCDE\"}]}"); + + IssueClient client = new DefaultIssueClient(requestFactory); + Issues issues = client.filter("5"); + + assertThat(httpServer.requestedPath()).isEqualTo("/api/issues/filter?filter=5"); + assertThat(issues.list()).hasSize(1); + assertThat(issues.list().get(0).key()).isEqualTo("ABCDE"); + } + @Test public void should_set_severity() { HttpRequestFactory requestFactory = new HttpRequestFactory(httpServer.url()); -- 2.39.5