rosa-build/app/controllers/api/v1/repositories_controller.rb

119 lines
3.5 KiB
Ruby
Raw Normal View History

class Api::V1::RepositoriesController < Api::V1::BaseController
2014-08-28 21:22:11 +01:00
respond_to :csv, only: :packages
2014-01-21 04:51:49 +00:00
2015-03-04 23:19:19 +00:00
before_action :authenticate_user!
skip_before_action :check_auth, only: [:show, :projects] if APP_CONFIG['anonymous_access']
2015-03-04 23:19:19 +00:00
skip_before_action :authenticate_user!, only: [:show, :projects] if APP_CONFIG['anonymous_access']
2015-03-19 23:31:41 +00:00
before_action :load_repository
def show
end
def projects
2014-08-28 21:22:11 +01:00
@projects = @repository.projects.recent.paginate(paginate_params)
end
def update
2012-10-14 13:39:58 +01:00
update_subject @repository
end
def add_member
2012-10-14 13:39:58 +01:00
add_member_to_subject @repository
end
def remove_member
2012-10-14 13:39:58 +01:00
remove_member_from_subject @repository
end
def destroy
2012-10-14 13:39:58 +01:00
destroy_subject @repository
end
def key_pair
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
2015-03-12 22:43:13 +00:00
raise Pundit::NotAuthorizedError
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
@repository.add_repo_lock_file
render_json_response @repository, "'.repo.lock' file has been added to repository successfully"
end
def remove_repo_lock_file
@repository.remove_repo_lock_file
render_json_response @repository, "'.repo.lock' file has been removed from repository successfully"
end
2012-10-14 14:38:06 +01:00
def add_project
2014-01-21 04:51:49 +00:00
if project = Project.where(id: params[:project_id]).first
2015-03-14 22:10:04 +00:00
if policy(project).read?
begin
@repository.projects << project
render_json_response @repository, "Project '#{project.id}' has been added to repository successfully"
rescue ActiveRecord::RecordInvalid
render_validation_error @repository, t('flash.repository.project_not_added')
end
else
render_validation_error @repository, 'You have no access to read this project'
2012-10-14 14:38:06 +01:00
end
else
render_validation_error @repository, "Project has not been added to repository"
end
end
def remove_project
project_id = params[:project_id]
2014-01-21 04:51:49 +00:00
ProjectToRepository.where(project_id: project_id, repository_id: @repository.id).destroy_all
2012-10-14 14:38:06 +01:00
render_json_response @repository, "Project '#{project_id}' has been removed from repository successfully"
end
def signatures
key_pair = @repository.key_pair
key_pair.destroy if key_pair
key_pair = @repository.build_key_pair subject_params(Repository, KeyPair)
2012-10-14 14:38:06 +01:00
key_pair.user_id = current_user.id
authorize key_pair, :create?
2012-10-14 14:38:06 +01:00
if key_pair.save
render_json_response @repository, 'Signatures have been updated for repository successfully'
else
render_json_response @repository, error_message(key_pair, 'Signatures have not been updated for repository'), 422
2012-10-14 14:38:06 +01:00
end
end
2015-03-19 23:31:41 +00:00
private
# Private: before_action hook which loads Repository.
def load_repository
authorize @repository = Repository.find(params[:id])
2015-03-19 23:31:41 +00:00
end
end