Skip to content

Commit 13a902a

Browse files
committed
Refactorize jobs finding logic
1 parent 9d27ce1 commit 13a902a

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

app/finders/runner_jobs_finder.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class RunnerJobsFinder
2+
attr_reader :runner, :params
3+
4+
def initialize(runner, params = {})
5+
@runner = runner
6+
@params = params
7+
end
8+
9+
def execute
10+
items = @runner.builds
11+
items = by_status(items)
12+
items
13+
end
14+
15+
private
16+
17+
def by_status(items)
18+
return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status])
19+
20+
items.where(status: params[:status])
21+
end
22+
end

lib/api/runners.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,14 @@ class Runners < Grape::API
9090
end
9191
params do
9292
requires :id, type: Integer, desc: 'The ID of the runner'
93-
optional :status, type: String, desc: 'Status of job'
93+
optional :status, type: String, desc: 'Status of the job', values: Ci::Build::AVAILABLE_STATUSES
9494
use :pagination
9595
end
9696
get ':id/jobs' do
9797
runner = get_runner(params[:id])
9898
authenticate_list_runners_jobs!(runner)
9999

100-
jobs = runner.builds
101-
if params[:status]
102-
not_found!('Status') unless Ci::Build::AVAILABLE_STATUSES.include?(params[:status])
103-
jobs = jobs.where(status: params[:status].to_sym)
104-
end
100+
jobs = RunnerJobsFinder.new(runner, params).execute
105101

106102
present paginate(jobs), with: Entities::JobBasicWithProject
107103
end
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require 'spec_helper'
2+
3+
describe RunnerJobsFinder do
4+
let(:project) { create(:project) }
5+
let(:runner) { create(:ci_runner, :shared) }
6+
7+
subject { described_class.new(runner, params).execute }
8+
9+
describe '#execute' do
10+
context 'when params is empty' do
11+
let(:params) { {} }
12+
let!(:job) { create(:ci_build, runner: runner, project: project) }
13+
let!(:job1) { create(:ci_build, project: project) }
14+
15+
it 'returns all jobs assigned to Runner' do
16+
is_expected.to match_array(job)
17+
is_expected.not_to match_array(job1)
18+
end
19+
end
20+
21+
context 'when params contains status' do
22+
HasStatus::AVAILABLE_STATUSES.each do |target_status|
23+
context "when status is #{target_status}" do
24+
let(:params) { { status: target_status } }
25+
let!(:job) { create(:ci_build, runner: runner, project: project, status: target_status) }
26+
27+
before do
28+
exception_status = HasStatus::AVAILABLE_STATUSES - [target_status]
29+
create(:ci_build, runner: runner, project: project, status: exception_status.first)
30+
end
31+
32+
it 'returns matched job' do
33+
is_expected.to eq([job])
34+
end
35+
end
36+
end
37+
end
38+
end
39+
end

spec/requests/api/runners_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,10 @@ def update_runner(id, user, args)
401401
end
402402

403403
context 'when invalid status is provided' do
404-
it 'return 404' do
404+
it 'return 400' do
405405
get api("/runners/#{specific_runner.id}/jobs?status=non-existing", admin)
406406

407-
expect(response).to have_gitlab_http_status(404)
407+
expect(response).to have_gitlab_http_status(400)
408408
end
409409
end
410410
end
@@ -454,10 +454,10 @@ def update_runner(id, user, args)
454454
end
455455

456456
context 'when invalid status is provided' do
457-
it 'return 404' do
457+
it 'return 400' do
458458
get api("/runners/#{specific_runner.id}/jobs?status=non-existing", user)
459459

460-
expect(response).to have_gitlab_http_status(404)
460+
expect(response).to have_gitlab_http_status(400)
461461
end
462462
end
463463
end

0 commit comments

Comments
 (0)