...

Source file src/github.com/xanzy/go-gitlab/system_hooks.go

Documentation: github.com/xanzy/go-gitlab

     1  //
     2  // Copyright 2021, 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  
    17  package gitlab
    18  
    19  import (
    20  	"fmt"
    21  	"net/http"
    22  	"time"
    23  )
    24  
    25  // SystemHooksService handles communication with the system hooks related
    26  // methods of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/system_hooks.html
    29  type SystemHooksService struct {
    30  	client *Client
    31  }
    32  
    33  // Hook represents a GitLap system hook.
    34  //
    35  // GitLab API docs: https://docs.gitlab.com/ee/api/system_hooks.html
    36  type Hook struct {
    37  	ID                     int        `json:"id"`
    38  	URL                    string     `json:"url"`
    39  	CreatedAt              *time.Time `json:"created_at"`
    40  	PushEvents             bool       `json:"push_events"`
    41  	TagPushEvents          bool       `json:"tag_push_events"`
    42  	MergeRequestsEvents    bool       `json:"merge_requests_events"`
    43  	RepositoryUpdateEvents bool       `json:"repository_update_events"`
    44  	EnableSSLVerification  bool       `json:"enable_ssl_verification"`
    45  }
    46  
    47  func (h Hook) String() string {
    48  	return Stringify(h)
    49  }
    50  
    51  // ListHooks gets a list of system hooks.
    52  //
    53  // GitLab API docs:
    54  // https://docs.gitlab.com/ee/api/system_hooks.html#list-system-hooks
    55  func (s *SystemHooksService) ListHooks(options ...RequestOptionFunc) ([]*Hook, *Response, error) {
    56  	req, err := s.client.NewRequest(http.MethodGet, "hooks", nil, options)
    57  	if err != nil {
    58  		return nil, nil, err
    59  	}
    60  
    61  	var h []*Hook
    62  	resp, err := s.client.Do(req, &h)
    63  	if err != nil {
    64  		return nil, resp, err
    65  	}
    66  
    67  	return h, resp, nil
    68  }
    69  
    70  // GetHook get a single system hook.
    71  //
    72  // GitLab API docs:
    73  // https://docs.gitlab.com/ee/api/system_hooks.html#get-system-hook
    74  func (s *SystemHooksService) GetHook(hook int, options ...RequestOptionFunc) (*Hook, *Response, error) {
    75  	u := fmt.Sprintf("hooks/%d", hook)
    76  
    77  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    78  	if err != nil {
    79  		return nil, nil, err
    80  	}
    81  
    82  	var h *Hook
    83  	resp, err := s.client.Do(req, &h)
    84  	if err != nil {
    85  		return nil, resp, err
    86  	}
    87  
    88  	return h, resp, nil
    89  }
    90  
    91  // AddHookOptions represents the available AddHook() options.
    92  //
    93  // GitLab API docs:
    94  // https://docs.gitlab.com/ee/api/system_hooks.html#add-new-system-hook
    95  type AddHookOptions struct {
    96  	URL                    *string `url:"url,omitempty" json:"url,omitempty"`
    97  	Token                  *string `url:"token,omitempty" json:"token,omitempty"`
    98  	PushEvents             *bool   `url:"push_events,omitempty" json:"push_events,omitempty"`
    99  	TagPushEvents          *bool   `url:"tag_push_events,omitempty" json:"tag_push_events,omitempty"`
   100  	MergeRequestsEvents    *bool   `url:"merge_requests_events,omitempty" json:"merge_requests_events,omitempty"`
   101  	RepositoryUpdateEvents *bool   `url:"repository_update_events,omitempty" json:"repository_update_events,omitempty"`
   102  	EnableSSLVerification  *bool   `url:"enable_ssl_verification,omitempty" json:"enable_ssl_verification,omitempty"`
   103  }
   104  
   105  // AddHook adds a new system hook hook.
   106  //
   107  // GitLab API docs:
   108  // https://docs.gitlab.com/ee/api/system_hooks.html#add-new-system-hook
   109  func (s *SystemHooksService) AddHook(opt *AddHookOptions, options ...RequestOptionFunc) (*Hook, *Response, error) {
   110  	req, err := s.client.NewRequest(http.MethodPost, "hooks", opt, options)
   111  	if err != nil {
   112  		return nil, nil, err
   113  	}
   114  
   115  	h := new(Hook)
   116  	resp, err := s.client.Do(req, h)
   117  	if err != nil {
   118  		return nil, resp, err
   119  	}
   120  
   121  	return h, resp, nil
   122  }
   123  
   124  // HookEvent represents an event trigger by a GitLab system hook.
   125  //
   126  // GitLab API docs: https://docs.gitlab.com/ee/api/system_hooks.html
   127  type HookEvent struct {
   128  	EventName  string `json:"event_name"`
   129  	Name       string `json:"name"`
   130  	Path       string `json:"path"`
   131  	ProjectID  int    `json:"project_id"`
   132  	OwnerName  string `json:"owner_name"`
   133  	OwnerEmail string `json:"owner_email"`
   134  }
   135  
   136  func (h HookEvent) String() string {
   137  	return Stringify(h)
   138  }
   139  
   140  // TestHook tests a system hook.
   141  //
   142  // GitLab API docs:
   143  // https://docs.gitlab.com/ee/api/system_hooks.html#test-system-hook
   144  func (s *SystemHooksService) TestHook(hook int, options ...RequestOptionFunc) (*HookEvent, *Response, error) {
   145  	u := fmt.Sprintf("hooks/%d", hook)
   146  
   147  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   148  	if err != nil {
   149  		return nil, nil, err
   150  	}
   151  
   152  	h := new(HookEvent)
   153  	resp, err := s.client.Do(req, h)
   154  	if err != nil {
   155  		return nil, resp, err
   156  	}
   157  
   158  	return h, resp, nil
   159  }
   160  
   161  // DeleteHook deletes a system hook. This is an idempotent API function and
   162  // returns 200 OK even if the hook is not available. If the hook is deleted it
   163  // is also returned as JSON.
   164  //
   165  // GitLab API docs:
   166  // https://docs.gitlab.com/ee/api/system_hooks.html#delete-system-hook
   167  func (s *SystemHooksService) DeleteHook(hook int, options ...RequestOptionFunc) (*Response, error) {
   168  	u := fmt.Sprintf("hooks/%d", hook)
   169  
   170  	req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  
   175  	return s.client.Do(req, nil)
   176  }
   177  

View as plain text