Commit a163d9a4 authored by Sander van Harmelen's avatar Sander van Harmelen Committed by GitHub

Add notification settings API (#130)

* Added support for notification settings API

* Refactor the notification settings API
parent a688802c
...@@ -127,6 +127,7 @@ type Client struct { ...@@ -127,6 +127,7 @@ type Client struct {
Milestones *MilestonesService Milestones *MilestonesService
Namespaces *NamespacesService Namespaces *NamespacesService
Notes *NotesService Notes *NotesService
NotificationSettings *NotificationSettingsService
Projects *ProjectsService Projects *ProjectsService
ProjectSnippets *ProjectSnippetsService ProjectSnippets *ProjectSnippetsService
Pipelines *PipelinesService Pipelines *PipelinesService
...@@ -186,8 +187,9 @@ func newClient(httpClient *http.Client, tokenType tokenType, token string) *Clie ...@@ -186,8 +187,9 @@ func newClient(httpClient *http.Client, tokenType tokenType, token string) *Clie
c.Labels = &LabelsService{client: c} c.Labels = &LabelsService{client: c}
c.MergeRequests = &MergeRequestsService{client: c} c.MergeRequests = &MergeRequestsService{client: c}
c.Milestones = &MilestonesService{client: c} c.Milestones = &MilestonesService{client: c}
c.Notes = &NotesService{client: c}
c.Namespaces = &NamespacesService{client: c} c.Namespaces = &NamespacesService{client: c}
c.Notes = &NotesService{client: c}
c.NotificationSettings = &NotificationSettingsService{client: c}
c.Projects = &ProjectsService{client: c} c.Projects = &ProjectsService{client: c}
c.ProjectSnippets = &ProjectSnippetsService{client: c} c.ProjectSnippets = &ProjectSnippetsService{client: c}
c.Pipelines = &PipelinesService{client: c} c.Pipelines = &PipelinesService{client: c}
......
package gitlab
import (
"errors"
"fmt"
"net/url"
)
// NotificationSettingsService handles communication with the notification settings
// related methods of the GitLab API.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/notification_settings.html
type NotificationSettingsService struct {
client *Client
}
// NotificationLevel represents a notification level.
type NotificationLevel string
// List of valid notification levels.
const (
CustomNotificationLevel NotificationLevel = "custom"
DisabledNotificationLevel NotificationLevel = "disabled"
GlobalNotificationLevel NotificationLevel = "global"
MentionNotificationLevel NotificationLevel = "mention"
ParticipatingNotificationLevel NotificationLevel = "participating"
WatchNotificationLevel NotificationLevel = "watch"
)
// NotificationSettings represents the Gitlab notification setting.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/notification_settings.html#notification-settings
type NotificationSettings struct {
Level NotificationLevel `json:"level"`
NotificationEmail string `json:"notification_email"`
Events *NotificationEvents `json:"events"`
}
// NotificationEvents represents the avialable notification setting events.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/notification_settings.html#notification-settings
type NotificationEvents struct {
CloseIssue bool `json:"close_issue"`
CloseMergeRequest bool `json:"close_merge_request"`
FailedPipeline bool `json:"failed_pipeline"`
MergeMergeRequest bool `json:"merge_merge_request"`
NewIssue bool `json:"new_issue"`
NewMergeRequest bool `json:"new_merge_request"`
NewNote bool `json:"new_note"`
ReassignIssue bool `json:"reassign_issue"`
ReassignMergeRequest bool `json:"reassign_merge_request"`
ReopenIssue bool `json:"reopen_issue"`
ReopenMergeRequest bool `json:"reopen_merge_request"`
SuccessPipeline bool `json:"success_pipeline"`
}
func (ns NotificationSettings) String() string {
return Stringify(ns)
}
// GetGlobalSettings returns current notification settings and email address.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/notification_settings.html#global-notification-settings
func (s *NotificationSettingsService) GetGlobalSettings() (*NotificationSettings, *Response, error) {
u := "notification_settings"
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
ns := new(NotificationSettings)
resp, err := s.client.Do(req, ns)
if err != nil {
return nil, resp, err
}
return ns, resp, err
}
// NotificationSettingsOptions represents the available options that can be passed
// to the API when updating the notification settings.
type NotificationSettingsOptions struct {
Level *NotificationLevel `url:"level,omitempty" json:"level,omitempty"`
NotificationEmail *string `url:"notification_email,omitempty" json:"notification_email,omitempty"`
CloseIssue *bool `url:"close_issue,omitempty" json:"close_issue,omitempty"`
CloseMergeRequest *bool `url:"close_merge_request,omitempty" json:"close_merge_request,omitempty"`
FailedPipeline *bool `url:"failed_pipeline,omitempty" json:"failed_pipeline,omitempty"`
MergeMergeRequest *bool `url:"merge_merge_request,omitempty" json:"merge_merge_request,omitempty"`
NewIssue *bool `url:"new_issue,omitempty" json:"new_issue,omitempty"`
NewMergeRequest *bool `url:"new_merge_request,omitempty" json:"new_merge_request,omitempty"`
NewNote *bool `url:"new_note,omitempty" json:"new_note,omitempty"`
ReassignIssue *bool `url:"reassign_issue,omitempty" json:"reassign_issue,omitempty"`
ReassignMergeRequest *bool `url:"reassign_merge_request,omitempty" json:"reassign_merge_request,omitempty"`
ReopenIssue *bool `url:"reopen_issue,omitempty" json:"reopen_issue,omitempty"`
ReopenMergeRequest *bool `url:"reopen_merge_request,omitempty" json:"reopen_merge_request,omitempty"`
SuccessPipeline *bool `url:"success_pipeline,omitempty" json:"success_pipeline,omitempty"`
}
// UpdateGlobalSettings updates current notification settings and email address.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/notification_settings.html#update-global-notification-settings
func (s *NotificationSettingsService) UpdateGlobalSettings(opt *NotificationSettingsOptions) (*NotificationSettings, *Response, error) {
if opt.Level != nil && *opt.Level == GlobalNotificationLevel {
return nil, nil, errors.New(
"notification level 'global' is not valid for global notification settings")
}
u := "notification_settings"
req, err := s.client.NewRequest("PUT", u, opt)
if err != nil {
return nil, nil, err
}
ns := new(NotificationSettings)
resp, err := s.client.Do(req, ns)
if err != nil {
return nil, resp, err
}
return ns, resp, err
}
// GetSettingsForGroup returns current group notification settings.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/notification_settings.html#group-project-level-notification-settings
func (s *NotificationSettingsService) GetSettingsForGroup(gid interface{}) (*NotificationSettings, *Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("groups/%s/notification_settings", url.QueryEscape(group))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
ns := new(NotificationSettings)
resp, err := s.client.Do(req, ns)
if err != nil {
return nil, resp, err
}
return ns, resp, err
}
// GetSettingsForProject returns current project notification settings.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/notification_settings.html#group-project-level-notification-settings
func (s *NotificationSettingsService) GetSettingsForProject(pid interface{}) (*NotificationSettings, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/notification_settings", url.QueryEscape(project))
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
ns := new(NotificationSettings)
resp, err := s.client.Do(req, ns)
if err != nil {
return nil, resp, err
}
return ns, resp, err
}
// UpdateSettingsForGroup updates current group notification settings.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/notification_settings.html#update-group-project-level-notification-settings
func (s *NotificationSettingsService) UpdateSettingsForGroup(gid interface{}, opt *NotificationSettingsOptions) (*NotificationSettings, *Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("groups/%s/notification_settings", url.QueryEscape(group))
req, err := s.client.NewRequest("PUT", u, opt)
if err != nil {
return nil, nil, err
}
ns := new(NotificationSettings)
resp, err := s.client.Do(req, ns)
if err != nil {
return nil, resp, err
}
return ns, resp, err
}
// UpdateSettingsForProject updates current project notification settings.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/notification_settings.html#update-group-project-level-notification-settings
func (s *NotificationSettingsService) UpdateSettingsForProject(pid interface{}, opt *NotificationSettingsOptions) (*NotificationSettings, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/notification_settings", url.QueryEscape(project))
req, err := s.client.NewRequest("PUT", u, opt)
if err != nil {
return nil, nil, err
}
ns := new(NotificationSettings)
resp, err := s.client.Do(req, ns)
if err != nil {
return nil, resp, err
}
return ns, resp, err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment