2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
module API
2021-01-03 14:25:43 +05:30
class DeployTokens < :: API :: Base
2020-04-08 14:13:33 +05:30
include PaginationParams
2023-01-13 00:05:48 +05:30
deploy_tokens_tags = %w[ deploy_tokens ]
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
2020-04-08 14:13:33 +05:30
helpers do
def scope_params
scopes = params . delete ( :scopes )
2020-04-22 19:07:51 +05:30
result_hash = Hashie :: Mash . new
2020-04-08 14:13:33 +05:30
result_hash [ :read_registry ] = scopes . include? ( 'read_registry' )
2020-04-22 19:07:51 +05:30
result_hash [ :write_registry ] = scopes . include? ( 'write_registry' )
2020-05-24 23:13:21 +05:30
result_hash [ :read_package_registry ] = scopes . include? ( 'read_package_registry' )
result_hash [ :write_package_registry ] = scopes . include? ( 'write_package_registry' )
2020-04-08 14:13:33 +05:30
result_hash [ :read_repository ] = scopes . include? ( 'read_repository' )
result_hash
end
2021-06-08 01:23:25 +05:30
params :filter_params do
optional :active , type : Boolean , desc : 'Limit by active status'
end
2020-04-08 14:13:33 +05:30
end
2023-01-13 00:05:48 +05:30
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-04-08 14:13:33 +05:30
success Entities :: DeployToken
2023-01-13 00:05:48 +05:30
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' }
]
is_array true
tags deploy_tokens_tags
2020-04-08 14:13:33 +05:30
end
params do
use :pagination
2021-06-08 01:23:25 +05:30
use :filter_params
2020-04-08 14:13:33 +05:30
end
get 'deploy_tokens' do
authenticated_as_admin!
2021-06-08 01:23:25 +05:30
deploy_tokens = :: DeployTokens :: TokensFinder . new (
current_user ,
:all ,
declared_params
) . execute
present paginate ( deploy_tokens ) , with : Entities :: DeployToken
2020-04-08 14:13:33 +05:30
end
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'
2020-04-08 14:13:33 +05:30
end
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
params do
use :pagination
2021-06-08 01:23:25 +05:30
use :filter_params
2020-04-08 14:13:33 +05:30
end
2023-01-13 00:05:48 +05:30
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-04-08 14:13:33 +05:30
success Entities :: DeployToken
2023-01-13 00:05:48 +05:30
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not found' }
]
is_array true
tags deploy_tokens_tags
2020-04-08 14:13:33 +05:30
end
get ':id/deploy_tokens' do
authorize! ( :read_deploy_token , user_project )
2021-06-08 01:23:25 +05:30
deploy_tokens = :: DeployTokens :: TokensFinder . new (
current_user ,
user_project ,
declared_params
) . execute
present paginate ( deploy_tokens ) , with : Entities :: DeployToken
2020-04-08 14:13:33 +05:30
end
params do
requires :name , type : String , desc : " New deploy token's name "
2022-08-27 11:52:29 +05:30
requires :scopes ,
type : Array [ String ] ,
coerce_with : :: API :: Validations :: Types :: CommaSeparatedToArray . coerce ,
values : :: DeployToken :: AVAILABLE_SCOPES . map ( & :to_s ) ,
2023-01-13 00:05:48 +05:30
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-04-22 19:07:51 +05:30
optional :username , type : String , desc : 'Username for deploy token. Default is `gitlab+deploy-token-{n}`'
2020-04-08 14:13:33 +05:30
end
desc 'Create a project deploy token' do
2023-01-13 00:05:48 +05:30
detail 'Creates a new deploy token for a project. This feature was introduced in GitLab 12.9.'
2020-04-08 14:13:33 +05:30
success Entities :: DeployTokenWithToken
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_tokens_tags
2020-04-08 14:13:33 +05:30
end
post ':id/deploy_tokens' do
authorize! ( :create_deploy_token , user_project )
2020-04-22 19:07:51 +05:30
result = :: Projects :: DeployTokens :: CreateService . new (
2020-04-08 14:13:33 +05:30
user_project , current_user , scope_params . merge ( declared ( params , include_missing : false , include_parent_namespaces : false ) )
) . execute
2020-04-22 19:07:51 +05:30
if result [ :status ] == :success
present result [ :deploy_token ] , with : Entities :: DeployTokenWithToken
else
render_api_error! ( result [ :message ] , result [ :http_status ] )
end
2020-04-08 14:13:33 +05:30
end
2022-05-07 20:08:51 +05:30
desc 'Get a project deploy token' do
2023-01-13 00:05:48 +05:30
detail " Get a single project's deploy token by ID. This feature was introduced in GitLab 14.9. "
2022-05-07 20:08:51 +05:30
success Entities :: DeployToken
2023-01-13 00:05:48 +05:30
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
tags deploy_tokens_tags
2022-05-07 20:08:51 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :token_id , type : Integer , desc : 'The ID of the deploy token'
2022-05-07 20:08:51 +05:30
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-04-08 14:13:33 +05:30
desc 'Delete a project deploy token' do
2023-01-13 00:05:48 +05:30
detail 'This feature was introduced in GitLab 12.9.'
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
tags deploy_tokens_tags
2020-04-08 14:13:33 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :token_id , type : Integer , desc : 'The ID of the deploy token'
2020-04-08 14:13:33 +05:30
end
delete ':id/deploy_tokens/:token_id' do
authorize! ( :destroy_deploy_token , user_project )
2020-04-22 19:07:51 +05:30
:: Projects :: DeployTokens :: DestroyService . new (
user_project , current_user , token_id : params [ :token_id ]
) . execute
2020-04-08 14:13:33 +05:30
no_content!
end
end
params do
2023-01-13 00:05:48 +05:30
requires :id , types : [ Integer , String ] , desc : 'The ID or URL-encoded path of the group owned by the authenticated user'
2020-04-08 14:13:33 +05:30
end
resource :groups , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
params do
use :pagination
2021-06-08 01:23:25 +05:30
use :filter_params
2020-04-08 14:13:33 +05:30
end
2023-01-13 00:05:48 +05:30
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-04-08 14:13:33 +05:30
success Entities :: DeployToken
2023-01-13 00:05:48 +05:30
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not found' }
]
is_array true
tags deploy_tokens_tags
2020-04-08 14:13:33 +05:30
end
get ':id/deploy_tokens' do
authorize! ( :read_deploy_token , user_group )
2021-06-08 01:23:25 +05:30
deploy_tokens = :: DeployTokens :: TokensFinder . new (
current_user ,
user_group ,
declared_params
) . execute
present paginate ( deploy_tokens ) , with : Entities :: DeployToken
2020-04-08 14:13:33 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :name , type : String , desc : " New deploy token's name "
2022-08-27 11:52:29 +05:30
requires :scopes ,
type : Array [ String ] ,
coerce_with : :: API :: Validations :: Types :: CommaSeparatedToArray . coerce ,
values : :: DeployToken :: AVAILABLE_SCOPES . map ( & :to_s ) ,
2023-01-13 00:05:48 +05:30
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-04-22 19:07:51 +05:30
optional :username , type : String , desc : 'Username for deploy token. Default is `gitlab+deploy-token-{n}`'
2020-04-08 14:13:33 +05:30
end
desc 'Create a group deploy token' do
2023-01-13 00:05:48 +05:30
detail 'Creates a new deploy token for a group. This feature was introduced in GitLab 12.9.'
2020-04-08 14:13:33 +05:30
success Entities :: DeployTokenWithToken
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_tokens_tags
2020-04-08 14:13:33 +05:30
end
post ':id/deploy_tokens' do
authorize! ( :create_deploy_token , user_group )
2020-04-22 19:07:51 +05:30
result = :: Groups :: DeployTokens :: CreateService . new (
2020-04-08 14:13:33 +05:30
user_group , current_user , scope_params . merge ( declared ( params , include_missing : false , include_parent_namespaces : false ) )
) . execute
2020-04-22 19:07:51 +05:30
if result [ :status ] == :success
present result [ :deploy_token ] , with : Entities :: DeployTokenWithToken
else
render_api_error! ( result [ :message ] , result [ :http_status ] )
end
2020-04-08 14:13:33 +05:30
end
2022-05-07 20:08:51 +05:30
desc 'Get a group deploy token' do
2023-01-13 00:05:48 +05:30
detail " Get a single group's deploy token by ID. This feature was introduced in GitLab 14.9. "
2022-05-07 20:08:51 +05:30
success Entities :: DeployToken
2023-01-13 00:05:48 +05:30
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
tags deploy_tokens_tags
2022-05-07 20:08:51 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :token_id , type : Integer , desc : 'The ID of the deploy token'
2022-05-07 20:08:51 +05:30
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-04-08 14:13:33 +05:30
desc 'Delete a group deploy token' do
2023-01-13 00:05:48 +05:30
detail 'Removes a deploy token from the group. This feature was introduced in GitLab 12.9.'
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
tags deploy_tokens_tags
2020-04-08 14:13:33 +05:30
end
2020-04-22 19:07:51 +05:30
params do
2023-01-13 00:05:48 +05:30
requires :token_id , type : Integer , desc : 'The ID of the deploy token'
2020-04-22 19:07:51 +05:30
end
2020-04-08 14:13:33 +05:30
delete ':id/deploy_tokens/:token_id' do
authorize! ( :destroy_deploy_token , user_group )
2020-04-22 19:07:51 +05:30
:: Groups :: DeployTokens :: DestroyService . new (
user_group , current_user , token_id : params [ :token_id ]
) . execute
2020-04-08 14:13:33 +05:30
2020-04-22 19:07:51 +05:30
no_content!
2020-04-08 14:13:33 +05:30
end
end
end
end