summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinya Maeda <shinya@gitlab.com>2018-10-17 16:02:24 +0900
committerShinya Maeda <shinya@gitlab.com>2018-10-17 16:02:24 +0900
commit5bb89233b8826ea2acfecd7b05db8fde62e3f588 (patch)
treeb839fd8e27826fa74052073ce556d938329b7f11
parent441d0f77d95376970566762fa154f57d699dfb40 (diff)
parent9dd4492a77d676abea4c461237939843e6cc2b4a (diff)
downloadgitlab-ce-5bb89233b8826ea2acfecd7b05db8fde62e3f588.tar.gz
Merge branch 'stateful_deployments' into deployment-status-presenter
-rw-r--r--app/models/ci/build.rb2
-rw-r--r--app/models/deployment.rb44
-rw-r--r--app/models/environment.rb2
3 files changed, 17 insertions, 31 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index cdfe8175a42..1b2d965e268 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -21,7 +21,7 @@ module Ci
upload_multiple_artifacts: -> (build) { build.publishes_artifacts_reports? }
}.freeze
- has_one :last_deployment, -> { order('deployments.id DESC') }, as: :deployable, class_name: 'Deployment'
+ has_one :last_deployment, -> { success.order('deployments.id DESC') }, as: :deployable, class_name: 'Deployment'
has_many :trace_sections, class_name: 'Ci::BuildTraceSection'
has_many :trace_chunks, class_name: 'Ci::BuildTraceChunk', foreign_key: :build_id
diff --git a/app/models/deployment.rb b/app/models/deployment.rb
index 60abc15c056..98d1b13d829 100644
--- a/app/models/deployment.rb
+++ b/app/models/deployment.rb
@@ -23,63 +23,49 @@ class Deployment < ActiveRecord::Base
scope :for_environment, -> (environment) { where(environment_id: environment) }
- enum status: HasStatus::STATUSES_ENUM
+ enum status: {
+ created: 0,
+ running: 1,
+ success: 2,
+ failed: 3,
+ canceled: 4
+ }
- state_machine :status, initial: :created do
- event :enqueue do
- transition created: :pending
- transition [:success, :failed, :canceled, :skipped] => :running
- end
+ # Override enum's method to support legacy deployment records that do not have `status` value
+ scope :success, -> () { where('status = (?) OR status IS NULL', statuses[:success]) }
+ state_machine :status, initial: :created do
event :run do
transition any - [:running] => :running
end
- event :skip do
- transition any - [:skipped] => :skipped
+ event :succeed do
+ transition any - [:success] => :success
end
event :drop do
transition any - [:failed] => :failed
end
- event :succeed do
- transition any - [:success] => :success
- end
-
event :cancel do
transition any - [:canceled] => :canceled
end
-
- event :block do
- transition any - [:manual] => :manual
- end
-
- event :delay do
- transition any - [:scheduled] => :scheduled
- end
end
def update_status
retry_optimistic_lock(self) do
case deployable.try(:status)
- when 'created' then nil
- when 'pending' then enqueue
when 'running' then run
when 'success' then succeed
when 'failed' then drop
- when 'canceled' then cancel
- when 'manual' then block
- when 'scheduled' then delay
- when 'skipped', nil then skip
+ when 'skipped', 'canceled' then cancel
else
- raise HasStatus::UnknownStatusError,
- "Unknown status `#{statuses.latest.status}`"
+ # no-op
end
end
end
- # To set legacy deployment status to :success
+ # Override enum's method to support legacy deployment records that do not have `status` value
def success?
return true if status.nil?
diff --git a/app/models/environment.rb b/app/models/environment.rb
index 2cca4a8efad..c584e4e563b 100644
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -10,7 +10,7 @@ class Environment < ActiveRecord::Base
has_many :deployments, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
- has_one :last_deployment, -> { order('deployments.id DESC') }, class_name: 'Deployment'
+ has_one :last_deployment, -> { success.order('deployments.id DESC') }, class_name: 'Deployment'
has_one :last_successful_deployment, -> { success.order('deployments.id DESC') }, class_name: 'Deployment'
before_validation :nullify_external_url