2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
module API
2017-08-17 22:00:37 +05:30
# Deployments RESTful API endpoints
2016-09-13 17:45:13 +05:30
class Deployments < Grape :: API
2017-08-17 22:00:37 +05:30
include PaginationParams
2016-09-13 17:45:13 +05:30
before { authenticate! }
params do
requires :id , type : String , desc : 'The project ID'
end
2019-02-15 15:39:39 +05:30
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2016-09-13 17:45:13 +05:30
desc 'Get all deployments of the project' do
detail 'This feature was introduced in GitLab 8.11.'
success Entities :: Deployment
end
params do
2017-08-17 22:00:37 +05:30
use :pagination
2020-01-01 13:55:28 +05:30
optional :order_by , type : String , values : DeploymentsFinder :: ALLOWED_SORT_VALUES , default : DeploymentsFinder :: DEFAULT_SORT_VALUE , desc : 'Return deployments ordered by specified value'
optional :sort , type : String , values : DeploymentsFinder :: ALLOWED_SORT_DIRECTIONS , default : DeploymentsFinder :: DEFAULT_SORT_DIRECTION , desc : 'Sort by asc (ascending) or desc (descending)'
optional :updated_after , type : DateTime , desc : 'Return deployments updated after the specified date'
optional :updated_before , type : DateTime , desc : 'Return deployments updated before the specified date'
2020-03-13 15:44:24 +05:30
optional :environment ,
type : String ,
desc : 'The name of the environment to filter deployments by'
optional :status ,
type : String ,
values : Deployment . statuses . keys ,
desc : 'The status to filter deployments by'
2016-09-13 17:45:13 +05:30
end
2020-01-01 13:55:28 +05:30
2016-09-13 17:45:13 +05:30
get ':id/deployments' do
authorize! :read_deployment , user_project
2020-01-01 13:55:28 +05:30
deployments = DeploymentsFinder . new ( user_project , params ) . execute
present paginate ( deployments ) , with : Entities :: Deployment
2016-09-13 17:45:13 +05:30
end
desc 'Gets a specific deployment' do
detail 'This feature was introduced in GitLab 8.11.'
success Entities :: Deployment
end
params do
2019-03-02 22:35:43 +05:30
requires :deployment_id , type : Integer , desc : 'The deployment ID'
2016-09-13 17:45:13 +05:30
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-12-21 20:55:43 +05:30
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
2020-03-13 15:44:24 +05:30
helpers Helpers :: MergeRequestsHelpers
desc 'Get all merge requests of a deployment' do
detail 'This feature was introduced in GitLab 12.7.'
success Entities :: MergeRequestBasic
end
params do
2020-04-08 14:13:33 +05:30
use :pagination
2020-03-13 15:44:24 +05:30
requires :deployment_id , type : Integer , desc : 'The deployment ID'
use :merge_requests_base_params
end
get ':id/deployments/:deployment_id/merge_requests' do
authorize! :read_deployment , user_project
mr_params = declared_params . merge ( deployment_id : params [ :deployment_id ] )
merge_requests = MergeRequestsFinder . new ( current_user , mr_params ) . execute
2020-04-08 14:13:33 +05:30
present paginate ( merge_requests ) , { with : Entities :: MergeRequestBasic , current_user : current_user }
2020-03-13 15:44:24 +05:30
end
2016-09-13 17:45:13 +05:30
end
end
end