diff options
author | Fabrice Bellingard <bellingard@gmail.com> | 2011-10-24 18:48:36 +0200 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2011-10-24 18:49:55 +0200 |
commit | f40963af9acbad8d329efac215e516ed5fd79902 (patch) | |
tree | 44d368de5c6941d26b979b148226e8f140e12678 | |
parent | 5a5593f9a4d8688ab28a087413e57c350342ed9a (diff) | |
download | sonarqube-f40963af9acbad8d329efac215e516ed5fd79902.tar.gz sonarqube-f40963af9acbad8d329efac215e516ed5fd79902.zip |
SONAR-2919 Updated WS API for events
-rw-r--r-- | sonar-server/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb | 71 | ||||
-rw-r--r-- | sonar-server/src/main/webapp/WEB-INF/app/controllers/project_controller.rb | 15 |
2 files changed, 63 insertions, 23 deletions
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb index c41e6be51b2..074ab49d257 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb @@ -110,13 +110,12 @@ class Api::EventsController < Api::ApiController # # POST /api/events # Required parameters : + # - resource (id or key) - must be a root project. # - name # - category # # Optional parameters : - # - resource (id or key) # - description - # - data # - dateTime (ISO format : 2010-12-25T23:59:59+0100) # # Example : @@ -125,21 +124,49 @@ class Api::EventsController < Api::ApiController def create begin load_resource(:admin, params[:resource]) - - date=parse_datetime(params[:dateTime], true) + raise "Resource must be a root project" unless @resource.qualifier=='TRK' + + root_snapshot=nil + if (params[:dateTime]) + # try to find a snapshot on that day + date = parse_datetime(params[:dateTime], true) + root_snapshot = Snapshot.find(:last, :conditions => ["project_id = ? AND created_at >= ? AND created_at <= ?", @resource.id, date, date + 1.day], :order => :created_at) + raise "No snapshot exists for given date" unless root_snapshot + else + root_snapshot = Snapshot.find(:last, :conditions => ["project_id = ?", @resource.id], :order => :created_at) + end + + raise "A version already exists on this resource." if params[:category]==EventCategory::KEY_VERSION && root_snapshot.event(EventCategory::KEY_VERSION) + + # Create events for the root project and every submodule + event_to_return = nil + name = params[:name] + desc = params[:description] + category = params[:category] + snapshots = Snapshot.find(:all, :include => 'events', :conditions => ["(root_snapshot_id = ? OR id = ?) AND scope = 'PRJ'", root_snapshot.id, root_snapshot.id]) + snapshots.each do |snapshot| + # if this is a 'Version' event, must propagate the version number to the snapshot + if category==EventCategory::KEY_VERSION + snapshot.version = name + snapshot.save! + end + # and then create the event linked to the updated snapshot + event=Event.new( + :name => name, + :description => desc, + :category => category, + :snapshot => snapshot, + :resource_id => snapshot.project_id, + :event_date => snapshot.created_at + ) + event.save! + event_to_return = event if snapshot.project_id = @resource.id + end + - event=Event.new( - :name => params[:name], - :description => params[:description], - :event_date => date, - :category => params[:category], - :resource_id => (@resource ? @resource.id : nil), - :data => params[:data] - ) - event.save! respond_to do |format| - format.json { render :json => jsonp(events_to_json([event])) } - format.xml { render :xml => events_to_xml([event]) } + format.json { render :json => jsonp(events_to_json([event_to_return])) } + format.xml { render :xml => events_to_xml([event_to_return]) } format.text { render :text => text_not_supported } end @@ -158,7 +185,19 @@ class Api::EventsController < Api::ApiController begin event=Event.find(params[:id]) load_resource(:admin, event.resource_id) - event.delete + + events = [] + name = event.name + category = event.category + description = event.description + snapshots = Snapshot.find(:all, :include => 'events', :conditions => ["(root_snapshot_id = ? OR id = ?) AND scope = 'PRJ'", event.snapshot_id, event.snapshot_id]) + snapshots.each do |snapshot| + snapshot.events.reject {|e| e.name!=name || e.category!=category}.each do |event| + events << event + end + end + Event.delete(events.map {|e| e.id}) + render_success("Event deleted") rescue ActiveRecord::RecordNotFound => e diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/project_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/project_controller.rb index af742db153a..b36c5004c47 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/project_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/project_controller.rb @@ -186,8 +186,14 @@ class ProjectController < ApplicationController unless params[:version_name].blank? snapshots = find_project_snapshots(snapshot.id) + # We update all the related snapshots to have a version attribute in sync with the new name + snapshots.each do |snapshot| + snapshot.version = params[:version_name] + snapshot.save! + end + # And then we update/create the event on each snapshot if snapshot.event(EventCategory::KEY_VERSION) - # We update all the related events + # This is an update: we update all the related events Event.update_all( {:name => params[:version_name]}, ["category = ? AND snapshot_id IN (?)", EventCategory::KEY_VERSION, snapshots.map{|s| s.id}]) flash[:notice] = message('project_history.version_updated', :params => params[:version_name]) @@ -200,11 +206,6 @@ class ProjectController < ApplicationController end flash[:notice] = message('project_history.version_created', :params => params[:version_name]) end - # And we update all the related snapshots to have a version attribute in sync with the new name - snapshots.each do |snapshot| - snapshot.version = params[:version_name] - snapshot.save! - end end redirect_to :action => 'history', :id => snapshot.root_project_id @@ -241,7 +242,7 @@ class ProjectController < ApplicationController e = Event.new(params[:event]) e.snapshot = snapshot e.resource_id = snapshot.project_id - e.event_date = event.snapshot.created_at + e.event_date = snapshot.created_at e.save! end flash[:notice] = message('project_history.event_created', :params => event.name) |