#341: added new action #packages into Repository API

This commit is contained in:
Vokhmin Alexey V 2014-01-21 01:24:22 +04:00
parent 11a85c4133
commit e98624ec41
6 changed files with 111 additions and 4 deletions

View File

@ -6,12 +6,26 @@ class Api::V1::BaseController < ApplicationController
rescue_from CanCan::AccessDenied do |exception| rescue_from CanCan::AccessDenied do |exception|
respond_to do |format| respond_to do |format|
format.json { render :json => {:message => t("flash.exception_message")}.to_json, :status => 403 } format.json { render json: {message: t('flash.exception_message')}.to_json, status: 403 }
format.csv { render text: t('flash.exception_message'), status: 403 }
end end
end end
protected protected
def set_csv_file_headers(file_name)
headers['Content-Type'] = 'text/csv'
headers['Content-disposition'] = "attachment; filename=\"#{file_name}.csv\""
end
def set_streaming_headers
#nginx doc: Setting this to "no" will allow unbuffered responses suitable for Comet and HTTP streaming applications
headers['X-Accel-Buffering'] = 'no'
headers['Cache-Control'] ||= 'no-cache'
headers.delete 'Content-Length'
end
def set_locale def set_locale
I18n.locale = :en I18n.locale = :en
end end

View File

@ -1,6 +1,6 @@
class Api::V1::RepositoriesController < Api::V1::BaseController class Api::V1::RepositoriesController < Api::V1::BaseController
before_filter :authenticate_user! # before_filter :authenticate_user!
skip_before_filter :authenticate_user!, :only => [:show, :projects] if APP_CONFIG['anonymous_access'] skip_before_filter :authenticate_user!, :only => [:show, :projects] if APP_CONFIG['anonymous_access']
load_and_authorize_resource :repository, :through => :platform, :shallow => true load_and_authorize_resource :repository, :through => :platform, :shallow => true
@ -32,6 +32,36 @@ class Api::V1::RepositoriesController < Api::V1::BaseController
def key_pair def key_pair
end end
# Only one request per 15 minutes for each platform
def packages
key, now = [@repository.platform.id, :repository_packages], Time.zone.now
last_request = Rails.cache.read(key)
if last_request.present? && last_request + 15.minutes > now
raise CanCan::AccessDenied
else
Rails.cache.write(key, now, expires_at: 15.minutes)
respond_to do |format|
format.csv do
set_csv_file_headers :packages
set_streaming_headers
response.status = 200
# setting the body to an enumerator, rails will iterate this enumerator
self.response_body = Enumerator.new do |y|
y << Api::V1::RepositoryPackagePresenter.csv_header.to_s
BuildList::Package.by_repository(@repository) do |package|
y << Api::V1::RepositoryPackagePresenter.new(package).to_csv_row.to_s
end
end
end
end
end
end
def add_repo_lock_file def add_repo_lock_file
@repository.add_repo_lock_file @repository.add_repo_lock_file
render_json_response @repository, "'.repo.lock' file has been added to repository successfully" render_json_response @repository, "'.repo.lock' file has been added to repository successfully"

View File

@ -122,7 +122,7 @@ class Ability
can([:read, :projects_list, :projects], Repository, read_relations_for('repositories')) {|repository| can? :show, repository.platform} can([:read, :projects_list, :projects], Repository, read_relations_for('repositories')) {|repository| can? :show, repository.platform}
can([:read, :projects_list, :projects], Repository, read_relations_for('repositories', 'platforms')) {|repository| local_reader? repository.platform} can([:read, :projects_list, :projects], Repository, read_relations_for('repositories', 'platforms')) {|repository| local_reader? repository.platform}
can([:create, :edit, :update, :destroy, :projects_list, :projects, :add_project, :remove_project, :regenerate_metadata, :sync_lock_file, :add_repo_lock_file, :remove_repo_lock_file], Repository) {|repository| local_admin? repository.platform} can([:create, :edit, :update, :destroy, :projects_list, :projects, :add_project, :remove_project, :regenerate_metadata, :sync_lock_file, :add_repo_lock_file, :remove_repo_lock_file], Repository) {|repository| local_admin? repository.platform}
can([:remove_members, :remove_member, :add_member, :signatures], Repository) {|repository| owner?(repository.platform) || local_admin?(repository.platform)} can([:remove_members, :remove_member, :add_member, :signatures, :packages], Repository) {|repository| owner?(repository.platform) || local_admin?(repository.platform)}
can([:add_project, :remove_project], Repository) {|repository| repository.members.exists?(:id => user.id)} can([:add_project, :remove_project], Repository) {|repository| repository.members.exists?(:id => user.id)}
can(:clear, Platform) {|platform| owner?(platform) && platform.personal?} can(:clear, Platform) {|platform| owner?(platform) && platform.personal?}
can(:regenerate_metadata, Platform) {|platform| owner?(platform) || local_admin?(platform)} can(:regenerate_metadata, Platform) {|platform| owner?(platform) || local_admin?(platform)}

View File

@ -28,7 +28,6 @@ class BuildList::Package < ActiveRecord::Base
project.maintainer project.maintainer
end end
# Comparison between versions # Comparison between versions
# @param [BuildList::Package] other # @param [BuildList::Package] other
# @return [Number] -1 if +other+ is greater than, 0 if +other+ is equal to, # @return [Number] -1 if +other+ is greater than, 0 if +other+ is equal to,
@ -37,6 +36,15 @@ class BuildList::Package < ActiveRecord::Base
RPM::C.rpmvercmp to_vre_epoch_zero, other.to_vre_epoch_zero RPM::C.rpmvercmp to_vre_epoch_zero, other.to_vre_epoch_zero
end end
def self.by_repository(repository, &block)
# find_each will batch the results instead of getting all in one go
actual.where(
build_lists: {save_to_repository_id: repository}
).joins(build_list: :save_to_repository).includes(project: :maintainer).find_each do |package|
yield package
end
end
protected protected
def set_epoch def set_epoch

View File

@ -0,0 +1,54 @@
require 'csv'
class Api::V1::RepositoryPackagePresenter < ApplicationPresenter
CSV_HEADERS = {
project_owner: 'Project owner',
project_name: 'Project name',
package_name: 'Package name',
epoch: 'Epoch',
version: 'Version',
release: 'Release',
maintainer_uname: 'Maintainer uname',
maintainer_email: 'Maintainer email',
committer_uname: 'Committer uname',
committer_email: 'Committer email'
}
attr_reader :package
delegate *%i(project name epoch version release assignee build_list), to: :package
def initialize(package)
@package = package
end
def to_csv_row
commit = project.repo.commit(build_list.commit_hash)
committer = User.where(email: commit.committer.email).first || commit.committer if commit
if committer.is_a? User
committer_uname, committer_email = committer.uname, committer.email
elsif committer.is_a? Grit::Actor
committer_uname, committer_email = committer.name, committer.email
end
CSV::Row.new(
CSV_HEADERS.keys,
[
project.owner_uname,
project.name,
name,
epoch,
version,
release,
assignee.uname,
assignee.email,
committer_uname,
committer_email
]
)
end
def self.csv_header
# Using ruby's built-in CSV::Row class
# true - means its a header
CSV::Row.new CSV_HEADERS.keys, CSV_HEADERS.values, true
end
end

View File

@ -44,6 +44,7 @@ Rosa::Application.routes.draw do
member { member {
get :projects get :projects
get :key_pair get :key_pair
get :packages
put :add_member put :add_member
delete :remove_member delete :remove_member
put :add_project put :add_project