split up Ci::Runner.owned_or_shared scope

This commit is contained in:
Alexis Reigel 2017-10-04 09:27:27 +02:00
parent 81c0c57acd
commit d6167a9214
No known key found for this signature in database
GPG Key ID: 55ADA7C7B683B329
2 changed files with 40 additions and 34 deletions

View File

@ -21,17 +21,17 @@ module Ci
before_validation :set_default_values before_validation :set_default_values
scope :specific, ->() { where(is_shared: false) } scope :specific, -> { where(is_shared: false) }
scope :shared, ->() { where(is_shared: true) } scope :shared, -> { where(is_shared: true) }
scope :active, ->() { where(active: true) } scope :active, -> { where(active: true) }
scope :paused, ->() { where(active: false) } scope :paused, -> { where(active: false) }
scope :online, ->() { where('contacted_at > ?', contact_time_deadline) } scope :online, -> { where('contacted_at > ?', contact_time_deadline) }
scope :ordered, ->() { order(id: :desc) } scope :ordered, -> { order(id: :desc) }
scope :belonging_to_project, -> (project_id) {
scope :owned_or_shared, ->(project_id) do joins(:runner_projects).where(ci_runner_projects: { project_id: project_id })
project_runners = joins(:runner_projects).where(ci_runner_projects: { project_id: project_id }) }
scope :belonging_to_group, -> (project_id) {
group_runners = joins( joins(
%{ %{
INNER JOIN ci_runner_groups ON ci_runner_groups.runner_id = ci_runners.id INNER JOIN ci_runner_groups ON ci_runner_groups.runner_id = ci_runners.id
INNER JOIN namespaces ON namespaces.id = ci_runner_groups.group_id INNER JOIN namespaces ON namespaces.id = ci_runner_groups.group_id
@ -43,13 +43,13 @@ module Ci
AND AND
projects.group_runners_enabled = :true projects.group_runners_enabled = :true
}, },
project_id: project_id, project_id: project_id,
true: true true: true
) )
}
shared_runners = where(is_shared: true) scope :owned_or_shared, -> (project_id) do
union = Gitlab::SQL::Union.new([belonging_to_project(project_id), belonging_to_group(project_id), shared])
union = Gitlab::SQL::Union.new([project_runners, group_runners, shared_runners])
from("(#{union.to_sql}) ci_runners") from("(#{union.to_sql}) ci_runners")
end end

View File

@ -49,7 +49,23 @@ describe Ci::Runner do
end end
end end
describe '.owned_or_shared' do describe '.shared' do
it 'returns the shared group runner' do
group = create :group
runner = create :ci_runner, :shared, groups: [group]
expect(described_class.shared).to eq [runner]
end
it 'returns the shared project runner' do
project = create :project
runner = create :ci_runner, :shared, projects: [project]
expect(described_class.shared).to eq [runner]
end
end
describe '.belonging_to_project' do
it 'returns the specific project runner' do it 'returns the specific project runner' do
# own # own
specific_project = create :project specific_project = create :project
@ -59,16 +75,11 @@ describe Ci::Runner do
other_project = create :project other_project = create :project
create :ci_runner, :specific, projects: [other_project] create :ci_runner, :specific, projects: [other_project]
expect(described_class.owned_or_shared(specific_project.id)).to eq [specific_runner] expect(described_class.belonging_to_project(specific_project.id)).to eq [specific_runner]
end
it 'returns the shared project runner' do
project = create :project
runner = create :ci_runner, :shared, projects: [project]
expect(described_class.owned_or_shared(0)).to eq [runner]
end end
end
describe '.belonging_to_group' do
it 'returns the specific group runner' do it 'returns the specific group runner' do
# own # own
specific_group = create :group specific_group = create :group
@ -80,7 +91,7 @@ describe Ci::Runner do
create :project, group: other_group create :project, group: other_group
create :ci_runner, :specific, groups: [other_group] create :ci_runner, :specific, groups: [other_group]
expect(described_class.owned_or_shared(specific_project.id)).to eq [specific_runner] expect(described_class.belonging_to_group(specific_project.id)).to eq [specific_runner]
end end
it 'does not return the group runner if the project has group runners disabled' do it 'does not return the group runner if the project has group runners disabled' do
@ -88,16 +99,11 @@ describe Ci::Runner do
specific_project = create :project, group: specific_group, group_runners_enabled: false specific_project = create :project, group: specific_group, group_runners_enabled: false
create :ci_runner, :specific, groups: [specific_group] create :ci_runner, :specific, groups: [specific_group]
expect(described_class.owned_or_shared(specific_project.id)).to be_empty expect(described_class.belonging_to_group(specific_project.id)).to be_empty
end
it 'returns the shared group runner' do
group = create :group
runner = create :ci_runner, :shared, groups: [group]
expect(described_class.owned_or_shared(0)).to eq [runner]
end end
end
describe '.owned_or_shared' do
it 'returns a globally shared, a project specific and a group specific runner' do it 'returns a globally shared, a project specific and a group specific runner' do
# group specific # group specific
group = create :group group = create :group