Commit 3f85c3ef authored by Kamil Trzcinski's avatar Kamil Trzcinski

Initial support for closing environments

parent 2c01b19f
......@@ -110,6 +110,10 @@ module Ci
project.builds_enabled? && commands.present? && manual? && skipped?
end
def closes_environment?(name)
environment == name && options.fetch(:environment, {}).fetch(:close, false)
end
def play(current_user = nil)
# Try to queue a current build
if self.enqueue
......
......@@ -77,6 +77,19 @@ class Deployment < ActiveRecord::Base
take
end
def close_action
return nil unless manual_actions
@close_action ||=
manual_actions.find do |manual_action|
manual_action.try(:closes_environment?, deployable.environment)
end
end
def closeable?
close_action.present?
end
private
def ref_path
......
......@@ -19,6 +19,24 @@ class Environment < ActiveRecord::Base
allow_nil: true,
addressable_url: true
delegate :closeable?, :close_action, to: :last_deployment, allow_nil: true
scope :opened, -> { where(state: [:opened]) }
scope :closed, -> { where(state: [:closed]) }
state_machine :state, initial: :opened do
event :close do
transition opened: :closed
end
event :reopen do
transition closed: :opened
end
state :opened
state :closed
end
def last_deployment
deployments.last
end
......
......@@ -4,6 +4,13 @@ class CreateDeploymentService < BaseService
def execute(deployable = nil)
environment = find_or_create_environment
if close?
environment.close
return
end
environment.reopen
deployment = project.deployments.create(
environment: environment,
ref: params[:ref],
......@@ -14,7 +21,6 @@ class CreateDeploymentService < BaseService
)
deployment.update_merge_request_metrics!
deployment
end
......@@ -44,6 +50,10 @@ class CreateDeploymentService < BaseService
options[:url]
end
def close?
options[:close]
end
def options
params[:options] || {}
end
......
class AddStateToEnvironment < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
add_column :environments, :state, :string
end
end
......@@ -8,7 +8,7 @@ module Gitlab
class Environment < Entry
include Validatable
ALLOWED_KEYS = %i[name url]
ALLOWED_KEYS = %i[name url close]
validations do
validate do
......@@ -35,6 +35,8 @@ module Gitlab
length: { maximum: 255 },
addressable_url: true,
allow_nil: true
validates :close, boolean: true, allow_nil: true
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment