2020-02-20 03:08:57 +00:00
# frozen_string_literal: true
module API
2020-10-15 00:08:42 +00:00
class DeployTokens < :: API :: Base
2020-02-20 03:08:57 +00:00
include PaginationParams
2020-10-29 12:08:50 +00:00
feature_category :continuous_delivery
2022-04-29 12:10:13 +00:00
urgency :low
2020-10-29 12:08:50 +00:00
2020-03-10 09:08:10 +00:00
helpers do
def scope_params
scopes = params . delete ( :scopes )
2020-04-21 15:21:10 +00:00
result_hash = Hashie :: Mash . new
2020-03-10 09:08:10 +00:00
result_hash [ :read_registry ] = scopes . include? ( 'read_registry' )
2020-04-14 00:09:57 +00:00
result_hash [ :write_registry ] = scopes . include? ( 'write_registry' )
2020-05-14 03:07:52 +00:00
result_hash [ :read_package_registry ] = scopes . include? ( 'read_package_registry' )
result_hash [ :write_package_registry ] = scopes . include? ( 'write_package_registry' )
2020-03-10 09:08:10 +00:00
result_hash [ :read_repository ] = scopes . include? ( 'read_repository' )
result_hash
end
2021-04-30 15:09:50 +00:00
params :filter_params do
optional :active , type : Boolean , desc : 'Limit by active status'
end
2020-03-10 09:08:10 +00:00
end
2022-10-31 18:09:25 +00:00
desc 'List all deploy tokens' do
detail 'Get a list of all deploy tokens across the GitLab instance. This endpoint requires administrator access. This feature was introduced in GitLab 12.9.'
2020-02-20 03:08:57 +00:00
success Entities :: DeployToken
2022-10-31 18:09:25 +00:00
is_array true
tags %w[ deploy_tokens ]
2020-02-20 03:08:57 +00:00
end
params do
use :pagination
2021-04-30 15:09:50 +00:00
use :filter_params
2020-02-20 03:08:57 +00:00
end
get 'deploy_tokens' do
2020-02-28 12:09:05 +00:00
authenticated_as_admin!
2021-04-30 15:09:50 +00:00
deploy_tokens = :: DeployTokens :: TokensFinder . new (
current_user ,
:all ,
declared_params
) . execute
present paginate ( deploy_tokens ) , with : Entities :: DeployToken
2020-02-20 03:08:57 +00:00
end
2020-02-28 12:09:05 +00:00
params do
2022-10-31 18:09:25 +00:00
requires :id , types : [ String , Integer ] , desc : 'The ID or URL-encoded path of the project owned by the authenticated user'
2020-02-28 12:09:05 +00:00
end
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
params do
use :pagination
2021-04-30 15:09:50 +00:00
use :filter_params
2020-02-28 12:09:05 +00:00
end
2022-10-31 18:09:25 +00:00
desc 'List project deploy tokens' do
detail " Get a list of a project's deploy tokens. This feature was introduced in GitLab 12.9. "
2020-02-28 12:09:05 +00:00
success Entities :: DeployToken
2022-10-31 18:09:25 +00:00
is_array true
tags %w[ deploy_tokens ]
2020-02-28 12:09:05 +00:00
end
get ':id/deploy_tokens' do
authorize! ( :read_deploy_token , user_project )
2021-04-30 15:09:50 +00:00
deploy_tokens = :: DeployTokens :: TokensFinder . new (
current_user ,
user_project ,
declared_params
) . execute
present paginate ( deploy_tokens ) , with : Entities :: DeployToken
2020-02-28 12:09:05 +00:00
end
2020-03-10 09:08:10 +00:00
params do
requires :name , type : String , desc : " New deploy token's name "
2022-08-11 00:09:45 +00:00
requires :scopes ,
type : Array [ String ] ,
coerce_with : :: API :: Validations :: Types :: CommaSeparatedToArray . coerce ,
values : :: DeployToken :: AVAILABLE_SCOPES . map ( & :to_s ) ,
2022-10-31 18:09:25 +00:00
desc : 'Indicates the deploy token scopes. Must be at least one of `read_repository`, `read_registry`, `write_registry`, `read_package_registry`, or `write_package_registry`.'
optional :expires_at , type : DateTime , desc : 'Expiration date for the deploy token. Does not expire if no value is provided. Expected in ISO 8601 format (`2019-03-15T08:00:00Z`).'
2020-03-26 18:08:03 +00:00
optional :username , type : String , desc : 'Username for deploy token. Default is `gitlab+deploy-token-{n}`'
2020-03-10 09:08:10 +00:00
end
desc 'Create a project deploy token' do
2022-10-31 18:09:25 +00:00
detail 'Creates a new deploy token for a project. This feature was introduced in GitLab 12.9.'
2020-03-10 09:08:10 +00:00
success Entities :: DeployTokenWithToken
2022-10-31 18:09:25 +00:00
tags %w[ deploy_tokens ]
2020-03-10 09:08:10 +00:00
end
post ':id/deploy_tokens' do
authorize! ( :create_deploy_token , user_project )
2020-04-01 12:08:00 +00:00
result = :: Projects :: DeployTokens :: CreateService . new (
2020-03-10 09:08:10 +00:00
user_project , current_user , scope_params . merge ( declared ( params , include_missing : false , include_parent_namespaces : false ) )
) . execute
2020-04-01 12:08:00 +00:00
if result [ :status ] == :success
present result [ :deploy_token ] , with : Entities :: DeployTokenWithToken
else
render_api_error! ( result [ :message ] , result [ :http_status ] )
end
2020-03-10 09:08:10 +00:00
end
2020-03-12 12:09:17 +00:00
2022-03-11 03:08:14 +00:00
desc 'Get a project deploy token' do
2022-10-31 18:09:25 +00:00
detail " Get a single project's deploy token by ID. This feature was introduced in GitLab 14.9. "
2022-03-11 03:08:14 +00:00
success Entities :: DeployToken
2022-10-31 18:09:25 +00:00
tags %w[ deploy_tokens ]
2022-03-11 03:08:14 +00:00
end
params do
2022-10-31 18:09:25 +00:00
requires :token_id , type : Integer , desc : 'The ID of the deploy token'
2022-03-11 03:08:14 +00:00
end
get ':id/deploy_tokens/:token_id' do
authorize! ( :read_deploy_token , user_project )
deploy_token = user_project . deploy_tokens . find ( params [ :token_id ] )
present deploy_token , with : Entities :: DeployToken
end
2020-03-12 12:09:17 +00:00
desc 'Delete a project deploy token' do
2022-10-31 18:09:25 +00:00
detail 'This feature was introduced in GitLab 12.9.'
tags %w[ deploy_tokens ]
2020-03-12 12:09:17 +00:00
end
params do
2022-10-31 18:09:25 +00:00
requires :token_id , type : Integer , desc : 'The ID of the deploy token'
2020-03-12 12:09:17 +00:00
end
delete ':id/deploy_tokens/:token_id' do
authorize! ( :destroy_deploy_token , user_project )
2020-04-02 12:08:18 +00:00
:: Projects :: DeployTokens :: DestroyService . new (
user_project , current_user , token_id : params [ :token_id ]
) . execute
2020-03-12 12:09:17 +00:00
no_content!
end
2020-02-28 12:09:05 +00:00
end
2020-03-09 00:08:14 +00:00
params do
2022-10-31 18:09:25 +00:00
requires :id , types : [ Integer , String ] , desc : 'The ID or URL-encoded path of the group owned by the authenticated user'
2020-03-09 00:08:14 +00:00
end
resource :groups , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2020-03-11 06:10:11 +00:00
params do
use :pagination
2021-04-30 15:09:50 +00:00
use :filter_params
2020-03-11 06:10:11 +00:00
end
2022-10-31 18:09:25 +00:00
desc 'List group deploy tokens' do
detail " Get a list of a group's deploy tokens. This feature was introduced in GitLab 12.9. "
2020-03-11 06:10:11 +00:00
success Entities :: DeployToken
2022-10-31 18:09:25 +00:00
is_array true
tags %w[ deploy_tokens ]
2020-03-11 06:10:11 +00:00
end
get ':id/deploy_tokens' do
authorize! ( :read_deploy_token , user_group )
2021-04-30 15:09:50 +00:00
deploy_tokens = :: DeployTokens :: TokensFinder . new (
current_user ,
user_group ,
declared_params
) . execute
present paginate ( deploy_tokens ) , with : Entities :: DeployToken
2020-03-11 06:10:11 +00:00
end
2020-03-11 21:09:19 +00:00
params do
2022-10-31 18:09:25 +00:00
requires :name , type : String , desc : " New deploy token's name "
2022-08-11 00:09:45 +00:00
requires :scopes ,
type : Array [ String ] ,
coerce_with : :: API :: Validations :: Types :: CommaSeparatedToArray . coerce ,
values : :: DeployToken :: AVAILABLE_SCOPES . map ( & :to_s ) ,
2022-10-31 18:09:25 +00:00
desc : 'Indicates the deploy token scopes. Must be at least one of `read_repository`, `read_registry`, `write_registry`, `read_package_registry`, or `write_package_registry`'
optional :expires_at , type : DateTime , desc : 'Expiration date for the deploy token. Does not expire if no value is provided. Expected in ISO 8601 format (`2019-03-15T08:00:00Z`)'
2020-03-26 18:08:03 +00:00
optional :username , type : String , desc : 'Username for deploy token. Default is `gitlab+deploy-token-{n}`'
2020-03-11 21:09:19 +00:00
end
desc 'Create a group deploy token' do
2022-10-31 18:09:25 +00:00
detail 'Creates a new deploy token for a group. This feature was introduced in GitLab 12.9.'
2020-03-11 21:09:19 +00:00
success Entities :: DeployTokenWithToken
2022-10-31 18:09:25 +00:00
tags %w[ deploy_tokens ]
2020-03-11 21:09:19 +00:00
end
post ':id/deploy_tokens' do
authorize! ( :create_deploy_token , user_group )
2020-04-01 12:08:00 +00:00
result = :: Groups :: DeployTokens :: CreateService . new (
2020-03-11 21:09:19 +00:00
user_group , current_user , scope_params . merge ( declared ( params , include_missing : false , include_parent_namespaces : false ) )
) . execute
2020-04-01 12:08:00 +00:00
if result [ :status ] == :success
present result [ :deploy_token ] , with : Entities :: DeployTokenWithToken
else
render_api_error! ( result [ :message ] , result [ :http_status ] )
end
2020-03-11 21:09:19 +00:00
end
2022-03-11 03:08:14 +00:00
desc 'Get a group deploy token' do
2022-10-31 18:09:25 +00:00
detail " Get a single group's deploy token by ID. This feature was introduced in GitLab 14.9. "
2022-03-11 03:08:14 +00:00
success Entities :: DeployToken
2022-10-31 18:09:25 +00:00
tags %w[ deploy_tokens ]
2022-03-11 03:08:14 +00:00
end
params do
2022-10-31 18:09:25 +00:00
requires :token_id , type : Integer , desc : 'The ID of the deploy token'
2022-03-11 03:08:14 +00:00
end
get ':id/deploy_tokens/:token_id' do
authorize! ( :read_deploy_token , user_group )
deploy_token = user_group . deploy_tokens . find ( params [ :token_id ] )
present deploy_token , with : Entities :: DeployToken
end
2020-03-09 00:08:14 +00:00
desc 'Delete a group deploy token' do
2022-10-31 18:09:25 +00:00
detail 'Removes a deploy token from the group. This feature was introduced in GitLab 12.9.'
tags %w[ deploy_tokens ]
2020-03-09 00:08:14 +00:00
end
2020-04-02 12:08:18 +00:00
params do
2022-10-31 18:09:25 +00:00
requires :token_id , type : Integer , desc : 'The ID of the deploy token'
2020-04-02 12:08:18 +00:00
end
2020-03-09 00:08:14 +00:00
delete ':id/deploy_tokens/:token_id' do
authorize! ( :destroy_deploy_token , user_group )
2020-04-02 12:08:18 +00:00
:: Groups :: DeployTokens :: DestroyService . new (
user_group , current_user , token_id : params [ :token_id ]
) . execute
2020-03-09 00:08:14 +00:00
2020-04-02 12:08:18 +00:00
no_content!
2020-03-09 00:08:14 +00:00
end
end
2020-02-20 03:08:57 +00:00
end
end