diff options
-rw-r--r-- | app/finders/runner_jobs_finder.rb | 22 | ||||
-rw-r--r-- | lib/api/runners.rb | 8 | ||||
-rw-r--r-- | spec/finders/runner_jobs_finder_spec.rb | 39 | ||||
-rw-r--r-- | spec/requests/api/runners_spec.rb | 8 |
4 files changed, 67 insertions, 10 deletions
diff --git a/app/finders/runner_jobs_finder.rb b/app/finders/runner_jobs_finder.rb new file mode 100644 index 00000000000..52340f94523 --- /dev/null +++ b/app/finders/runner_jobs_finder.rb @@ -0,0 +1,22 @@ +class RunnerJobsFinder + attr_reader :runner, :params + + def initialize(runner, params = {}) + @runner = runner + @params = params + end + + def execute + items = @runner.builds + items = by_status(items) + items + end + + private + + def by_status(items) + return items unless HasStatus::AVAILABLE_STATUSES.include?(params[:status]) + + items.where(status: params[:status]) + end +end diff --git a/lib/api/runners.rb b/lib/api/runners.rb index 3ea7340d9be..996457c5dfe 100644 --- a/lib/api/runners.rb +++ b/lib/api/runners.rb @@ -90,18 +90,14 @@ module API end params do requires :id, type: Integer, desc: 'The ID of the runner' - optional :status, type: String, desc: 'Status of job' + optional :status, type: String, desc: 'Status of the job', values: Ci::Build::AVAILABLE_STATUSES use :pagination end get ':id/jobs' do runner = get_runner(params[:id]) authenticate_list_runners_jobs!(runner) - jobs = runner.builds - if params[:status] - not_found!('Status') unless Ci::Build::AVAILABLE_STATUSES.include?(params[:status]) - jobs = jobs.where(status: params[:status].to_sym) - end + jobs = RunnerJobsFinder.new(runner, params).execute present paginate(jobs), with: Entities::JobBasicWithProject end diff --git a/spec/finders/runner_jobs_finder_spec.rb b/spec/finders/runner_jobs_finder_spec.rb new file mode 100644 index 00000000000..4275b1a7ff1 --- /dev/null +++ b/spec/finders/runner_jobs_finder_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +describe RunnerJobsFinder do + let(:project) { create(:project) } + let(:runner) { create(:ci_runner, :shared) } + + subject { described_class.new(runner, params).execute } + + describe '#execute' do + context 'when params is empty' do + let(:params) { {} } + let!(:job) { create(:ci_build, runner: runner, project: project) } + let!(:job1) { create(:ci_build, project: project) } + + it 'returns all jobs assigned to Runner' do + is_expected.to match_array(job) + is_expected.not_to match_array(job1) + end + end + + context 'when params contains status' do + HasStatus::AVAILABLE_STATUSES.each do |target_status| + context "when status is #{target_status}" do + let(:params) { { status: target_status } } + let!(:job) { create(:ci_build, runner: runner, project: project, status: target_status) } + + before do + exception_status = HasStatus::AVAILABLE_STATUSES - [target_status] + create(:ci_build, runner: runner, project: project, status: exception_status.first) + end + + it 'returns matched job' do + is_expected.to eq([job]) + end + end + end + end + end +end diff --git a/spec/requests/api/runners_spec.rb b/spec/requests/api/runners_spec.rb index 80a8495808d..ec5cad4f4fd 100644 --- a/spec/requests/api/runners_spec.rb +++ b/spec/requests/api/runners_spec.rb @@ -401,10 +401,10 @@ describe API::Runners do end context 'when invalid status is provided' do - it 'return 404' do + it 'return 400' do get api("/runners/#{specific_runner.id}/jobs?status=non-existing", admin) - expect(response).to have_gitlab_http_status(404) + expect(response).to have_gitlab_http_status(400) end end end @@ -454,10 +454,10 @@ describe API::Runners do end context 'when invalid status is provided' do - it 'return 404' do + it 'return 400' do get api("/runners/#{specific_runner.id}/jobs?status=non-existing", user) - expect(response).to have_gitlab_http_status(404) + expect(response).to have_gitlab_http_status(400) end end end |