Skip to content

Commit

Permalink
Merge pull request #5024 from vpereira/xml-builder
Browse files Browse the repository at this point in the history
Use xml builder to render xml response
  • Loading branch information
bgeuken committed Jun 2, 2018
2 parents 620af28 + ae09669 commit 1f2bf04
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 56 deletions.
57 changes: 9 additions & 48 deletions src/api/app/controllers/source_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,8 @@ def index
end

def projectlist
# list all projects (visible to user)
output = Rails.cache.fetch(['projectlist', Project.maximum(:updated_at), Relationship.forbidden_project_ids]) do
dir = Project.pluck(:name).sort
output = ''
output << "<?xml version='1.0' encoding='UTF-8'?>\n"
output << "<directory>\n"
output << dir.map { |item| " <entry name=\"#{::Builder::XChar.encode(item)}\"/>\n" }.join
output << "</directory>\n"
end
render xml: output
@project_names = Project.pluck(:name).sort
render formats: [:xml]
end

def set_issues_default
Expand Down Expand Up @@ -110,25 +102,12 @@ def show_project
return
end

render xml: render_project_packages
render_project_packages
end

def render_project_packages
packages = nil
if params.key? :expand
packages = @project.expand_all_packages
else
packages = @project.packages.pluck(:name)
end
output = ''
output << "<directory count='#{packages.length}'>\n"
if params.key? :expand
output << packages.map { |p| " <entry name=\"#{p[0]}\" originproject=\"#{p[1]}\"/>\n" }.join
else
output << packages.map { |p| " <entry name=\"#{p}\"/>\n" }.join
end
output << "</directory>\n"
output
@packages = params.key?(:expand) ? @project.expand_all_packages : @project.packages.pluck(:name)
render locals: { expand: params.key?(:expand) }, formats: [:xml]
end

# DELETE /source/:project
Expand Down Expand Up @@ -877,15 +856,7 @@ class RepoDependency < APIException
# create a id collection of all projects doing a project link to this one
# POST /source/<project>?cmd=showlinked
def project_command_showlinked
builder = Builder::XmlMarkup.new(indent: 2)
xml = builder.collection do |c|
@project.linked_by_projects.each do |l|
p = {}
p[:name] = l.name
c.project(p)
end
end
render xml: xml
render 'source/project_command_showlinked', formats: [:xml]
end

# lock a project
Expand Down Expand Up @@ -1213,7 +1184,9 @@ def package_command_getprojectservices
# create a id collection of all packages doing a package source link to this one
# POST /source/<project>/<package>?cmd=showlinked
def package_command_showlinked
unless @package
if @package
render 'source/package_command_showlinked', formats: [:xml]
else
# package comes from remote instance or is hidden

# FIXME: return an empty list for now
Expand All @@ -1223,19 +1196,7 @@ def package_command_showlinked
# answer = Backend::Connection.post path
# render :text => answer.body, :content_type => 'text/xml'
render xml: '<collection/>'
return
end

builder = Builder::XmlMarkup.new(indent: 2)
xml = builder.collection do |c|
@package.find_linking_packages.each do |l|
p = {}
p[:project] = l.project.name
p[:name] = l.name
c.package(p)
end
end
render xml: xml
end

# POST /source/<project>/<package>?cmd=collectbuildenv
Expand Down
5 changes: 5 additions & 0 deletions src/api/app/views/source/index.xml.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
xml.directory do
@project_names.map do |project_name|
xml.entry(name: project_name)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
xml.collection do
@package.find_linking_packages.map do |pkg|
xml.package(name: pkg.name, project: pkg.project.name)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
xml.collection do
@project.linked_by_projects.map do |prj|
xml.project(name: prj.name)
end
end
9 changes: 9 additions & 0 deletions src/api/app/views/source/show_project.xml.builder
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
xml.directory(count: @packages.count) do
@packages.map do |name, project|
if expand
xml.entry(name: name, originproject: project)
else
xml.entry(name: name)
end
end
end
15 changes: 7 additions & 8 deletions src/api/app/views/source/verboseproductlist.xml.builder
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# avoid to render, parser and re-render here, since it can be a hughe content

output = ''
output << "<productlist count='#{@products.length}'>\n"
@products.each do |p|
output << p.to_axml
xml.productlist(count: @products.count) do
@products.map do |product|
xml.product(name: product.name, originproject: product.package.project.name, originpackage: product.package.name) do
xml.cpe product.cpe
xml.version product.version
end
end
end
output << "</productlist>\n"
return output

0 comments on commit 1f2bf04

Please sign in to comment.