2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-06 05:37:51 -04:00
|
|
|
module API
|
2020-10-14 20:08:42 -04:00
|
|
|
class DeployKeys < ::API::Base
|
2017-01-16 23:45:07 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2013-06-06 05:37:51 -04:00
|
|
|
before { authenticate! }
|
2016-06-01 22:25:22 -04:00
|
|
|
|
2020-10-29 08:08:50 -04:00
|
|
|
feature_category :continuous_delivery
|
|
|
|
|
2018-01-05 10:23:44 -05:00
|
|
|
helpers do
|
|
|
|
def add_deploy_keys_project(project, attrs = {})
|
|
|
|
project.deploy_keys_projects.create(attrs)
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-01-05 10:23:44 -05:00
|
|
|
def find_by_deploy_key(project, key_id)
|
|
|
|
project.deploy_keys_projects.find_by!(deploy_key: key_id)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-01-05 10:23:44 -05:00
|
|
|
end
|
|
|
|
|
2017-01-16 23:45:07 -05:00
|
|
|
desc 'Return all deploy keys'
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2016-06-01 22:25:22 -04:00
|
|
|
get "deploy_keys" do
|
|
|
|
authenticated_as_admin!
|
|
|
|
|
2020-06-22 14:08:47 -04:00
|
|
|
present paginate(DeployKey.all), with: Entities::DeployKey
|
2016-06-01 22:25:22 -04:00
|
|
|
end
|
2013-06-06 05:37:51 -04:00
|
|
|
|
2016-08-03 10:45:35 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of the project'
|
|
|
|
end
|
2018-11-08 07:18:17 -05:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-06-01 22:25:22 -04:00
|
|
|
before { authorize_admin_project }
|
2013-06-06 05:37:51 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
desc "Get a specific project's deploy keys" do
|
2018-01-05 10:23:44 -05:00
|
|
|
success Entities::DeployKeysProject
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
2017-01-16 23:45:07 -05:00
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-06 08:39:22 -05:00
|
|
|
get ":id/deploy_keys" do
|
2021-03-24 08:09:32 -04:00
|
|
|
keys = user_project.deploy_keys_projects.preload(deploy_key: :user)
|
2018-01-05 10:23:44 -05:00
|
|
|
|
|
|
|
present paginate(keys), with: Entities::DeployKeysProject
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2013-06-06 05:37:51 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
desc 'Get single deploy key' do
|
2018-01-05 10:23:44 -05:00
|
|
|
success Entities::DeployKeysProject
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key_id, type: Integer, desc: 'The ID of the deploy key'
|
|
|
|
end
|
|
|
|
get ":id/deploy_keys/:key_id" do
|
2018-01-05 10:23:44 -05:00
|
|
|
key = find_by_deploy_key(user_project, params[:key_id])
|
|
|
|
|
|
|
|
present key, with: Entities::DeployKeysProject
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
|
|
|
|
2018-03-28 12:53:52 -04:00
|
|
|
desc 'Add new deploy key to a project' do
|
2018-01-05 10:23:44 -05:00
|
|
|
success Entities::DeployKeysProject
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key, type: String, desc: 'The new deploy key'
|
|
|
|
requires :title, type: String, desc: 'The name of the deploy key'
|
2017-04-05 02:22:28 -04:00
|
|
|
optional :can_push, type: Boolean, desc: "Can deploy key push to the project's repository"
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-06 08:39:22 -05:00
|
|
|
post ":id/deploy_keys" do
|
|
|
|
params[:key].strip!
|
|
|
|
|
|
|
|
# Check for an existing key joined to this project
|
2018-03-28 12:53:52 -04:00
|
|
|
deploy_key_project = user_project.deploy_keys_projects
|
2018-01-05 10:23:44 -05:00
|
|
|
.joins(:deploy_key)
|
|
|
|
.find_by(keys: { key: params[:key] })
|
|
|
|
|
2018-03-28 12:53:52 -04:00
|
|
|
if deploy_key_project
|
|
|
|
present deploy_key_project, with: Entities::DeployKeysProject
|
2017-02-06 08:39:22 -05:00
|
|
|
break
|
2016-06-01 22:25:22 -04:00
|
|
|
end
|
2013-06-06 05:37:51 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
# Check for available deploy keys in other projects
|
|
|
|
key = current_user.accessible_deploy_keys.find_by(key: params[:key])
|
|
|
|
if key
|
2018-03-28 12:53:52 -04:00
|
|
|
deploy_key_project = add_deploy_keys_project(user_project, deploy_key: key, can_push: !!params[:can_push])
|
2018-01-05 10:23:44 -05:00
|
|
|
|
2018-03-28 12:53:52 -04:00
|
|
|
present deploy_key_project, with: Entities::DeployKeysProject
|
2017-02-06 08:39:22 -05:00
|
|
|
break
|
2017-01-23 11:06:57 -05:00
|
|
|
end
|
2016-06-01 22:25:22 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
# Create a new deploy key
|
2018-03-28 12:53:52 -04:00
|
|
|
deploy_key_attributes = declared_params.except(:can_push).merge(user: current_user)
|
|
|
|
deploy_key_project = add_deploy_keys_project(user_project, deploy_key_attributes: deploy_key_attributes, can_push: !!params[:can_push])
|
2018-01-05 10:23:44 -05:00
|
|
|
|
2018-03-28 12:53:52 -04:00
|
|
|
if deploy_key_project.valid?
|
|
|
|
present deploy_key_project, with: Entities::DeployKeysProject
|
2017-02-06 08:39:22 -05:00
|
|
|
else
|
2018-03-28 12:53:52 -04:00
|
|
|
render_validation_error!(deploy_key_project)
|
2017-03-31 08:54:38 -04:00
|
|
|
end
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-03-31 08:54:38 -04:00
|
|
|
|
|
|
|
desc 'Update an existing deploy key for a project' do
|
2020-06-22 14:08:47 -04:00
|
|
|
success Entities::DeployKey
|
2017-03-31 08:54:38 -04:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key_id, type: Integer, desc: 'The ID of the deploy key'
|
|
|
|
optional :title, type: String, desc: 'The name of the deploy key'
|
|
|
|
optional :can_push, type: Boolean, desc: "Can deploy key push to the project's repository"
|
|
|
|
at_least_one_of :title, :can_push
|
|
|
|
end
|
|
|
|
put ":id/deploy_keys/:key_id" do
|
2018-01-05 10:23:44 -05:00
|
|
|
deploy_keys_project = find_by_deploy_key(user_project, params[:key_id])
|
2017-03-31 08:54:38 -04:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
if !can?(current_user, :update_deploy_key, deploy_keys_project.deploy_key) &&
|
|
|
|
!can?(current_user, :update_deploy_keys_project, deploy_keys_project)
|
|
|
|
forbidden!(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
update_params = {}
|
|
|
|
update_params[:can_push] = params[:can_push] if params.key?(:can_push)
|
|
|
|
update_params[:deploy_key_attributes] = { id: params[:key_id] }
|
2017-03-31 08:54:38 -04:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
if can?(current_user, :update_deploy_key, deploy_keys_project.deploy_key)
|
|
|
|
update_params[:deploy_key_attributes][:title] = params[:title] if params.key?(:title)
|
|
|
|
end
|
2018-01-05 10:23:44 -05:00
|
|
|
|
2019-10-16 20:07:27 -04:00
|
|
|
result = deploy_keys_project.update(update_params)
|
2018-01-17 08:42:32 -05:00
|
|
|
|
2018-01-05 10:23:44 -05:00
|
|
|
if result
|
|
|
|
present deploy_keys_project, with: Entities::DeployKeysProject
|
2017-03-31 08:54:38 -04:00
|
|
|
else
|
2018-01-05 10:23:44 -05:00
|
|
|
render_validation_error!(deploy_keys_project)
|
2013-06-06 05:37:51 -04:00
|
|
|
end
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
2013-06-06 05:37:51 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
desc 'Enable a deploy key for a project' do
|
|
|
|
detail 'This feature was added in GitLab 8.11'
|
2020-06-22 14:08:47 -04:00
|
|
|
success Entities::DeployKey
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key_id, type: Integer, desc: 'The ID of the deploy key'
|
|
|
|
end
|
|
|
|
post ":id/deploy_keys/:key_id/enable" do
|
|
|
|
key = ::Projects::EnableDeployKeyService.new(user_project,
|
|
|
|
current_user, declared_params).execute
|
2016-08-03 08:45:32 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
if key
|
2020-06-22 14:08:47 -04:00
|
|
|
present key, with: Entities::DeployKey
|
2017-02-06 08:39:22 -05:00
|
|
|
else
|
|
|
|
not_found!('Deploy Key')
|
2016-08-03 08:45:32 -04:00
|
|
|
end
|
2017-02-06 08:39:22 -05:00
|
|
|
end
|
2016-08-03 08:45:32 -04:00
|
|
|
|
2017-02-06 08:39:22 -05:00
|
|
|
desc 'Delete deploy key for a project' do
|
|
|
|
success Key
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :key_id, type: Integer, desc: 'The ID of the deploy key'
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-02-06 08:39:22 -05:00
|
|
|
delete ":id/deploy_keys/:key_id" do
|
2018-05-04 06:40:37 -04:00
|
|
|
deploy_key_project = user_project.deploy_keys_projects.find_by(deploy_key_id: params[:key_id])
|
|
|
|
not_found!('Deploy Key') unless deploy_key_project
|
2017-02-20 03:33:42 -05:00
|
|
|
|
2018-05-04 06:40:37 -04:00
|
|
|
destroy_conditionally!(deploy_key_project)
|
2013-06-06 05:37:51 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2013-06-06 05:37:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|