Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/gitlab/client/pipelines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/latest_pipeline.json
Original file line number Diff line number Diff line change
@@ -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"}
33 changes: 33 additions & 0 deletions spec/gitlab/client/pipelines_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Loading