debian-mirror-gitlab/app/models/ci/pending_build.rb

73 lines
1.9 KiB
Ruby
Raw Normal View History

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