Commit 874f40ab authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'mo-add-scopes-for-pending-builds-tags' into 'master'

Add scopes for pending builds tags

See merge request gitlab-org/gitlab!70214
parents 6a89e66e 7112bd9a
......@@ -13,6 +13,13 @@ module Ci
scope :ref_protected, -> { where(protected: true) }
scope :queued_before, ->(time) { where(arel_table[:created_at].lt(time)) }
scope :with_instance_runners, -> { where(instance_runners_enabled: true) }
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
class << self
def upsert_from_build!(build)
......
......@@ -24,7 +24,7 @@ module Ci
def builds_matching_tag_ids(relation, ids)
if ::Feature.enabled?(:ci_queueing_denormalize_tags_information, runner, default_enabled: :yaml)
relation.where('tag_ids <@ ARRAY[?]::int[]', runner.tags_ids)
relation.for_tags(runner.tags_ids)
else
relation.merge(CommitStatus.matches_tag_ids(ids, table: 'ci_pending_builds', column: 'build_id'))
end
......
......@@ -8,5 +8,6 @@ FactoryBot.define do
instance_runners_enabled { true }
namespace { project.namespace }
minutes_exceeded { false }
tag_ids { build.tags_ids }
end
end
......@@ -34,6 +34,47 @@ RSpec.describe Ci::PendingBuild do
end
end
end
describe '.for_tags' do
subject(:pending_builds) { described_class.for_tags(tag_ids) }
let_it_be(:pending_build_with_tags) { create(:ci_pending_build, tag_ids: [1, 2]) }
let_it_be(:pending_build_without_tags) { create(:ci_pending_build) }
context 'when tag_ids match pending builds' do
let(:tag_ids) { [1, 2] }
it 'returns matching pending builds' do
expect(pending_builds).to contain_exactly(pending_build_with_tags, pending_build_without_tags)
end
end
context 'when tag_ids does not match pending builds' do
let(:tag_ids) { [non_existing_record_id] }
it 'returns matching pending builds without tags' do
expect(pending_builds).to contain_exactly(pending_build_without_tags)
end
end
context 'when tag_ids is not provided' do
context 'with a nil value' do
let(:tag_ids) { nil }
it 'returns matching pending builds without tags' do
expect(pending_builds).to contain_exactly(pending_build_without_tags)
end
end
context 'with an empty array' do
let(:tag_ids) { [] }
it 'returns matching pending builds without tags' do
expect(pending_builds).to contain_exactly(pending_build_without_tags)
end
end
end
end
end
describe '.upsert_from_build!' do
......
......@@ -40,12 +40,16 @@ module Ci
context 'runner follow tag list' do
it "picks build with the same tag" do
pending_job.update!(tag_list: ["linux"])
pending_job.reload
pending_job.create_queuing_entry!
specific_runner.update!(tag_list: ["linux"])
expect(execute(specific_runner)).to eq(pending_job)
end
it "does not pick build with different tag" do
pending_job.update!(tag_list: ["linux"])
pending_job.reload
pending_job.create_queuing_entry!
specific_runner.update!(tag_list: ["win32"])
expect(execute(specific_runner)).to be_falsey
end
......@@ -56,6 +60,8 @@ module Ci
it "does not pick build with tag" do
pending_job.update!(tag_list: ["linux"])
pending_job.reload
pending_job.create_queuing_entry!
expect(execute(specific_runner)).to be_falsey
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment