2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-08-03 06:08:50 +00:00
|
|
|
class CommitStatus < Ci::ApplicationRecord
|
2020-06-22 15:09:27 +00:00
|
|
|
include Ci::HasStatus
|
2016-06-13 11:34:36 +00:00
|
|
|
include Importable
|
2016-10-07 12:52:30 +00:00
|
|
|
include AfterCommitQueue
|
2018-05-15 18:03:09 +00:00
|
|
|
include Presentable
|
2018-05-31 18:32:36 +00:00
|
|
|
include EnumWithNil
|
2020-07-15 12:09:26 +00:00
|
|
|
include BulkInsertableAssociations
|
2021-06-22 12:08:05 +00:00
|
|
|
include TaggableQueries
|
2016-03-31 17:51:28 +00:00
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
self.table_name = 'ci_builds'
|
|
|
|
|
2017-06-02 10:16:11 +00:00
|
|
|
belongs_to :user
|
2017-03-17 23:06:11 +00:00
|
|
|
belongs_to :project
|
2016-08-11 13:22:35 +00:00
|
|
|
belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id
|
2017-04-06 13:32:56 +00:00
|
|
|
belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline'
|
2015-10-06 10:01:16 +00:00
|
|
|
|
2020-05-21 06:08:25 +00: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 12:43:37 +00:00
|
|
|
delegate :commit, to: :pipeline
|
2019-11-20 18:06:04 +00:00
|
|
|
delegate :sha, :short_sha, :before_sha, to: :pipeline
|
2016-06-21 12:43:37 +00:00
|
|
|
|
2016-06-13 11:34:36 +00:00
|
|
|
validates :pipeline, presence: true, unless: :importing?
|
2017-06-09 06:57:20 +00:00
|
|
|
validates :name, presence: true, unless: :importing?
|
2015-10-06 10:01:16 +00:00
|
|
|
|
2015-10-12 10:15:48 +00:00
|
|
|
alias_attribute :author, :user
|
2017-11-24 15:30:08 +00:00
|
|
|
alias_attribute :pipeline_id, :commit_id
|
2017-06-02 10:16:11 +00:00
|
|
|
|
2016-10-03 10:22:51 +00:00
|
|
|
scope :failed_but_allowed, -> do
|
2017-03-06 13:43:32 +00:00
|
|
|
where(allow_failure: true, status: [:failed, :canceled])
|
2016-10-03 10:22:51 +00:00
|
|
|
end
|
2018-09-21 06:42:29 +00:00
|
|
|
|
2022-04-04 15:09:26 +00:00
|
|
|
scope :order_id_desc, -> { order(id: :desc) }
|
2020-09-11 12:08:50 +00:00
|
|
|
|
2016-10-03 10:22:51 +00:00
|
|
|
scope :exclude_ignored, -> do
|
2017-03-06 13:43:32 +00:00
|
|
|
# We want to ignore failed but allowed to fail jobs.
|
|
|
|
#
|
|
|
|
# TODO, we also skip ignored optional manual actions.
|
2016-10-04 16:10:23 +00:00
|
|
|
where("allow_failure = ? OR status IN (?)",
|
2017-03-03 13:35:19 +00:00
|
|
|
false, all_state_names - [:failed, :canceled, :manual])
|
2016-10-03 10:22:51 +00:00
|
|
|
end
|
2016-10-03 10:33:43 +00:00
|
|
|
|
2017-05-09 11:14:45 +00:00
|
|
|
scope :latest, -> { where(retried: [false, nil]) }
|
2017-04-16 11:14:39 +00:00
|
|
|
scope :retried, -> { where(retried: true) }
|
2017-02-14 10:38:19 +00:00
|
|
|
scope :ordered, -> { order(:name) }
|
2020-01-17 15:08:37 +00:00
|
|
|
scope :ordered_by_stage, -> { order(stage_idx: :asc) }
|
2016-12-05 13:17:42 +00:00
|
|
|
scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
|
2021-11-05 18:12:21 +00:00
|
|
|
scope :retried_ordered, -> { retried.order(name: :asc, id: :desc).includes(project: :namespace) }
|
2020-10-13 15:08:53 +00:00
|
|
|
scope :ordered_by_pipeline, -> { order(pipeline_id: :asc) }
|
2019-08-13 13:14:33 +00:00
|
|
|
scope :before_stage, -> (index) { where('stage_idx < ?', index) }
|
|
|
|
scope :for_stage, -> (index) { where(stage_idx: index) }
|
2017-02-14 10:38:19 +00:00
|
|
|
scope :after_stage, -> (index) { where('stage_idx > ?', index) }
|
2021-12-06 18:14:09 +00:00
|
|
|
scope :for_project, -> (project_id) { where(project_id: project_id) }
|
2019-12-18 00:08:09 +00:00
|
|
|
scope :for_ref, -> (ref) { where(ref: ref) }
|
|
|
|
scope :by_name, -> (name) { where(name: name) }
|
2021-02-11 15:09:11 +00:00
|
|
|
scope :in_pipelines, ->(pipelines) { where(pipeline: pipelines) }
|
2021-04-01 21:09:22 +00:00
|
|
|
scope :with_pipeline, -> { joins(:pipeline) }
|
2021-08-28 06:10:17 +00:00
|
|
|
scope :updated_at_before, ->(date) { where('ci_builds.updated_at < ?', date) }
|
2021-08-28 15:11:11 +00:00
|
|
|
scope :created_at_before, ->(date) { where('ci_builds.created_at < ?', date) }
|
2021-10-06 21:12:16 +00:00
|
|
|
scope :scheduled_at_before, ->(date) {
|
|
|
|
where('ci_builds.scheduled_at IS NOT NULL AND ci_builds.scheduled_at < ?', date)
|
|
|
|
}
|
2019-12-18 00:08:09 +00:00
|
|
|
|
2021-08-20 18:12:04 +00:00
|
|
|
# The scope applies `pluck` to split the queries. Use with care.
|
2019-12-18 00:08:09 +00:00
|
|
|
scope :for_project_paths, -> (paths) do
|
2021-08-20 18:12:04 +00:00
|
|
|
# Pluck is used to split this query. Splitting the query is required for database decomposition for `ci_*` tables.
|
|
|
|
# https://docs.gitlab.com/ee/development/database/transaction_guidelines.html#database-decomposition-and-sharding
|
|
|
|
project_ids = Project.where_full_path_in(Array(paths)).pluck(:id)
|
2021-12-06 18:14:09 +00:00
|
|
|
|
|
|
|
for_project(project_ids)
|
2019-12-18 00:08:09 +00:00
|
|
|
end
|
2015-10-06 10:01:16 +00:00
|
|
|
|
2019-10-01 12:05:59 +00:00
|
|
|
scope :with_preloads, -> do
|
|
|
|
preload(:project, :user)
|
|
|
|
end
|
|
|
|
|
2020-01-17 15:08:37 +00:00
|
|
|
scope :with_project_preload, -> do
|
|
|
|
preload(project: :namespace)
|
|
|
|
end
|
|
|
|
|
2020-03-25 09:08:11 +00:00
|
|
|
scope :match_id_and_lock_version, -> (items) do
|
2020-01-17 15:08:37 +00:00
|
|
|
# it expects that items are an array of attributes to match
|
|
|
|
# each hash needs to have `id` and `lock_version`
|
2020-03-25 09:08:11 +00:00
|
|
|
or_conditions = items.inject(none) do |relation, item|
|
|
|
|
match = CommitStatus.default_scoped.where(item.slice(:id, :lock_version))
|
|
|
|
|
2020-01-17 15:08:37 +00:00
|
|
|
relation.or(match)
|
|
|
|
end
|
2020-03-25 09:08:11 +00:00
|
|
|
|
|
|
|
merge(or_conditions)
|
2020-01-17 15:08:37 +00:00
|
|
|
end
|
|
|
|
|
2021-01-11 18:10:43 +00:00
|
|
|
# We use `Enums::Ci::CommitStatus.failure_reasons` here so that EE can more easily
|
2018-11-13 16:29:14 +00:00
|
|
|
# extend this `Hash` with new values.
|
2021-01-11 18:10:43 +00:00
|
|
|
enum_with_nil failure_reason: Enums::Ci::CommitStatus.failure_reasons
|
2017-08-30 18:20:54 +00:00
|
|
|
|
2021-03-09 12:08:52 +00:00
|
|
|
default_value_for :retried, false
|
|
|
|
|
2017-10-06 09:45:23 +00:00
|
|
|
##
|
|
|
|
# We still create some CommitStatuses outside of CreatePipelineService.
|
|
|
|
#
|
|
|
|
# These are pages deployments and external statuses.
|
|
|
|
#
|
2017-10-11 13:32:19 +00:00
|
|
|
before_create unless: :importing? do
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-10-11 13:32:19 +00:00
|
|
|
Ci::EnsureStageService.new(project, user).execute(self) do |stage|
|
|
|
|
self.run_after_commit { StageUpdateWorker.perform_async(stage.id) }
|
2017-10-06 09:45:23 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2017-10-06 09:45:23 +00:00
|
|
|
end
|
|
|
|
|
2020-01-17 15:08:37 +00:00
|
|
|
before_save if: :status_changed?, unless: :importing? do
|
2020-06-03 00:08:38 +00: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 15:10:03 +00:00
|
|
|
if latest?
|
2020-01-17 15:08:37 +00: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 13:22:35 +00:00
|
|
|
state_machine :status do
|
2016-08-18 21:36:54 +00:00
|
|
|
event :process do
|
2017-03-03 13:35:19 +00:00
|
|
|
transition [:skipped, :manual] => :created
|
2016-08-18 21:36:54 +00:00
|
|
|
end
|
|
|
|
|
2017-07-24 09:33:01 +00:00
|
|
|
event :enqueue do
|
2019-03-03 23:56:20 +00: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 12:08:01 +00:00
|
|
|
transition [:created, :skipped, :manual, :scheduled] => :pending, if: :all_met_to_become_pending?
|
2017-07-24 09:33:01 +00:00
|
|
|
end
|
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
event :run do
|
|
|
|
transition pending: :running
|
|
|
|
end
|
|
|
|
|
2016-08-11 13:22:35 +00:00
|
|
|
event :skip do
|
2019-12-24 12:08:01 +00:00
|
|
|
transition [:created, :waiting_for_resource, :preparing, :pending] => :skipped
|
2016-08-11 13:22:35 +00:00
|
|
|
end
|
|
|
|
|
2015-10-06 10:01:16 +00:00
|
|
|
event :drop do
|
2021-12-08 06:13:27 +00:00
|
|
|
transition [:created, :waiting_for_resource, :preparing, :pending, :running, :manual, :scheduled] => :failed
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
event :success do
|
2019-12-24 12:08:01 +00:00
|
|
|
transition [:created, :waiting_for_resource, :preparing, :pending, :running] => :success
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
event :cancel do
|
2019-12-24 12:08:01 +00:00
|
|
|
transition [:created, :waiting_for_resource, :preparing, :pending, :running, :manual, :scheduled] => :canceled
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2019-12-24 12:08:01 +00:00
|
|
|
before_transition [:created, :waiting_for_resource, :preparing, :skipped, :manual, :scheduled] => :pending do |commit_status|
|
2020-05-22 09:08:09 +00:00
|
|
|
commit_status.queued_at = Time.current
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2019-02-27 02:13:06 +00:00
|
|
|
before_transition [:created, :preparing, :pending] => :running do |commit_status|
|
2020-05-22 09:08:09 +00:00
|
|
|
commit_status.started_at = Time.current
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2016-10-26 09:34:40 +00:00
|
|
|
before_transition any => [:success, :failed, :canceled] do |commit_status|
|
2020-05-22 09:08:09 +00:00
|
|
|
commit_status.finished_at = Time.current
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2017-08-30 18:20:54 +00:00
|
|
|
before_transition any => :failed do |commit_status, transition|
|
2022-01-19 15:14:05 +00:00
|
|
|
reason = ::Gitlab::Ci::Build::Status::Reason
|
|
|
|
.fabricate(commit_status, transition.args.first)
|
|
|
|
|
|
|
|
commit_status.failure_reason = reason.failure_reason_enum
|
|
|
|
commit_status.allow_failure = true if reason.force_allow_failure?
|
2017-08-30 18:20:54 +00:00
|
|
|
end
|
|
|
|
|
2020-12-21 15:10:05 +00: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 12:13:08 +00:00
|
|
|
after_transition do |commit_status, transition|
|
2016-10-12 13:21:11 +00:00
|
|
|
next if transition.loopback?
|
2020-01-17 15:08:37 +00:00
|
|
|
next if commit_status.processed?
|
|
|
|
next unless commit_status.project
|
2016-10-08 18:42:09 +00:00
|
|
|
|
2021-05-25 09:10:54 +00:00
|
|
|
last_arg = transition.args.last
|
|
|
|
transition_options = last_arg.is_a?(Hash) && last_arg.extractable_options? ? last_arg : {}
|
|
|
|
|
2016-10-08 18:25:16 +00:00
|
|
|
commit_status.run_after_commit do
|
2021-05-25 09:10:54 +00:00
|
|
|
PipelineProcessWorker.perform_async(pipeline_id) unless transition_options[:skip_pipeline_processing]
|
2021-12-03 12:10:23 +00:00
|
|
|
|
2022-01-20 00:15:02 +00:00
|
|
|
expire_etag_cache!
|
2016-09-19 13:03:51 +00:00
|
|
|
end
|
2016-09-19 12:13:08 +00:00
|
|
|
end
|
2021-04-15 15:09:11 +00: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 10:01:16 +00:00
|
|
|
end
|
|
|
|
|
2019-08-13 13:14:33 +00:00
|
|
|
def self.names
|
|
|
|
select(:name)
|
|
|
|
end
|
|
|
|
|
2020-01-17 15:08:37 +00:00
|
|
|
def self.update_as_processed!
|
2020-05-27 15:08:11 +00: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 15:08:37 +00:00
|
|
|
end
|
|
|
|
|
2020-01-29 18:08:47 +00:00
|
|
|
def self.locking_enabled?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-03-22 17:22:34 +00:00
|
|
|
def locking_enabled?
|
2019-04-23 09:30:18 +00:00
|
|
|
will_save_change_to_status?
|
2017-03-22 17:22:34 +00:00
|
|
|
end
|
|
|
|
|
2016-09-07 12:52:13 +00:00
|
|
|
def group_name
|
2022-05-03 00:08:25 +00:00
|
|
|
# [\b\s:] -> whitespace or column
|
|
|
|
# (\[.*\])|(\d+[\s:\/\\]+\d+) -> variables/matrix or parallel-jobs numbers
|
|
|
|
# {1,3} -> number of times that matches the variables/matrix or parallel-jobs numbers
|
|
|
|
# we limit this to 3 because of possible abuse
|
|
|
|
regex = %r{([\b\s:]+((\[.*\])|(\d+[\s:\/\\]+\d+))){1,3}\s*\z}
|
|
|
|
|
|
|
|
name.to_s.sub(regex, '').strip
|
2016-09-07 12:52:13 +00:00
|
|
|
end
|
|
|
|
|
2016-10-03 10:22:51 +00:00
|
|
|
def failed_but_allowed?
|
2016-03-17 14:54:56 +00:00
|
|
|
allow_failure? && (failed? || canceled?)
|
2016-02-19 18:58:43 +00:00
|
|
|
end
|
|
|
|
|
2021-04-26 18:09:45 +00:00
|
|
|
# Time spent running.
|
2016-11-22 05:36:19 +00:00
|
|
|
def duration
|
2021-04-26 18:09:45 +00: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 05:36:19 +00:00
|
|
|
end
|
|
|
|
|
2020-01-17 15:08:37 +00:00
|
|
|
def latest?
|
|
|
|
!retried?
|
|
|
|
end
|
|
|
|
|
2016-09-14 13:04:12 +00:00
|
|
|
def playable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-06-04 10:44:00 +00:00
|
|
|
def retryable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2017-06-12 09:20:19 +00:00
|
|
|
def cancelable?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-10-23 10:58:41 +00:00
|
|
|
def archived?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2016-11-22 05:36:19 +00:00
|
|
|
def stuck?
|
|
|
|
false
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
2015-10-12 13:45:46 +00:00
|
|
|
|
2016-11-22 05:36:19 +00:00
|
|
|
def has_trace?
|
2015-10-12 19:12:31 +00:00
|
|
|
false
|
|
|
|
end
|
2016-12-08 08:51:36 +00:00
|
|
|
|
2019-12-24 12:08:01 +00:00
|
|
|
def all_met_to_become_pending?
|
2021-02-01 09:09:28 +00:00
|
|
|
true
|
2019-12-24 12:08:01 +00:00
|
|
|
end
|
|
|
|
|
2017-04-06 13:32:56 +00:00
|
|
|
def auto_canceled?
|
|
|
|
canceled? && auto_canceled_by_id?
|
|
|
|
end
|
|
|
|
|
2016-12-08 16:52:24 +00:00
|
|
|
def detailed_status(current_user)
|
2017-02-22 22:54:59 +00:00
|
|
|
Gitlab::Ci::Status::Factory
|
|
|
|
.new(self, current_user)
|
|
|
|
.fabricate!
|
2016-12-08 08:51:36 +00:00
|
|
|
end
|
2016-12-22 20:31:42 +00:00
|
|
|
|
2016-12-27 15:59:42 +00:00
|
|
|
def sortable_name
|
2017-06-13 07:22:22 +00:00
|
|
|
name.to_s.split(/(\d+)/).map do |v|
|
2016-12-22 20:31:42 +00:00
|
|
|
v =~ /\d+/ ? v.to_i : v
|
|
|
|
end
|
|
|
|
end
|
2020-01-17 15:08:37 +00:00
|
|
|
|
2020-04-23 15:09:55 +00:00
|
|
|
def recoverable?
|
|
|
|
failed? && !unrecoverable_failure?
|
|
|
|
end
|
|
|
|
|
2021-03-09 12:08:52 +00:00
|
|
|
def update_older_statuses_retried!
|
2021-03-24 18:09:31 +00:00
|
|
|
pipeline
|
|
|
|
.statuses
|
2021-03-09 12:08:52 +00:00
|
|
|
.latest
|
|
|
|
.where(name: name)
|
|
|
|
.where.not(id: id)
|
|
|
|
.update_all(retried: true, processed: true)
|
|
|
|
end
|
|
|
|
|
2021-12-03 12:10:23 +00:00
|
|
|
def expire_etag_cache!
|
|
|
|
job_path = Gitlab::Routing.url_helpers.project_build_path(project, id, format: :json)
|
|
|
|
|
|
|
|
Gitlab::EtagCaching::Store.new.touch(job_path)
|
|
|
|
end
|
|
|
|
|
2020-01-17 15:08:37 +00:00
|
|
|
private
|
|
|
|
|
2020-04-23 15:09:55 +00:00
|
|
|
def unrecoverable_failure?
|
|
|
|
script_failure? || missing_dependency_failure? || archived_failure? || scheduler_failure? || data_integrity_failure?
|
|
|
|
end
|
2015-10-06 10:01:16 +00:00
|
|
|
end
|
2020-05-14 12:08:21 +00:00
|
|
|
|
2021-05-11 21:10:21 +00:00
|
|
|
CommitStatus.prepend_mod_with('CommitStatus')
|