From b494915f437b301d24c2d42035dc9a03ed9af7e5 Mon Sep 17 00:00:00 2001 From: Adam Snyder Date: Sat, 10 Dec 2022 17:57:16 -0800 Subject: [PATCH] New GetLatestPipeline() client method --- pipelines.go | 33 ++++++++++++++++++++++++++++++++- pipelines_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/pipelines.go b/pipelines.go index a26697c1f..7fd32ee36 100644 --- a/pipelines.go +++ b/pipelines.go @@ -216,6 +216,37 @@ func (s *PipelinesService) GetPipeline(pid interface{}, pipeline int, options .. return p, resp, err } +// GetLatestPipelineOptions represents the available GetLatestPipeline() options. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/pipelines.html#get-the-latest-pipeline +type GetLatestPipelineOptions struct { + Ref *string `url:"ref,omitempty" json:"ref,omitempty"` +} + +// GetPipeline gets the latest pipeline for a specific ref in a project. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/pipelines.html#get-the-latest-pipeline +func (s *PipelinesService) GetLatestPipeline(pid interface{}, opt *GetLatestPipelineOptions, options ...RequestOptionFunc) (*Pipeline, *Response, error) { + project, err := parseID(pid) + if err != nil { + return nil, nil, err + } + u := fmt.Sprintf("projects/%s/pipelines/latest", PathEscape(project)) + + req, err := s.client.NewRequest(http.MethodGet, u, opt, options) + if err != nil { + return nil, nil, err + } + + p := new(Pipeline) + resp, err := s.client.Do(req, p) + if err != nil { + return nil, resp, err + } + + return p, resp, err +} + // GetPipelineVariables gets the variables of a single project pipeline. // // GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#get-variables-of-a-pipeline @@ -333,7 +364,7 @@ func (s *PipelinesService) RetryPipelineBuild(pid interface{}, pipeline int, opt // CancelPipelineBuild cancels a pipeline builds // // GitLab API docs: -//https://docs.gitlab.com/ce/api/pipelines.html#cancel-a-pipelines-builds +// https://docs.gitlab.com/ce/api/pipelines.html#cancel-a-pipelines-builds func (s *PipelinesService) CancelPipelineBuild(pid interface{}, pipeline int, options ...RequestOptionFunc) (*Pipeline, *Response, error) { project, err := parseID(pid) if err != nil { diff --git a/pipelines_test.go b/pipelines_test.go index 0807e0294..1794f5e2f 100644 --- a/pipelines_test.go +++ b/pipelines_test.go @@ -21,6 +21,8 @@ import ( "net/http" "reflect" "testing" + + "github.com/stretchr/testify/assert" ) func TestListProjectPipelines(t *testing.T) { @@ -64,6 +66,40 @@ func TestGetPipeline(t *testing.T) { } } +func TestGetLatestPipeline(t *testing.T) { + mux, server, client := setup(t) + defer teardown(server) + + mux.HandleFunc("/api/v4/projects/1/pipelines/latest", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + testParams(t, r, "") + fmt.Fprint(w, `{"id":1,"status":"success"}`) + }) + + pipeline, _, err := client.Pipelines.GetLatestPipeline(1, nil) + + assert.NoError(t, err) + assert.Equal(t, &Pipeline{ID: 1, Status: "success"}, pipeline) +} + +func TestGetLatestPipeline_WithRef(t *testing.T) { + mux, server, client := setup(t) + defer teardown(server) + + mux.HandleFunc("/api/v4/projects/1/pipelines/latest", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + testParams(t, r, "ref=abc") + fmt.Fprint(w, `{"id":1,"status":"success"}`) + }) + + pipeline, _, err := client.Pipelines.GetLatestPipeline(1, &GetLatestPipelineOptions{ + Ref: String("abc"), + }) + + assert.NoError(t, err) + assert.Equal(t, &Pipeline{ID: 1, Status: "success"}, pipeline) +} + func TestGetPipelineVariables(t *testing.T) { mux, server, client := setup(t) defer teardown(server)