2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module API
2021-01-03 14:25:43 +05:30
class DeployKeys < :: API :: Base
2017-08-17 22:00:37 +05:30
include PaginationParams
2023-01-13 00:05:48 +05:30
deploy_keys_tags = %w[ deploy_keys ]
2014-09-02 18:07:02 +05:30
before { authenticate! }
2016-08-24 12:49:21 +05:30
2021-01-29 00:20:46 +05:30
feature_category :continuous_delivery
2022-07-16 23:28:13 +05:30
urgency :low
2021-01-29 00:20:46 +05:30
2018-03-17 18:26:18 +05:30
helpers do
def add_deploy_keys_project ( project , attrs = { } )
project . deploy_keys_projects . create ( attrs )
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def find_by_deploy_key ( project , key_id )
project . deploy_keys_projects . find_by! ( deploy_key : key_id )
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
end
2023-01-13 00:05:48 +05:30
desc 'List all deploy keys' do
detail 'Get a list of all deploy keys across all projects of the GitLab instance. This endpoint requires administrator access and is not available on GitLab.com.'
success Entities :: DeployKey
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' }
]
is_array true
tags deploy_keys_tags
end
2017-08-17 22:00:37 +05:30
params do
use :pagination
2021-12-11 22:18:48 +05:30
optional :public , type : Boolean , default : false , desc : " Only return deploy keys that are public "
2017-08-17 22:00:37 +05:30
end
2016-08-24 12:49:21 +05:30
get " deploy_keys " do
authenticated_as_admin!
2021-12-11 22:18:48 +05:30
deploy_keys = params [ :public ] ? DeployKey . are_public : DeployKey . all
present paginate ( deploy_keys . including_projects_with_write_access ) , with : Entities :: DeployKey , include_projects_with_write_access : true
2016-08-24 12:49:21 +05:30
end
2014-09-02 18:07:02 +05:30
2016-09-13 17:45:13 +05:30
params do
2023-01-13 00:05:48 +05:30
requires :id , types : [ String , Integer ] , desc : 'The ID or URL-encoded path of the project owned by the authenticated user'
2016-09-13 17:45:13 +05:30
end
2019-02-15 15:39:39 +05:30
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2016-08-24 12:49:21 +05:30
before { authorize_admin_project }
2014-09-02 18:07:02 +05:30
2023-01-13 00:05:48 +05:30
desc 'List deploy keys for project' do
detail " Get a list of a project's deploy keys. "
2018-03-17 18:26:18 +05:30
success Entities :: DeployKeysProject
2023-01-13 00:05:48 +05:30
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
is_array true
tags deploy_keys_tags
2017-08-17 22:00:37 +05:30
end
params do
use :pagination
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
get " :id/deploy_keys " do
2021-04-29 21:17:54 +05:30
keys = user_project . deploy_keys_projects . preload ( deploy_key : :user )
2018-03-17 18:26:18 +05:30
present paginate ( keys ) , with : Entities :: DeployKeysProject
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-08-24 12:49:21 +05:30
2023-01-13 00:05:48 +05:30
desc 'Get a single deploy key' do
detail 'Get a single key.'
2018-03-17 18:26:18 +05:30
success Entities :: DeployKeysProject
2023-01-13 00:05:48 +05:30
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
tags deploy_keys_tags
2017-08-17 22:00:37 +05:30
end
params do
requires :key_id , type : Integer , desc : 'The ID of the deploy key'
end
get " :id/deploy_keys/:key_id " do
2018-03-17 18:26:18 +05:30
key = find_by_deploy_key ( user_project , params [ :key_id ] )
present key , with : Entities :: DeployKeysProject
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
2023-01-13 00:05:48 +05:30
desc 'Add deploy key' do
detail " Creates a new deploy key for a project. If the deploy key already exists in another project, it's joined to the current project only if the original one is accessible by the same user. "
2018-03-17 18:26:18 +05:30
success Entities :: DeployKeysProject
2023-01-13 00:05:48 +05:30
failure [
{ code : 400 , message : 'Bad request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
tags deploy_keys_tags
2017-08-17 22:00:37 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :key , type : String , desc : 'New deploy key'
requires :title , type : String , desc : " New deploy key's title "
2017-08-17 22:00:37 +05:30
optional :can_push , type : Boolean , desc : " Can deploy key push to the project's repository "
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
post " :id/deploy_keys " do
params [ :key ] . strip!
2016-08-24 12:49:21 +05:30
2017-08-17 22:00:37 +05:30
# Check for an existing key joined to this project
2018-05-09 12:01:36 +05:30
deploy_key_project = user_project . deploy_keys_projects
2018-03-17 18:26:18 +05:30
. joins ( :deploy_key )
. find_by ( keys : { key : params [ :key ] } )
2018-05-09 12:01:36 +05:30
if deploy_key_project
present deploy_key_project , with : Entities :: DeployKeysProject
2017-08-17 22:00:37 +05:30
break
2014-09-02 18:07:02 +05:30
end
2017-08-17 22:00:37 +05:30
# Check for available deploy keys in other projects
key = current_user . accessible_deploy_keys . find_by ( key : params [ :key ] )
if key
2018-05-09 12:01:36 +05:30
deploy_key_project = add_deploy_keys_project ( user_project , deploy_key : key , can_push : ! ! params [ :can_push ] )
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
present deploy_key_project , with : Entities :: DeployKeysProject
2017-08-17 22:00:37 +05:30
break
2016-09-13 17:45:13 +05:30
end
2017-08-17 22:00:37 +05:30
# Create a new deploy key
2018-05-09 12:01:36 +05:30
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-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
if deploy_key_project . valid?
present deploy_key_project , with : Entities :: DeployKeysProject
2017-08-17 22:00:37 +05:30
else
2018-05-09 12:01:36 +05:30
render_validation_error! ( deploy_key_project )
2016-09-13 17:45:13 +05:30
end
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-09-13 17:45:13 +05:30
2023-01-13 00:05:48 +05:30
desc 'Update deploy key' do
detail 'Updates a deploy key for a project.'
2020-07-01 16:08:20 +05:30
success Entities :: DeployKey
2023-01-13 00:05:48 +05:30
failure [
{ code : 400 , message : 'Bad request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not found' }
]
tags deploy_keys_tags
2017-09-10 17:25:29 +05:30
end
params do
requires :key_id , type : Integer , desc : 'The ID of the deploy key'
2023-01-13 00:05:48 +05:30
optional :title , type : String , desc : " New deploy key's title "
2017-09-10 17:25:29 +05:30
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-03-17 18:26:18 +05:30
deploy_keys_project = find_by_deploy_key ( user_project , params [ :key_id ] )
2017-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
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-09-10 17:25:29 +05:30
2019-12-21 20:55:43 +05:30
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-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
result = deploy_keys_project . update ( update_params )
2018-03-17 18:26:18 +05:30
if result
present deploy_keys_project , with : Entities :: DeployKeysProject
2017-09-10 17:25:29 +05:30
else
2018-03-17 18:26:18 +05:30
render_validation_error! ( deploy_keys_project )
2017-09-10 17:25:29 +05:30
end
end
2023-01-13 00:05:48 +05:30
desc 'Enable a deploy key' do
detail 'Enables a deploy key for a project so this can be used. Returns the enabled key, with a status code 201 when successful. This feature was added in GitLab 8.11.'
2020-07-01 16:08:20 +05:30
success Entities :: DeployKey
2023-01-13 00:05:48 +05:30
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
tags deploy_keys_tags
2017-08-17 22:00:37 +05:30
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-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
if key
2020-07-01 16:08:20 +05:30
present key , with : Entities :: DeployKey
2017-08-17 22:00:37 +05:30
else
not_found! ( 'Deploy Key' )
2016-09-13 17:45:13 +05:30
end
2017-08-17 22:00:37 +05:30
end
2016-09-13 17:45:13 +05:30
2023-01-13 00:05:48 +05:30
desc 'Delete deploy key' do
detail " Removes a deploy key from the project. If the deploy key is used only for this project, it's deleted from the system. "
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
tags deploy_keys_tags
end
2017-08-17 22:00:37 +05:30
params do
requires :key_id , type : Integer , desc : 'The ID of the deploy key'
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
delete " :id/deploy_keys/:key_id " do
2018-06-03 19:52:53 +05:30
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-08-17 22:00:37 +05:30
2018-06-03 19:52:53 +05:30
destroy_conditionally! ( deploy_key_project )
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
end
end
end