debian-mirror-gitlab/app/models/commit_status.rb

326 lines
9.6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2021-10-27 15:23:28 +05:30
class CommitStatus < Ci::ApplicationRecord
2022-10-11 01:57:18 +05:30
include Ci::Partitionable
2020-07-28 23:09:34 +05:30
include Ci::HasStatus
2016-06-22 15:30:34 +05:30
include Importable
2016-11-03 12:29:30 +05:30
include AfterCommitQueue
2018-10-15 14:42:47 +05:30
include Presentable
2020-07-28 23:09:34 +05:30
include BulkInsertableAssociations
2021-09-30 23:02:18 +05:30
include TaggableQueries
2023-06-20 00:43:36 +05:30
include SafelyChangeColumnDefault
2016-06-02 11:05:42 +05:30
2015-10-24 18:46:33 +05:30
self.table_name = 'ci_builds'
2023-07-09 08:55:56 +05:30
self.sequence_name = 'ci_builds_id_seq'
2023-05-27 22:25:52 +05:30
self.primary_key = :id
2022-10-11 01:57:18 +05:30
partitionable scope: :pipeline
2023-06-20 00:43:36 +05:30
columns_changing_default :partition_id
2022-08-13 15:12:31 +05:30
2017-09-10 17:25:29 +05:30
belongs_to :user
2017-08-17 22:00:37 +05:30
belongs_to :project
2023-05-27 22:25:52 +05:30
belongs_to :pipeline, class_name: 'Ci::Pipeline', foreign_key: :commit_id, inverse_of: :statuses
belongs_to :auto_canceled_by, class_name: 'Ci::Pipeline', inverse_of: :auto_canceled_jobs
2022-10-11 01:57:18 +05:30
belongs_to :ci_stage, class_name: 'Ci::Stage', foreign_key: :stage_id
2015-10-24 18:46:33 +05:30
2020-06-23 00:09:42 +05:30
has_many :needs, class_name: 'Ci::BuildNeed', foreign_key: :build_id, inverse_of: :build
2023-01-13 00:05:48 +05:30
attribute :retried, default: false
2020-06-23 00:09:42 +05:30
enum scheduling_type: { stage: 0, dag: 1 }, _prefix: true
2023-01-13 00:05:48 +05:30
# We use `Enums::Ci::CommitStatus.failure_reasons` here so that EE can more easily
# extend this `Hash` with new values.
2023-05-27 22:25:52 +05:30
enum failure_reason: Enums::Ci::CommitStatus.failure_reasons
2020-06-23 00:09:42 +05:30
2016-06-22 15:30:34 +05:30
delegate :commit, to: :pipeline
2020-01-01 13:55:28 +05:30
delegate :sha, :short_sha, :before_sha, to: :pipeline
2016-06-22 15:30:34 +05:30
validates :pipeline, presence: true, unless: :importing?
2017-09-10 17:25:29 +05:30
validates :name, presence: true, unless: :importing?
2023-07-09 08:55:56 +05:30
validates :stage, :ref, :target_url, :description, length: { maximum: 255 }
2015-10-24 18:46:33 +05:30
alias_attribute :author, :user
2018-03-17 18:26:18 +05:30
alias_attribute :pipeline_id, :commit_id
2017-09-10 17:25:29 +05:30
2016-11-03 12:29:30 +05:30
scope :failed_but_allowed, -> do
where(allow_failure: true, status: [:failed, :canceled])
end
2022-06-21 17:19:12 +05:30
scope :order_id_desc, -> { order(id: :desc) }
2020-11-24 15:15:51 +05:30
2017-08-17 22:00:37 +05:30
scope :latest, -> { where(retried: [false, nil]) }
scope :retried, -> { where(retried: true) }
scope :ordered, -> { order(:name) }
2020-03-13 15:44:24 +05:30
scope :ordered_by_stage, -> { order(stage_idx: :asc) }
2017-08-17 22:00:37 +05:30
scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
2021-12-11 22:18:48 +05:30
scope :retried_ordered, -> { retried.order(name: :asc, id: :desc).includes(project: :namespace) }
2021-01-03 14:25:43 +05:30
scope :ordered_by_pipeline, -> { order(pipeline_id: :asc) }
2019-10-12 21:52:04 +05:30
scope :before_stage, -> (index) { where('stage_idx < ?', index) }
scope :for_stage, -> (index) { where(stage_idx: index) }
2017-08-17 22:00:37 +05:30
scope :after_stage, -> (index) { where('stage_idx > ?', index) }
2022-01-26 12:08:38 +05:30
scope :for_project, -> (project_id) { where(project_id: project_id) }
2020-01-01 13:55:28 +05:30
scope :for_ref, -> (ref) { where(ref: ref) }
scope :by_name, -> (name) { where(name: name) }
2021-02-11 23:33:58 +05:30
scope :in_pipelines, ->(pipelines) { where(pipeline: pipelines) }
2021-04-29 21:17:54 +05:30
scope :with_pipeline, -> { joins(:pipeline) }
2023-07-09 08:55:56 +05:30
scope :updated_at_before, ->(date) { where("#{quoted_table_name}.updated_at < ?", date) }
scope :created_at_before, ->(date) { where("#{quoted_table_name}.created_at < ?", date) }
2021-11-18 22:05:49 +05:30
scope :scheduled_at_before, ->(date) {
2023-07-09 08:55:56 +05:30
where("#{quoted_table_name}.scheduled_at IS NOT NULL AND #{quoted_table_name}.scheduled_at < ?", date)
2021-11-18 22:05:49 +05:30
}
2023-03-17 16:20:25 +05:30
scope :with_when_executed, ->(when_executed) { where(when: when_executed) }
2023-07-09 08:55:56 +05:30
scope :with_type, ->(type) { where(type: type) }
2020-01-01 13:55:28 +05:30
2021-11-11 11:23:49 +05:30
# The scope applies `pluck` to split the queries. Use with care.
2020-01-01 13:55:28 +05:30
scope :for_project_paths, -> (paths) do
2021-11-11 11:23:49 +05:30
# 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)
2022-01-26 12:08:38 +05:30
for_project(project_ids)
2020-01-01 13:55:28 +05:30
end
2019-10-12 21:52:04 +05:30
2019-12-21 20:55:43 +05:30
scope :with_preloads, -> do
preload(:project, :user)
end
2020-03-13 15:44:24 +05:30
scope :with_project_preload, -> do
preload(project: :namespace)
2019-10-12 21:52:04 +05:30
end
2020-04-22 19:07:51 +05:30
scope :match_id_and_lock_version, -> (items) do
2020-03-13 15:44:24 +05:30
# it expects that items are an array of attributes to match
# each hash needs to have `id` and `lock_version`
2020-04-22 19:07:51 +05:30
or_conditions = items.inject(none) do |relation, item|
match = CommitStatus.default_scoped.where(item.slice(:id, :lock_version))
2020-03-13 15:44:24 +05:30
relation.or(match)
end
2020-04-22 19:07:51 +05:30
merge(or_conditions)
2019-10-12 21:52:04 +05:30
end
2018-03-17 18:26:18 +05:30
##
# We still create some CommitStatuses outside of CreatePipelineService.
#
# These are pages deployments and external statuses.
#
before_create unless: :importing? do
2023-04-23 21:23:45 +05:30
next if Feature.enabled?(:ci_remove_ensure_stage_service, project)
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ServiceClass
2018-03-17 18:26:18 +05:30
Ci::EnsureStageService.new(project, user).execute(self) do |stage|
self.run_after_commit { StageUpdateWorker.perform_async(stage.id) }
2016-09-13 17:45:13 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ServiceClass
2018-03-17 18:26:18 +05:30
end
2016-09-13 17:45:13 +05:30
2020-03-13 15:44:24 +05:30
before_save if: :status_changed?, unless: :importing? do
2020-06-23 00:09:42 +05:30
# 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-10-24 23:57:45 +05:30
if latest?
2020-03-13 15:44:24 +05:30
self.processed = false # force refresh of all dependent ones
elsif retried?
self.processed = true # retried are considered to be already processed
end
end
2018-03-17 18:26:18 +05:30
state_machine :status do
2016-09-13 17:45:13 +05:30
event :process do
2017-08-17 22:00:37 +05:30
transition [:skipped, :manual] => :created
2016-08-24 12:49:21 +05:30
end
2018-03-17 18:26:18 +05:30
event :enqueue do
2019-07-07 11:18:12 +05:30
# A CommitStatus will never have prerequisites, but this event
# is shared by Ci::Build, which cannot progress unless prerequisites
# are satisfied.
2020-03-13 15:44:24 +05:30
transition [:created, :skipped, :manual, :scheduled] => :pending, if: :all_met_to_become_pending?
2018-03-17 18:26:18 +05:30
end
2015-10-24 18:46:33 +05:30
event :run do
transition pending: :running
end
2016-09-13 17:45:13 +05:30
event :skip do
2020-03-13 15:44:24 +05:30
transition [:created, :waiting_for_resource, :preparing, :pending] => :skipped
2016-09-13 17:45:13 +05:30
end
2015-10-24 18:46:33 +05:30
event :drop do
2022-01-26 12:08:38 +05:30
transition [:created, :waiting_for_resource, :preparing, :pending, :running, :manual, :scheduled] => :failed
2015-10-24 18:46:33 +05:30
end
event :success do
2020-03-13 15:44:24 +05:30
transition [:created, :waiting_for_resource, :preparing, :pending, :running] => :success
2015-10-24 18:46:33 +05:30
end
event :cancel do
2020-03-13 15:44:24 +05:30
transition [:created, :waiting_for_resource, :preparing, :pending, :running, :manual, :scheduled] => :canceled
2016-09-13 17:45:13 +05:30
end
2020-03-13 15:44:24 +05:30
before_transition [:created, :waiting_for_resource, :preparing, :skipped, :manual, :scheduled] => :pending do |commit_status|
2020-06-23 00:09:42 +05:30
commit_status.queued_at = Time.current
2015-10-24 18:46:33 +05:30
end
2019-07-07 11:18:12 +05:30
before_transition [:created, :preparing, :pending] => :running do |commit_status|
2020-06-23 00:09:42 +05:30
commit_status.started_at = Time.current
2015-10-24 18:46:33 +05:30
end
2017-08-17 22:00:37 +05:30
before_transition any => [:success, :failed, :canceled] do |commit_status|
2020-06-23 00:09:42 +05:30
commit_status.finished_at = Time.current
2015-10-24 18:46:33 +05:30
end
2018-03-17 18:26:18 +05:30
before_transition any => :failed do |commit_status, transition|
2022-03-02 08:16:31 +05:30
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?
2018-03-17 18:26:18 +05:30
end
2021-03-08 18:12:59 +05:30
before_transition [:skipped, :manual] => :created do |commit_status, transition|
transition.args.first.try do |user|
commit_status.user = user
end
end
2016-09-13 17:45:13 +05:30
after_transition do |commit_status, transition|
2016-11-03 12:29:30 +05:30
next if transition.loopback?
2020-03-13 15:44:24 +05:30
next if commit_status.processed?
next unless commit_status.project
2016-11-03 12:29:30 +05:30
2021-09-04 01:27:46 +05:30
last_arg = transition.args.last
transition_options = last_arg.is_a?(Hash) && last_arg.extractable_options? ? last_arg : {}
2016-11-03 12:29:30 +05:30
commit_status.run_after_commit do
2021-09-04 01:27:46 +05:30
PipelineProcessWorker.perform_async(pipeline_id) unless transition_options[:skip_pipeline_processing]
2022-01-26 12:08:38 +05:30
2022-03-02 08:16:31 +05:30
expire_etag_cache!
2016-11-03 12:29:30 +05:30
end
2015-12-23 02:04:40 +05:30
end
2016-06-02 11:05:42 +05:30
after_transition any => :failed do |commit_status|
2016-11-03 12:29:30 +05:30
commit_status.run_after_commit do
2021-04-29 21:17:54 +05:30
::Gitlab::Ci::Pipeline::Metrics.job_failure_reason_counter.increment(reason: commit_status.failure_reason)
2016-11-03 12:29:30 +05:30
end
2016-06-02 11:05:42 +05:30
end
2015-10-24 18:46:33 +05:30
end
2019-10-12 21:52:04 +05:30
def self.names
select(:name)
end
2020-03-13 15:44:24 +05:30
def self.update_as_processed!
2020-06-23 00:09:42 +05:30
# 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-03-13 15:44:24 +05:30
end
def self.locking_enabled?
false
end
2017-08-17 22:00:37 +05:30
def locking_enabled?
2019-07-31 22:56:46 +05:30
will_save_change_to_status?
2017-08-17 22:00:37 +05:30
end
2015-10-24 18:46:33 +05:30
2016-09-29 09:46:39 +05:30
def group_name
2022-05-03 16:02:30 +05:30
# [\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-29 09:46:39 +05:30
end
2021-06-08 01:23:25 +05:30
# Time spent running.
2017-08-17 22:00:37 +05:30
def duration
2021-06-08 01:23:25 +05:30
calculate_duration(started_at, finished_at)
end
# Time spent in the pending state.
def queued_duration
calculate_duration(queued_at, started_at)
2017-08-17 22:00:37 +05:30
end
2020-03-13 15:44:24 +05:30
def latest?
!retried?
end
2016-09-29 09:46:39 +05:30
def playable?
false
end
2017-09-10 17:25:29 +05:30
def retryable?
false
end
def cancelable?
false
end
2018-12-13 13:39:08 +05:30
def archived?
false
end
2017-08-17 22:00:37 +05:30
def stuck?
false
2015-10-24 18:46:33 +05:30
end
2017-08-17 22:00:37 +05:30
def has_trace?
2015-10-24 18:46:33 +05:30
false
end
2017-08-17 22:00:37 +05:30
2020-03-13 15:44:24 +05:30
def all_met_to_become_pending?
2021-03-11 19:13:27 +05:30
true
2020-03-13 15:44:24 +05:30
end
2017-08-17 22:00:37 +05:30
def auto_canceled?
canceled? && auto_canceled_by_id?
end
def detailed_status(current_user)
Gitlab::Ci::Status::Factory
.new(self, current_user)
.fabricate!
end
def sortable_name
2017-09-10 17:25:29 +05:30
name.to_s.split(/(\d+)/).map do |v|
2017-08-17 22:00:37 +05:30
v =~ /\d+/ ? v.to_i : v
end
end
2020-03-13 15:44:24 +05:30
2020-05-24 23:13:21 +05:30
def recoverable?
failed? && !unrecoverable_failure?
end
2021-04-17 20:07:23 +05:30
def update_older_statuses_retried!
2021-04-29 21:17:54 +05:30
pipeline
.statuses
2021-04-17 20:07:23 +05:30
.latest
.where(name: name)
.where.not(id: id)
.update_all(retried: true, processed: true)
end
2022-01-26 12:08:38 +05:30
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
2022-10-11 01:57:18 +05:30
def stage_name
ci_stage&.name
end
2020-03-13 15:44:24 +05:30
private
2020-05-24 23:13:21 +05:30
def unrecoverable_failure?
script_failure? || missing_dependency_failure? || archived_failure? || scheduler_failure? || data_integrity_failure?
end
2015-10-24 18:46:33 +05:30
end