diff options
author | Stas Vilchik <vilchiks@gmail.com> | 2016-12-19 16:27:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-19 16:27:35 +0100 |
commit | c688ff54703b1dc3bdbc4a05f919c71b28a23aad (patch) | |
tree | 14811aa6c3351f883619f8e47b702064f38abedc /server | |
parent | 0ad2a12e2030cb7a3b9609ffff64aaa210b93320 (diff) | |
download | sonarqube-c688ff54703b1dc3bdbc4a05f919c71b28a23aad.tar.gz sonarqube-c688ff54703b1dc3bdbc4a05f919c71b28a23aad.zip |
SONAR-7283 Drop Ruby WS api/events/* (#1476)
Diffstat (limited to 'server')
3 files changed, 0 insertions, 495 deletions
diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb deleted file mode 100644 index c0e3b5f4414..00000000000 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb +++ /dev/null @@ -1,287 +0,0 @@ -# -# SonarQube, open source software quality management tool. -# Copyright (C) 2008-2016 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. -# -class Api::EventsController < Api::ApiController - - # GET /api/events?resource=xxx&category=xxx - # Examples : - # - global events : curl -v http://localhost:9000/api/events -u admin:admin - # - resource events : curl -v http://localhost:9000/api/events?resource=org.apache.struts:struts-parent - # - def index - begin - load_resource(:user, params[:resource]) - - conditions=[] - values={} - - if params[:categories] - conditions<<'category IN (:categs)' - values[:categs]=params[:categories].split(',') - end - - if @resource - conditions<<'component_uuid=:component_uuid' - values[:component_uuid]=@resource.uuid - else - conditions<<'component_uuid IS NULL' - end - - from=nil - if params[:fromDateTime] - from=parse_datetime(params[:fromDateTime], false) - elsif params[:fromDate] - from=Date::strptime(params[:fromDate]) - end - if from - conditions<<'event_date>=:from' - values[:from]=from.to_i*1000 - end - - to=nil - if params[:toDateTime] - to=parse_datetime(params[:toDateTime], false) - elsif params[:toDate] - to=Date::strptime(params[:toDate]) - end - if to - conditions<<'event_date<=:to' - values[:to]=to.to_i*1000 - end - - events=Event.find(:all, :conditions => [conditions.join(' AND '), values], :order => 'event_date DESC') - - respond_to do |format| - format.json { render :json => jsonp(events_to_json(events)) } - format.xml { render :xml => events_to_xml(events) } - format.text { render :text => text_not_supported } - end - - rescue ApiException => e - render_error(e.msg, e.code) - - rescue Exception => e - logger.error("Fails to execute #{request.url} : #{e.message}") - render_error(e.message) - end - end - - # GET /api/events/:id - # Example : curl -v http://localhost:9000/api/events/3 - def show - begin - event=Event.find(params[:id]) - load_resource_by_uuid(:user, event.component_uuid) - - respond_to do |format| - format.json { render :json => jsonp(events_to_json([event])) } - format.xml { render :xml => events_to_xml([event]) } - format.text { render :text => text_not_supported } - end - - rescue ActiveRecord::RecordNotFound => e - render_error(e.message, 404) - - rescue ApiException => e - render_error(e.msg, e.code) - - rescue Exception => e - logger.error("Fails to execute #{request.url} : #{e.message}") - render_error(e.message) - end - end - - # - # POST /api/events - # Required parameters : - # - resource (id or key) - must be a root project. - # - name - # - category - # - # Optional parameters : - # - description - # - dateTime (ISO format : 2010-12-25T23:59:59+0100) - # - # Example : - # curl -d "name=foo&category=bar&dateTime=2010-12-25T23%3A59%3A59%2B0100" http://localhost:9000/api/events -v -u admin:admin - # - def create - begin - load_resource(:admin, params[:resource]) - raise "Resource must be a root project" unless @resource.scope=='PRJ' && (@resource.qualifier=='TRK' || @resource.qualifier=='VW' || @resource.qualifier=='DEV') - - analysis=nil - if (params[:dateTime]) - # try to find a snapshot on that day - date = parse_datetime(params[:dateTime], true) - analysis = Snapshot.find(:last, :conditions => ["component_uuid = ? AND created_at >= ? AND created_at <= ?", @resource.uuid, date.to_i*1000, (date + 1.day).to_i*1000], :order => :created_at) - raise "No snapshot exists for given date" unless analysis - else - analysis = Snapshot.find(:last, :conditions => ["component_uuid = ?", @resource.uuid], :order => :created_at) - end - - raise "A version already exists on this resource." if params[:category]==EventCategory::KEY_VERSION && analysis.event(EventCategory::KEY_VERSION) - raise "An event '#{params[:name]}' (category '#{params[:category]}') already exists on this resource." if Event.already_exists(@resource.last_analysis.uuid, params[:name], params[:category]) - - event_to_return = nil - name = params[:name] - desc = params[:description] - category = params[:category] - if category==EventCategory::KEY_VERSION - analysis.version = name - analysis.save! - end - event=Event.new( - :name => name, - :description => desc, - :category => category, - :snapshot => analysis, - :component_uuid => analysis.component_uuid, - :event_date => analysis.created_at - ) - event.save! - - respond_to do |format| - format.json { render :json => jsonp(events_to_json([event])) } - format.xml { render :xml => events_to_xml([event]) } - format.text { render :text => text_not_supported } - end - - rescue ApiException => e - render_error(e.msg, e.code) - - rescue Exception => e - render_error(e.message, 400) - end - end - - - # DELETE /api/events/1 - # Example : curl -X DELETE http://localhost:9000/api/events/9 -v - def destroy - begin - event=Event.find(params[:id]) - load_resource_by_uuid(:admin, event.component_uuid) - - events = [] - name = event.name - category = event.category - snapshot = Snapshot.find(:first, :include => 'events', :conditions => ["uuid = ? AND scope = 'PRJ'", event.analysis_uuid]) - snapshot.events.reject {|e| e.name!=name || e.category!=category}.each do |event| - events << event - end - - Event.transaction do - events.map { |e| e.id }.each_slice(999) do |safe_for_oracle_ids| - Event.delete(safe_for_oracle_ids) - end - end - - render_success("Event deleted") - - rescue ActiveRecord::RecordNotFound => e - render_error(e.message, 404) - - rescue ApiException => e - render_error(e.msg, e.code) - - rescue Exception => e - logger.error("Fails to execute #{request.url} : #{e.message}") - render_error(e.message) - end - end - - - private - - def load_resource(required_resource_role, resource_key=nil) - if resource_key - @resource=Project.by_key(resource_key) - if @resource.nil? - raise ApiException.new 404, "Resource not found: #{resource_key}" - end - - unless has_role?(required_resource_role, @resource) - raise ApiException.new 401, "Unauthorized" - end - else - # global events - unless is_admin? - raise ApiException.new 401, "Unauthorized" - end - end - end - - def load_resource_by_uuid(required_resource_role, component_uuid=nil) - if component_uuid - @resource=Project.first(:conditions => {:uuid => component_uuid}) - if @resource.nil? - raise ApiException.new 404, "Component uuid not found: #{component_uuid}" - end - - unless has_role?(required_resource_role, @resource) - raise ApiException.new 401, "Unauthorized" - end - else - # global events - unless is_admin? - raise ApiException.new 401, "Unauthorized" - end - end - end - - def events_to_json(events=[]) - json=[] - events.each do |event| - json<<event_to_json(event) - end - json - end - - def event_to_json(event) - hash={} - hash[:id]=event.id.to_s - hash[:rk]=event.resource.key if event.resource - hash[:n]=event.name if event.name - hash[:c]=event.category - hash[:dt]=Api::Utils.format_datetime(event.event_date) if event.event_date - hash[:ds]=event.description if event.description - hash - end - - def events_to_xml(events, xml=Builder::XmlMarkup.new(:indent => 0)) - xml.events do - events.each do |event| - event_to_xml(event, xml) - end - end - end - - def event_to_xml(event, xml=Builder::XmlMarkup.new(:indent => 0)) - xml.event do - xml.id(event.id.to_s) - xml.name(event.name) if event.name - xml.resourceKey(event.resource.key) if event.resource - xml.category(event.category) - xml.date(Api::Utils.format_datetime(event.event_date)) if event.event_date - xml.description(event.description) if event.description - end - end - -end diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/models/event.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/models/event.rb deleted file mode 100644 index 8f848afcdb2..00000000000 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/models/event.rb +++ /dev/null @@ -1,86 +0,0 @@ -# -# SonarQube, open source software quality management tool. -# Copyright (C) 2008-2016 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. -# -class Event < ActiveRecord::Base - - validates_presence_of :event_date - validates_length_of :name, :within => 1..400 - validates_length_of :category, :within => 1..50 - - belongs_to :resource, :class_name => 'Project', :foreign_key => 'component_uuid', :primary_key => 'uuid' - belongs_to :snapshot, :class_name => 'Snapshot', :foreign_key => 'analysis_uuid', :primary_key => 'uuid' - - before_save :populate_snapshot - - def created_at - long_to_date(:created_at) - end - - def created_at=(date) - write_attribute(:created_at, date.to_i*1000) - end - - def event_date - long_to_date(:event_date) - end - - def event_date=(date) - write_attribute(:event_date, date.to_i*1000) - end - - def long_to_date(attribute) - date_in_long = read_attribute(attribute) - Time.at(date_in_long/1000) if date_in_long - end - - def fullname - if category - "#{category} #{name}" - else - name - end - end - - # Use this method to display event description, as missing descriptions are not stored in the same way - # on different DBs (see https://jira.sonarsource.com/browse/SONAR-3326) - def description_text - description || '' - end - - # - # For a given snapshot, checks if an event with the same name & category - # exists in the history of the corresponding resource (= in any existing - # processed snapshot for this resource). - # - def self.already_exists(componentUuid, event_name, event_category) - snapshots = Snapshot.find(:all, :conditions => ["status='P' AND component_uuid=?", componentUuid], :include => 'events') - snapshots.each do |snapshot| - snapshot.events.each do |event| - return true if event.name==event_name && event.category==event_category - end - end - - return false - end - - def populate_snapshot - self.created_at=DateTime.now unless self.created_at - self.snapshot=Snapshot.snapshot_by_date(snapshot.uuid, event_date) unless self.snapshot - end -end diff --git a/server/sonar-web/src/main/webapp/WEB-INF/app/models/event_category.rb b/server/sonar-web/src/main/webapp/WEB-INF/app/models/event_category.rb deleted file mode 100644 index 2b9c28a129f..00000000000 --- a/server/sonar-web/src/main/webapp/WEB-INF/app/models/event_category.rb +++ /dev/null @@ -1,122 +0,0 @@ - # - # SonarQube, open source software quality management tool. - # Copyright (C) 2008-2016 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 - # License along with {library}; if not, write to the Free Software - # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - # -class EventCategory - PREFIX='sonar.events.category.' - NAME_MAX_LENGTH=511 - PREFIX.length - - KEY_VERSION='Version' - KEY_ALERT='Alert' - KEY_PROFILE='Profile' - KEY_OTHER='Other' - - def initialize(name=nil, description=nil) - @name=name - @description=description - end - - def name - @name ? @name.strip : nil - end - - def description - @description - end - - def save - errors=validate - if errors.empty? - Property.set(EventCategory.property_key(name), description) - end - errors - end - - def delete - Property.clear(EventCategory.property_key(name)) - Event.update_all("category=null", "category='#{name}'") - end - - def rename(from) - errors=save - if errors.empty? - from_categ=EventCategory.category(from) - if from_categ - Event.update_all({:category => name}, "category='#{from_categ.name}'") - from_categ.delete - end - end - errors - end - - def valid? - validate.length==0 - end - - def validate - errors=[] - errors<<'Name is empty' if name.blank? - errors<<"Name is too long (#{NAME_MAX_LENGTH} characters max.)" if name.length>NAME_MAX_LENGTH - errors<<'Description is empty' if description.blank? - errors<<'Core categories can not be updated or deleted' if not editable? - errors - end - - def <=>(other) - name <=> other.name - end - - def editable? - !([KEY_VERSION, KEY_ALERT, KEY_PROFILE, KEY_OTHER].include?(name)) - end - - def self.defaults - [ - EventCategory.new(KEY_VERSION, 'Application version'), - EventCategory.new(KEY_ALERT, 'Alert'), - EventCategory.new(KEY_PROFILE, 'Profile change'), - EventCategory.new(KEY_OTHER, 'Other events') - ] - end - - def self.other_category - EventCategory.new(KEY_OTHER, 'Other events') - end - - def self.categories(include_defaults=false) - events= (include_defaults ? defaults : []) - - Property.by_key_prefix(PREFIX).each do |property| - name=property.key[PREFIX.size..-1] - events<<EventCategory.new(name, property.value) - end - - events.sort{|e1,e2| e1.name<=>e2.name} - end - - def self.category(name) - desc=Property.value(property_key(name)) - desc ? EventCategory.new(name, desc) : nil - end - - private - - def self.property_key(name) - "#{PREFIX}#{name}" - end -end |