+++ /dev/null
-#
-# 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::DependenciesController < Api::ResourceRestController
-
- def rest_call
- conditions=[]
- values={}
-
- if params[:parent]
- conditions<<'dependencies.parent_dependency_id=:parent'
- values[:parent]=params[:parent].to_i
- elsif params[:id]
- conditions<<'dependencies.id=:id'
- values[:id]=params[:id].to_i
- else
- snapshot=@resource.last_snapshot
- direction=params[:dir]
- if direction=='in'
- conditions<<'dependencies.to_snapshot_id=:tosid'
- values[:tosid]=snapshot.id
- elsif direction=='out'
- conditions<<'dependencies.from_snapshot_id=:fromsid'
- values[:fromsid]=snapshot.id
- else
- conditions<<'(dependencies.from_snapshot_id=:fromsid OR dependencies.to_snapshot_id=:tosid)'
- values[:fromsid]=snapshot.id
- values[:tosid]=snapshot.id
- end
- end
-
- dependencies=Dependency.find(:all, :include => ['from','to'], :conditions => [conditions.join(' AND '), values])
-
- dependencies=dependencies.sort do |x,y|
- result=(x.from.name(true) <=> y.from.name(true))
- if result==0
- result=(x.to.name(true) <=> y.to.name(true))
- end
- result
- end
-
- rest_render(dependencies)
- end
-
- private
-
- def rest_to_json(dependencies)
- result=[]
- dependencies.each do |dependency|
- result<<dependency.to_json(params)
- end
- JSON(result)
- end
-
- def rest_to_xml(dependencies)
- xml = Builder::XmlMarkup.new(:indent => 0)
- xml.instruct!
- xml.deps do
- dependencies.each do |dependency|
- xml.dep do
- dependency.to_xml(xml, params)
- end
- end
- end
- end
-end
+++ /dev/null
-#
-# 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.
-#
-class Api::DependencyTreeController < Api::ApiController
-
- def index
- begin
- @resource = load_resource(params[:resource], :user)
- scopes = (params[:scopes] || 'PRJ,DIR,FIL,PAC,CLA').split(',')
- dependencies=[]
- if @resource.last_snapshot
- project_sid = @resource.last_snapshot.project_snapshot.id
- dependencies = Dependency.find(:all, :include => ['to','to_snapshot'], :conditions => ["project_snapshot_id=? and (from_scope in (?) or to_scope in (?))", project_sid, scopes, scopes])
- end
-
- dependencies_by_from={}
- dependencies.each do |dep|
- dependencies_by_from[dep.from_snapshot_id]||=[]
- dependencies_by_from[dep.from_snapshot_id]<<dep
- end
-
- respond_to do |format|
- format.json{ render :json => jsonp(to_json(dependencies_by_from, @resource.last_snapshot.id)) }
- format.xml { render :xml => xml_not_supported}
- format.text { render :text => text_not_supported}
- end
-
- rescue ApiException => e
- render_error(e.msg, e.code)
- end
- end
-
- private
-
- def load_resource(resource_id, role=nil)
- resource=Project.by_key(resource_id)
- if resource.nil?
- raise ApiException.new 404, "Resource not found: #{resource_id}"
- end
- if role && !has_role?(role, resource)
- raise ApiException.new 401, "Unauthorized"
- end
- resource
- end
-
- def to_json(dependencies_by_from, from_sid)
- json = []
- dependencies = dependencies_by_from.delete(from_sid)
- if dependencies
- dependencies.each do |dep|
- hash={
- :did => dep.id.to_s,
- :rid => dep.to.id.to_s,
- :w => dep.weight,
- :u => dep.usage,
- :s => dep.to_scope,
- :q => dep.to_snapshot.qualifier}
- hash[:v]=dep.to_snapshot.version if dep.to_snapshot.version
- if dep.to
- hash[:k]=dep.to.key
- hash[:n]=dep.to.name(true)
- else
- hash[:k]=''
- hash[:n]='[missing title, please re-analyze the project]'
- end
-
- to=to_json(dependencies_by_from, dep.to_snapshot_id)
- hash[:to]=to if to && !to.empty?
- json<<hash
- end
- end
- json
- end
-end