2020-02-19 22:08:57 -05:00
# frozen_string_literal: true
module API
2020-10-14 20:08:42 -04:00
class DeployTokens < :: API :: Base
2020-02-19 22:08:57 -05:00
include PaginationParams
2020-10-29 08:08:50 -04:00
feature_category :continuous_delivery
2020-03-10 05:08:10 -04:00
helpers do
def scope_params
scopes = params . delete ( :scopes )
2020-04-21 11:21:10 -04:00
result_hash = Hashie :: Mash . new
2020-03-10 05:08:10 -04:00
result_hash [ :read_registry ] = scopes . include? ( 'read_registry' )
2020-04-13 20:09:57 -04:00
result_hash [ :write_registry ] = scopes . include? ( 'write_registry' )
2020-05-13 23:07:52 -04:00
result_hash [ :read_package_registry ] = scopes . include? ( 'read_package_registry' )
result_hash [ :write_package_registry ] = scopes . include? ( 'write_package_registry' )
2020-03-10 05:08:10 -04:00
result_hash [ :read_repository ] = scopes . include? ( 'read_repository' )
result_hash
end
end
2020-02-19 22:08:57 -05:00
desc 'Return all deploy tokens' do
detail 'This feature was introduced in GitLab 12.9.'
success Entities :: DeployToken
end
params do
use :pagination
end
get 'deploy_tokens' do
2020-02-28 07:09:05 -05:00
authenticated_as_admin!
2020-02-19 22:08:57 -05:00
present paginate ( DeployToken . all ) , with : Entities :: DeployToken
end
2020-02-28 07:09:05 -05:00
params do
2020-03-11 17:09:19 -04:00
requires :id , type : String , desc : 'The ID of a project'
2020-02-28 07:09:05 -05:00
end
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
params do
use :pagination
end
desc 'List deploy tokens for a project' do
detail 'This feature was introduced in GitLab 12.9'
success Entities :: DeployToken
end
get ':id/deploy_tokens' do
authorize! ( :read_deploy_token , user_project )
present paginate ( user_project . deploy_tokens ) , with : Entities :: DeployToken
end
2020-03-10 05:08:10 -04:00
params do
requires :name , type : String , desc : " New deploy token's name "
2020-06-29 17:09:07 -04:00
requires :scopes , type : Array [ String ] , coerce_with : :: API :: Validations :: Types :: CommaSeparatedToArray . coerce , values : :: DeployToken :: AVAILABLE_SCOPES . map ( & :to_s ) ,
2020-05-13 23:07:52 -04: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".'
2020-03-26 14:08:03 -04:00
optional :expires_at , type : DateTime , desc : 'Expiration date for the deploy token. Does not expire if no value is provided.'
optional :username , type : String , desc : 'Username for deploy token. Default is `gitlab+deploy-token-{n}`'
2020-03-10 05:08:10 -04:00
end
desc 'Create a project deploy token' do
detail 'This feature was introduced in GitLab 12.9'
success Entities :: DeployTokenWithToken
end
post ':id/deploy_tokens' do
authorize! ( :create_deploy_token , user_project )
2020-04-01 08:08:00 -04:00
result = :: Projects :: DeployTokens :: CreateService . new (
2020-03-10 05:08:10 -04:00
user_project , current_user , scope_params . merge ( declared ( params , include_missing : false , include_parent_namespaces : false ) )
) . execute
2020-04-01 08:08:00 -04: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 05:08:10 -04:00
end
2020-03-12 08:09:17 -04:00
desc 'Delete a project deploy token' do
detail 'This feature was introduced in GitLab 12.9'
end
params do
requires :token_id , type : Integer , desc : 'The deploy token ID'
end
delete ':id/deploy_tokens/:token_id' do
authorize! ( :destroy_deploy_token , user_project )
2020-04-02 08:08:18 -04:00
:: Projects :: DeployTokens :: DestroyService . new (
user_project , current_user , token_id : params [ :token_id ]
) . execute
2020-03-12 08:09:17 -04:00
no_content!
end
2020-02-28 07:09:05 -05:00
end
2020-03-08 20:08:14 -04:00
params do
2020-03-11 17:09:19 -04:00
requires :id , type : String , desc : 'The ID of a group'
2020-03-08 20:08:14 -04:00
end
resource :groups , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2020-03-11 02:10:11 -04:00
params do
use :pagination
end
desc 'List deploy tokens for a group' do
detail 'This feature was introduced in GitLab 12.9'
success Entities :: DeployToken
end
get ':id/deploy_tokens' do
authorize! ( :read_deploy_token , user_group )
present paginate ( user_group . deploy_tokens ) , with : Entities :: DeployToken
end
2020-03-11 17:09:19 -04:00
params do
requires :name , type : String , desc : 'The name of the deploy token'
2020-06-29 17:09:07 -04:00
requires :scopes , type : Array [ String ] , coerce_with : :: API :: Validations :: Types :: CommaSeparatedToArray . coerce , values : :: DeployToken :: AVAILABLE_SCOPES . map ( & :to_s ) ,
2020-05-13 23:07:52 -04: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".'
2020-03-26 14:08:03 -04:00
optional :expires_at , type : DateTime , desc : 'Expiration date for the deploy token. Does not expire if no value is provided.'
optional :username , type : String , desc : 'Username for deploy token. Default is `gitlab+deploy-token-{n}`'
2020-03-11 17:09:19 -04:00
end
desc 'Create a group deploy token' do
detail 'This feature was introduced in GitLab 12.9'
success Entities :: DeployTokenWithToken
end
post ':id/deploy_tokens' do
authorize! ( :create_deploy_token , user_group )
2020-04-01 08:08:00 -04:00
result = :: Groups :: DeployTokens :: CreateService . new (
2020-03-11 17:09:19 -04:00
user_group , current_user , scope_params . merge ( declared ( params , include_missing : false , include_parent_namespaces : false ) )
) . execute
2020-04-01 08:08:00 -04: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 17:09:19 -04:00
end
2020-03-08 20:08:14 -04:00
desc 'Delete a group deploy token' do
detail 'This feature was introduced in GitLab 12.9'
end
2020-04-02 08:08:18 -04:00
params do
requires :token_id , type : Integer , desc : 'The deploy token ID'
end
2020-03-08 20:08:14 -04:00
delete ':id/deploy_tokens/:token_id' do
authorize! ( :destroy_deploy_token , user_group )
2020-04-02 08:08:18 -04:00
:: Groups :: DeployTokens :: DestroyService . new (
user_group , current_user , token_id : params [ :token_id ]
) . execute
2020-03-08 20:08:14 -04:00
2020-04-02 08:08:18 -04:00
no_content!
2020-03-08 20:08:14 -04:00
end
end
2020-02-19 22:08:57 -05:00
end
end