From b7ed102ea601fb4c6f65c5a982058f8c92883d31 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Tue, 21 Nov 2017 12:37:07 +0100 Subject: [PATCH] Allow filtering by 'status' --- lib/api/runners.rb | 9 +++++- spec/requests/api/runners_spec.rb | 50 ++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/lib/api/runners.rb b/lib/api/runners.rb index b92a2c36cf3..18f9f142580 100644 --- a/lib/api/runners.rb +++ b/lib/api/runners.rb @@ -90,13 +90,20 @@ module API end params do requires :id, type: Integer, desc: 'The ID of the runner' + optional :status, type: String, desc: 'Status of job' use :pagination end get ':id/jobs' do runner = get_runner(params[:id]) authenticate_list_runners_jobs!(runner) - present paginate(runner.builds.running), with: Entities::JobWithProject + 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 + + present paginate(jobs), with: Entities::JobWithProject end end diff --git a/spec/requests/api/runners_spec.rb b/spec/requests/api/runners_spec.rb index d68fa13af1e..1ffbb06803f 100644 --- a/spec/requests/api/runners_spec.rb +++ b/spec/requests/api/runners_spec.rb @@ -370,8 +370,8 @@ describe API::Runners do expect(response).to have_gitlab_http_status(200) expect(response).to include_pagination_headers - expect(json_response.length).to eq(1) - expect(json_response[0]).to include('id' => job_2.id) + expect(json_response).to be_an(Array) + expect(json_response.length).to eq(2) end end @@ -382,8 +382,29 @@ describe API::Runners do expect(response).to have_gitlab_http_status(200) expect(response).to include_pagination_headers + expect(json_response).to be_an(Array) + expect(json_response.length).to eq(2) + end + end + + context 'when valid status is provided' do + it 'return filtered jobs' do + get api("/runners/#{specific_runner.id}/jobs?status=failed", admin) + + expect(response).to have_gitlab_http_status(200) + expect(response).to include_pagination_headers + + expect(json_response).to be_an(Array) expect(json_response.length).to eq(1) - expect(json_response[0]).to include('id' => job_4.id) + expect(json_response.first).to include('id' => job_5.id) + end + end + + context 'when invalid status is provided' do + it 'return 404' do + get api("/runners/#{specific_runner.id}/jobs?status=non-existing", admin) + + expect(response).to have_gitlab_http_status(404) end end end @@ -414,8 +435,29 @@ describe API::Runners do expect(response).to have_gitlab_http_status(200) expect(response).to include_pagination_headers + expect(json_response).to be_an(Array) + expect(json_response.length).to eq(2) + end + end + + context 'when valid status is provided' do + it 'return filtered jobs' do + get api("/runners/#{specific_runner.id}/jobs?status=failed", user) + + expect(response).to have_gitlab_http_status(200) + expect(response).to include_pagination_headers + + expect(json_response).to be_an(Array) expect(json_response.length).to eq(1) - expect(json_response[0]).to include('id' => job_4.id) + expect(json_response.first).to include('id' => job_5.id) + end + end + + context 'when invalid status is provided' do + it 'return 404' do + get api("/runners/#{specific_runner.id}/jobs?status=non-existing", user) + + expect(response).to have_gitlab_http_status(404) end end end