diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-10-13 14:10:45 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-10-14 09:14:05 +0200 |
commit | 7ba4efecca71f9f804188298bc4a732a52ace7c7 (patch) | |
tree | 3abc1b88e06115a958d1114420fb86086b2813f2 | |
parent | 3afe7a9001453a7106c488a1429e3bff91c8545f (diff) | |
download | sonarqube-7ba4efecca71f9f804188298bc4a732a52ace7c7.tar.gz sonarqube-7ba4efecca71f9f804188298bc4a732a52ace7c7.zip |
Revert "SONAR-6309 Delete RoR WS profiles/list"
This reverts commit 7992a218b2dd4df3734d38ea1a2a71514d1e2949.
3 files changed, 261 insertions, 0 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProfilesWs.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProfilesWs.java index 3ec2957d78c..b66a056b9ad 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProfilesWs.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProfilesWs.java @@ -20,6 +20,8 @@ package org.sonar.server.qualityprofile.ws; +import com.google.common.io.Resources; +import org.sonar.api.server.ws.RailsHandler; import org.sonar.api.server.ws.WebService; public class ProfilesWs implements WebService { @@ -39,6 +41,24 @@ public class ProfilesWs implements WebService { .setSince("4.4"); restoreAction.define(controller); + defineListAction(controller); + controller.done(); } + + private static void defineListAction(NewController controller) { + WebService.NewAction action = controller.createAction("list") + .setDescription("Get a list of profiles") + .setSince("3.3") + .setHandler(RailsHandler.INSTANCE) + .setResponseExample(Resources.getResource(ProfilesWs.class, "example-list.json")); + + action.createParam("language") + .setDescription("Profile language") + .setExampleValue("java"); + action.createParam("project") + .setDescription("Project key or id") + .setExampleValue("org.codehaus.sonar:sonar"); + RailsHandler.addJsonOnlyFormatParam(action); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ProfilesWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ProfilesWsTest.java new file mode 100644 index 00000000000..fd6e3c29eb7 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/ProfilesWsTest.java @@ -0,0 +1,69 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.qualityprofile.ws; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.resources.Languages; +import org.sonar.api.server.ws.RailsHandler; +import org.sonar.api.server.ws.WebService; +import org.sonar.server.qualityprofile.QProfileBackuper; +import org.sonar.server.user.UserSession; +import org.sonar.server.ws.WsTester; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class ProfilesWsTest { + + WsTester ws; + + @Before + public void setUp() { + ws = new WsTester(new ProfilesWs( + new OldRestoreAction(mock(QProfileBackuper.class), mock(Languages.class), mock(UserSession.class)) + )); + } + + @Test + public void define_controller() { + WebService.Controller controller = controller(); + assertThat(controller).isNotNull(); + assertThat(controller.path()).isEqualTo("api/profiles"); + assertThat(controller.description()).isNotEmpty(); + assertThat(controller.actions()).hasSize(2); + } + + @Test + public void define_list_action() { + WebService.Controller controller = controller(); + + WebService.Action listProfiles = controller.action("list"); + assertThat(listProfiles).isNotNull(); + assertThat(listProfiles.handler()).isInstanceOf(RailsHandler.class); + assertThat(listProfiles.responseExampleAsString()).isNotEmpty(); + assertThat(listProfiles.params()).hasSize(3); + } + + private WebService.Controller controller() { + return ws.controller("api/profiles"); + } +} diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/profiles_controller.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/profiles_controller.rb new file mode 100644 index 00000000000..00766fef820 --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/profiles_controller.rb @@ -0,0 +1,172 @@ +# +# SonarQube, open source software quality management tool. +# Copyright (C) 2008-2014 SonarSource +# mailto:contact AT sonarsource DOT com +# +# SonarQube is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 3 of the License, or (at your option) any later version. +# +# SonarQube is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +require 'json' + +class Api::ProfilesController < Api::ApiController + + # GET /api/profiles/list?[language=<language][&project=<project id or key>] + # + # Since v.3.3 + # + # ==== Examples + # - get all the profiles : GET /api/profiles/list + # - get all the Java profiles : GET /api/profiles/list?language=java + # - get the profiles used by the project 'foo' : GET /api/profiles/list?project=foo + # - get the Java profile used by the project 'foo' : GET /api/profiles/list?project=foo&language=java + def list + language = params[:language] + project_key = params[:project] + + profiles = [] + default_profile_by_language = {} + if project_key.present? + project = Project.by_key(project_key) + not_found('Unknown project') unless project + if language.present? + default_profile_by_language[language] = Internal.qprofile_service.getDefault(language) + profile = Internal.quality_profiles.findProfileByProjectAndLanguage(project.id, language) + profiles << profile if profile + # Return default profile if the project is not associate to a profile + profiles << default_profile_by_language[language] unless profile + else + Api::Utils.languages.each do |language| + default_profile_by_language[language.getKey()] = Internal.qprofile_service.getDefault(language.getKey()) + profile = Internal.quality_profiles.findProfileByProjectAndLanguage(project.id, language.getKey()) + profiles << profile if profile + # Return default profile if the project is not associate to a profile + profiles << default_profile_by_language[language.getKey()] unless profile + end + end + elsif language.present? + profiles = Internal.quality_profiles.profilesByLanguage(language).to_a + else + profiles = Internal.quality_profiles.allProfiles().to_a + end + + # Populate the map of default profile by language by searching for all profiles languages + # We have to do that as the profiles list do not contain this information (maybe we should add it?) + profiles.each do |p| + lang = p.language + unless default_profile_by_language[lang] + default_profile_by_language[lang] = Internal.qprofile_service.getDefault(lang.to_s) + end + end + + json = profiles.compact.map { |profile| { + :key => profile.key, + :name => profile.name, + :language => profile.language, + :default => default_profile_by_language[profile.language].name == profile.name + } } + respond_to do |format| + format.json { render :json => jsonp(json) } + format.xml { render :xml => xml_not_supported } + format.text { render :text => text_not_supported } + end + end + + private + + def validation_messages_to_json(messages) + hash={} + hash[:errors]=messages.getErrors().to_a.map { |message| message } + hash[:warnings]=messages.getWarnings().to_a.map { |message| message } + hash[:infos]=messages.getInfos().to_a.map { |message| message } + hash + end + + def validation_result_to_json(result) + hash={} + hash[:warnings]=result.warnings().to_a.map { |message| message } + hash[:infos]=result.infos().to_a.map { |message| message } + hash + end + + def filter_rules + conditions=['active_rules.profile_id=?'] + condition_values=[@profile.id] + + if params[:rule_repositories].present? + conditions<<'rules.plugin_name in (?)' + condition_values<<params[:rule_repositories].split(',') + end + + if params[:rule_severities].present? + conditions<<'failure_level in (?)' + condition_values<<params[:rule_severities].split(',').map { |severity| Sonar::RulePriority.id(severity) } + end + + ActiveRule.find(:all, :include => [:rule, {:active_rule_parameters => :rules_parameter}], :conditions => [conditions.join(' AND ')].concat(condition_values)) + end + + def to_json + result={} + result[:name]=@profile.name + result[:language]=@profile.language + result[:parent]=@profile.parent_kee if @profile.parent_kee.present? + result[:default]=@profile.default_profile? + + rules=[] + @active_rules.each do |active_rule| + hash={} + hash[:key]=active_rule.rule.plugin_rule_key + hash[:repo]=active_rule.rule.plugin_name + hash[:severity]=active_rule.priority_text + hash[:inheritance]=active_rule.inheritance if active_rule.inheritance + params_hash=[] + active_rule.active_rule_parameters.each do |param| + params_hash<<{:key => param.name, :value => param.value} + end + hash[:params]=params_hash unless params_hash.empty? + rules<<hash + end + result[:rules]=rules unless rules.empty? + + [result] + end + + def to_xml + xml = Builder::XmlMarkup.new(:indent => 0) + xml.instruct! + + xml.profile do + xml.name(@profile.name) + xml.language(@profile.language) + xml.parent(@profile.parent_kee) if @profile.parent_kee.present? + xml.default(@profile.default_profile?) + + @active_rules.each do |active_rule| + xml.rule do + xml.key(active_rule.rule.plugin_rule_key) + xml.repo(active_rule.rule.plugin_name) + xml.severity(active_rule.priority_text) + xml.inheritance(active_rule.inheritance) if active_rule.inheritance + active_rule.active_rule_parameters.each do |param| + xml.param do + xml.key(param.name) + xml.value(param.value) + end + end + end + end + end + end + +end |