Skip to content

Commit

Permalink
[api] support product listing per project
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianschroeter committed Feb 11, 2014
1 parent 96258aa commit 9cc0f22
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/api/api/api.txt
Expand Up @@ -436,7 +436,8 @@ Parameters:

deleted: show deleted package instead of existing
expand: include also packages from linked projects
view: issues, optional, can be used to show all tracked issues for all packages in project
view: issues, can be used to show all tracked issues for all packages in project
productlists, shows all containing products, unifies result when used with expand


GET /source/<project>/<package>
Expand Down
19 changes: 18 additions & 1 deletion src/api/app/controllers/source_controller.rb
Expand Up @@ -102,7 +102,9 @@ def show_project
raise Project::UnknownObjectError.new project_name unless @project
# we let the backend list the packages after we verified the project is visible
if params.has_key? :view
if params['view'] == 'issues'
if params['view'] == 'productlist'
render xml: render_project_productlist
elsif params['view'] == 'issues'
render_project_issues
else
pass_to_backend
Expand All @@ -128,6 +130,21 @@ def render_project_packages
output
end

def render_project_productlist
products=nil
if params.has_key? :expand
products = @project.expand_all_products
else
products = Product.joins(:package).where("packages.project_id = ? and packages.name = '_product'", @project.id).pluck(:name, :package_id)
end
products = @project.map_products_to_packages(products)
output = String.new
output << "<directory count='#{products.length}'>\n"
output << products.map { |p| p[1].nil? ? " <entry name=\"#{p[0]}\"/>\n" : " <entry name=\"#{p[0]}\" originproject=\"#{p[1]}\" mtime=\"#{p[2]}\"/>\n" }.join
output << "</directory>\n"
output
end

# DELETE /source/:project
def delete_project
project_name = params[:project]
Expand Down
31 changes: 31 additions & 0 deletions src/api/app/models/project.rb
Expand Up @@ -920,6 +920,28 @@ def expand_all_packages
return packages
end

# return array of [:name, :package_id] tuples for all products
# this function is making the products uniq
def expand_all_products( p_map = Hash.new )
products = Product.joins(:package).where("packages.project_id = ? and packages.name = '_product'", self.id).pluck(:name, :package_id)
products.each { |name, package_id| p_map[name] = 1 } # existing packages map
# second path, all packages from indirect linked projects
self.linkedprojects.each do |lp|
if lp.linked_db_project.nil?
# FIXME: this is a remote project
else
lp.linked_db_project.expand_all_products(p_map).each do |name, package_id|
unless p_map[name]
products << [name, package_id]
p_map[name] = 1
end
end
end
end

return products
end

# this is needed to displaying package and project names
# packages is an array of :name, :db_project_id
# return [package_name, project_name] where project_name is nil
Expand All @@ -940,6 +962,15 @@ def map_packages_to_projects(packages)
ret
end

def map_products_to_packages(packages)
ret = []
packages.each do |p|
package = Package.find_by_id p[1]
ret << [p[0], package.project.name, package.updated_at.to_i]
end
ret
end

def project_type
@project_type ||= DbProjectType.find(type_id).name
end
Expand Down
12 changes: 12 additions & 0 deletions src/api/test/functional/product_test.rb
Expand Up @@ -26,6 +26,17 @@ def test_simple_product_file
assert_response :success
end

# product views in a project
get "/source/home:tom:temporary?view=productlist"
assert_response :success
assert_xml_tag :tag => "entry",
:attributes => { :name => "simple", :originproject => "home:tom:temporary" }
get "/source/home:tom:temporary?view=productlist&expand=1"
assert_response :success
assert_xml_tag :tag => "entry",
:attributes => { :name => "simple", :originproject => "home:tom:temporary" }

# product views in a package
get "/source/home:tom:temporary/_product?view=issues"
assert_response :success
assert_xml_tag :tag => "kind", :content => "product"
Expand All @@ -39,6 +50,7 @@ def test_simple_product_file
get "/source/home:tom:temporary/_product?view=products&product=DOES_NOT_EXIST"
assert_response :success
assert_no_xml_tag :tag => "name", :content => "simple"

product = Package.find_by_project_and_name("home:tom:temporary","_product").products.first
assert_equal "simple", product.name
assert_equal "cpe:/a:OBS_Fuzzies:simple:11.2", product.cpe
Expand Down

0 comments on commit 9cc0f22

Please sign in to comment.