...

Source file src/github.com/xanzy/go-gitlab/notifications.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  	"errors"
    21  	"fmt"
    22  	"net/http"
    23  )
    24  
    25  // NotificationSettingsService handles communication with the notification settings
    26  // related methods of the GitLab API.
    27  //
    28  // GitLab API docs: https://docs.gitlab.com/ee/api/notification_settings.html
    29  type NotificationSettingsService struct {
    30  	client *Client
    31  }
    32  
    33  // NotificationSettings represents the Gitlab notification setting.
    34  //
    35  // GitLab API docs:
    36  // https://docs.gitlab.com/ee/api/notification_settings.html#valid-notification-levels
    37  type NotificationSettings struct {
    38  	Level             NotificationLevelValue `json:"level"`
    39  	NotificationEmail string                 `json:"notification_email"`
    40  	Events            *NotificationEvents    `json:"events"`
    41  }
    42  
    43  // NotificationEvents represents the available notification setting events.
    44  //
    45  // GitLab API docs:
    46  // https://docs.gitlab.com/ee/api/notification_settings.html#valid-notification-levels
    47  type NotificationEvents struct {
    48  	CloseIssue                bool `json:"close_issue"`
    49  	CloseMergeRequest         bool `json:"close_merge_request"`
    50  	FailedPipeline            bool `json:"failed_pipeline"`
    51  	FixedPipeline             bool `json:"fixed_pipeline"`
    52  	IssueDue                  bool `json:"issue_due"`
    53  	MergeWhenPipelineSucceeds bool `json:"merge_when_pipeline_succeeds"`
    54  	MergeMergeRequest         bool `json:"merge_merge_request"`
    55  	MovedProject              bool `json:"moved_project"`
    56  	NewIssue                  bool `json:"new_issue"`
    57  	NewMergeRequest           bool `json:"new_merge_request"`
    58  	NewEpic                   bool `json:"new_epic"`
    59  	NewNote                   bool `json:"new_note"`
    60  	PushToMergeRequest        bool `json:"push_to_merge_request"`
    61  	ReassignIssue             bool `json:"reassign_issue"`
    62  	ReassignMergeRequest      bool `json:"reassign_merge_request"`
    63  	ReopenIssue               bool `json:"reopen_issue"`
    64  	ReopenMergeRequest        bool `json:"reopen_merge_request"`
    65  	SuccessPipeline           bool `json:"success_pipeline"`
    66  }
    67  
    68  func (ns NotificationSettings) String() string {
    69  	return Stringify(ns)
    70  }
    71  
    72  // GetGlobalSettings returns current notification settings and email address.
    73  //
    74  // GitLab API docs:
    75  // https://docs.gitlab.com/ee/api/notification_settings.html#global-notification-settings
    76  func (s *NotificationSettingsService) GetGlobalSettings(options ...RequestOptionFunc) (*NotificationSettings, *Response, error) {
    77  	u := "notification_settings"
    78  
    79  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
    80  	if err != nil {
    81  		return nil, nil, err
    82  	}
    83  
    84  	ns := new(NotificationSettings)
    85  	resp, err := s.client.Do(req, ns)
    86  	if err != nil {
    87  		return nil, resp, err
    88  	}
    89  
    90  	return ns, resp, nil
    91  }
    92  
    93  // NotificationSettingsOptions represents the available options that can be passed
    94  // to the API when updating the notification settings.
    95  type NotificationSettingsOptions struct {
    96  	Level                     *NotificationLevelValue `url:"level,omitempty" json:"level,omitempty"`
    97  	NotificationEmail         *string                 `url:"notification_email,omitempty" json:"notification_email,omitempty"`
    98  	CloseIssue                *bool                   `url:"close_issue,omitempty" json:"close_issue,omitempty"`
    99  	CloseMergeRequest         *bool                   `url:"close_merge_request,omitempty" json:"close_merge_request,omitempty"`
   100  	FailedPipeline            *bool                   `url:"failed_pipeline,omitempty" json:"failed_pipeline,omitempty"`
   101  	FixedPipeline             *bool                   `url:"fixed_pipeline,omitempty" json:"fixed_pipeline,omitempty"`
   102  	IssueDue                  *bool                   `url:"issue_due,omitempty" json:"issue_due,omitempty"`
   103  	MergeMergeRequest         *bool                   `url:"merge_merge_request,omitempty" json:"merge_merge_request,omitempty"`
   104  	MergeWhenPipelineSucceeds *bool                   `url:"merge_when_pipeline_succeeds,omitempty" json:"merge_when_pipeline_succeeds,omitempty"`
   105  	MovedProject              *bool                   `url:"moved_project,omitempty" json:"moved_project,omitempty"`
   106  	NewEpic                   *bool                   `url:"new_epic,omitempty" json:"new_epic,omitempty"`
   107  	NewIssue                  *bool                   `url:"new_issue,omitempty" json:"new_issue,omitempty"`
   108  	NewMergeRequest           *bool                   `url:"new_merge_request,omitempty" json:"new_merge_request,omitempty"`
   109  	NewNote                   *bool                   `url:"new_note,omitempty" json:"new_note,omitempty"`
   110  	PushToMergeRequest        *bool                   `url:"push_to_merge_request,omitempty" json:"push_to_merge_request,omitempty"`
   111  	ReassignIssue             *bool                   `url:"reassign_issue,omitempty" json:"reassign_issue,omitempty"`
   112  	ReassignMergeRequest      *bool                   `url:"reassign_merge_request,omitempty" json:"reassign_merge_request,omitempty"`
   113  	ReopenIssue               *bool                   `url:"reopen_issue,omitempty" json:"reopen_issue,omitempty"`
   114  	ReopenMergeRequest        *bool                   `url:"reopen_merge_request,omitempty" json:"reopen_merge_request,omitempty"`
   115  	SuccessPipeline           *bool                   `url:"success_pipeline,omitempty" json:"success_pipeline,omitempty"`
   116  }
   117  
   118  // UpdateGlobalSettings updates current notification settings and email address.
   119  //
   120  // GitLab API docs:
   121  // https://docs.gitlab.com/ee/api/notification_settings.html#update-global-notification-settings
   122  func (s *NotificationSettingsService) UpdateGlobalSettings(opt *NotificationSettingsOptions, options ...RequestOptionFunc) (*NotificationSettings, *Response, error) {
   123  	if opt.Level != nil && *opt.Level == GlobalNotificationLevel {
   124  		return nil, nil, errors.New(
   125  			"notification level 'global' is not valid for global notification settings")
   126  	}
   127  
   128  	u := "notification_settings"
   129  
   130  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   131  	if err != nil {
   132  		return nil, nil, err
   133  	}
   134  
   135  	ns := new(NotificationSettings)
   136  	resp, err := s.client.Do(req, ns)
   137  	if err != nil {
   138  		return nil, resp, err
   139  	}
   140  
   141  	return ns, resp, nil
   142  }
   143  
   144  // GetSettingsForGroup returns current group notification settings.
   145  //
   146  // GitLab API docs:
   147  // https://docs.gitlab.com/ee/api/notification_settings.html#group--project-level-notification-settings
   148  func (s *NotificationSettingsService) GetSettingsForGroup(gid interface{}, options ...RequestOptionFunc) (*NotificationSettings, *Response, error) {
   149  	group, err := parseID(gid)
   150  	if err != nil {
   151  		return nil, nil, err
   152  	}
   153  	u := fmt.Sprintf("groups/%s/notification_settings", PathEscape(group))
   154  
   155  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   156  	if err != nil {
   157  		return nil, nil, err
   158  	}
   159  
   160  	ns := new(NotificationSettings)
   161  	resp, err := s.client.Do(req, ns)
   162  	if err != nil {
   163  		return nil, resp, err
   164  	}
   165  
   166  	return ns, resp, nil
   167  }
   168  
   169  // GetSettingsForProject returns current project notification settings.
   170  //
   171  // GitLab API docs:
   172  // https://docs.gitlab.com/ee/api/notification_settings.html#group--project-level-notification-settings
   173  func (s *NotificationSettingsService) GetSettingsForProject(pid interface{}, options ...RequestOptionFunc) (*NotificationSettings, *Response, error) {
   174  	project, err := parseID(pid)
   175  	if err != nil {
   176  		return nil, nil, err
   177  	}
   178  	u := fmt.Sprintf("projects/%s/notification_settings", PathEscape(project))
   179  
   180  	req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
   181  	if err != nil {
   182  		return nil, nil, err
   183  	}
   184  
   185  	ns := new(NotificationSettings)
   186  	resp, err := s.client.Do(req, ns)
   187  	if err != nil {
   188  		return nil, resp, err
   189  	}
   190  
   191  	return ns, resp, nil
   192  }
   193  
   194  // UpdateSettingsForGroup updates current group notification settings.
   195  //
   196  // GitLab API docs:
   197  // https://docs.gitlab.com/ee/api/notification_settings.html#update-groupproject-level-notification-settings
   198  func (s *NotificationSettingsService) UpdateSettingsForGroup(gid interface{}, opt *NotificationSettingsOptions, options ...RequestOptionFunc) (*NotificationSettings, *Response, error) {
   199  	group, err := parseID(gid)
   200  	if err != nil {
   201  		return nil, nil, err
   202  	}
   203  	u := fmt.Sprintf("groups/%s/notification_settings", PathEscape(group))
   204  
   205  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   206  	if err != nil {
   207  		return nil, nil, err
   208  	}
   209  
   210  	ns := new(NotificationSettings)
   211  	resp, err := s.client.Do(req, ns)
   212  	if err != nil {
   213  		return nil, resp, err
   214  	}
   215  
   216  	return ns, resp, nil
   217  }
   218  
   219  // UpdateSettingsForProject updates current project notification settings.
   220  //
   221  // GitLab API docs:
   222  // https://docs.gitlab.com/ee/api/notification_settings.html#update-groupproject-level-notification-settings
   223  func (s *NotificationSettingsService) UpdateSettingsForProject(pid interface{}, opt *NotificationSettingsOptions, options ...RequestOptionFunc) (*NotificationSettings, *Response, error) {
   224  	project, err := parseID(pid)
   225  	if err != nil {
   226  		return nil, nil, err
   227  	}
   228  	u := fmt.Sprintf("projects/%s/notification_settings", PathEscape(project))
   229  
   230  	req, err := s.client.NewRequest(http.MethodPut, u, opt, options)
   231  	if err != nil {
   232  		return nil, nil, err
   233  	}
   234  
   235  	ns := new(NotificationSettings)
   236  	resp, err := s.client.Do(req, ns)
   237  	if err != nil {
   238  		return nil, resp, err
   239  	}
   240  
   241  	return ns, resp, nil
   242  }
   243  

View as plain text