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
2021-01-03 14:25:43 +05:30
class Deployments < :: API :: Base
2017-08-17 22:00:37 +05:30
include PaginationParams
2023-01-13 00:05:48 +05:30
deployments_tags = %w[ deployments ]
2016-09-13 17:45:13 +05:30
before { authenticate! }
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
2016-09-13 17:45:13 +05:30
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'
2016-09-13 17:45:13 +05:30
end
2019-02-15 15:39:39 +05:30
resource :projects , requirements : API :: NAMESPACE_OR_PROJECT_REQUIREMENTS do
2023-01-13 00:05:48 +05:30
desc 'List project deployments' do
detail 'Get a list of deployments in a project. This feature was introduced in GitLab 8.11.'
2016-09-13 17:45:13 +05:30
success Entities :: Deployment
2023-01-13 00:05:48 +05:30
failure [
{ code : 400 , message : 'Bad request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
is_array true
tags deployments_tags
2016-09-13 17:45:13 +05:30
end
params do
2017-08-17 22:00:37 +05:30
use :pagination
2023-01-13 00:05:48 +05:30
optional :order_by ,
type : String ,
values : DeploymentsFinder :: ALLOWED_SORT_VALUES ,
default : DeploymentsFinder :: DEFAULT_SORT_VALUE ,
desc : 'Return deployments ordered by either one of `id`, `iid`, `created_at`, `updated_at` or `ref` fields. Default is `id`'
optional :sort ,
type : String ,
values : DeploymentsFinder :: ALLOWED_SORT_DIRECTIONS ,
default : DeploymentsFinder :: DEFAULT_SORT_DIRECTION ,
desc : 'Return deployments sorted in `asc` or `desc` order. Default is `asc`'
optional :updated_after ,
type : DateTime ,
desc : 'Return deployments updated after the specified date. Expected in ISO 8601 format (`2019-03-15T08:00:00Z`)'
optional :updated_before ,
type : DateTime ,
desc : 'Return deployments updated before the specified date. Expected in ISO 8601 format (`2019-03-15T08:00:00Z`)'
2023-03-04 22:38:38 +05:30
optional :finished_after ,
type : DateTime ,
desc : 'Return deployments finished after the specified date. Expected in ISO 8601 format (`2019-03-15T08:00:00Z`)'
optional :finished_before ,
type : DateTime ,
desc : 'Return deployments finished before the specified date. Expected in ISO 8601 format (`2019-03-15T08:00:00Z`)'
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 ,
2023-01-13 00:05:48 +05:30
desc : 'The status to filter deployments by. One of `created`, `running`, `success`, `failed`, `canceled`, or `blocked`'
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
2021-04-29 21:17:54 +05:30
deployments =
2023-03-04 22:38:38 +05:30
DeploymentsFinder . new ( declared_params ( include_missing : false ) . merge ( project : user_project ) )
2021-04-29 21:17:54 +05:30
. execute . with_api_entity_associations
2020-01-01 13:55:28 +05:30
present paginate ( deployments ) , with : Entities :: Deployment
2021-06-08 01:23:25 +05:30
rescue DeploymentsFinder :: InefficientQueryError = > e
bad_request! ( e . message )
2016-09-13 17:45:13 +05:30
end
2023-01-13 00:05:48 +05:30
desc 'Get a specific deployment' do
2016-09-13 17:45:13 +05:30
detail 'This feature was introduced in GitLab 8.11.'
2022-04-04 11:22:00 +05:30
success Entities :: DeploymentExtended
2023-01-13 00:05:48 +05:30
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
tags deployments_tags
2016-09-13 17:45:13 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :deployment_id , type : Integer , desc : 'The ID of the deployment'
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 ] )
2022-04-04 11:22:00 +05:30
present deployment , with : Entities :: DeploymentExtended
2016-09-13 17:45:13 +05:30
end
2019-12-21 20:55:43 +05:30
2023-01-13 00:05:48 +05:30
desc 'Create a deployment' do
detail 'This feature was introduced in GitLab 12.4.'
2022-04-04 11:22:00 +05:30
success Entities :: DeploymentExtended
2023-01-13 00:05:48 +05:30
failure [
{ code : 400 , message : 'Bad request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
tags deployments_tags
2019-12-21 20:55:43 +05:30
end
params do
requires :environment ,
type : String ,
2023-01-13 00:05:48 +05:30
desc : 'The name of the environment to create the deployment for'
2019-12-21 20:55:43 +05:30
requires :sha ,
type : String ,
2023-01-13 00:05:48 +05:30
desc : 'The SHA of the commit that is deployed'
2019-12-21 20:55:43 +05:30
requires :ref ,
type : String ,
2023-01-13 00:05:48 +05:30
desc : 'The name of the branch or tag that is deployed'
2019-12-21 20:55:43 +05:30
requires :tag ,
type : Boolean ,
2023-01-13 00:05:48 +05:30
desc : 'A boolean that indicates if the deployed ref is a tag (`true`) or not (`false`)'
2019-12-21 20:55:43 +05:30
requires :status ,
type : String ,
2023-01-13 00:05:48 +05:30
desc : 'The status to filter deployments by. One of `running`, `success`, `failed`, or `canceled`' ,
2019-12-21 20:55:43 +05:30
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?
2023-01-13 00:05:48 +05:30
render_validation_error! ( environment )
2019-12-21 20:55:43 +05:30
end
authorize! ( :create_deployment , environment )
service = :: Deployments :: CreateService
. new ( environment , current_user , declared_params )
deployment = service . execute
if deployment . persisted?
2022-04-04 11:22:00 +05:30
present ( deployment , with : Entities :: DeploymentExtended , current_user : current_user )
2019-12-21 20:55:43 +05:30
else
render_validation_error! ( deployment )
end
end
2023-01-13 00:05:48 +05:30
desc 'Update a deployment' do
detail 'This feature was introduced in GitLab 12.4.'
2022-04-04 11:22:00 +05:30
success Entities :: DeploymentExtended
2023-01-13 00:05:48 +05:30
failure [
{ code : 400 , message : 'Bad request' } ,
{ code : 401 , message : 'Unauthorized' } ,
{ code : 403 , message : 'Forbidden' } ,
{ code : 404 , message : 'Not found' }
]
tags deployments_tags
2019-12-21 20:55:43 +05:30
end
params do
requires :status ,
2022-08-27 11:52:29 +05:30
type : String ,
2023-01-13 00:05:48 +05:30
desc : 'The new status of the deployment. One of `running`, `success`, `failed`, or `canceled`' ,
2022-08-27 11:52:29 +05:30
values : %w[ running success failed canceled ]
2019-12-21 20:55:43 +05:30
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
2022-04-04 11:22:00 +05:30
present ( deployment , with : Entities :: DeploymentExtended , current_user : current_user )
2019-12-21 20:55:43 +05:30
else
render_validation_error! ( deployment )
end
end
2020-03-13 15:44:24 +05:30
2023-01-13 00:05:48 +05:30
desc 'Delete a specific deployment' do
detail 'Delete a specific deployment that is not currently the last deployment for an environment or in a running state. This feature was introduced in GitLab 15.3.'
http_codes [
[ 204 , 'Deployment destroyed' ] ,
[ 403 , 'Forbidden' ] ,
[ 400 , '"Cannot destroy running deployment" or "Deployment currently deployed to environment"' ]
]
tags deployments_tags
2022-08-27 11:52:29 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :deployment_id , type : Integer , desc : 'The ID of the deployment'
2022-08-27 11:52:29 +05:30
end
delete ':id/deployments/:deployment_id' do
deployment = user_project . deployments . find ( params [ :deployment_id ] )
authorize! ( :destroy_deployment , deployment )
destroy_conditionally! ( deployment ) do
result = :: Ci :: Deployments :: DestroyService . new ( user_project , current_user ) . execute ( deployment )
if result [ :status ] == :error
render_api_error! ( result [ :message ] , result [ :http_status ] || 400 )
end
end
end
2020-03-13 15:44:24 +05:30
helpers Helpers :: MergeRequestsHelpers
2023-01-13 00:05:48 +05:30
desc 'List of merge requests associated with a deployment' do
detail 'Retrieves the list of merge requests shipped with a given deployment. This feature was introduced in GitLab 12.7.'
2020-03-13 15:44:24 +05:30
success Entities :: MergeRequestBasic
2023-01-13 00:05:48 +05:30
failure [
{ code : 401 , message : 'Unauthorized' } ,
{ code : 404 , message : 'Not found' }
]
is_array true
tags deployments_tags
2020-03-13 15:44:24 +05:30
end
params do
2020-04-08 14:13:33 +05:30
use :pagination
2023-01-13 00:05:48 +05:30
requires :deployment_id , type : Integer , desc : 'The ID of the deployment'
2020-03-13 15:44:24 +05:30
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
2022-03-02 08:16:31 +05:30
API :: Deployments . prepend_mod