Skip to content

Commit 0eeb4be

Browse files
committed
Introduced Ci::Runner.specific_for for getting specific runners:
for a particular project.
1 parent 4f7f325 commit 0eeb4be

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

app/controllers/projects/runners_controller.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ class Projects::RunnersController < Projects::ApplicationController
77
def index
88
@runners = project.runners.ordered
99
@specific_runners = current_user.ci_authorized_runners.
10-
where.not(id: project.runners).
11-
ordered.page(params[:page]).per(20)
10+
specific_for(project).ordered.page(params[:page]).per(20)
1211
@shared_runners = Ci::Runner.shared.active
1312
@shared_runners_count = @shared_runners.count(:all)
1413
end

app/models/ci/runner.rb

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class Runner < ActiveRecord::Base
2626
.where("ci_runner_projects.gl_project_id = :project_id OR ci_runners.is_shared = true", project_id: project_id)
2727
end
2828

29+
scope :specific_for, ->(project) do
30+
where(locked: false).where.not(id: project.runners).specific
31+
end
32+
2933
validate :tag_constraints
3034

3135
acts_as_taggable

spec/models/ci/runner_spec.rb

+54
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,60 @@
112112
end
113113
end
114114

115+
describe :specific_for do
116+
let(:runner) { create(:ci_runner) }
117+
let(:project) { create(:project) }
118+
let(:another_project) { create(:project) }
119+
120+
before { project.runners << runner }
121+
122+
context 'with shared runners' do
123+
before { runner.update(is_shared: true) }
124+
125+
context 'should not give owned runner' do
126+
subject { Ci::Runner.specific_for(project) }
127+
128+
it { is_expected.to be_empty }
129+
end
130+
131+
context 'should not give shared runner' do
132+
subject { Ci::Runner.specific_for(another_project) }
133+
134+
it { is_expected.to be_empty }
135+
end
136+
end
137+
138+
context 'with unlocked runner' do
139+
context 'should not give owned runner' do
140+
subject { Ci::Runner.specific_for(project) }
141+
142+
it { is_expected.to be_empty }
143+
end
144+
145+
context 'should give a specific runner' do
146+
subject { Ci::Runner.specific_for(another_project) }
147+
148+
it { is_expected.to contain_exactly(runner) }
149+
end
150+
end
151+
152+
context 'with locked runner' do
153+
before { runner.update(locked: true) }
154+
155+
context 'should not give owned runner' do
156+
subject { Ci::Runner.specific_for(project) }
157+
158+
it { is_expected.to be_empty }
159+
end
160+
161+
context 'should not give a locked runner' do
162+
subject { Ci::Runner.specific_for(another_project) }
163+
164+
it { is_expected.to be_empty }
165+
end
166+
end
167+
end
168+
115169
describe "belongs_to_one_project?" do
116170
it "returns false if there are two projects runner assigned to" do
117171
runner = FactoryGirl.create(:ci_runner)

0 commit comments

Comments
 (0)