system_hooks.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. package gitlab
  17. import (
  18. "fmt"
  19. "time"
  20. )
  21. // SystemHooksService handles communication with the system hooks related
  22. // methods of the GitLab API.
  23. //
  24. // GitLab API docs: https://docs.gitlab.com/ce/api/system_hooks.html
  25. type SystemHooksService struct {
  26. client *Client
  27. }
  28. // Hook represents a GitLap system hook.
  29. //
  30. // GitLab API docs: https://docs.gitlab.com/ce/api/system_hooks.html
  31. type Hook struct {
  32. ID int `json:"id"`
  33. URL string `json:"url"`
  34. CreatedAt *time.Time `json:"created_at"`
  35. }
  36. func (h Hook) String() string {
  37. return Stringify(h)
  38. }
  39. // ListHooks gets a list of system hooks.
  40. //
  41. // GitLab API docs:
  42. // https://docs.gitlab.com/ce/api/system_hooks.html#list-system-hooks
  43. func (s *SystemHooksService) ListHooks(options ...OptionFunc) ([]*Hook, *Response, error) {
  44. req, err := s.client.NewRequest("GET", "hooks", nil, options)
  45. if err != nil {
  46. return nil, nil, err
  47. }
  48. var h []*Hook
  49. resp, err := s.client.Do(req, &h)
  50. if err != nil {
  51. return nil, resp, err
  52. }
  53. return h, resp, err
  54. }
  55. // AddHookOptions represents the available AddHook() options.
  56. //
  57. // GitLab API docs:
  58. // https://docs.gitlab.com/ce/api/system_hooks.html#add-new-system-hook-hook
  59. type AddHookOptions struct {
  60. URL *string `url:"url,omitempty" json:"url,omitempty"`
  61. }
  62. // AddHook adds a new system hook hook.
  63. //
  64. // GitLab API docs:
  65. // https://docs.gitlab.com/ce/api/system_hooks.html#add-new-system-hook-hook
  66. func (s *SystemHooksService) AddHook(opt *AddHookOptions, options ...OptionFunc) (*Hook, *Response, error) {
  67. req, err := s.client.NewRequest("POST", "hooks", opt, options)
  68. if err != nil {
  69. return nil, nil, err
  70. }
  71. h := new(Hook)
  72. resp, err := s.client.Do(req, h)
  73. if err != nil {
  74. return nil, resp, err
  75. }
  76. return h, resp, err
  77. }
  78. // HookEvent represents an event trigger by a GitLab system hook.
  79. //
  80. // GitLab API docs: https://docs.gitlab.com/ce/api/system_hooks.html
  81. type HookEvent struct {
  82. EventName string `json:"event_name"`
  83. Name string `json:"name"`
  84. Path string `json:"path"`
  85. ProjectID int `json:"project_id"`
  86. OwnerName string `json:"owner_name"`
  87. OwnerEmail string `json:"owner_email"`
  88. }
  89. func (h HookEvent) String() string {
  90. return Stringify(h)
  91. }
  92. // TestHook tests a system hook.
  93. //
  94. // GitLab API docs:
  95. // https://docs.gitlab.com/ce/api/system_hooks.html#test-system-hook
  96. func (s *SystemHooksService) TestHook(hook int, options ...OptionFunc) (*HookEvent, *Response, error) {
  97. u := fmt.Sprintf("hooks/%d", hook)
  98. req, err := s.client.NewRequest("GET", u, nil, options)
  99. if err != nil {
  100. return nil, nil, err
  101. }
  102. h := new(HookEvent)
  103. resp, err := s.client.Do(req, h)
  104. if err != nil {
  105. return nil, resp, err
  106. }
  107. return h, resp, err
  108. }
  109. // DeleteHook deletes a system hook. This is an idempotent API function and
  110. // returns 200 OK even if the hook is not available. If the hook is deleted it
  111. // is also returned as JSON.
  112. //
  113. // GitLab API docs:
  114. // https://docs.gitlab.com/ce/api/system_hooks.html#delete-system-hook
  115. func (s *SystemHooksService) DeleteHook(hook int, options ...OptionFunc) (*Response, error) {
  116. u := fmt.Sprintf("hooks/%d", hook)
  117. req, err := s.client.NewRequest("DELETE", u, nil, options)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return s.client.Do(req, nil)
  122. }