Skip to content

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 b494915 commit a856572
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 60 deletions.
52 changes: 26 additions & 26 deletions pipelines.go
Expand Up @@ -216,77 +216,77 @@ 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.
// GetPipelineVariables gets the variables of a single project pipeline.
//
// 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) {
// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#get-variables-of-a-pipeline
func (s *PipelinesService) GetPipelineVariables(pid interface{}, pipeline int, options ...RequestOptionFunc) ([]*PipelineVariable, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/pipelines/latest", PathEscape(project))
u := fmt.Sprintf("projects/%s/pipelines/%d/variables", PathEscape(project), pipeline)

req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}

p := new(Pipeline)
resp, err := s.client.Do(req, p)
var p []*PipelineVariable
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.
// GetPipelineTestReport gets the test report of a single project pipeline.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#get-variables-of-a-pipeline
func (s *PipelinesService) GetPipelineVariables(pid interface{}, pipeline int, options ...RequestOptionFunc) ([]*PipelineVariable, *Response, error) {
// GitLab API docs: https://docs.gitlab.com/ee/api/pipelines.html#get-a-pipelines-test-report
func (s *PipelinesService) GetPipelineTestReport(pid interface{}, pipeline int, options ...RequestOptionFunc) (*PipelineTestReport, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/pipelines/%d/variables", PathEscape(project), pipeline)
u := fmt.Sprintf("projects/%s/pipelines/%d/test_report", PathEscape(project), pipeline)

req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}

var p []*PipelineVariable
resp, err := s.client.Do(req, &p)
p := new(PipelineTestReport)
resp, err := s.client.Do(req, p)
if err != nil {
return nil, resp, err
}

return p, resp, err
}

// GetPipelineTestReport gets the test report of a single project pipeline.
// GetLatestPipelineOptions represents the available GetLatestPipeline() options.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/pipelines.html#get-a-pipelines-test-report
func (s *PipelinesService) GetPipelineTestReport(pid interface{}, pipeline int, options ...RequestOptionFunc) (*PipelineTestReport, *Response, error) {
// 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/%d/test_report", PathEscape(project), pipeline)
u := fmt.Sprintf("projects/%s/pipelines/latest", PathEscape(project))

req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
if err != nil {
return nil, nil, err
}

p := new(PipelineTestReport)
p := new(Pipeline)
resp, err := s.client.Do(req, p)
if err != nil {
return nil, resp, err
Expand Down
68 changes: 34 additions & 34 deletions pipelines_test.go
Expand Up @@ -66,40 +66,6 @@ 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)
Expand Down Expand Up @@ -222,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 a856572

Please sign in to comment.