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