Commit 1c2ae800 authored by Johan Brandhorst's avatar Johan Brandhorst Committed by Sander van Harmelen

Change Build Triggers to PipelinTriggers. (#152)

* Add StatusNoContent as successful status.

See documentation for expected return statuses: https://docs.gitlab.com/ce/api/README.html#status-codes

Also change the codes to http library codes.

* Change Build Triggers to PipelinTriggers.

This is in line with the updated v4 API. See https://docs.gitlab.com/ce/api/pipeline_triggers.html and https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/9713 for more information.

* Move Pipeline Triggers to a separate file.

Creates the new service PipelineTriggersService for handling requests to the Pipeline Triggers part of the GitLab API.
parent 005950e6
......@@ -183,6 +183,7 @@ type Client struct {
Projects *ProjectsService
ProjectSnippets *ProjectSnippetsService
Pipelines *PipelinesService
PipelineTriggers *PipelineTriggersService
Repositories *RepositoriesService
RepositoryFiles *RepositoryFilesService
Services *ServicesService
......@@ -246,6 +247,7 @@ func newClient(httpClient *http.Client, tokenType tokenType, token string) *Clie
c.Projects = &ProjectsService{client: c}
c.ProjectSnippets = &ProjectSnippetsService{client: c}
c.Pipelines = &PipelinesService{client: c}
c.PipelineTriggers = &PipelineTriggersService{client: c}
c.Repositories = &RepositoriesService{client: c}
c.RepositoryFiles = &RepositoryFilesService{client: c}
c.Services = &ServicesService{client: c}
......@@ -472,7 +474,7 @@ func (e *ErrorResponse) Error() string {
// CheckResponse checks the API response for errors, and returns them if present.
func CheckResponse(r *http.Response) error {
switch r.StatusCode {
case 200, 201, 304:
case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusNotModified:
return nil
}
......
package gitlab
import (
"fmt"
"net/url"
"time"
)
// PipelineTriggersService handles Project pipeline triggers.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipeline_triggers.html
type PipelineTriggersService struct {
client *Client
}
// PipelineTrigger represents a project pipeline trigger.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipeline_triggers.html#pipeline-triggers
type PipelineTrigger struct {
ID int `json:"id"`
Description string `json:"description"`
CreatedAt *time.Time `json:"created_at"`
DeletedAt *time.Time `json:"deleted_at"`
LastUsed *time.Time `json:"last_used"`
Token string `json:"token"`
UpdatedAt *time.Time `json:"updated_at"`
Owner *User `json:"owner"`
}
// ListPipelineTriggersOptions represents the available ListPipelineTriggers() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipeline_triggers.html#list-project-triggers
type ListPipelineTriggersOptions struct {
ListOptions
}
// ListPipelineTriggers gets a list of project triggers.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipeline_triggers.html#list-project-triggers
func (s *PipelineTriggersService) ListPipelineTriggers(pid interface{}, opt *ListPipelineTriggersOptions, options ...OptionFunc) ([]*PipelineTrigger, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/triggers", url.QueryEscape(project))
req, err := s.client.NewRequest("GET", u, opt, options)
if err != nil {
return nil, nil, err
}
var pt []*PipelineTrigger
resp, err := s.client.Do(req, &pt)
if err != nil {
return nil, resp, err
}
return pt, resp, err
}
// GetPipelineTrigger gets a specific pipeline trigger for a project.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipeline_triggers.html#get-trigger-details
func (s *PipelineTriggersService) GetPipelineTrigger(pid interface{}, trigger int, options ...OptionFunc) (*PipelineTrigger, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/triggers/%d", url.QueryEscape(project), trigger)
req, err := s.client.NewRequest("GET", u, nil, options)
if err != nil {
return nil, nil, err
}
pt := new(PipelineTrigger)
resp, err := s.client.Do(req, pt)
if err != nil {
return nil, resp, err
}
return pt, resp, err
}
// AddPipelineTriggerOptions represents the available AddPipelineTrigger() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipeline_triggers.html#create-a-project-trigger
type AddPipelineTriggerOptions struct {
Description *string `url:"description,omitempty" json:"description,omitempty"`
}
// AddPipelineTrigger adds a pipeline trigger to a specified project.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipeline_triggers.html#create-a-project-trigger
func (s *PipelineTriggersService) AddPipelineTrigger(pid interface{}, opt *AddPipelineTriggerOptions, options ...OptionFunc) (*PipelineTrigger, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/triggers", url.QueryEscape(project))
req, err := s.client.NewRequest("POST", u, opt, options)
if err != nil {
return nil, nil, err
}
pt := new(PipelineTrigger)
resp, err := s.client.Do(req, pt)
if err != nil {
return nil, resp, err
}
return pt, resp, err
}
// EditPipelineTriggerOptions represents the available EditPipelineTrigger() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipeline_triggers.html#update-a-project-trigger
type EditPipelineTriggerOptions struct {
Description *string `url:"description,omitempty" json:"description,omitempty"`
}
// EditPipelineTrigger edits a trigger for a specified project.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipeline_triggers.html#update-a-project-trigger
func (s *PipelineTriggersService) EditPipelineTrigger(pid interface{}, trigger int, opt *EditPipelineTriggerOptions, options ...OptionFunc) (*PipelineTrigger, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/triggers/%d", url.QueryEscape(project), trigger)
req, err := s.client.NewRequest("PUT", u, opt, options)
if err != nil {
return nil, nil, err
}
pt := new(PipelineTrigger)
resp, err := s.client.Do(req, pt)
if err != nil {
return nil, resp, err
}
return pt, resp, err
}
// TakeOwnershipOfPipelineTrigger sets the owner of the specified
// pipeline trigger to the user issuing the request.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipeline_triggers.html#take-ownership-of-a-project-trigger
func (s *PipelineTriggersService) TakeOwnershipOfPipelineTrigger(pid interface{}, trigger int, options ...OptionFunc) (*PipelineTrigger, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/triggers/%d/take_ownership", url.QueryEscape(project), trigger)
req, err := s.client.NewRequest("POST", u, nil, options)
if err != nil {
return nil, nil, err
}
pt := new(PipelineTrigger)
resp, err := s.client.Do(req, pt)
if err != nil {
return nil, resp, err
}
return pt, resp, err
}
// DeletePipelineTrigger removes a trigger from a project.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/pipeline_triggers.html#remove-a-project-trigger
func (s *PipelineTriggersService) DeletePipelineTrigger(pid interface{}, trigger int, options ...OptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("projects/%s/triggers/%d", url.QueryEscape(project), trigger)
req, err := s.client.NewRequest("DELETE", u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
......@@ -913,120 +913,6 @@ func (s *ProjectsService) DeleteProjectHook(pid interface{}, hook int, options .
return s.client.Do(req, nil)
}
// BuildTrigger represents a project build trigger.
//
// GitLab API docs:
// https://gitlab.com/gitlab-org/gitlab-ce/blob/8-16-stable/doc/api/build_triggers.md#build-triggers
type BuildTrigger struct {
CreatedAt *time.Time `json:"created_at"`
DeletedAt *time.Time `json:"deleted_at"`
LastUsed *time.Time `json:"last_used"`
Token string `json:"token"`
UpdatedAt *time.Time `json:"updated_at"`
}
// ListBuildTriggersOptions represents the available ListBuildTriggers() options.
//
// GitLab API docs:
// https://gitlab.com/gitlab-org/gitlab-ce/blob/8-16-stable/doc/api/build_triggers.md#list-project-triggers
type ListBuildTriggersOptions struct {
ListOptions
}
// ListBuildTriggers gets a list of project triggers.
//
// GitLab API docs:
// https://gitlab.com/gitlab-org/gitlab-ce/blob/8-16-stable/doc/api/build_triggers.md#list-project-triggers
func (s *ProjectsService) ListBuildTriggers(pid interface{}, opt *ListBuildTriggersOptions, options ...OptionFunc) ([]*BuildTrigger, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/triggers", url.QueryEscape(project))
req, err := s.client.NewRequest("GET", u, opt, options)
if err != nil {
return nil, nil, err
}
var bt []*BuildTrigger
resp, err := s.client.Do(req, &bt)
if err != nil {
return nil, resp, err
}
return bt, resp, err
}
// GetBuildTrigger gets a specific build trigger for a project.
//
// GitLab API docs:
// https://gitlab.com/gitlab-org/gitlab-ce/blob/8-16-stable/doc/api/build_triggers.md#get-trigger-details
func (s *ProjectsService) GetBuildTrigger(pid interface{}, token string, options ...OptionFunc) (*BuildTrigger, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/triggers/%v", url.QueryEscape(project), token)
req, err := s.client.NewRequest("GET", u, nil, options)
if err != nil {
return nil, nil, err
}
bt := new(BuildTrigger)
resp, err := s.client.Do(req, bt)
if err != nil {
return nil, resp, err
}
return bt, resp, err
}
// AddBuildTrigger adds a build trigger to a specified project.
//
// GitLab API docs:
// https://gitlab.com/gitlab-org/gitlab-ce/blob/8-16-stable/doc/api/build_triggers.md#create-a-project-trigger
func (s *ProjectsService) AddBuildTrigger(pid interface{}, options ...OptionFunc) (*BuildTrigger, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/triggers", url.QueryEscape(project))
req, err := s.client.NewRequest("POST", u, nil, options)
if err != nil {
return nil, nil, err
}
bt := new(BuildTrigger)
resp, err := s.client.Do(req, bt)
if err != nil {
return nil, resp, err
}
return bt, resp, err
}
// DeleteBuildTrigger removes a trigger from a project.
//
// GitLab API docs:
// https://gitlab.com/gitlab-org/gitlab-ce/blob/8-16-stable/doc/api/build_triggers.md#remove-a-project-trigger
func (s *ProjectsService) DeleteBuildTrigger(pid interface{}, token string, options ...OptionFunc) (*Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("projects/%s/triggers/%s", url.QueryEscape(project), token)
req, err := s.client.NewRequest("DELETE", u, nil, options)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
}
// ProjectForkRelation represents a project fork relationship.
//
// GitLab API docs:
......
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