diff --git a/app/controllers/api/v1/advisories_controller.rb b/app/controllers/api/v1/advisories_controller.rb new file mode 100644 index 000000000..f4496442f --- /dev/null +++ b/app/controllers/api/v1/advisories_controller.rb @@ -0,0 +1,36 @@ +# -*- encoding : utf-8 -*- +class Api::V1::AdvisoriesController < Api::V1::BaseController + before_filter :authenticate_user! + skip_before_filter :authenticate_user! if APP_CONFIG['anonymous_access'] + load_resource :find_by => :advisory_id + authorize_resource + + def index + @advisories = @advisories.scoped(:include => :platforms). + paginate(paginate_params) + end + + def show + fetch_packages_info + end + + protected + + # this method fetches and structurize packages attached to current advisory. + def fetch_packages_info + @packages_info = Hash.new { |h, k| h[k] = {} } # maaagic, it's maaagic ;) + @advisory.build_lists.find_in_batches(:include => [:save_to_platform, :packages, :project]) do |batch| + batch.each do |build_list| + tmp = build_list.packages.inject({:srpm => nil, :rpm => []}) do |h, p| + p.package_type == 'binary' ? h[:rpm] << p.fullname : h[:srpm] = p.fullname + h + end + h = { build_list.project => tmp } + @packages_info[build_list.save_to_platform].merge!(h) do |pr, old, new| + {:srpm => new[:srpm], :rpm => old[:rpm].concat(new[:rpm]).uniq} + end + end + end + end + +end diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index 488c215f2..a44addbf1 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -68,7 +68,7 @@ class Api::V1::BaseController < ApplicationController id = status != 200 ? nil : subject.id render :json => { - subject.class.name.downcase.to_sym => { + subject.class.name.underscore.to_sym => { :id => id, :message => message } diff --git a/app/views/api/v1/advisories/_advisory.json.jbuilder b/app/views/api/v1/advisories/_advisory.json.jbuilder new file mode 100644 index 000000000..5205ddc29 --- /dev/null +++ b/app/views/api/v1/advisories/_advisory.json.jbuilder @@ -0,0 +1,12 @@ +json.id advisory.advisory_id +json.(advisory, :description) +json.platforms advisory.platforms do |json_platform, platform| + json_platform.(platform, :id, :released) + json_platform.url api_v1_platform_path(platform.id, :format => :json) +end +json.projects advisory.projects do |json_project, project| + json_project.(project, :id, :name) + json_project.fullname project.name_with_owner + json_project.url api_v1_project_path(project.id, :format => :json) +end +json.url api_v1_advisory_path(advisory.advisory_id, :format => :json) \ No newline at end of file diff --git a/app/views/api/v1/advisories/index.json.jbuilder b/app/views/api/v1/advisories/index.json.jbuilder new file mode 100644 index 000000000..c0248c828 --- /dev/null +++ b/app/views/api/v1/advisories/index.json.jbuilder @@ -0,0 +1,4 @@ +json.advisories @advisories do |json, advisory| + json.partial! 'advisory', :advisory => advisory, :json => json +end +json.url api_v1_advisories_path(:format => :json) \ No newline at end of file diff --git a/app/views/api/v1/advisories/show.json.jbuilder b/app/views/api/v1/advisories/show.json.jbuilder new file mode 100644 index 000000000..0083293ee --- /dev/null +++ b/app/views/api/v1/advisories/show.json.jbuilder @@ -0,0 +1,24 @@ +json.advisory do |json| + json.partial! 'advisory', :advisory => @advisory, :json => json + json.created_at @advisory.created_at.to_i + json.updated_at @advisory.updated_at.to_i + json.build_lists @advisory.build_lists do |json_build_list, build_list| + json_build_list.(build_list, :id) + json_build_list.url api_v1_build_list_path(build_list.id, :format => :json) + end + + json.affected_in @packages_info do |json_platform, package_info| + platform = package_info[0] + json_platform.(platform, :id) + json_platform.url api_v1_platform_path(platform.id, :format => :json) + json_platform.projects package_info[1] do |json_project, info| + project = info[0] + json_project.(project, :id) + json_project.url api_v1_project_path(project.id, :format => :json) + packages = info[1] + json_project.srpm packages[:srpm] + json_project.rpm packages[:rpm] + end + end + +end diff --git a/config/routes.rb b/config/routes.rb index bd1691c70..7f9fff5d8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,7 @@ Rosa::Application.routes.draw do namespace :api do namespace :v1 do + resources :advisories, :only => [:index, :show] resources :build_lists, :only => [:index, :create, :show] do member { get :publish