2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Ci
|
2019-07-07 11:18:12 +05:30
|
|
|
class Stage < ApplicationRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
extend Gitlab::Ci::Model
|
|
|
|
include Importable
|
2020-07-28 23:09:34 +05:30
|
|
|
include Ci::HasStatus
|
2018-03-17 18:26:18 +05:30
|
|
|
include Gitlab::OptimisticLocking
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
enum status: Ci::HasStatus::STATUSES_ENUM
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
belongs_to :project
|
|
|
|
belongs_to :pipeline
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
has_many :statuses, class_name: 'CommitStatus', foreign_key: :stage_id
|
2020-05-24 23:13:21 +05:30
|
|
|
has_many :latest_statuses, -> { ordered.latest }, class_name: 'CommitStatus', foreign_key: :stage_id
|
2020-03-13 15:44:24 +05:30
|
|
|
has_many :processables, class_name: 'Ci::Processable', foreign_key: :stage_id
|
2018-03-17 18:26:18 +05:30
|
|
|
has_many :builds, foreign_key: :stage_id
|
2019-03-02 22:35:43 +05:30
|
|
|
has_many :bridges, foreign_key: :stage_id
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
scope :ordered, -> { order(position: :asc) }
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
with_options unless: :importing? do
|
|
|
|
validates :project, presence: true
|
|
|
|
validates :pipeline, presence: true
|
|
|
|
validates :name, presence: true
|
|
|
|
validates :position, presence: true
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
after_initialize do
|
2018-03-17 18:26:18 +05:30
|
|
|
self.status = DEFAULT_STATUS if self.status.nil?
|
|
|
|
end
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
before_validation unless: :importing? do
|
|
|
|
next if position.present?
|
|
|
|
|
|
|
|
self.position = statuses.select(:stage_idx)
|
|
|
|
.where('stage_idx IS NOT NULL')
|
|
|
|
.group(:stage_idx)
|
|
|
|
.order('COUNT(*) DESC')
|
|
|
|
.first&.stage_idx.to_i
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
state_machine :status, initial: :created do
|
|
|
|
event :enqueue do
|
2020-05-24 23:13:21 +05:30
|
|
|
transition any - [:pending] => :pending
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
event :request_resource do
|
|
|
|
transition any - [:waiting_for_resource] => :waiting_for_resource
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
event :prepare do
|
|
|
|
transition any - [:preparing] => :preparing
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
event :run do
|
|
|
|
transition any - [:running] => :running
|
|
|
|
end
|
|
|
|
|
|
|
|
event :skip do
|
|
|
|
transition any - [:skipped] => :skipped
|
|
|
|
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
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
event :delay do
|
|
|
|
transition any - [:scheduled] => :scheduled
|
|
|
|
end
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def set_status(new_status)
|
2021-04-17 20:07:23 +05:30
|
|
|
retry_optimistic_lock(self, name: 'ci_stage_set_status') do
|
2019-12-21 20:55:43 +05:30
|
|
|
case new_status
|
2018-11-08 19:23:39 +05:30
|
|
|
when 'created' then nil
|
2020-03-13 15:44:24 +05:30
|
|
|
when 'waiting_for_resource' then request_resource
|
2019-07-07 11:18:12 +05:30
|
|
|
when 'preparing' then prepare
|
2018-03-17 18:26:18 +05:30
|
|
|
when 'pending' then enqueue
|
|
|
|
when 'running' then run
|
|
|
|
when 'success' then succeed
|
|
|
|
when 'failed' then drop
|
|
|
|
when 'canceled' then cancel
|
|
|
|
when 'manual' then block
|
2018-12-05 23:21:45 +05:30
|
|
|
when 'scheduled' then delay
|
2018-11-08 19:23:39 +05:30
|
|
|
when 'skipped', nil then skip
|
|
|
|
else
|
2020-07-28 23:09:34 +05:30
|
|
|
raise Ci::HasStatus::UnknownStatusError,
|
2019-12-21 20:55:43 +05:30
|
|
|
"Unknown status `#{new_status}`"
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def update_legacy_status
|
|
|
|
set_status(latest_stage_status.to_s)
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def groups
|
2020-04-22 19:07:51 +05:30
|
|
|
@groups ||= Ci::Group.fabricate(project, self)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def has_warnings?
|
2020-10-24 23:57:45 +05:30
|
|
|
number_of_warnings > 0
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def number_of_warnings
|
|
|
|
BatchLoader.for(id).batch(default_value: 0) do |stage_ids, loader|
|
2021-03-11 19:13:27 +05:30
|
|
|
::CommitStatus.where(stage_id: stage_ids)
|
2018-11-08 19:23:39 +05:30
|
|
|
.latest
|
|
|
|
.failed_but_allowed
|
|
|
|
.group(:stage_id)
|
|
|
|
.count
|
|
|
|
.each { |id, amount| loader.call(id, amount) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def detailed_status(current_user)
|
|
|
|
Gitlab::Ci::Status::Stage::Factory
|
|
|
|
.new(self, current_user)
|
|
|
|
.fabricate!
|
|
|
|
end
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
def manual_playable?
|
|
|
|
blocked? || skipped?
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
|
|
|
def latest_stage_status
|
2021-04-17 20:07:23 +05:30
|
|
|
statuses.latest.composite_status(project: project) || 'skipped'
|
2019-12-21 20:55:43 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|