diff --git a/app/finders/pipelines_finder.rb b/app/finders/pipelines_finder.rb index f187a3b61fefb9c73c2ff996f1662260ddf9f985..0a487839aff761670b4730163f0f4b00ce3a55fa 100644 --- a/app/finders/pipelines_finder.rb +++ b/app/finders/pipelines_finder.rb @@ -14,6 +14,7 @@ class PipelinesFinder items = by_scope(items) items = by_status(items) items = by_ref(items) + items = by_sha(items) items = by_name(items) items = by_username(items) items = by_yaml_errors(items) @@ -69,6 +70,14 @@ class PipelinesFinder end end + def by_sha(items) + if params[:sha].present? + items.where(sha: params[:sha]) + else + items + end + end + def by_name(items) if params[:name].present? items.joins(:user).where(users: { name: params[:name] }) diff --git a/changelogs/unreleased/jramsay-44880-filter-pipelines-by-sha.yml b/changelogs/unreleased/jramsay-44880-filter-pipelines-by-sha.yml new file mode 100644 index 0000000000000000000000000000000000000000..3654aa28ff4d20e1daac9411a6bf7fded739279b --- /dev/null +++ b/changelogs/unreleased/jramsay-44880-filter-pipelines-by-sha.yml @@ -0,0 +1,5 @@ +--- +title: Add sha filter to pipelines list API +merge_request: 18125 +author: +type: changed diff --git a/doc/api/pipelines.md b/doc/api/pipelines.md index a6631cab8c320f2f2a5026577bda3920448a6719..899f5da664799fa786e841ad3e9dea5817f81cbd 100644 --- a/doc/api/pipelines.md +++ b/doc/api/pipelines.md @@ -14,6 +14,7 @@ GET /projects/:id/pipelines | `scope` | string | no | The scope of pipelines, one of: `running`, `pending`, `finished`, `branches`, `tags` | | `status` | string | no | The status of pipelines, one of: `running`, `pending`, `success`, `failed`, `canceled`, `skipped` | | `ref` | string | no | The ref of pipelines | +| `sha` | string | no | The sha or pipelines | | `yaml_errors`| boolean | no | Returns pipelines with invalid configurations | | `name`| string | no | The name of the user who triggered pipelines | | `username`| string | no | The username of the user who triggered pipelines | diff --git a/lib/api/pipelines.rb b/lib/api/pipelines.rb index d2b8b832e4e4b9f763224369981aafa709f5d1a0..735591fedd532a10a3d96c2b825cc47ff3a04662 100644 --- a/lib/api/pipelines.rb +++ b/lib/api/pipelines.rb @@ -19,6 +19,7 @@ module API optional :status, type: String, values: HasStatus::AVAILABLE_STATUSES, desc: 'The status of pipelines' optional :ref, type: String, desc: 'The ref of pipelines' + optional :sha, type: String, desc: 'The sha of pipelines' optional :yaml_errors, type: Boolean, desc: 'Returns pipelines with invalid configurations' optional :name, type: String, desc: 'The name of the user who triggered pipelines' optional :username, type: String, desc: 'The username of the user who triggered pipelines' diff --git a/spec/finders/pipelines_finder_spec.rb b/spec/finders/pipelines_finder_spec.rb index 2b19cda35b06186ed0dc9907959443081339bd58..d6253b605b90ec77773e5753a0e0f35a9fdce542 100644 --- a/spec/finders/pipelines_finder_spec.rb +++ b/spec/finders/pipelines_finder_spec.rb @@ -203,5 +203,25 @@ describe PipelinesFinder do end end end + + context 'when sha is specified' do + let!(:pipeline) { create(:ci_pipeline, project: project, sha: '97de212e80737a608d939f648d959671fb0a0142') } + + context 'when sha exists' do + let(:params) { { sha: '97de212e80737a608d939f648d959671fb0a0142' } } + + it 'returns matched pipelines' do + is_expected.to eq([pipeline]) + end + end + + context 'when sha does not exist' do + let(:params) { { sha: 'invalid-sha' } } + + it 'returns empty' do + is_expected.to be_empty + end + end + end end end