Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Commit 837fee3

Browse files
authored
Merge pull request #1596 from armsnyder/get-latest-pipeline
New GetLatestPipeline() client method
2 parents 05207f5 + 91b78c1 commit 837fee3

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

pipelines.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,37 @@ func (s *PipelinesService) GetPipelineTestReport(pid interface{}, pipeline int,
264264
return p, resp, err
265265
}
266266

267+
// GetLatestPipelineOptions represents the available GetLatestPipeline() options.
268+
//
269+
// GitLab API docs: https://docs.gitlab.com/ee/api/pipelines.html#get-the-latest-pipeline
270+
type GetLatestPipelineOptions struct {
271+
Ref *string `url:"ref,omitempty" json:"ref,omitempty"`
272+
}
273+
274+
// GetLatestPipeline gets the latest pipeline for a specific ref in a project.
275+
//
276+
// GitLab API docs: https://docs.gitlab.com/ee/api/pipelines.html#get-the-latest-pipeline
277+
func (s *PipelinesService) GetLatestPipeline(pid interface{}, opt *GetLatestPipelineOptions, options ...RequestOptionFunc) (*Pipeline, *Response, error) {
278+
project, err := parseID(pid)
279+
if err != nil {
280+
return nil, nil, err
281+
}
282+
u := fmt.Sprintf("projects/%s/pipelines/latest", PathEscape(project))
283+
284+
req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
285+
if err != nil {
286+
return nil, nil, err
287+
}
288+
289+
p := new(Pipeline)
290+
resp, err := s.client.Do(req, p)
291+
if err != nil {
292+
return nil, resp, err
293+
}
294+
295+
return p, resp, err
296+
}
297+
267298
// CreatePipelineOptions represents the available CreatePipeline() options.
268299
//
269300
// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#create-a-new-pipeline
@@ -333,7 +364,7 @@ func (s *PipelinesService) RetryPipelineBuild(pid interface{}, pipeline int, opt
333364
// CancelPipelineBuild cancels a pipeline builds
334365
//
335366
// GitLab API docs:
336-
//https://docs.gitlab.com/ce/api/pipelines.html#cancel-a-pipelines-builds
367+
// https://docs.gitlab.com/ce/api/pipelines.html#cancel-a-pipelines-builds
337368
func (s *PipelinesService) CancelPipelineBuild(pid interface{}, pipeline int, options ...RequestOptionFunc) (*Pipeline, *Response, error) {
338369
project, err := parseID(pid)
339370
if err != nil {

pipelines_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"net/http"
2222
"reflect"
2323
"testing"
24+
25+
"github.com/stretchr/testify/assert"
2426
)
2527

2628
func TestListProjectPipelines(t *testing.T) {
@@ -186,6 +188,40 @@ func TestGetPipelineTestReport(t *testing.T) {
186188
}
187189
}
188190

191+
func TestGetLatestPipeline(t *testing.T) {
192+
mux, server, client := setup(t)
193+
defer teardown(server)
194+
195+
mux.HandleFunc("/api/v4/projects/1/pipelines/latest", func(w http.ResponseWriter, r *http.Request) {
196+
testMethod(t, r, http.MethodGet)
197+
testParams(t, r, "")
198+
fmt.Fprint(w, `{"id":1,"status":"success"}`)
199+
})
200+
201+
pipeline, _, err := client.Pipelines.GetLatestPipeline(1, nil)
202+
203+
assert.NoError(t, err)
204+
assert.Equal(t, &Pipeline{ID: 1, Status: "success"}, pipeline)
205+
}
206+
207+
func TestGetLatestPipeline_WithRef(t *testing.T) {
208+
mux, server, client := setup(t)
209+
defer teardown(server)
210+
211+
mux.HandleFunc("/api/v4/projects/1/pipelines/latest", func(w http.ResponseWriter, r *http.Request) {
212+
testMethod(t, r, http.MethodGet)
213+
testParams(t, r, "ref=abc")
214+
fmt.Fprint(w, `{"id":1,"status":"success"}`)
215+
})
216+
217+
pipeline, _, err := client.Pipelines.GetLatestPipeline(1, &GetLatestPipelineOptions{
218+
Ref: String("abc"),
219+
})
220+
221+
assert.NoError(t, err)
222+
assert.Equal(t, &Pipeline{ID: 1, Status: "success"}, pipeline)
223+
}
224+
189225
func TestCreatePipeline(t *testing.T) {
190226
mux, server, client := setup(t)
191227
defer teardown(server)

0 commit comments

Comments
 (0)