2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-26 07:37:02 +00:00
|
|
|
module API
|
|
|
|
# Environments RESTfull API endpoints
|
2020-06-29 21:09:07 +00:00
|
|
|
class Environments < Grape::API::Instance
|
2016-11-21 19:15:46 +00:00
|
|
|
include PaginationParams
|
|
|
|
|
2016-07-26 07:37:02 +00:00
|
|
|
before { authenticate! }
|
|
|
|
|
2016-07-29 10:14:36 +00:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The project ID'
|
|
|
|
end
|
2018-11-08 12:18:17 +00:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-07-29 10:14:36 +00:00
|
|
|
desc 'Get all environments of the project' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.11.'
|
|
|
|
success Entities::Environment
|
|
|
|
end
|
2016-08-01 06:42:09 +00:00
|
|
|
params do
|
2016-11-21 19:15:46 +00:00
|
|
|
use :pagination
|
2019-06-24 12:18:40 +00:00
|
|
|
optional :name, type: String, desc: 'Returns the environment with this name'
|
|
|
|
optional :search, type: String, desc: 'Returns list of environments matching the search criteria'
|
|
|
|
mutually_exclusive :name, :search, message: 'cannot be used together'
|
2016-08-01 06:42:09 +00:00
|
|
|
end
|
2016-07-26 07:37:02 +00:00
|
|
|
get ':id/environments' do
|
|
|
|
authorize! :read_environment, user_project
|
|
|
|
|
2019-06-24 12:18:40 +00:00
|
|
|
environments = ::EnvironmentsFinder.new(user_project, current_user, params).find
|
|
|
|
|
|
|
|
present paginate(environments), with: Entities::Environment, current_user: current_user
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
|
2016-07-29 10:14:36 +00:00
|
|
|
desc 'Creates a new environment' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.11.'
|
|
|
|
success Entities::Environment
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :name, type: String, desc: 'The name of the environment to be created'
|
|
|
|
optional :external_url, type: String, desc: 'URL on which this deployment is viewable'
|
2016-12-08 01:09:18 +00:00
|
|
|
optional :slug, absence: { message: "is automatically generated and cannot be changed" }
|
2016-07-29 10:14:36 +00:00
|
|
|
end
|
2016-07-26 07:37:02 +00:00
|
|
|
post ':id/environments' do
|
|
|
|
authorize! :create_environment, user_project
|
|
|
|
|
2016-11-14 13:44:27 +00:00
|
|
|
environment = user_project.environments.create(declared_params)
|
2016-07-26 07:37:02 +00:00
|
|
|
|
2016-07-29 10:14:36 +00:00
|
|
|
if environment.persisted?
|
2019-01-31 04:21:35 +00:00
|
|
|
present environment, with: Entities::Environment, current_user: current_user
|
2016-07-26 07:37:02 +00:00
|
|
|
else
|
|
|
|
render_validation_error!(environment)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-29 10:14:36 +00:00
|
|
|
desc 'Updates an existing environment' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.11.'
|
|
|
|
success Entities::Environment
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :environment_id, type: Integer, desc: 'The environment ID'
|
|
|
|
optional :name, type: String, desc: 'The new environment name'
|
|
|
|
optional :external_url, type: String, desc: 'The new URL on which this deployment is viewable'
|
2016-12-08 01:09:18 +00:00
|
|
|
optional :slug, absence: { message: "is automatically generated and cannot be changed" }
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
put ':id/environments/:environment_id' do
|
|
|
|
authorize! :update_environment, user_project
|
|
|
|
|
|
|
|
environment = user_project.environments.find(params[:environment_id])
|
2016-11-14 13:44:27 +00:00
|
|
|
|
|
|
|
update_params = declared_params(include_missing: false).extract!(:name, :external_url)
|
2016-07-29 10:14:36 +00:00
|
|
|
if environment.update(update_params)
|
2019-01-31 04:21:35 +00:00
|
|
|
present environment, with: Entities::Environment, current_user: current_user
|
2016-07-26 07:37:02 +00:00
|
|
|
else
|
|
|
|
render_validation_error!(environment)
|
|
|
|
end
|
|
|
|
end
|
2016-07-29 10:14:36 +00:00
|
|
|
|
|
|
|
desc 'Deletes an existing environment' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.11.'
|
|
|
|
success Entities::Environment
|
|
|
|
end
|
|
|
|
params do
|
2019-01-16 12:09:29 +00:00
|
|
|
requires :environment_id, type: Integer, desc: 'The environment ID'
|
2016-07-29 10:14:36 +00:00
|
|
|
end
|
|
|
|
delete ':id/environments/:environment_id' do
|
2020-03-25 09:08:11 +00:00
|
|
|
authorize! :read_environment, user_project
|
2016-07-29 10:14:36 +00:00
|
|
|
|
|
|
|
environment = user_project.environments.find(params[:environment_id])
|
2020-03-25 09:08:11 +00:00
|
|
|
authorize! :destroy_environment, environment
|
2016-07-29 10:14:36 +00:00
|
|
|
|
2017-03-02 12:14:13 +00:00
|
|
|
destroy_conditionally!(environment)
|
2016-07-29 10:14:36 +00:00
|
|
|
end
|
2017-02-28 13:55:21 +00:00
|
|
|
|
|
|
|
desc 'Stops an existing environment' do
|
|
|
|
success Entities::Environment
|
|
|
|
end
|
|
|
|
params do
|
2019-01-16 12:09:29 +00:00
|
|
|
requires :environment_id, type: Integer, desc: 'The environment ID'
|
2017-02-28 13:55:21 +00:00
|
|
|
end
|
|
|
|
post ':id/environments/:environment_id/stop' do
|
2018-07-10 08:11:04 +00:00
|
|
|
authorize! :read_environment, user_project
|
2017-02-28 13:55:21 +00:00
|
|
|
|
|
|
|
environment = user_project.environments.find(params[:environment_id])
|
2018-07-10 08:11:04 +00:00
|
|
|
authorize! :stop_environment, environment
|
2017-02-28 13:55:21 +00:00
|
|
|
|
|
|
|
environment.stop_with_action!(current_user)
|
|
|
|
|
|
|
|
status 200
|
2019-01-31 04:21:35 +00:00
|
|
|
present environment, with: Entities::Environment, current_user: current_user
|
2017-02-28 13:55:21 +00:00
|
|
|
end
|
2019-04-09 09:16:57 +00:00
|
|
|
|
|
|
|
desc 'Get a single environment' do
|
|
|
|
success Entities::Environment
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :environment_id, type: Integer, desc: 'The environment ID'
|
|
|
|
end
|
|
|
|
get ':id/environments/:environment_id' do
|
|
|
|
authorize! :read_environment, user_project
|
|
|
|
|
|
|
|
environment = user_project.environments.find(params[:environment_id])
|
|
|
|
present environment, with: Entities::Environment, current_user: current_user,
|
|
|
|
except: [:project, { last_deployment: [:environment] }],
|
|
|
|
last_deployment: true
|
|
|
|
end
|
2016-07-26 07:37:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|