2020-04-08 14:13:33 +05:30
# frozen_string_literal: true
module API
2020-07-28 23:09:34 +05:30
class DeployTokens < Grape :: API :: Instance
2020-04-08 14:13:33 +05:30
include PaginationParams
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
end
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
service_unavailable! unless Feature . enabled? ( :deploy_tokens_api , default_enabled : true )
authenticated_as_admin!
present paginate ( DeployToken . all ) , with : Entities :: DeployToken
end
params do
requires :id , type : String , desc : 'The ID of a project'
end
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
before do
service_unavailable! unless Feature . enabled? ( :deploy_tokens_api , user_project , default_enabled : true )
end
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
params do
requires :name , type : String , desc : " New deploy token's name "
2020-07-28 23:09:34 +05:30
requires :scopes , type : Array [ String ] , coerce_with : :: API :: Validations :: Types :: CommaSeparatedToArray . coerce , values : :: DeployToken :: AVAILABLE_SCOPES . map ( & :to_s ) ,
2020-05-24 23:13:21 +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".'
2020-04-22 19:07:51 +05:30
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-04-08 14:13:33 +05:30
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-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
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-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
requires :id , type : String , desc : 'The ID of a group'
end
resource :groups , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
before do
service_unavailable! unless Feature . enabled? ( :deploy_tokens_api , user_group , default_enabled : true )
end
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
params do
requires :name , type : String , desc : 'The name of the deploy token'
2020-07-28 23:09:34 +05:30
requires :scopes , type : Array [ String ] , coerce_with : :: API :: Validations :: Types :: CommaSeparatedToArray . coerce , values : :: DeployToken :: AVAILABLE_SCOPES . map ( & :to_s ) ,
2020-05-24 23:13:21 +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".'
2020-04-22 19:07:51 +05:30
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-04-08 14:13:33 +05:30
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-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
desc 'Delete a group deploy token' do
detail 'This feature was introduced in GitLab 12.9'
end
2020-04-22 19:07:51 +05:30
params do
requires :token_id , type : Integer , desc : 'The deploy token ID'
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