include group runners in scope

This commit is contained in:
Alexis Reigel 2017-03-01 17:19:12 +01:00 committed by Alexis Reigel
parent 9a07bc819f
commit 295184f6a5
No known key found for this signature in database
GPG key ID: 55ADA7C7B683B329
2 changed files with 75 additions and 6 deletions

View file

@ -14,6 +14,8 @@ module Ci
has_many :builds
has_many :runner_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_many :projects, through: :runner_projects
has_many :runner_groups
has_many :groups, through: :runner_groups
has_one :last_build, ->() { order('id DESC') }, class_name: 'Ci::Build'
@ -27,8 +29,33 @@ module Ci
scope :ordered, ->() { order(id: :desc) }
scope :owned_or_shared, ->(project_id) do
joins('LEFT JOIN ci_runner_projects ON ci_runner_projects.runner_id = ci_runners.id')
.where("ci_runner_projects.project_id = :project_id OR ci_runners.is_shared = true", project_id: project_id)
joins(
%{
-- project runners
LEFT JOIN ci_runner_projects ON ci_runner_projects.runner_id = ci_runners.id
-- group runners
LEFT JOIN ci_runner_groups ON ci_runner_groups.runner_id = ci_runners.id
LEFT JOIN namespaces ON namespaces.id = ci_runner_groups.group_id
LEFT JOIN projects group_projects ON group_projects.namespace_id = namespaces.id
}
).where(
%{
-- project runners
ci_runner_projects.project_id = :project_id
OR
-- group runners
group_projects.id = :project_id
OR
-- shared runners
ci_runners.is_shared = true
},
project_id: project_id
)
end
scope :assignable_for, ->(project) do

View file

@ -52,19 +52,61 @@ describe Ci::Runner do
describe 'scopes' do
describe 'owned_or_shared' do
it 'returns the specific project runner' do
# own
specific_project = create :project
other_project = create :project
specific_runner = create :ci_runner, :specific, projects: [specific_project]
other_runner = create :ci_runner, :specific, projects: [other_project]
# other
other_project = create :project
create :ci_runner, :specific, projects: [other_project]
expect(described_class.owned_or_shared(specific_project.id)).to eq [specific_runner]
end
it 'returns the shared projects' do
runner = create :ci_runner, :shared
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
it 'returns the specific group runner' do
# own
specific_group = create :group
specific_project = create :project, group: specific_group
specific_runner = create :ci_runner, :specific, groups: [specific_group]
# other
other_group = create :group
create :project, group: other_group
create :ci_runner, :specific, groups: [other_group]
expect(described_class.owned_or_shared(specific_project.id)).to eq [specific_runner]
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
it 'returns a globally shared, a project specific and a group specific runner' do
# group specific
group = create :group
project = create :project, group: group
group_runner = create :ci_runner, :specific, groups: [group]
# project specific
project_runner = create :ci_runner, :specific, projects: [project]
# globally shared
shared_runner = create :ci_runner, :shared
expect(described_class.owned_or_shared(project.id)).to match_array [
group_runner, project_runner, shared_runner
]
end
end
end