Commit e1f05b93 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Use explicit events to transition between states

parent 8acbc9e0
......@@ -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'
......
......@@ -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|
......
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