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

64 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-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'
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-09-30 23:02:18 +05:30
2021-09-04 01:27:46 +05:30
def self.upsert_from_build!(build)
2021-09-30 23:02:18 +05:30
entry = self.new(args_from_build(build))
2021-09-04 01:27:46 +05:30
entry.validate!
self.upsert(entry.attributes.compact, returning: %w[build_id], unique_by: :build_id)
end
2021-09-30 23:02:18 +05:30
def self.args_from_build(build)
args = {
build: build,
project: build.project,
2021-10-27 15:23:28 +05:30
protected: build.protected?,
namespace: build.project.namespace
2021-09-30 23:02:18 +05:30
}
if Feature.enabled?(:ci_pending_builds_maintain_shared_runners_data, type: :development, default_enabled: :yaml)
args.merge(instance_runners_enabled: shareable?(build))
else
args
end
end
private_class_method :args_from_build
def self.shareable?(build)
shared_runner_enabled?(build) &&
builds_access_level?(build) &&
project_not_removed?(build)
end
private_class_method :shareable?
def self.shared_runner_enabled?(build)
build.project.shared_runners.exists?
end
private_class_method :shared_runner_enabled?
def self.project_not_removed?(build)
!build.project.pending_delete?
end
private_class_method :project_not_removed?
def self.builds_access_level?(build)
build.project.project_feature.builds_access_level.nil? || build.project.project_feature.builds_access_level > 0
end
private_class_method :builds_access_level?
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')