summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2016-10-19 07:53:05 +0000
committerRémy Coutable <remy@rymai.me>2016-10-19 07:53:05 +0000
commitf0c7e6713f2778a2b52ab8091c398a96982380de (patch)
tree0de29d6f6a0ce06832dd183a357c8c1519dda8cb /app/models
parentf64e36c44832db125beab5923c0177ff69ccedba (diff)
parent19300a1a3dff8974a3e653d71dcc9d528436efd8 (diff)
downloadgitlab-ce-f0c7e6713f2778a2b52ab8091c398a96982380de.tar.gz
Merge branch '22191-delete-dynamic-envs-mr' into 'master'
Delete dynamic environments - Adds "close environment" action to a merge request - Adds tabs to environments list - Adds close button to each environment row in environments list - Replaces Destroy button with Close button inside an environment - Adds close button to builds list inside an environment #### Configuration In order to enable stopping environments a valid `.gitlab-ci.yml` syntax has to be used: ``` review: environment: name: review/$app on_stop: stop_review stop_review: script: echo Delete My App when: manual environment: name: review/$app action: stop ``` This MR requires that `stop_review` has to have: `when`, `environment:name` and `environment:action` defined. The next MR after this one will verify that and enforce that these settings are configured. It will also implicitly configure these settings, making it possible to define it like this: ``` review: environment: name: review/$app on_stop: stop_review stop_review: script: echo Delete My App ``` Closes #22191 See merge request !6669
Diffstat (limited to 'app/models')
-rw-r--r--app/models/deployment.rb13
-rw-r--r--app/models/environment.rb28
-rw-r--r--app/models/project.rb2
3 files changed, 41 insertions, 2 deletions
diff --git a/app/models/deployment.rb b/app/models/deployment.rb
index 3d9902d496e..1f8c5fb3d85 100644
--- a/app/models/deployment.rb
+++ b/app/models/deployment.rb
@@ -34,7 +34,7 @@ class Deployment < ActiveRecord::Base
end
def manual_actions
- deployable.try(:other_actions)
+ @manual_actions ||= deployable.try(:other_actions)
end
def includes_commit?(commit)
@@ -84,6 +84,17 @@ class Deployment < ActiveRecord::Base
take
end
+ def stop_action
+ return nil unless on_stop.present?
+ return nil unless manual_actions
+
+ @stop_action ||= manual_actions.find_by(name: on_stop)
+ end
+
+ def stoppable?
+ stop_action.present?
+ end
+
def formatted_deployment_time
created_at.to_time.in_time_zone.to_s(:medium)
end
diff --git a/app/models/environment.rb b/app/models/environment.rb
index d970bc0a005..d575f1dc73a 100644
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -19,6 +19,24 @@ class Environment < ActiveRecord::Base
allow_nil: true,
addressable_url: true
+ delegate :stop_action, to: :last_deployment, allow_nil: true
+
+ scope :available, -> { with_state(:available) }
+ scope :stopped, -> { with_state(:stopped) }
+
+ state_machine :state, initial: :available do
+ event :start do
+ transition stopped: :available
+ end
+
+ event :stop do
+ transition available: :stopped
+ end
+
+ state :available
+ state :stopped
+ end
+
def last_deployment
deployments.last
end
@@ -66,4 +84,14 @@ class Environment < ActiveRecord::Base
external_url.gsub(/\A.*?:\/\//, '')
end
+
+ def stoppable?
+ available? && stop_action.present?
+ end
+
+ def stop!(current_user)
+ return unless stoppable?
+
+ stop_action.play(current_user)
+ end
end
diff --git a/app/models/project.rb b/app/models/project.rb
index aee74c3dba1..db7301219e5 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -1293,7 +1293,7 @@ class Project < ActiveRecord::Base
environment_ids.where(ref: ref)
end
- environments.where(id: environment_ids).select do |environment|
+ environments.available.where(id: environment_ids).select do |environment|
environment.includes_commit?(commit)
end
end