2021-05-19 15:10:40 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
2021-08-03 06:08:50 +00:00
|
|
|
class PendingBuild < Ci::ApplicationRecord
|
2021-09-03 00:09:01 +00:00
|
|
|
include EachBatch
|
|
|
|
|
2021-05-19 15:10:40 +00:00
|
|
|
belongs_to :project
|
|
|
|
belongs_to :build, class_name: 'Ci::Build'
|
2021-08-03 12:09:42 +00:00
|
|
|
belongs_to :namespace, inverse_of: :pending_builds, class_name: 'Namespace'
|
|
|
|
|
|
|
|
validates :namespace, presence: true
|
2021-05-19 15:10:40 +00:00
|
|
|
|
2021-06-22 12:08:05 +00:00
|
|
|
scope :ref_protected, -> { where(protected: true) }
|
|
|
|
scope :queued_before, ->(time) { where(arel_table[:created_at].lt(time)) }
|
2021-08-10 12:11:00 +00:00
|
|
|
scope :with_instance_runners, -> { where(instance_runners_enabled: true) }
|
2021-09-16 12:09:35 +00:00
|
|
|
scope :for_tags, ->(tag_ids) do
|
|
|
|
if tag_ids.present?
|
|
|
|
where('ci_pending_builds.tag_ids <@ ARRAY[?]::int[]', Array.wrap(tag_ids))
|
|
|
|
else
|
|
|
|
where("ci_pending_builds.tag_ids = '{}'")
|
|
|
|
end
|
|
|
|
end
|
2021-06-22 12:08:05 +00:00
|
|
|
|
2021-08-20 12:09:31 +00:00
|
|
|
class << self
|
|
|
|
def upsert_from_build!(build)
|
|
|
|
entry = self.new(args_from_build(build))
|
2021-05-19 15:10:40 +00:00
|
|
|
|
2021-08-20 12:09:31 +00:00
|
|
|
entry.validate!
|
2021-05-19 15:10:40 +00:00
|
|
|
|
2021-08-20 12:09:31 +00:00
|
|
|
self.upsert(entry.attributes.compact, returning: %w[build_id], unique_by: :build_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def args_from_build(build)
|
2021-09-15 15:10:13 +00:00
|
|
|
project = build.project
|
|
|
|
|
2021-08-20 12:09:31 +00:00
|
|
|
args = {
|
|
|
|
build: build,
|
2021-09-15 15:10:13 +00:00
|
|
|
project: project,
|
2021-08-20 12:09:31 +00:00
|
|
|
protected: build.protected?,
|
2021-09-15 15:10:13 +00:00
|
|
|
namespace: project.namespace
|
2021-08-20 12:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if Feature.enabled?(:ci_pending_builds_maintain_tags_data, type: :development, default_enabled: :yaml)
|
|
|
|
args.store(:tag_ids, build.tags_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
if Feature.enabled?(:ci_pending_builds_maintain_shared_runners_data, type: :development, default_enabled: :yaml)
|
2021-09-15 15:10:13 +00:00
|
|
|
args.store(:instance_runners_enabled, shared_runners_enabled?(project))
|
2021-08-20 12:09:31 +00:00
|
|
|
end
|
2021-07-13 15:08:38 +00:00
|
|
|
|
2021-09-15 15:10:13 +00:00
|
|
|
if Feature.enabled?(:ci_pending_builds_maintain_namespace_traversal_ids, type: :development, default_enabled: :yaml)
|
|
|
|
args.store(:namespace_traversal_ids, project.namespace.traversal_ids) if group_runners_enabled?(project)
|
|
|
|
end
|
2021-07-13 15:08:38 +00:00
|
|
|
|
2021-09-15 15:10:13 +00:00
|
|
|
args
|
2021-08-20 12:09:31 +00:00
|
|
|
end
|
2021-07-13 15:08:38 +00:00
|
|
|
|
2021-09-15 15:10:13 +00:00
|
|
|
def shared_runners_enabled?(project)
|
|
|
|
builds_enabled?(project) && project.shared_runners_enabled?
|
2021-08-20 12:09:31 +00:00
|
|
|
end
|
2021-07-13 15:08:38 +00:00
|
|
|
|
2021-09-15 15:10:13 +00:00
|
|
|
def group_runners_enabled?(project)
|
|
|
|
builds_enabled?(project) && project.group_runners_enabled?
|
2021-08-20 12:09:31 +00:00
|
|
|
end
|
2021-07-13 15:08:38 +00:00
|
|
|
|
2021-09-15 15:10:13 +00:00
|
|
|
def builds_enabled?(project)
|
|
|
|
project.builds_enabled? && !project.pending_delete?
|
2021-08-20 12:09:31 +00:00
|
|
|
end
|
2021-07-13 15:08:38 +00:00
|
|
|
end
|
2021-05-19 15:10:40 +00:00
|
|
|
end
|
|
|
|
end
|
2021-08-03 12:09:42 +00:00
|
|
|
|
|
|
|
Ci::PendingBuild.prepend_mod_with('Ci::PendingBuild')
|