2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class CommitStatus < ApplicationRecord
|
2020-06-22 11:09:27 -04:00
|
|
|
include Ci::HasStatus
|
2016-06-13 07:34:36 -04:00
|
|
|
include Importable
|
2016-10-07 08:52:30 -04:00
|
|
|
include AfterCommitQueue
|
2018-05-15 14:03:09 -04:00
|
|
|
include Presentable
|
2018-05-31 14:32:36 -04:00
|
|
|
include EnumWithNil
|
2020-07-15 08:09:26 -04:00
|
|
|
include BulkInsertableAssociations
|
2016-03-31 13:51:28 -04:00
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
self.table_name = 'ci_builds'
|
|
|
|
|
2017-06-02 06:16:11 -04:00
|
|
|
belongs_to :user
|
2017-03-17 19:06:11 -04:00
|
|
|
belongs_to :project
|
2016-08-11 09:22:35 -04:00
|
|
|
belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
|
2017-04-06 09:32:56 -04:00
|
|
|
belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2020-05-21 02:08:25 -04:00
|
|
|
has_many :needs, class_name: 'Ci::BuildNeed', foreign_key: :build_id, inverse_of: :build
|
|
|
|
|
|
|
|
enum scheduling_type: { stage: 0, dag: 1 }, _prefix: true
|
|
|
|
|
2016-06-21 08:43:37 -04:00
|
|
|
delegate :commit, to: :pipeline
|
2019-11-20 13:06:04 -05:00
|
|
|
delegate :sha, :short_sha, :before_sha, to: :pipeline
|
2016-06-21 08:43:37 -04:00
|
|
|
|
2016-06-13 07:34:36 -04:00
|
|
|
validates :pipeline, presence: true, unless: :importing?
|
2017-06-09 02:57:20 -04:00
|
|
|
validates :name, presence: true, unless: :importing?
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2015-10-12 06:15:48 -04:00
|
|
|
alias_attribute :author, :user
|
2017-11-24 10:30:08 -05:00
|
|
|
alias_attribute :pipeline_id, :commit_id
|
2017-06-02 06:16:11 -04:00
|
|
|
|
2016-10-03 06:22:51 -04:00
|
|
|
scope :failed_but_allowed, -> do
|
2017-03-06 08:43:32 -05:00
|
|
|
where(allow_failure: true, status: [:failed, :canceled])
|
2016-10-03 06:22:51 -04:00
|
|
|
end
|
2018-09-21 02:42:29 -04:00
|
|
|
|
2020-09-11 08:08:50 -04:00
|
|
|
scope :order_id_desc, -> { order('ci_builds.id DESC') }
|
|
|
|
|
2016-10-03 06:22:51 -04:00
|
|
|
scope :exclude_ignored, -> do
|
2017-03-06 08:43:32 -05:00
|
|
|
# We want to ignore failed but allowed to fail jobs.
|
|
|
|
#
|
|
|
|
# TODO, we also skip ignored optional manual actions.
|
2016-10-04 12:10:23 -04:00
|
|
|
where("allow_failure = ? OR status IN (?)",
|
2017-03-03 08:35:19 -05:00
|
|
|
false, all_state_names - [:failed, :canceled, :manual])
|
2016-10-03 06:22:51 -04:00
|
|
|
end
|
2016-10-03 06:33:43 -04:00
|
|
|
|
2017-05-09 07:14:45 -04:00
|
|
|
scope :latest, -> { where(retried: [false, nil]) }
|
2017-04-16 07:14:39 -04:00
|
|
|
scope :retried, -> { where(retried: true) }
|
2017-02-14 05:38:19 -05:00
|
|
|
scope :ordered, -> { order(:name) }
|
2020-01-17 10:08:37 -05:00
|
|
|
scope :ordered_by_stage, -> { order(stage_idx: :asc) }
|
2016-12-05 08:17:42 -05:00
|
|
|
scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
|
|
|
|
scope :retried_ordered, -> { retried.ordered.includes(project: :namespace) }
|
2020-10-13 11:08:53 -04:00
|
|
|
scope :ordered_by_pipeline, -> { order(pipeline_id: :asc) }
|
2019-08-13 09:14:33 -04:00
|
|
|
scope :before_stage, -> (index) { where('stage_idx < ?', index) }
|
|
|
|
scope :for_stage, -> (index) { where(stage_idx: index) }
|
2017-02-14 05:38:19 -05:00
|
|
|
scope :after_stage, -> (index) { where('stage_idx > ?', index) }
|
2019-12-17 19:08:09 -05:00
|
|
|
scope :for_ref, -> (ref) { where(ref: ref) }
|
|
|
|
scope :by_name, -> (name) { where(name: name) }
|
2021-02-11 10:09:11 -05:00
|
|
|
scope :in_pipelines, ->(pipelines) { where(pipeline: pipelines) }
|
2021-03-31 14:09:19 -04:00
|
|
|
scope :eager_load_pipeline, -> { eager_load(:pipeline, project: { namespace: :route }) }
|
2021-04-01 17:09:22 -04:00
|
|
|
scope :with_pipeline, -> { joins(:pipeline) }
|
2019-12-17 19:08:09 -05:00
|
|
|
|
|
|
|
scope :for_project_paths, -> (paths) do
|
|
|
|
where(project: Project.where_full_path_in(Array(paths)))
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
|
2019-10-01 08:05:59 -04:00
|
|
|
scope :with_preloads, -> do
|
|
|
|
preload(:project, :user)
|
|
|
|
end
|
|
|
|
|
2020-01-17 10:08:37 -05:00
|
|
|
scope :with_project_preload, -> do
|
|
|
|
preload(project: :namespace)
|
|
|
|
end
|
|
|
|
|
2020-03-25 05:08:11 -04:00
|
|
|
scope :match_id_and_lock_version, -> (items) do
|
2020-01-17 10:08:37 -05:00
|
|
|
# it expects that items are an array of attributes to match
|
|
|
|
# each hash needs to have `id` and `lock_version`
|
2020-03-25 05:08:11 -04:00
|
|
|
or_conditions = items.inject(none) do |relation, item|
|
|
|
|
match = CommitStatus.default_scoped.where(item.slice(:id, :lock_version))
|
|
|
|
|
2020-01-17 10:08:37 -05:00
|
|
|
relation.or(match)
|
|
|
|
end
|
2020-03-25 05:08:11 -04:00
|
|
|
|
|
|
|
merge(or_conditions)
|
2020-01-17 10:08:37 -05:00
|
|
|
end
|
|
|
|
|
2021-01-11 13:10:43 -05:00
|
|
|
# We use `Enums::Ci::CommitStatus.failure_reasons` here so that EE can more easily
|
2018-11-13 11:29:14 -05:00
|
|
|
# extend this `Hash` with new values.
|
2021-01-11 13:10:43 -05:00
|
|
|
enum_with_nil failure_reason: Enums::Ci::CommitStatus.failure_reasons
|
2017-08-30 14:20:54 -04:00
|
|
|
|
2021-03-09 07:08:52 -05:00
|
|
|
default_value_for :retried, false
|
|
|
|
|
2017-10-06 05:45:23 -04:00
|
|
|
##
|
|
|
|
# We still create some CommitStatuses outside of CreatePipelineService.
|
|
|
|
#
|
|
|
|
# These are pages deployments and external statuses.
|
|
|
|
#
|
2017-10-11 09:32:19 -04:00
|
|
|
before_create unless: :importing? do
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-10-11 09:32:19 -04:00
|
|
|
Ci::EnsureStageService.new(project, user).execute(self) do |stage|
|
|
|
|
self.run_after_commit { StageUpdateWorker.perform_async(stage.id) }
|
2017-10-06 05:45:23 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2017-10-06 05:45:23 -04:00
|
|
|
end
|
|
|
|
|
2020-01-17 10:08:37 -05:00
|
|
|
before_save if: :status_changed?, unless: :importing? do
|
2020-06-02 20:08:38 -04:00
|
|
|
# we mark `processed` as always changed:
|
|
|
|
# another process might change its value and our object
|
|
|
|
# will not be refreshed to pick the change
|
|
|
|
self.processed_will_change!
|
|
|
|
|
2020-08-13 11:10:03 -04:00
|
|
|
if latest?
|
2020-01-17 10:08:37 -05:00
|
|
|
self.processed = false # force refresh of all dependent ones
|
|
|
|
elsif retried?
|
|
|
|
self.processed = true # retried are considered to be already processed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
state_machine :status do
|
2016-08-18 17:36:54 -04:00
|
|
|
event :process do
|
2017-03-03 08:35:19 -05:00
|
|
|
transition [:skipped, :manual] => :created
|
2016-08-18 17:36:54 -04:00
|
|
|
end
|
|
|
|
|
2017-07-24 05:33:01 -04:00
|
|
|
event :enqueue do
|
2019-03-03 18:56:20 -05:00
|
|
|
# A CommitStatus will never have prerequisites, but this event
|
|
|
|
# is shared by Ci::Build, which cannot progress unless prerequisites
|
|
|
|
# are satisfied.
|
2019-12-24 07:08:01 -05:00
|
|
|
transition [:created, :skipped, :manual, :scheduled] => :pending, if: :all_met_to_become_pending?
|
2017-07-24 05:33:01 -04:00
|
|
|
end
|
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
event :run do
|
|
|
|
transition pending: :running
|
|
|
|
end
|
|
|
|
|
2016-08-11 09:22:35 -04:00
|
|
|
event :skip do
|
2019-12-24 07:08:01 -05:00
|
|
|
transition [:created, :waiting_for_resource, :preparing, :pending] => :skipped
|
2016-08-11 09:22:35 -04:00
|
|
|
end
|
|
|
|
|
2015-10-06 06:01:16 -04:00
|
|
|
event :drop do
|
2019-12-24 07:08:01 -05:00
|
|
|
transition [:created, :waiting_for_resource, :preparing, :pending, :running, :scheduled] => :failed
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
event :success do
|
2019-12-24 07:08:01 -05:00
|
|
|
transition [:created, :waiting_for_resource, :preparing, :pending, :running] => :success
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
event :cancel do
|
2019-12-24 07:08:01 -05:00
|
|
|
transition [:created, :waiting_for_resource, :preparing, :pending, :running, :manual, :scheduled] => :canceled
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2019-12-24 07:08:01 -05:00
|
|
|
before_transition [:created, :waiting_for_resource, :preparing, :skipped, :manual, :scheduled] => :pending do |commit_status|
|
2020-05-22 05:08:09 -04:00
|
|
|
commit_status.queued_at = Time.current
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2019-02-26 21:13:06 -05:00
|
|
|
before_transition [:created, :preparing, :pending] => :running do |commit_status|
|
2020-05-22 05:08:09 -04:00
|
|
|
commit_status.started_at = Time.current
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2016-10-26 05:34:40 -04:00
|
|
|
before_transition any => [:success, :failed, :canceled] do |commit_status|
|
2020-05-22 05:08:09 -04:00
|
|
|
commit_status.finished_at = Time.current
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2017-08-30 14:20:54 -04:00
|
|
|
before_transition any => :failed do |commit_status, transition|
|
|
|
|
failure_reason = transition.args.first
|
2018-10-26 04:01:28 -04:00
|
|
|
commit_status.failure_reason = CommitStatus.failure_reasons[failure_reason]
|
2017-08-30 14:20:54 -04:00
|
|
|
end
|
|
|
|
|
2020-12-21 10:10:05 -05:00
|
|
|
before_transition [:skipped, :manual] => :created do |commit_status, transition|
|
|
|
|
transition.args.first.try do |user|
|
|
|
|
commit_status.user = user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-19 08:13:08 -04:00
|
|
|
after_transition do |commit_status, transition|
|
2016-10-12 09:21:11 -04:00
|
|
|
next if transition.loopback?
|
2020-01-17 10:08:37 -05:00
|
|
|
next if commit_status.processed?
|
|
|
|
next unless commit_status.project
|
2016-10-08 14:42:09 -04:00
|
|
|
|
2021-05-25 05:10:54 -04:00
|
|
|
last_arg = transition.args.last
|
|
|
|
transition_options = last_arg.is_a?(Hash) && last_arg.extractable_options? ? last_arg : {}
|
|
|
|
|
2016-10-08 14:25:16 -04:00
|
|
|
commit_status.run_after_commit do
|
2021-05-25 05:10:54 -04:00
|
|
|
PipelineProcessWorker.perform_async(pipeline_id) unless transition_options[:skip_pipeline_processing]
|
2017-11-24 10:30:08 -05:00
|
|
|
ExpireJobCacheWorker.perform_async(id)
|
2016-09-19 09:03:51 -04:00
|
|
|
end
|
2016-09-19 08:13:08 -04:00
|
|
|
end
|
2021-04-15 11:09:11 -04:00
|
|
|
|
|
|
|
after_transition any => :failed do |commit_status|
|
|
|
|
commit_status.run_after_commit do
|
|
|
|
::Gitlab::Ci::Pipeline::Metrics.job_failure_reason_counter.increment(reason: commit_status.failure_reason)
|
|
|
|
end
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
|
|
|
|
2019-08-13 09:14:33 -04:00
|
|
|
def self.names
|
|
|
|
select(:name)
|
|
|
|
end
|
|
|
|
|
2020-01-17 10:08:37 -05:00
|
|
|
def self.update_as_processed!
|
2020-05-27 11:08:11 -04:00
|
|
|
# Marks items as processed
|
|
|
|
# we do not increase `lock_version`, as we are the one
|
|
|
|
# holding given lock_version (Optimisitc Locking)
|
|
|
|
update_all(processed: true)
|
2020-01-17 10:08:37 -05:00
|
|
|
end
|
|
|
|
|
2020-01-29 13:08:47 -05:00
|
|
|
def self.locking_enabled?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-03-22 13:22:34 -04:00
|
|
|
def locking_enabled?
|
2019-04-23 05:30:18 -04:00
|
|
|
will_save_change_to_status?
|
2017-03-22 13:22:34 -04:00
|
|
|
end
|
|
|
|
|
2016-09-07 08:52:13 -04:00
|
|
|
def group_name
|
2021-04-01 20:09:32 -04:00
|
|
|
name.to_s.sub(%r{([\b\s:]+((\[.*\])|(\d+[\s:\/\\]+\d+)))+\s*\z}, '').strip
|
2016-09-07 08:52:13 -04:00
|
|
|
end
|
|
|
|
|
2016-10-03 06:22:51 -04:00
|
|
|
def failed_but_allowed?
|
2016-03-17 10:54:56 -04:00
|
|
|
allow_failure? && (failed? || canceled?)
|
2016-02-19 13:58:43 -05:00
|
|
|
end
|
|
|
|
|
2021-04-26 14:09:45 -04:00
|
|
|
# Time spent running.
|
2016-11-22 00:36:19 -05:00
|
|
|
def duration
|
2021-04-26 14:09:45 -04:00
|
|
|
calculate_duration(started_at, finished_at)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Time spent in the pending state.
|
|
|
|
def queued_duration
|
|
|
|
calculate_duration(queued_at, started_at)
|
2016-11-22 00:36:19 -05:00
|
|
|
end
|
|
|
|
|
2020-01-17 10:08:37 -05:00
|
|
|
def latest?
|
|
|
|
!retried?
|
|
|
|
end
|
|
|
|
|
2016-09-14 09:04:12 -04:00
|
|
|
def playable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-06-04 06:44:00 -04:00
|
|
|
def retryable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-06-12 05:20:19 -04:00
|
|
|
def cancelable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-10-23 06:58:41 -04:00
|
|
|
def archived?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2016-11-22 00:36:19 -05:00
|
|
|
def stuck?
|
|
|
|
false
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
2015-10-12 09:45:46 -04:00
|
|
|
|
2016-11-22 00:36:19 -05:00
|
|
|
def has_trace?
|
2015-10-12 15:12:31 -04:00
|
|
|
false
|
|
|
|
end
|
2016-12-08 03:51:36 -05:00
|
|
|
|
2019-12-24 07:08:01 -05:00
|
|
|
def all_met_to_become_pending?
|
2021-02-01 04:09:28 -05:00
|
|
|
true
|
2019-12-24 07:08:01 -05:00
|
|
|
end
|
|
|
|
|
2017-04-06 09:32:56 -04:00
|
|
|
def auto_canceled?
|
|
|
|
canceled? && auto_canceled_by_id?
|
|
|
|
end
|
|
|
|
|
2016-12-08 11:52:24 -05:00
|
|
|
def detailed_status(current_user)
|
2017-02-22 17:54:59 -05:00
|
|
|
Gitlab::Ci::Status::Factory
|
|
|
|
.new(self, current_user)
|
|
|
|
.fabricate!
|
2016-12-08 03:51:36 -05:00
|
|
|
end
|
2016-12-22 15:31:42 -05:00
|
|
|
|
2016-12-27 10:59:42 -05:00
|
|
|
def sortable_name
|
2017-06-13 03:22:22 -04:00
|
|
|
name.to_s.split(/(\d+)/).map do |v|
|
2016-12-22 15:31:42 -05:00
|
|
|
v =~ /\d+/ ? v.to_i : v
|
|
|
|
end
|
|
|
|
end
|
2020-01-17 10:08:37 -05:00
|
|
|
|
2020-04-23 11:09:55 -04:00
|
|
|
def recoverable?
|
|
|
|
failed? && !unrecoverable_failure?
|
|
|
|
end
|
|
|
|
|
2021-03-09 07:08:52 -05:00
|
|
|
def update_older_statuses_retried!
|
2021-03-24 14:09:31 -04:00
|
|
|
pipeline
|
|
|
|
.statuses
|
2021-03-09 07:08:52 -05:00
|
|
|
.latest
|
|
|
|
.where(name: name)
|
|
|
|
.where.not(id: id)
|
|
|
|
.update_all(retried: true, processed: true)
|
|
|
|
end
|
|
|
|
|
2020-01-17 10:08:37 -05:00
|
|
|
private
|
|
|
|
|
2020-04-23 11:09:55 -04:00
|
|
|
def unrecoverable_failure?
|
|
|
|
script_failure? || missing_dependency_failure? || archived_failure? || scheduler_failure? || data_integrity_failure?
|
|
|
|
end
|
2015-10-06 06:01:16 -04:00
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
CommitStatus.prepend_mod_with('CommitStatus')
|