#698: added API for Advisories

This commit is contained in:
Vokhmin Alexey V 2012-10-18 15:22:31 +04:00
parent 3413a681af
commit b7eff22c23
6 changed files with 78 additions and 1 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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