2021-05-19 11:10:40 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Ci
|
2021-08-03 02:08:50 -04:00
|
|
|
class PendingBuild < Ci::ApplicationRecord
|
2021-09-02 20:09:01 -04:00
|
|
|
include EachBatch
|
|
|
|
|
2021-05-19 11:10:40 -04:00
|
|
|
belongs_to :project
|
|
|
|
belongs_to :build, class_name: 'Ci::Build'
|
2021-08-03 08:09:42 -04:00
|
|
|
belongs_to :namespace, inverse_of: :pending_builds, class_name: 'Namespace'
|
|
|
|
|
|
|
|
validates :namespace, presence: true
|
2021-05-19 11:10:40 -04:00
|
|
|
|
2021-06-22 08:08:05 -04:00
|
|
|
scope :ref_protected, -> { where(protected: true) }
|
|
|
|
scope :queued_before, ->(time) { where(arel_table[:created_at].lt(time)) }
|
2021-08-10 08:11:00 -04:00
|
|
|
scope :with_instance_runners, -> { where(instance_runners_enabled: true) }
|
2021-09-16 08:09:35 -04: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 08:08:05 -04:00
|
|
|
|
2021-08-20 08:09:31 -04:00
|
|
|
class << self
|
|
|
|
def upsert_from_build!(build)
|
|
|
|
entry = self.new(args_from_build(build))
|
2021-05-19 11:10:40 -04:00
|
|
|
|
2021-08-20 08:09:31 -04:00
|
|
|
entry.validate!
|
2021-05-19 11:10:40 -04:00
|
|
|
|
2021-08-20 08:09:31 -04:00
|
|
|
self.upsert(entry.attributes.compact, returning: %w[build_id], unique_by: :build_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def args_from_build(build)
|
2021-09-15 11:10:13 -04:00
|
|
|
project = build.project
|
|
|
|
|
2021-08-20 08:09:31 -04:00
|
|
|
args = {
|
|
|
|
build: build,
|
2021-09-15 11:10:13 -04:00
|
|
|
project: project,
|
2021-08-20 08:09:31 -04:00
|
|
|
protected: build.protected?,
|
2022-07-12 14:08:46 -04:00
|
|
|
namespace: project.namespace,
|
|
|
|
tag_ids: build.tags_ids,
|
|
|
|
instance_runners_enabled: shared_runners_enabled?(project)
|
2021-08-20 08:09:31 -04:00
|
|
|
}
|
|
|
|
|
2022-07-12 14:08:46 -04:00
|
|
|
if group_runners_enabled?(project)
|
|
|
|
args.store(:namespace_traversal_ids, project.namespace.traversal_ids)
|
2021-09-15 11:10:13 -04:00
|
|
|
end
|
2021-07-13 11:08:38 -04:00
|
|
|
|
2021-09-15 11:10:13 -04:00
|
|
|
args
|
2021-08-20 08:09:31 -04:00
|
|
|
end
|
2021-07-13 11:08:38 -04:00
|
|
|
|
2021-09-15 11:10:13 -04:00
|
|
|
def shared_runners_enabled?(project)
|
|
|
|
builds_enabled?(project) && project.shared_runners_enabled?
|
2021-08-20 08:09:31 -04:00
|
|
|
end
|
2021-07-13 11:08:38 -04:00
|
|
|
|
2021-09-15 11:10:13 -04:00
|
|
|
def group_runners_enabled?(project)
|
|
|
|
builds_enabled?(project) && project.group_runners_enabled?
|
2021-08-20 08:09:31 -04:00
|
|
|
end
|
2021-07-13 11:08:38 -04:00
|
|
|
|
2021-09-15 11:10:13 -04:00
|
|
|
def builds_enabled?(project)
|
|
|
|
project.builds_enabled? && !project.pending_delete?
|
2021-08-20 08:09:31 -04:00
|
|
|
end
|
2021-07-13 11:08:38 -04:00
|
|
|
end
|
2021-05-19 11:10:40 -04:00
|
|
|
end
|
|
|
|
end
|
2021-08-03 08:09:42 -04:00
|
|
|
|
|
|
|
Ci::PendingBuild.prepend_mod_with('Ci::PendingBuild')
|