debian-mirror-gitlab/app/models/concerns/ci/has_status.rb

130 lines
4.4 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module Ci
module HasStatus
extend ActiveSupport::Concern
DEFAULT_STATUS = 'created'
BLOCKED_STATUS = %w[manual scheduled].freeze
AVAILABLE_STATUSES = %w[created waiting_for_resource preparing pending running success failed canceled skipped manual scheduled].freeze
2022-07-16 23:28:13 +05:30
STARTED_STATUSES = %w[running success failed].freeze
2020-07-28 23:09:34 +05:30
ACTIVE_STATUSES = %w[waiting_for_resource preparing pending running].freeze
COMPLETED_STATUSES = %w[success failed canceled skipped].freeze
2022-07-16 23:28:13 +05:30
STOPPED_STATUSES = COMPLETED_STATUSES + BLOCKED_STATUS
2020-07-28 23:09:34 +05:30
ORDERED_STATUSES = %w[failed preparing pending running waiting_for_resource manual scheduled canceled success skipped created].freeze
PASSED_WITH_WARNINGS_STATUSES = %w[failed canceled].to_set.freeze
EXCLUDE_IGNORED_STATUSES = %w[manual failed canceled].to_set.freeze
2022-07-16 23:28:13 +05:30
ALIVE_STATUSES = (ACTIVE_STATUSES + ['created']).freeze
CANCELABLE_STATUSES = (ALIVE_STATUSES + ['scheduled']).freeze
2020-07-28 23:09:34 +05:30
STATUSES_ENUM = { created: 0, pending: 1, running: 2, success: 3,
2022-08-27 11:52:29 +05:30
failed: 4, canceled: 5, skipped: 6, manual: 7,
scheduled: 8, preparing: 9, waiting_for_resource: 10 }.freeze
2020-07-28 23:09:34 +05:30
UnknownStatusError = Class.new(StandardError)
class_methods do
2022-08-13 15:12:31 +05:30
def composite_status
2020-10-24 23:57:45 +05:30
Gitlab::Ci::Status::Composite
2022-08-13 15:12:31 +05:30
.new(all, with_allow_failure: columns_hash.key?('allow_failure'))
2020-10-24 23:57:45 +05:30
.status
2020-07-28 23:09:34 +05:30
end
def started_at
all.minimum(:started_at)
end
def finished_at
all.maximum(:finished_at)
end
def all_state_names
state_machines.values.flat_map(&:states).flat_map { |s| s.map(&:name) }
end
def completed_statuses
COMPLETED_STATUSES.map(&:to_sym)
end
2022-07-16 23:28:13 +05:30
def stopped_statuses
STOPPED_STATUSES.map(&:to_sym)
end
2020-07-28 23:09:34 +05:30
end
included do
validates :status, inclusion: { in: AVAILABLE_STATUSES }
state_machine :status, initial: :created do
state :created, value: 'created'
state :waiting_for_resource, value: 'waiting_for_resource'
state :preparing, value: 'preparing'
state :pending, value: 'pending'
state :running, value: 'running'
state :failed, value: 'failed'
state :success, value: 'success'
state :canceled, value: 'canceled'
state :skipped, value: 'skipped'
state :manual, value: 'manual'
state :scheduled, value: 'scheduled'
end
scope :created, -> { with_status(:created) }
scope :waiting_for_resource, -> { with_status(:waiting_for_resource) }
scope :preparing, -> { with_status(:preparing) }
scope :relevant, -> { without_status(:created) }
scope :running, -> { with_status(:running) }
scope :pending, -> { with_status(:pending) }
scope :success, -> { with_status(:success) }
scope :failed, -> { with_status(:failed) }
scope :canceled, -> { with_status(:canceled) }
scope :skipped, -> { with_status(:skipped) }
scope :manual, -> { with_status(:manual) }
scope :scheduled, -> { with_status(:scheduled) }
2022-07-16 23:28:13 +05:30
scope :alive, -> { with_status(*ALIVE_STATUSES) }
scope :alive_or_scheduled, -> { with_status(*klass::CANCELABLE_STATUSES) }
2020-07-28 23:09:34 +05:30
scope :created_or_pending, -> { with_status(:created, :pending) }
scope :running_or_pending, -> { with_status(:running, :pending) }
scope :finished, -> { with_status(:success, :failed, :canceled) }
scope :failed_or_canceled, -> { with_status(:failed, :canceled) }
2021-10-27 15:23:28 +05:30
scope :complete, -> { with_status(completed_statuses) }
2020-07-28 23:09:34 +05:30
scope :incomplete, -> { without_statuses(completed_statuses) }
2021-11-18 22:05:49 +05:30
scope :waiting_for_resource_or_upcoming, -> { with_status(:created, :scheduled, :waiting_for_resource) }
2020-07-28 23:09:34 +05:30
scope :cancelable, -> do
2022-05-07 20:08:51 +05:30
where(status: klass::CANCELABLE_STATUSES)
2020-07-28 23:09:34 +05:30
end
scope :without_statuses, -> (names) do
with_status(all_state_names - names.to_a)
end
end
def started?
2022-07-16 23:28:13 +05:30
STARTED_STATUSES.include?(status) && !!started_at
2020-07-28 23:09:34 +05:30
end
def active?
ACTIVE_STATUSES.include?(status)
end
def complete?
COMPLETED_STATUSES.include?(status)
end
2023-01-13 00:05:48 +05:30
def incomplete?
COMPLETED_STATUSES.exclude?(status)
end
2020-07-28 23:09:34 +05:30
def blocked?
BLOCKED_STATUS.include?(status)
end
private
2021-06-08 01:23:25 +05:30
def calculate_duration(start_time, end_time)
return unless start_time
(end_time || Time.current) - start_time
2020-07-28 23:09:34 +05:30
end
end
end