From: Simon Brandhof Date: Mon, 1 Oct 2012 15:18:48 +0000 (+0200) Subject: SONAR-3840 Add webservice to list Quality profiles X-Git-Tag: 3.3~152 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9a81353e8d590159ba0f4384f13330e20b5baf71;p=sonarqube.git SONAR-3840 Add webservice to list Quality profiles --- diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/profiles_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/profiles_controller.rb index 9a7652a0179..9df4d48838d 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/profiles_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/profiles_controller.rb @@ -17,47 +17,96 @@ # License along with Sonar; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 # -require 'fastercsv' -require "json" +require 'json' class Api::ProfilesController < Api::ApiController - verify :method => :post, :only => [:restore] - def index - begin - language=params[:language] - raise ApiException.new(400, "Missing parameter: language") if language.blank? - - name=params[:name] - if name.blank? - @profile=Profile.by_default(language) + # GET /api/profiles/list?[language=] + # + # ==== 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] + + if project_key.present? + project = Project.by_key(project_key) + not_found('Unknown project') unless project + if language.present? + profiles=[Profile.by_project_id(language, project.id, true)] else - @profile=Profile.find_by_name_and_language(name, language) + profiles=Api::Utils.languages.map { |lang| Profile.by_project_id(lang, project.id, true) } end - raise ApiException.new(404, "Profile not found") if @profile.nil? + elsif language.present? + profiles=Profile.all_by_language(language) + else + profiles=Profile.all + end - @active_rules=filter_rules() + json=profiles.compact.map { |profile| {:name => profile.name, :language => profile.language, :default => profile.default_profile?} } + 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 - respond_to do |format| - format.json { render :json => jsonp(to_json) } - format.xml { render :xml => to_xml } - format.text { render :text => text_not_supported } - end + # POST /api/profiles/destroy?language=&name= + def destroy + verify_post_request + access_denied unless has_role?(:admin) + require_parameters :language, :name - rescue ApiException => e - render_error(e.msg, e.code) + profile=Profile.find_by_name_and_language(params[:name], params[:language]) + if profile + bad_request('This profile can not be deleted') unless profile.deletable? + profile.destroy end + render_success(profile ? 'Profile destroyed' : 'Profile did not exist') end + # POST /api/profiles/set_as_default?language=&name= + def set_as_default + verify_post_request + access_denied unless has_role?(:admin) + require_parameters :language, :name + + profile=Profile.find_by_name_and_language(params[:name], params[:language]) + not_found('Profile not found') unless profile + profile.set_as_default + render_success + end + + # GET /api/profiles?language=[&name=] + def index + require_parameters :language + + language=params[:language] + name=params[:name] + if name.blank? + @profile=Profile.by_default(language) + else + @profile=Profile.find_by_name_and_language(name, language) + end + not_found('Profile not found') unless @profile + + @active_rules=filter_rules() + + respond_to do |format| + format.json { render :json => jsonp(to_json) } + format.xml { render :xml => to_xml } + format.text { render :text => text_not_supported } + end + end - # # Backup a profile. If output format is xml, then backup is directly returned. - # - # curl -u admin:admin http://localhost:9000/api/profiles/backup?language=java[&name=my_profile] -v - # + # GET /api/profiles/backup?language=[&name=my_profile] -v def backup access_denied unless has_role?(:admin) - bad_request('Missing parameter: language') if params[:language].blank? + require_parameters :language if params[:name].blank? profile=Profile.by_default(params[:language]) @@ -73,15 +122,13 @@ class Api::ProfilesController < Api::ApiController end end - # # Restore a profile backup. - # # curl -X POST -u admin:admin -F 'backup=backup' -v http://localhost:9000/api/profiles/restore # curl -X POST -u admin:admin -F 'backup=@backup.xml' -v http://localhost:9000/api/profiles/restore - # def restore + verify_post_request access_denied unless has_role?(:admin) - bad_request('Missing parameter: backup') if params[:backup].blank? + require_parameters :backup backup = Api::Utils.read_post_request_param(params[:backup])