summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2016-08-12 11:36:51 +0200
committerKamil Trzcinski <ayufan@ayufan.eu>2016-08-12 11:36:51 +0200
commite1f05b932de5553462793fb88fdea2ca54072d40 (patch)
treef4279bb6e81e7738ae2698a8529433cf26b5dd2a
parent8acbc9e0853c1af6e772bb00d121dd450830d1c1 (diff)
downloadgitlab-ce-e1f05b932de5553462793fb88fdea2ca54072d40.tar.gz
Use explicit events to transition between states
-rw-r--r--app/models/ci/pipeline.rb44
-rw-r--r--app/models/commit_status.rb2
2 files changed, 33 insertions, 13 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index 7a0430f277a..6aef91804a2 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -20,6 +20,14 @@ module Ci
after_save :keep_around_commits
state_machine :status, initial: :created do
+ event :queue do
+ transition :created => :pending
+ end
+
+ event :run do
+ transition [:pending, :success, :failed, :canceled, :skipped] => :running
+ end
+
event :skip do
transition any => :skipped
end
@@ -28,13 +36,12 @@ module Ci
transition any => :failed
end
- event :update_status do
- transition any => :pending, if: ->(pipeline) { pipeline.can_transition_to?('pending') }
- transition any => :running, if: ->(pipeline) { pipeline.can_transition_to?('running') }
- transition any => :failed, if: ->(pipeline) { pipeline.can_transition_to?('failed') }
- transition any => :success, if: ->(pipeline) { pipeline.can_transition_to?('success') }
- transition any => :canceled, if: ->(pipeline) { pipeline.can_transition_to?('canceled') }
- transition any => :skipped, if: ->(pipeline) { pipeline.can_transition_to?('skipped') }
+ event :succeed do
+ transition any => :success
+ end
+
+ event :cancel do
+ transition any => :canceled
end
after_transition [:created, :pending] => :running do |pipeline|
@@ -214,23 +221,36 @@ module Ci
Ci::ProcessPipelineService.new(project, user).execute(self)
end
+ def build_updated
+ case latest_builds_status
+ when 'pending'
+ queue
+ when 'running'
+ run
+ when 'success'
+ succeed
+ when 'failed'
+ drop
+ when 'canceled'
+ cancel
+ when 'skipped'
+ skip
+ end
+ end
+
def predefined_variables
[
{ key: 'CI_PIPELINE_ID', value: id.to_s, public: true }
]
end
- def can_transition_to?(expected_status)
- latest_status == expected_status
- end
-
def update_duration
update(duration: statuses.latest.duration)
end
private
- def latest_status
+ def latest_builds_status
return 'failed' unless yaml_errors.blank?
statuses.latest.status || 'skipped'
diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb
index 522fa5d6911..c21c8ce18db 100644
--- a/app/models/commit_status.rb
+++ b/app/models/commit_status.rb
@@ -70,7 +70,7 @@ class CommitStatus < ActiveRecord::Base
end
after_transition do |commit_status, transition|
- commit_status.pipeline.try(:update_status) unless transition.loopback?
+ commit_status.pipeline.try(:build_updated) unless transition.loopback?
end
after_transition [:created, :pending, :running] => :success do |commit_status|