system_hooks.go

  1//
  2// Copyright 2017, Sander van Harmelen
  3//
  4// Licensed under the Apache License, Version 2.0 (the "License");
  5// you may not use this file except in compliance with the License.
  6// You may obtain a copy of the License at
  7//
  8//     http://www.apache.org/licenses/LICENSE-2.0
  9//
 10// Unless required by applicable law or agreed to in writing, software
 11// distributed under the License is distributed on an "AS IS" BASIS,
 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13// See the License for the specific language governing permissions and
 14// limitations under the License.
 15//
 16
 17package gitlab
 18
 19import (
 20	"fmt"
 21	"time"
 22)
 23
 24// SystemHooksService handles communication with the system hooks related
 25// methods of the GitLab API.
 26//
 27// GitLab API docs: https://docs.gitlab.com/ce/api/system_hooks.html
 28type SystemHooksService struct {
 29	client *Client
 30}
 31
 32// Hook represents a GitLap system hook.
 33//
 34// GitLab API docs: https://docs.gitlab.com/ce/api/system_hooks.html
 35type Hook struct {
 36	ID        int        `json:"id"`
 37	URL       string     `json:"url"`
 38	CreatedAt *time.Time `json:"created_at"`
 39}
 40
 41func (h Hook) String() string {
 42	return Stringify(h)
 43}
 44
 45// ListHooks gets a list of system hooks.
 46//
 47// GitLab API docs:
 48// https://docs.gitlab.com/ce/api/system_hooks.html#list-system-hooks
 49func (s *SystemHooksService) ListHooks(options ...OptionFunc) ([]*Hook, *Response, error) {
 50	req, err := s.client.NewRequest("GET", "hooks", nil, options)
 51	if err != nil {
 52		return nil, nil, err
 53	}
 54
 55	var h []*Hook
 56	resp, err := s.client.Do(req, &h)
 57	if err != nil {
 58		return nil, resp, err
 59	}
 60
 61	return h, resp, err
 62}
 63
 64// AddHookOptions represents the available AddHook() options.
 65//
 66// GitLab API docs:
 67// https://docs.gitlab.com/ce/api/system_hooks.html#add-new-system-hook-hook
 68type AddHookOptions struct {
 69	URL *string `url:"url,omitempty" json:"url,omitempty"`
 70}
 71
 72// AddHook adds a new system hook hook.
 73//
 74// GitLab API docs:
 75// https://docs.gitlab.com/ce/api/system_hooks.html#add-new-system-hook-hook
 76func (s *SystemHooksService) AddHook(opt *AddHookOptions, options ...OptionFunc) (*Hook, *Response, error) {
 77	req, err := s.client.NewRequest("POST", "hooks", opt, options)
 78	if err != nil {
 79		return nil, nil, err
 80	}
 81
 82	h := new(Hook)
 83	resp, err := s.client.Do(req, h)
 84	if err != nil {
 85		return nil, resp, err
 86	}
 87
 88	return h, resp, err
 89}
 90
 91// HookEvent represents an event trigger by a GitLab system hook.
 92//
 93// GitLab API docs: https://docs.gitlab.com/ce/api/system_hooks.html
 94type HookEvent struct {
 95	EventName  string `json:"event_name"`
 96	Name       string `json:"name"`
 97	Path       string `json:"path"`
 98	ProjectID  int    `json:"project_id"`
 99	OwnerName  string `json:"owner_name"`
100	OwnerEmail string `json:"owner_email"`
101}
102
103func (h HookEvent) String() string {
104	return Stringify(h)
105}
106
107// TestHook tests a system hook.
108//
109// GitLab API docs:
110// https://docs.gitlab.com/ce/api/system_hooks.html#test-system-hook
111func (s *SystemHooksService) TestHook(hook int, options ...OptionFunc) (*HookEvent, *Response, error) {
112	u := fmt.Sprintf("hooks/%d", hook)
113
114	req, err := s.client.NewRequest("GET", u, nil, options)
115	if err != nil {
116		return nil, nil, err
117	}
118
119	h := new(HookEvent)
120	resp, err := s.client.Do(req, h)
121	if err != nil {
122		return nil, resp, err
123	}
124
125	return h, resp, err
126}
127
128// DeleteHook deletes a system hook. This is an idempotent API function and
129// returns 200 OK even if the hook is not available. If the hook is deleted it
130// is also returned as JSON.
131//
132// GitLab API docs:
133// https://docs.gitlab.com/ce/api/system_hooks.html#delete-system-hook
134func (s *SystemHooksService) DeleteHook(hook int, options ...OptionFunc) (*Response, error) {
135	u := fmt.Sprintf("hooks/%d", hook)
136
137	req, err := s.client.NewRequest("DELETE", u, nil, options)
138	if err != nil {
139		return nil, err
140	}
141
142	return s.client.Do(req, nil)
143}