Commit ae24b257 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Simplify state update of Ci::Commit object

parent 3d38e461
...@@ -29,16 +29,8 @@ module Ci ...@@ -29,16 +29,8 @@ module Ci
validates_presence_of :sha validates_presence_of :sha
validate :valid_commit_sha validate :valid_commit_sha
# Make sure that status is saved
before_save :status
before_save :started_at
before_save :finished_at
before_save :duration
# Invalidate object and save if when touched # Invalidate object and save if when touched
after_touch :reload after_touch :update_state
after_touch :invalidate
after_touch :save
def self.truncate_sha(sha) def self.truncate_sha(sha)
sha[0...8] sha[0...8]
...@@ -94,13 +86,6 @@ module Ci ...@@ -94,13 +86,6 @@ module Ci
end end
end end
def invalidate
write_attribute(:status, nil)
write_attribute(:started_at, nil)
write_attribute(:finished_at, nil)
write_attribute(:duration, nil)
end
def create_builds(user, trigger_request = nil) def create_builds(user, trigger_request = nil)
return unless config_processor return unless config_processor
config_processor.stages.any? do |stage| config_processor.stages.any? do |stage|
...@@ -137,22 +122,6 @@ module Ci ...@@ -137,22 +122,6 @@ module Ci
@retried ||= (statuses.order(id: :desc) - statuses.latest) @retried ||= (statuses.order(id: :desc) - statuses.latest)
end end
def status
read_attribute(:status) || update_status
end
def duration
read_attribute(:duration) || update_duration
end
def started_at
read_attribute(:started_at) || update_started_at
end
def finished_at
read_attribute(:finished_at) || update_finished_at
end
def coverage def coverage
coverage_array = latest.map(&:coverage).compact coverage_array = latest.map(&:coverage).compact
if coverage_array.size >= 1 if coverage_array.size >= 1
...@@ -188,45 +157,26 @@ module Ci ...@@ -188,45 +157,26 @@ module Ci
private private
def update_status def update_state
self.status = reload
if yaml_errors.present? self.status = if yaml_errors.present?
'failed' 'failed'
else else
latest.status || 'skipped' latest.status
end end
end self.started_at = statuses.minimum(:started_at)
self.finished_at = statuses.maximum(:finished_at)
def update_started_at
self.started_at =
statuses.minimum(:started_at)
end
def update_finished_at
self.finished_at =
statuses.maximum(:finished_at)
end
def update_duration
self.duration = begin self.duration = begin
duration_array = latest.map(&:duration).compact duration_array = latest.map(&:duration).compact
duration_array.reduce(:+).to_i duration_array.reduce(:+).to_i
end end
end
def update_statuses
update_status
update_started_at
update_finished_at
update_duration
save save
end end
def save_yaml_error(error) def save_yaml_error(error)
return if self.yaml_errors? return if self.yaml_errors?
self.yaml_errors = error self.yaml_errors = error
update_status update_state
save
end end
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