Commit 7907e757 authored by Balasankar "Balu" C's avatar Balasankar "Balu" C

Add ability to filter pipelines by source in API

Allow users to pass `source` as a query parameter to the Pipelines API
endpoints so that only pipelines that were started because of the
specified soruce are returned.
Signed-off-by: default avatarBalasankar "Balu" C <balasankar@gitlab.com>
parent a2a08751
......@@ -23,6 +23,7 @@ module Ci
items = by_iids(items)
items = by_scope(items)
items = by_status(items)
items = by_source(items)
items = by_ref(items)
items = by_sha(items)
items = by_name(items)
......@@ -87,6 +88,12 @@ module Ci
end
# rubocop: enable CodeReuse/ActiveRecord
def by_source(items)
return items unless ::Ci::Pipeline.sources.key?(params[:source])
items.with_pipeline_source(params[:source])
end
# rubocop: disable CodeReuse/ActiveRecord
def by_ref(items)
if params[:ref].present?
......
......@@ -318,6 +318,7 @@ module Ci
scope :created_before_id, -> (id) { where('ci_pipelines.id < ?', id) }
scope :before_pipeline, -> (pipeline) { created_before_id(pipeline.id).outside_pipeline_family(pipeline) }
scope :eager_load_project, -> { eager_load(project: [:route, { namespace: :route }]) }
scope :with_pipeline_source, -> (source) { where(source: source)}
scope :outside_pipeline_family, ->(pipeline) do
where.not(id: pipeline.same_family_pipeline_ids)
......
......@@ -52,6 +52,7 @@ module API
desc: 'Order pipelines'
optional :sort, type: String, values: %w[asc desc], default: 'desc',
desc: 'Sort pipelines'
optional :source, type: String, values: ::Ci::Pipeline.sources.keys
end
get ':id/pipelines' do
authorize! :read_pipeline, user_project
......
......@@ -252,6 +252,20 @@ RSpec.describe Ci::PipelinesFinder do
end
end
context "when source is specified" do
let(:params) { { source: 'web' } }
let!(:pipeline) { create(:ci_pipeline, project: project, source: 'web') }
before do
create(:ci_pipeline, project: project, source: 'push')
create(:ci_pipeline, project: project, source: 'chat')
end
it 'returns matched pipelines' do
is_expected.to eq([pipeline])
end
end
describe 'ordering' do
using RSpec::Parameterized::TableSyntax
......
......@@ -263,6 +263,20 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do
end
end
describe '.with_pipeline_source' do
subject { described_class.with_pipeline_source(source) }
let(:source) { 'web' }
let_it_be(:push_pipeline) { create(:ci_pipeline, source: :push) }
let_it_be(:web_pipeline) { create(:ci_pipeline, source: :web) }
let_it_be(:api_pipeline) { create(:ci_pipeline, source: :api) }
it 'contains pipelines created due to specified source' do
expect(subject).to contain_exactly(web_pipeline)
end
end
describe '.ci_sources' do
subject { described_class.ci_sources }
......
......@@ -294,6 +294,31 @@ RSpec.describe API::Ci::Pipelines do
end
end
end
context 'when a source is specified' do
before do
create(:ci_pipeline, project: project, source: :push)
create(:ci_pipeline, project: project, source: :web)
create(:ci_pipeline, project: project, source: :api)
end
it 'returns matched pipelines' do
get api("/projects/#{project.id}/pipelines", user), params: { source: 'web' }
expect(response).to have_gitlab_http_status(:ok)
expect(response).to include_pagination_headers
expect(json_response).not_to be_empty
json_response.each { |r| expect(r['source']).to eq('web') }
end
end
context 'when source is invalid' do
it 'returns bad_request' do
get api("/projects/#{project.id}/pipelines", user), params: { source: 'invalid-source' }
expect(response).to have_gitlab_http_status(:bad_request)
end
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment