]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2919 Updated WS API for events
authorFabrice Bellingard <bellingard@gmail.com>
Mon, 24 Oct 2011 16:48:36 +0000 (18:48 +0200)
committerFabrice Bellingard <bellingard@gmail.com>
Mon, 24 Oct 2011 16:49:55 +0000 (18:49 +0200)
sonar-server/src/main/webapp/WEB-INF/app/controllers/api/events_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/controllers/project_controller.rb

index c41e6be51b2059656050ac99006e96d1984f1df9..074ab49d257612fe93d45575d05b0048e1bf563d 100644 (file)
@@ -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
index af742db153a6c68d48774b6a672c223050b2737c..b36c5004c47c4e0f1a7efef090d3c2e3d3de6f90 100644 (file)
@@ -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)