environments.rb 5.97 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
module API
  # Environments RESTfull API endpoints
5
  class Environments < ::API::Base
6 7
    include PaginationParams

8 9
    before { authenticate! }

10 11
    feature_category :continuous_delivery

12 13 14
    params do
      requires :id, type: String, desc: 'The project ID'
    end
15
    resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
16 17 18 19
      desc 'Get all environments of the project' do
        detail 'This feature was introduced in GitLab 8.11.'
        success Entities::Environment
      end
20
      params do
21
        use :pagination
22 23 24
        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'
25
      end
26 27 28
      get ':id/environments' do
        authorize! :read_environment, user_project

29 30 31
        environments = ::EnvironmentsFinder.new(user_project, current_user, params).find

        present paginate(environments), with: Entities::Environment, current_user: current_user
32 33
      end

34 35 36 37 38 39 40
      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'
Nick Thomas's avatar
Nick Thomas committed
41
        optional :slug, absence: { message: "is automatically generated and cannot be changed" }
42
      end
43 44 45
      post ':id/environments' do
        authorize! :create_environment, user_project

46
        environment = user_project.environments.create(declared_params)
47

48
        if environment.persisted?
49
          present environment, with: Entities::Environment, current_user: current_user
50 51 52 53 54
        else
          render_validation_error!(environment)
        end
      end

55 56 57 58 59 60 61 62
      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'
Nick Thomas's avatar
Nick Thomas committed
63
        optional :slug, absence: { message: "is automatically generated and cannot be changed" }
64 65 66 67 68
      end
      put ':id/environments/:environment_id' do
        authorize! :update_environment, user_project

        environment = user_project.environments.find(params[:environment_id])
69 70

        update_params = declared_params(include_missing: false).extract!(:name, :external_url)
71
        if environment.update(update_params)
72
          present environment, with: Entities::Environment, current_user: current_user
73 74 75 76
        else
          render_validation_error!(environment)
        end
      end
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      desc "Delete multiple stopped review apps" do
        detail "Remove multiple stopped review environments older than a specific age"
        success Entities::Environment
      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 = {
          scheduled_entries: Entities::Environment.represent(result.scheduled_entries),
          unprocessable_entries: Entities::Environment.represent(result.unprocessable_entries)
        }

        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

105 106 107 108 109
      desc 'Deletes an existing environment' do
        detail 'This feature was introduced in GitLab 8.11.'
        success Entities::Environment
      end
      params do
110
        requires :environment_id, type: Integer, desc: 'The environment ID'
111 112
      end
      delete ':id/environments/:environment_id' do
113
        authorize! :read_environment, user_project
114 115

        environment = user_project.environments.find(params[:environment_id])
116
        authorize! :destroy_environment, environment
117

118
        destroy_conditionally!(environment)
119
      end
120 121 122 123 124

      desc 'Stops an existing environment' do
        success Entities::Environment
      end
      params do
125
        requires :environment_id, type: Integer, desc: 'The environment ID'
126 127
      end
      post ':id/environments/:environment_id/stop' do
128
        authorize! :read_environment, user_project
129 130

        environment = user_project.environments.find(params[:environment_id])
131
        authorize! :stop_environment, environment
132 133 134 135

        environment.stop_with_action!(current_user)

        status 200
136
        present environment, with: Entities::Environment, current_user: current_user
137
      end
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

      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
153 154 155
    end
  end
end