Repository: xanzy/go-gitlab
Pull request #1596 (2022-12-11 01:59:07)

New GetLatestPipeline() client method
Added a client method for https://docs.gitlab.com/ee/api/pipelines.html#get-the-latest-pipeline


Patch:
diff --git a/pipelines.go b/pipelines.go
index a26697c..d0380ed 100644
--- a/pipelines.go
+++ b/pipelines.go
@@ -264,6 +264,37 @@ func (s *PipelinesService) GetPipelineTestReport(pid interface{}, pipeline int,
 	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"`
+}
+
+// GetLatestPipeline 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
+}
+
 // CreatePipelineOptions represents the available CreatePipeline() options.
 //
 // 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
 // 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 {


Test patch:
diff --git a/pipelines_test.go b/pipelines_test.go
index 0807e02..6d77cea 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) {
@@ -186,6 +188,40 @@ func TestGetPipelineTestReport(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 TestCreatePipeline(t *testing.T) {
 	mux, server, client := setup(t)
 	defer teardown(server)