2012-08-17 14:40:57 +01:00
|
|
|
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 :authenticate_user!, only: [:show, :projects] if APP_CONFIG['anonymous_access']
|
2012-08-17 14:40:57 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
load_and_authorize_resource :repository, through: :platform, shallow: true
|
2012-09-26 18:15:11 +01:00
|
|
|
|
|
|
|
def show
|
2014-08-28 21:46:42 +01:00
|
|
|
respond_to :json
|
2012-10-14 13:00:49 +01:00
|
|
|
end
|
|
|
|
|
2012-10-19 14:52:36 +01:00
|
|
|
def projects
|
2014-08-28 21:22:11 +01:00
|
|
|
@projects = @repository.projects.recent.paginate(paginate_params)
|
2014-08-28 21:46:42 +01:00
|
|
|
respond_to :json
|
2012-10-19 14:52:36 +01:00
|
|
|
end
|
|
|
|
|
2012-10-14 13:00:49 +01:00
|
|
|
def update
|
2012-10-14 13:39:58 +01:00
|
|
|
update_subject @repository
|
2012-10-14 13:00:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_member
|
2012-10-14 13:39:58 +01:00
|
|
|
add_member_to_subject @repository
|
2012-10-14 13:00:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_member
|
2012-10-14 13:39:58 +01:00
|
|
|
remove_member_from_subject @repository
|
2012-10-14 13:00:49 +01:00
|
|
|
end
|
2012-09-26 18:15:11 +01:00
|
|
|
|
2012-10-14 13:00:49 +01:00
|
|
|
def destroy
|
2012-10-14 13:39:58 +01:00
|
|
|
destroy_subject @repository
|
2012-09-26 18:15:11 +01:00
|
|
|
end
|
|
|
|
|
2012-12-20 19:46:16 +00:00
|
|
|
def key_pair
|
2014-08-28 21:46:42 +01:00
|
|
|
respond_to :json
|
2012-12-20 19:46:16 +00:00
|
|
|
end
|
|
|
|
|
2014-01-20 21:24:22 +00:00
|
|
|
# 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
|
2014-01-20 21:24:22 +00:00
|
|
|
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
|
|
|
|
|
2013-07-29 13:24:41 +01:00
|
|
|
def add_repo_lock_file
|
|
|
|
@repository.add_repo_lock_file
|
2013-07-29 14:14:55 +01:00
|
|
|
render_json_response @repository, "'.repo.lock' file has been added to repository successfully"
|
2013-07-26 16:38:39 +01:00
|
|
|
end
|
|
|
|
|
2013-07-29 13:24:41 +01:00
|
|
|
def remove_repo_lock_file
|
|
|
|
@repository.remove_repo_lock_file
|
2013-07-29 14:14:55 +01:00
|
|
|
render_json_response @repository, "'.repo.lock' file has been removed from repository successfully"
|
2013-07-26 16:38:39 +01:00
|
|
|
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?
|
2013-11-13 09:33:36 +00:00
|
|
|
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
|
2012-10-14 15:44:10 +01:00
|
|
|
key_pair = @repository.build_key_pair(params[:repository])
|
2012-10-14 14:38:06 +01:00
|
|
|
key_pair.user_id = current_user.id
|
|
|
|
if key_pair.save
|
|
|
|
render_json_response @repository, 'Signatures have been updated for repository successfully'
|
|
|
|
else
|
2012-10-15 15:06:08 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
end
|