diff --git a/lib/gitlab/client/pipelines.rb b/lib/gitlab/client/pipelines.rb index 6a4f19880..0738b3bb4 100644 --- a/lib/gitlab/client/pipelines.rb +++ b/lib/gitlab/client/pipelines.rb @@ -31,6 +31,21 @@ def pipeline(project, id) get("/projects/#{url_encode project}/pipelines/#{id}") end + # Gets the latest pipeline for the most recent commit on a specific ref. + # + # @example + # Gitlab.latest_pipeline(5) + # Gitlab.latest_pipeline(5, ref: 'develop') + # + # @param [Integer, String] project The ID or name of a project. + # @param [Hash] options A customizable set of options. + # @option options [String] :ref The branch or tag to check for the latest pipeline. Defaults to the project's default branch. + # @return [Gitlab::ObjectifiedHash] + # @see https://docs.gitlab.com/ee/api/pipelines.html#get-the-latest-pipeline + def latest_pipeline(project, options = {}) + get("/projects/#{url_encode project}/pipelines/latest", query: options) + end + # Gets a single pipeline's test report. # # @example diff --git a/spec/fixtures/latest_pipeline.json b/spec/fixtures/latest_pipeline.json new file mode 100644 index 000000000..e1824c0d0 --- /dev/null +++ b/spec/fixtures/latest_pipeline.json @@ -0,0 +1 @@ +{"id":48,"status":"success","ref":"main","sha":"b91957a858320c0e17f3a0eca7cfacbff50ea29b","before_sha":"a91957a858320c0e17f3a0eca7cfacbff50ea29a","tag":false,"yaml_errors":null,"user":{"name":"Administrator","username":"root","id":1,"state":"active","avatar_url":"http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon","web_url":"http://localhost:3000/root"},"created_at":"2024-04-23T11:28:34.085Z","updated_at":"2024-04-23T11:32:35.169Z","started_at":null,"finished_at":"2024-04-23T11:32:35.145Z","committed_at":null,"duration":null,"web_url":"http://localhost:3000/root/test-project/-/pipelines/48"} diff --git a/spec/gitlab/client/pipelines_spec.rb b/spec/gitlab/client/pipelines_spec.rb index b82c2b688..d8b0eb492 100644 --- a/spec/gitlab/client/pipelines_spec.rb +++ b/spec/gitlab/client/pipelines_spec.rb @@ -38,6 +38,39 @@ end end + describe '.latest_pipeline' do + context 'without a ref option' do + before do + stub_get('/projects/3/pipelines/latest', 'latest_pipeline') + @pipeline = Gitlab.latest_pipeline(3) + end + + it 'gets the correct resource' do + expect(a_get('/projects/3/pipelines/latest')).to have_been_made + end + + it 'returns a single pipeline' do + expect(@pipeline).to be_a Gitlab::ObjectifiedHash + end + + it 'returns information about the latest pipeline' do + expect(@pipeline.id).to eq(48) + expect(@pipeline.ref).to eq('main') + end + end + + context 'with a ref option' do + before do + stub_get('/projects/3/pipelines/latest', 'latest_pipeline').with(query: { ref: 'develop' }) + @pipeline = Gitlab.latest_pipeline(3, ref: 'develop') + end + + it 'forwards the ref to the latest pipeline endpoint' do + expect(a_get('/projects/3/pipelines/latest').with(query: { ref: 'develop' })).to have_been_made + end + end + end + describe '.pipeline_test_report' do before do stub_get('/projects/3/pipelines/46/test_report', 'pipeline_test_report')