debian-mirror-gitlab/lib/api/environments.rb

157 lines
6.2 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
module API
# Environments RESTfull API endpoints
2021-01-03 14:25:43 +05:30
class Environments < ::API::Base
2017-08-17 22:00:37 +05:30
include PaginationParams
2016-09-13 17:45:13 +05:30
before { authenticate! }
2021-01-29 00:20:46 +05:30
feature_category :continuous_delivery
2016-09-13 17:45:13 +05:30
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 environments of the project' do
detail 'This feature was introduced in GitLab 8.11.'
success Entities::Environment
end
params do
2017-08-17 22:00:37 +05:30
use :pagination
2019-09-30 21:07:59 +05:30
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-09-13 17:45:13 +05:30
end
get ':id/environments' do
authorize! :read_environment, user_project
2021-06-08 01:23:25 +05:30
environments = ::Environments::EnvironmentsFinder.new(user_project, current_user, params).execute
2019-09-30 21:07:59 +05:30
present paginate(environments), with: Entities::Environment, current_user: current_user
2016-09-13 17:45:13 +05:30
end
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'
2017-08-17 22:00:37 +05:30
optional :slug, absence: { message: "is automatically generated and cannot be changed" }
2016-09-13 17:45:13 +05:30
end
post ':id/environments' do
authorize! :create_environment, user_project
2017-08-17 22:00:37 +05:30
environment = user_project.environments.create(declared_params)
2016-09-13 17:45:13 +05:30
if environment.persisted?
2019-03-13 22:55:13 +05:30
present environment, with: Entities::Environment, current_user: current_user
2016-09-13 17:45:13 +05:30
else
render_validation_error!(environment)
end
end
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'
2021-11-11 11:23:49 +05:30
# TODO: disallow renaming via the API https://gitlab.com/gitlab-org/gitlab/-/issues/338897
optional :name, type: String, desc: 'DEPRECATED: Renaming environment can lead to errors, this will be removed in 15.0'
2016-09-13 17:45:13 +05:30
optional :external_url, type: String, desc: 'The new URL on which this deployment is viewable'
2017-08-17 22:00:37 +05:30
optional :slug, absence: { message: "is automatically generated and cannot be changed" }
2016-09-13 17:45:13 +05:30
end
put ':id/environments/:environment_id' do
authorize! :update_environment, user_project
environment = user_project.environments.find(params[:environment_id])
2017-08-17 22:00:37 +05:30
update_params = declared_params(include_missing: false).extract!(:name, :external_url)
2016-09-13 17:45:13 +05:30
if environment.update(update_params)
2019-03-13 22:55:13 +05:30
present environment, with: Entities::Environment, current_user: current_user
2016-09-13 17:45:13 +05:30
else
render_validation_error!(environment)
end
end
2021-04-17 20:07:23 +05:30
desc "Delete multiple stopped review apps" do
detail "Remove multiple stopped review environments older than a specific age"
2021-10-27 15:23:28 +05:30
success Entities::EnvironmentBasic
2021-04-17 20:07:23 +05:30
end
params do
optional :before, type: Time, desc: "The timestamp before which environments can be deleted. Defaults to 30 days ago.", default: -> { 30.days.ago }
optional :limit, type: Integer, desc: "Maximum number of environments to delete. Defaults to 100.", default: 100, values: 1..1000
optional :dry_run, type: Boolean, desc: "If set, perform a dry run where no actual deletions will be performed. Defaults to true.", default: true
end
delete ":id/environments/review_apps" do
authorize! :read_environment, user_project
result = ::Environments::ScheduleToDeleteReviewAppsService.new(user_project, current_user, params).execute
response = {
2021-10-27 15:23:28 +05:30
scheduled_entries: Entities::EnvironmentBasic.represent(result.scheduled_entries),
unprocessable_entries: Entities::EnvironmentBasic.represent(result.unprocessable_entries)
2021-04-17 20:07:23 +05:30
}
if result.success?
status result.status
present response, current_user: current_user
else
render_api_error!(response.merge!(message: result.error_message), result.status)
end
end
2016-09-13 17:45:13 +05:30
desc 'Deletes an existing environment' do
detail 'This feature was introduced in GitLab 8.11.'
success Entities::Environment
end
params do
2019-03-02 22:35:43 +05:30
requires :environment_id, type: Integer, desc: 'The environment ID'
2016-09-13 17:45:13 +05:30
end
delete ':id/environments/:environment_id' do
2020-04-22 19:07:51 +05:30
authorize! :read_environment, user_project
2016-09-13 17:45:13 +05:30
environment = user_project.environments.find(params[:environment_id])
2020-04-22 19:07:51 +05:30
authorize! :destroy_environment, environment
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
destroy_conditionally!(environment)
2017-08-17 22:00:37 +05:30
end
desc 'Stops an existing environment' do
success Entities::Environment
end
params do
2019-03-02 22:35:43 +05:30
requires :environment_id, type: Integer, desc: 'The environment ID'
2017-08-17 22:00:37 +05:30
end
post ':id/environments/:environment_id/stop' do
2018-11-18 11:00:15 +05:30
authorize! :read_environment, user_project
2017-08-17 22:00:37 +05:30
environment = user_project.environments.find(params[:environment_id])
2018-11-18 11:00:15 +05:30
authorize! :stop_environment, environment
2017-08-17 22:00:37 +05:30
2022-06-21 17:19:12 +05:30
environment.stop_with_actions!(current_user)
2017-08-17 22:00:37 +05:30
status 200
2019-03-13 22:55:13 +05:30
present environment, with: Entities::Environment, current_user: current_user
2016-09-13 17:45:13 +05:30
end
2019-07-31 22:56:46 +05:30
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-09-13 17:45:13 +05:30
end
end
end