notifications.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package gitlab
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/url"
  6. )
  7. // NotificationSettingsService handles communication with the notification settings
  8. // related methods of the GitLab API.
  9. //
  10. // GitLab API docs: https://docs.gitlab.com/ce/api/notification_settings.html
  11. type NotificationSettingsService struct {
  12. client *Client
  13. }
  14. // NotificationSettings represents the Gitlab notification setting.
  15. //
  16. // GitLab API docs:
  17. // https://docs.gitlab.com/ce/api/notification_settings.html#notification-settings
  18. type NotificationSettings struct {
  19. Level NotificationLevelValue `json:"level"`
  20. NotificationEmail string `json:"notification_email"`
  21. Events *NotificationEvents `json:"events"`
  22. }
  23. // NotificationEvents represents the available notification setting events.
  24. //
  25. // GitLab API docs:
  26. // https://docs.gitlab.com/ce/api/notification_settings.html#notification-settings
  27. type NotificationEvents struct {
  28. CloseIssue bool `json:"close_issue"`
  29. CloseMergeRequest bool `json:"close_merge_request"`
  30. FailedPipeline bool `json:"failed_pipeline"`
  31. MergeMergeRequest bool `json:"merge_merge_request"`
  32. NewIssue bool `json:"new_issue"`
  33. NewMergeRequest bool `json:"new_merge_request"`
  34. NewNote bool `json:"new_note"`
  35. ReassignIssue bool `json:"reassign_issue"`
  36. ReassignMergeRequest bool `json:"reassign_merge_request"`
  37. ReopenIssue bool `json:"reopen_issue"`
  38. ReopenMergeRequest bool `json:"reopen_merge_request"`
  39. SuccessPipeline bool `json:"success_pipeline"`
  40. }
  41. func (ns NotificationSettings) String() string {
  42. return Stringify(ns)
  43. }
  44. // GetGlobalSettings returns current notification settings and email address.
  45. //
  46. // GitLab API docs:
  47. // https://docs.gitlab.com/ce/api/notification_settings.html#global-notification-settings
  48. func (s *NotificationSettingsService) GetGlobalSettings(options ...OptionFunc) (*NotificationSettings, *Response, error) {
  49. u := "notification_settings"
  50. req, err := s.client.NewRequest("GET", u, nil, options)
  51. if err != nil {
  52. return nil, nil, err
  53. }
  54. ns := new(NotificationSettings)
  55. resp, err := s.client.Do(req, ns)
  56. if err != nil {
  57. return nil, resp, err
  58. }
  59. return ns, resp, err
  60. }
  61. // NotificationSettingsOptions represents the available options that can be passed
  62. // to the API when updating the notification settings.
  63. type NotificationSettingsOptions struct {
  64. Level *NotificationLevelValue `url:"level,omitempty" json:"level,omitempty"`
  65. NotificationEmail *string `url:"notification_email,omitempty" json:"notification_email,omitempty"`
  66. CloseIssue *bool `url:"close_issue,omitempty" json:"close_issue,omitempty"`
  67. CloseMergeRequest *bool `url:"close_merge_request,omitempty" json:"close_merge_request,omitempty"`
  68. FailedPipeline *bool `url:"failed_pipeline,omitempty" json:"failed_pipeline,omitempty"`
  69. MergeMergeRequest *bool `url:"merge_merge_request,omitempty" json:"merge_merge_request,omitempty"`
  70. NewIssue *bool `url:"new_issue,omitempty" json:"new_issue,omitempty"`
  71. NewMergeRequest *bool `url:"new_merge_request,omitempty" json:"new_merge_request,omitempty"`
  72. NewNote *bool `url:"new_note,omitempty" json:"new_note,omitempty"`
  73. ReassignIssue *bool `url:"reassign_issue,omitempty" json:"reassign_issue,omitempty"`
  74. ReassignMergeRequest *bool `url:"reassign_merge_request,omitempty" json:"reassign_merge_request,omitempty"`
  75. ReopenIssue *bool `url:"reopen_issue,omitempty" json:"reopen_issue,omitempty"`
  76. ReopenMergeRequest *bool `url:"reopen_merge_request,omitempty" json:"reopen_merge_request,omitempty"`
  77. SuccessPipeline *bool `url:"success_pipeline,omitempty" json:"success_pipeline,omitempty"`
  78. }
  79. // UpdateGlobalSettings updates current notification settings and email address.
  80. //
  81. // GitLab API docs:
  82. // https://docs.gitlab.com/ce/api/notification_settings.html#update-global-notification-settings
  83. func (s *NotificationSettingsService) UpdateGlobalSettings(opt *NotificationSettingsOptions, options ...OptionFunc) (*NotificationSettings, *Response, error) {
  84. if opt.Level != nil && *opt.Level == GlobalNotificationLevel {
  85. return nil, nil, errors.New(
  86. "notification level 'global' is not valid for global notification settings")
  87. }
  88. u := "notification_settings"
  89. req, err := s.client.NewRequest("PUT", u, opt, options)
  90. if err != nil {
  91. return nil, nil, err
  92. }
  93. ns := new(NotificationSettings)
  94. resp, err := s.client.Do(req, ns)
  95. if err != nil {
  96. return nil, resp, err
  97. }
  98. return ns, resp, err
  99. }
  100. // GetSettingsForGroup returns current group notification settings.
  101. //
  102. // GitLab API docs:
  103. // https://docs.gitlab.com/ce/api/notification_settings.html#group-project-level-notification-settings
  104. func (s *NotificationSettingsService) GetSettingsForGroup(gid interface{}, options ...OptionFunc) (*NotificationSettings, *Response, error) {
  105. group, err := parseID(gid)
  106. if err != nil {
  107. return nil, nil, err
  108. }
  109. u := fmt.Sprintf("groups/%s/notification_settings", url.QueryEscape(group))
  110. req, err := s.client.NewRequest("GET", u, nil, options)
  111. if err != nil {
  112. return nil, nil, err
  113. }
  114. ns := new(NotificationSettings)
  115. resp, err := s.client.Do(req, ns)
  116. if err != nil {
  117. return nil, resp, err
  118. }
  119. return ns, resp, err
  120. }
  121. // GetSettingsForProject returns current project notification settings.
  122. //
  123. // GitLab API docs:
  124. // https://docs.gitlab.com/ce/api/notification_settings.html#group-project-level-notification-settings
  125. func (s *NotificationSettingsService) GetSettingsForProject(pid interface{}, options ...OptionFunc) (*NotificationSettings, *Response, error) {
  126. project, err := parseID(pid)
  127. if err != nil {
  128. return nil, nil, err
  129. }
  130. u := fmt.Sprintf("projects/%s/notification_settings", url.QueryEscape(project))
  131. req, err := s.client.NewRequest("GET", u, nil, options)
  132. if err != nil {
  133. return nil, nil, err
  134. }
  135. ns := new(NotificationSettings)
  136. resp, err := s.client.Do(req, ns)
  137. if err != nil {
  138. return nil, resp, err
  139. }
  140. return ns, resp, err
  141. }
  142. // UpdateSettingsForGroup updates current group notification settings.
  143. //
  144. // GitLab API docs:
  145. // https://docs.gitlab.com/ce/api/notification_settings.html#update-group-project-level-notification-settings
  146. func (s *NotificationSettingsService) UpdateSettingsForGroup(gid interface{}, opt *NotificationSettingsOptions, options ...OptionFunc) (*NotificationSettings, *Response, error) {
  147. group, err := parseID(gid)
  148. if err != nil {
  149. return nil, nil, err
  150. }
  151. u := fmt.Sprintf("groups/%s/notification_settings", url.QueryEscape(group))
  152. req, err := s.client.NewRequest("PUT", u, opt, options)
  153. if err != nil {
  154. return nil, nil, err
  155. }
  156. ns := new(NotificationSettings)
  157. resp, err := s.client.Do(req, ns)
  158. if err != nil {
  159. return nil, resp, err
  160. }
  161. return ns, resp, err
  162. }
  163. // UpdateSettingsForProject updates current project notification settings.
  164. //
  165. // GitLab API docs:
  166. // https://docs.gitlab.com/ce/api/notification_settings.html#update-group-project-level-notification-settings
  167. func (s *NotificationSettingsService) UpdateSettingsForProject(pid interface{}, opt *NotificationSettingsOptions, options ...OptionFunc) (*NotificationSettings, *Response, error) {
  168. project, err := parseID(pid)
  169. if err != nil {
  170. return nil, nil, err
  171. }
  172. u := fmt.Sprintf("projects/%s/notification_settings", url.QueryEscape(project))
  173. req, err := s.client.NewRequest("PUT", u, opt, options)
  174. if err != nil {
  175. return nil, nil, err
  176. }
  177. ns := new(NotificationSettings)
  178. resp, err := s.client.Do(req, ns)
  179. if err != nil {
  180. return nil, resp, err
  181. }
  182. return ns, resp, err
  183. }