Commit 9b456c8c authored by Igor Varavko's avatar Igor Varavko Committed by Sander van Harmelen

Added support for project pipelines (#116)

parent c451eb16
...@@ -37,6 +37,7 @@ includes all calls to the following services: ...@@ -37,6 +37,7 @@ includes all calls to the following services:
- [x] Groups - [x] Groups
- [x] Namespaces - [x] Namespaces
- [x] Settings - [x] Settings
- [x] Pipelines
## Usage ## Usage
......
...@@ -129,6 +129,7 @@ type Client struct { ...@@ -129,6 +129,7 @@ type Client struct {
Notes *NotesService Notes *NotesService
Projects *ProjectsService Projects *ProjectsService
ProjectSnippets *ProjectSnippetsService ProjectSnippets *ProjectSnippetsService
Pipelines *PipelinesService
Repositories *RepositoriesService Repositories *RepositoriesService
RepositoryFiles *RepositoryFilesService RepositoryFiles *RepositoryFilesService
Services *ServicesService Services *ServicesService
...@@ -188,6 +189,7 @@ func newClient(httpClient *http.Client, tokenType tokenType, token string) *Clie ...@@ -188,6 +189,7 @@ func newClient(httpClient *http.Client, tokenType tokenType, token string) *Clie
c.Namespaces = &NamespacesService{client: c} c.Namespaces = &NamespacesService{client: c}
c.Projects = &ProjectsService{client: c} c.Projects = &ProjectsService{client: c}
c.ProjectSnippets = &ProjectSnippetsService{client: c} c.ProjectSnippets = &ProjectSnippetsService{client: c}
c.Pipelines = &PipelinesService{client: c}
c.Repositories = &RepositoriesService{client: c} c.Repositories = &RepositoriesService{client: c}
c.RepositoryFiles = &RepositoryFilesService{client: c} c.RepositoryFiles = &RepositoryFilesService{client: c}
c.Services = &ServicesService{client: c} c.Services = &ServicesService{client: c}
......
//
// Copyright 2017, Igor Varavko
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package gitlab
import (
"fmt"
"net/url"
"time"
)
// PipelinesService handles communication with the repositories related
// methods of the GitLab API.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html
type PipelinesService struct {
client *Client
}
// Pipeline represents a GitLab pipeline.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html
type Pipeline struct {
ID int `json:"id"`
Status string `json:"status"`
Ref string `json:"ref"`
Sha string `json:"sha"`
BeforeSha string `json:"before_sha"`
Tag bool `json:"tag"`
YamlErrors string `json:"yaml_errors"`
User struct {
Name string `json:"name"`
Username string `json:"username"`
ID int `json:"id"`
State string `json:"state"`
AvatarURL string `json:"avatar_url"`
WebUrl string `json:"web_url"`
}
UpdatedAt *time.Time `json:"updated_at"`
CreatedAt *time.Time `json:"created_at"`
StartedAt *time.Time `json:"started_at"`
FinishedAt *time.Time `json:"finished_at"`
CommittedAt *time.Time `json:"committed_at"`
Duration int `json:"duration"`
Coverage string `json:"coverage"`
}
func (i Pipeline) String() string {
return Stringify(i)
}
// ListProjectPipelines gets a list of project piplines.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#list-project-pipelines
func (s *PipelinesService) ListProjectPipelines(pid interface{}) ([]*Pipeline, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/pipelines", url.QueryEscape(project))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var p []*Pipeline
resp, err := s.client.Do(req, &p)
if err != nil {
return nil, resp, err
}
return p, resp, err
}
// GetPipeline gets a single project pipeline.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#get-a-single-pipeline
func (s *PipelinesService) GetPipeline(pid interface{}, pipeline int) (*Pipeline, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/pipelines/%d", url.QueryEscape(project), pipeline)
req, err := s.client.NewRequest("GET", u, nil)
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
type CreatePipelineOptions struct {
Ref *string `url:"ref,omitempty" json:"ref"`
}
// CreatePipeline creates a new project pipeline.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/pipelines.html#create-a-new-pipeline
func (s *PipelinesService) CreatePipeline(pid interface{}, opt *CreatePipelineOptions) (*Pipeline, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/pipeline", url.QueryEscape(project))
req, err := s.client.NewRequest("POST", u, opt)
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
}
// RetryPipelineBuild retries failed builds in a pipeline
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipelines.html#retry-failed-builds-in-a-pipeline
func (s *PipelinesService) RetryPipelineBuild(pid interface{}, pipelineID int) (*Pipeline, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/pipelines/%d/retry", project, pipelineID)
req, err := s.client.NewRequest("POST", u, nil)
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
}
// CancelPipelineBuild cancels a pipeline builds
//
// GitLab API docs:
//https://docs.gitlab.com/ce/api/pipelines.html#cancel-a-pipelines-builds
func (s *PipelinesService) CancelPipelineBuild(pid interface{}, pipelineID int) (*Pipeline, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/pipelines/%d/cancel", project, pipelineID)
req, err := s.client.NewRequest("POST", u, nil)
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
}
package gitlab
import (
"fmt"
"net/http"
"reflect"
"testing"
)
func TestListProjectPipelines(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/1/pipelines", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `[{"id":1},{"id":2}]`)
})
piplines, _, err := client.Pipelines.ListProjectPipelines(1)
if err != nil {
t.Errorf("Pipelines.ListProjectPipelines returned error: %v", err)
}
want := []*Pipeline{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(want, piplines) {
t.Errorf("Pipelines.ListProjectPipelines returned %+v, want %+v", piplines, want)
}
}
func TestGetPipeline(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/1/pipelines/5949167", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"id":1,"status":"success"}`)
})
pipeline, _, err := client.Pipelines.GetPipeline(1, 5949167)
if err != nil {
t.Errorf("Pipelines.GetPipeline returned error: %v", err)
}
want := &Pipeline{ID: 1, Status: "success"}
if !reflect.DeepEqual(want, pipeline) {
t.Errorf("Pipelines.GetPipeline returned %+v, want %+v", pipeline, want)
}
}
func TestCreatePipeline(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/1/pipeline", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
testJSONBody(t, r, values{"ref": "master"})
fmt.Fprint(w, `{"id":1, "status":"pending"}`)
})
opt := &CreatePipelineOptions{Ref: String("master")}
pipeline, _, err := client.Pipelines.CreatePipeline(1, opt)
if err != nil {
t.Errorf("Pipelines.CreatePipeline returned error: %v", err)
}
want := &Pipeline{ID: 1, Status: "pending"}
if !reflect.DeepEqual(want, pipeline) {
t.Errorf("Pipelines.CreatePipeline returned %+v, want %+v", pipeline, want)
}
}
func TestRetryPipelineBuild(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/1/pipelines/5949167/retry", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprintln(w, `{"id":1, "status":"pending"}`)
})
pipeline, _, err := client.Pipelines.RetryPipelineBuild(1, 5949167)
if err != nil {
t.Errorf("Pipelines.RetryPipelineBuild returned error: %v", err)
}
want := &Pipeline{ID: 1, Status: "pending"}
if !reflect.DeepEqual(want, pipeline) {
t.Errorf("Pipelines.RetryPipelineBuild returned %+v, want %+v", pipeline, want)
}
}
func TestCancelPipelineBuild(t *testing.T) {
mux, server, client := setup()
defer teardown(server)
mux.HandleFunc("/projects/1/pipelines/5949167/cancel", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprintln(w, `{"id":1, "status":"canceled"}`)
})
pipeline, _, err := client.Pipelines.CancelPipelineBuild(1, 5949167)
if err != nil {
t.Errorf("Pipelines.CancelPipelineBuild returned error: %v", err)
}
want := &Pipeline{ID: 1, Status: "canceled"}
if !reflect.DeepEqual(want, pipeline) {
t.Errorf("Pipelines.CancelPipelineBuild returned %+v, want %+v", pipeline, want)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment