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

Initial support for closing environments

parent 2c01b19f
...@@ -110,6 +110,10 @@ module Ci ...@@ -110,6 +110,10 @@ module Ci
project.builds_enabled? && commands.present? && manual? && skipped? project.builds_enabled? && commands.present? && manual? && skipped?
end end
def closes_environment?(name)
environment == name && options.fetch(:environment, {}).fetch(:close, false)
end
def play(current_user = nil) def play(current_user = nil)
# Try to queue a current build # Try to queue a current build
if self.enqueue if self.enqueue
......
...@@ -77,6 +77,19 @@ class Deployment < ActiveRecord::Base ...@@ -77,6 +77,19 @@ class Deployment < ActiveRecord::Base
take take
end 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 private
def ref_path def ref_path
......
...@@ -19,6 +19,24 @@ class Environment < ActiveRecord::Base ...@@ -19,6 +19,24 @@ class Environment < ActiveRecord::Base
allow_nil: true, allow_nil: true,
addressable_url: 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 def last_deployment
deployments.last deployments.last
end end
......
...@@ -4,6 +4,13 @@ class CreateDeploymentService < BaseService ...@@ -4,6 +4,13 @@ class CreateDeploymentService < BaseService
def execute(deployable = nil) def execute(deployable = nil)
environment = find_or_create_environment environment = find_or_create_environment
if close?
environment.close
return
end
environment.reopen
deployment = project.deployments.create( deployment = project.deployments.create(
environment: environment, environment: environment,
ref: params[:ref], ref: params[:ref],
...@@ -14,7 +21,6 @@ class CreateDeploymentService < BaseService ...@@ -14,7 +21,6 @@ class CreateDeploymentService < BaseService
) )
deployment.update_merge_request_metrics! deployment.update_merge_request_metrics!
deployment deployment
end end
...@@ -44,6 +50,10 @@ class CreateDeploymentService < BaseService ...@@ -44,6 +50,10 @@ class CreateDeploymentService < BaseService
options[:url] options[:url]
end end
def close?
options[:close]
end
def options def options
params[:options] || {} params[:options] || {}
end 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 ...@@ -8,7 +8,7 @@ module Gitlab
class Environment < Entry class Environment < Entry
include Validatable include Validatable
ALLOWED_KEYS = %i[name url] ALLOWED_KEYS = %i[name url close]
validations do validations do
validate do validate do
...@@ -35,6 +35,8 @@ module Gitlab ...@@ -35,6 +35,8 @@ module Gitlab
length: { maximum: 255 }, length: { maximum: 255 },
addressable_url: true, addressable_url: true,
allow_nil: true allow_nil: true
validates :close, boolean: true, allow_nil: true
end end
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