2020-04-08 14:13:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
2021-10-27 15:23:28 +05:30
|
|
|
class Ref < Ci::ApplicationRecord
|
2020-10-24 23:57:45 +05:30
|
|
|
include AfterCommitQueue
|
2020-06-23 00:09:42 +05:30
|
|
|
include Gitlab::OptimisticLocking
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
FAILING_STATUSES = %w[failed broken still_failing].freeze
|
|
|
|
|
|
|
|
belongs_to :project, inverse_of: :ci_refs
|
|
|
|
has_many :pipelines, class_name: 'Ci::Pipeline', foreign_key: :ci_ref_id, inverse_of: :ci_ref
|
|
|
|
|
|
|
|
state_machine :status, initial: :unknown do
|
|
|
|
event :succeed do
|
|
|
|
transition unknown: :success
|
|
|
|
transition fixed: :success
|
|
|
|
transition %i[failed broken still_failing] => :fixed
|
2020-10-24 23:57:45 +05:30
|
|
|
transition success: same
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
event :do_fail do
|
|
|
|
transition unknown: :failed
|
|
|
|
transition %i[failed broken] => :still_failing
|
|
|
|
transition %i[success fixed] => :broken
|
|
|
|
end
|
|
|
|
|
|
|
|
state :unknown, value: 0
|
|
|
|
state :success, value: 1
|
|
|
|
state :failed, value: 2
|
|
|
|
state :fixed, value: 3
|
|
|
|
state :broken, value: 4
|
|
|
|
state :still_failing, value: 5
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
after_transition any => [:fixed, :success] do |ci_ref|
|
2021-03-08 18:12:59 +05:30
|
|
|
# Do not try to unlock if no artifacts are locked
|
|
|
|
next unless ci_ref.artifacts_locked?
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
ci_ref.run_after_commit do
|
|
|
|
Ci::PipelineSuccessUnlockArtifactsWorker.perform_async(ci_ref.last_finished_pipeline_id)
|
|
|
|
end
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def ensure_for(pipeline)
|
2023-06-20 00:43:36 +05:30
|
|
|
safe_find_or_create_by(project_id: pipeline.project_id, ref_path: pipeline.source_ref_path)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def failing_state?(status_name)
|
|
|
|
FAILING_STATUSES.include?(status_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_finished_pipeline_id
|
2020-07-28 23:09:34 +05:30
|
|
|
Ci::Pipeline.last_finished_for_ref_id(self.id)&.id
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def artifacts_locked?
|
|
|
|
self.pipelines.where(locked: :artifacts_locked).exists?
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def update_status_by!(pipeline)
|
2021-04-17 20:07:23 +05:30
|
|
|
retry_lock(self, name: 'ci_ref_update_status_by') do
|
2020-06-23 00:09:42 +05:30
|
|
|
next unless last_finished_pipeline_id == pipeline.id
|
|
|
|
|
|
|
|
case pipeline.status
|
|
|
|
when 'success' then self.succeed
|
|
|
|
when 'failed' then self.do_fail
|
|
|
|
end
|
|
|
|
|
|
|
|
self.status_name
|
|
|
|
end
|
|
|
|
end
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
end
|