2018-09-29 18:34:47 -04:00
# frozen_string_literal: true
2016-08-16 02:45:23 -04:00
module API
2017-03-06 12:19:15 -05:00
# Deployments RESTful API endpoints
2016-08-16 02:45:23 -04:00
class Deployments < Grape :: API
2016-11-21 14:15:46 -05:00
include PaginationParams
2016-08-16 02:45:23 -04:00
before { authenticate! }
params do
requires :id , type : String , desc : 'The project ID'
end
2018-11-08 07:18:17 -05:00
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2016-08-16 02:45:23 -04:00
desc 'Get all deployments of the project' do
detail 'This feature was introduced in GitLab 8.11.'
success Entities :: Deployment
end
params do
2016-11-21 14:15:46 -05:00
use :pagination
2019-11-15 19:06:14 -05:00
optional :order_by , type : String , values : %w[ id iid created_at updated_at ref ] , default : 'id' , desc : 'Return deployments ordered by `id` or `iid` or `created_at` or `updated_at` or `ref`'
2018-01-11 11:45:35 -05:00
optional :sort , type : String , values : %w[ asc desc ] , default : 'asc' , desc : 'Sort by asc (ascending) or desc (descending)'
2016-08-16 02:45:23 -04:00
end
2018-08-27 11:31:01 -04:00
# rubocop: disable CodeReuse/ActiveRecord
2016-08-16 02:45:23 -04:00
get ':id/deployments' do
authorize! :read_deployment , user_project
2018-01-11 11:45:35 -05:00
present paginate ( user_project . deployments . order ( params [ :order_by ] = > params [ :sort ] ) ) , with : Entities :: Deployment
2016-08-16 02:45:23 -04:00
end
2018-08-27 11:31:01 -04:00
# rubocop: enable CodeReuse/ActiveRecord
2016-08-16 02:45:23 -04:00
desc 'Gets a specific deployment' do
detail 'This feature was introduced in GitLab 8.11.'
success Entities :: Deployment
end
params do
2019-01-16 07:09:29 -05:00
requires :deployment_id , type : Integer , desc : 'The deployment ID'
2016-08-16 02:45:23 -04:00
end
get ':id/deployments/:deployment_id' do
authorize! :read_deployment , user_project
deployment = user_project . deployments . find ( params [ :deployment_id ] )
present deployment , with : Entities :: Deployment
end
2019-10-16 14:08:01 -04:00
desc 'Creates a new deployment' do
detail 'This feature was introduced in GitLab 12.4'
success Entities :: Deployment
end
params do
requires :environment ,
type : String ,
desc : 'The name of the environment to deploy to'
requires :sha ,
type : String ,
desc : 'The SHA of the commit that was deployed'
requires :ref ,
type : String ,
desc : 'The name of the branch or tag that was deployed'
requires :tag ,
type : Boolean ,
desc : 'A boolean indicating if the deployment ran for a tag'
requires :status ,
type : String ,
desc : 'The status of the deployment' ,
values : %w[ running success failed canceled ]
end
post ':id/deployments' do
authorize! ( :create_deployment , user_project )
authorize! ( :create_environment , user_project )
environment = user_project
. environments
. find_or_create_by_name ( params [ :environment ] )
unless environment . persisted?
render_validation_error! ( deployment )
end
authorize! ( :create_deployment , environment )
service = :: Deployments :: CreateService
. new ( environment , current_user , declared_params )
deployment = service . execute
if deployment . persisted?
present ( deployment , with : Entities :: Deployment , current_user : current_user )
else
render_validation_error! ( deployment )
end
end
desc 'Updates an existing deployment' do
detail 'This feature was introduced in GitLab 12.4'
success Entities :: Deployment
end
params do
requires :status ,
type : String ,
desc : 'The new status of the deployment' ,
values : %w[ running success failed canceled ]
end
put ':id/deployments/:deployment_id' do
authorize! ( :read_deployment , user_project )
deployment = user_project . deployments . find ( params [ :deployment_id ] )
authorize! ( :update_deployment , deployment )
if deployment . deployable
forbidden! ( 'Deployments created using GitLab CI can not be updated using the API' )
end
service = :: Deployments :: UpdateService . new ( deployment , declared_params )
if service . execute
present ( deployment , with : Entities :: Deployment , current_user : current_user )
else
render_validation_error! ( deployment )
end
end
2016-08-16 02:45:23 -04:00
end
end
end