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

Commit

Permalink
New GetLatestPipeline() client method
Browse files Browse the repository at this point in the history
  • Loading branch information
armsnyder committed Dec 11, 2022
1 parent 05207f5 commit 91b78c1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
33 changes: 32 additions & 1 deletion pipelines.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions pipelines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"net/http"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestListProjectPipelines(t *testing.T) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 91b78c1

Please sign in to comment.