From f592a685a38d4f1e518f4849c6420ac118cd70fe Mon Sep 17 00:00:00 2001 From: Patrick Rice Date: Sun, 8 Oct 2023 03:14:44 +0000 Subject: [PATCH] Add support for updating via the NotificationSettingsOptions --- notifications.go | 34 ++++++++++------- notifications_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 14 deletions(-) diff --git a/notifications.go b/notifications.go index 17fc2f39f..92c2cb189 100644 --- a/notifications.go +++ b/notifications.go @@ -93,20 +93,26 @@ func (s *NotificationSettingsService) GetGlobalSettings(options ...RequestOption // NotificationSettingsOptions represents the available options that can be passed // to the API when updating the notification settings. type NotificationSettingsOptions struct { - Level *NotificationLevelValue `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"` + Level *NotificationLevelValue `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"` + FixedPipeline *bool `url:"fixed_pipeline,omitempty" json:"fixed_pipeline,omitempty"` + IssueDue *bool `url:"issue_due,omitempty" json:"issue_due,omitempty"` + MergeMergeRequest *bool `url:"merge_merge_request,omitempty" json:"merge_merge_request,omitempty"` + MergeWhenPipelineSucceeds *bool `url:"merge_when_pipeline_succeeds,omitempty" json:"merge_when_pipeline_succeeds,omitempty"` + MovedProject *bool `url:"moved_project,omitempty" json:"moved_project,omitempty"` + NewEpic *bool `url:"new_epic,omitempty" json:"new_epic,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"` + PushToMergeRequest *bool `url:"push_to_merge_request,omitempty" json:"push_to_merge_request,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. diff --git a/notifications_test.go b/notifications_test.go index 45ac1d408..34be0ae61 100644 --- a/notifications_test.go +++ b/notifications_test.go @@ -17,7 +17,9 @@ package gitlab import ( + "encoding/json" "fmt" + "io" "net/http" "reflect" "testing" @@ -110,3 +112,88 @@ func TestGetProjectSettings(t *testing.T) { t.Errorf("NotificationSettings.GetSettingsForProject returned %+v, want %+v", settings, want) } } + +func TestUpdateProjectSettings(t *testing.T) { + mux, client := setup(t) + + // Create the request to send + var reqBody NotificationSettingsOptions + customLevel := notificationLevelTypes["custom"] + options := NotificationSettingsOptions{ + Level: &customLevel, + NewEpic: Bool(true), + MovedProject: Bool(true), + CloseIssue: Bool(true), + } + + // Handle the request on the server, and return a fully hydrated response + mux.HandleFunc("/api/v4/projects/1/notification_settings", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPut) + + // Store the body for later, so we can check only some values are marshalled properly for update + body, _ := io.ReadAll(r.Body) + json.Unmarshal(body, &reqBody) + + fmt.Fprintf(w, `{ + "level":"custom", + "events":{ + "new_note":true, + "new_issue":true, + "reopen_issue":true, + "close_issue":true, + "reassign_issue":true, + "issue_due":true, + "new_merge_request":true, + "push_to_merge_request":true, + "reopen_merge_request":true, + "close_merge_request":true, + "reassign_merge_request":true, + "merge_merge_request":true, + "failed_pipeline":true, + "fixed_pipeline":true, + "success_pipeline":true, + "moved_project":true, + "merge_when_pipeline_succeeds":true, + "new_epic":true + } + }`) + }) + + // Make the actual request + settings, _, err := client.NotificationSettings.UpdateSettingsForProject(1, &options) + if err != nil { + t.Errorf("NotifcationSettings.UpdateSettingsForProject returned error: %v", err) + } + + // Test the response and the request + wantResponse := &NotificationSettings{ + Level: customLevel, + Events: &NotificationEvents{ + NewEpic: true, + NewNote: true, + NewIssue: true, + ReopenIssue: true, + CloseIssue: true, + ReassignIssue: true, + IssueDue: true, + NewMergeRequest: true, + PushToMergeRequest: true, + ReopenMergeRequest: true, + CloseMergeRequest: true, + ReassignMergeRequest: true, + MergeMergeRequest: true, + FailedPipeline: true, + FixedPipeline: true, + SuccessPipeline: true, + MovedProject: true, + MergeWhenPipelineSucceeds: true, + }, + } + + if !reflect.DeepEqual(settings, wantResponse) { + t.Errorf("NotificationSettings.UpdateSettingsForProject returned for the response %+v, want %+v", settings, wantResponse) + } + if !reflect.DeepEqual(settings, wantResponse) { + t.Errorf("NotificationSettings.UpdateSettingsForProject send for the request %+v, want %+v", reqBody, options) + } +}