From ef1ada13845daf0aab33aa341128bb33457448bb Mon Sep 17 00:00:00 2001 From: Patrick Rice Date: Mon, 4 Mar 2024 00:54:08 +0000 Subject: [PATCH] Add permanently delete opt --- groups.go | 12 ++++++++++-- groups_test.go | 45 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/groups.go b/groups.go index fcbd68de1..1040684f0 100644 --- a/groups.go +++ b/groups.go @@ -560,17 +560,25 @@ func (s *GroupsService) UploadAvatar(gid interface{}, avatar io.Reader, filename return g, resp, nil } +// DeleteGroupOptions represents the available DeleteGroup() options. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#update-group +type DeleteGroupOptions struct { + PermanentlyRemove *bool `url:"permanently_remove,omitempty" json:"permanently_remove,omitempty"` + FullPath *string `url:"full_path,omitempty" json:"full_path,omitempty"` +} + // DeleteGroup removes group with all projects inside. // // GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#remove-group -func (s *GroupsService) DeleteGroup(gid interface{}, options ...RequestOptionFunc) (*Response, error) { +func (s *GroupsService) DeleteGroup(gid interface{}, opt *DeleteGroupOptions, options ...RequestOptionFunc) (*Response, error) { group, err := parseID(gid) if err != nil { return nil, err } u := fmt.Sprintf("groups/%s", PathEscape(group)) - req, err := s.client.NewRequest(http.MethodDelete, u, nil, options) + req, err := s.client.NewRequest(http.MethodDelete, u, opt, options) if err != nil { return nil, err } diff --git a/groups_test.go b/groups_test.go index b6f8e0499..d848e7424 100644 --- a/groups_test.go +++ b/groups_test.go @@ -3,6 +3,7 @@ package gitlab import ( "fmt" "net/http" + "net/url" "reflect" "testing" ) @@ -145,7 +146,7 @@ func TestDeleteGroup(t *testing.T) { w.WriteHeader(http.StatusAccepted) }) - resp, err := client.Groups.DeleteGroup(1) + resp, err := client.Groups.DeleteGroup(1, nil) if err != nil { t.Errorf("Groups.DeleteGroup returned error: %v", err) } @@ -157,6 +158,48 @@ func TestDeleteGroup(t *testing.T) { } } +func TestDeleteGroup_WithPermanentDelete(t *testing.T) { + mux, client := setup(t) + var params url.Values + + mux.HandleFunc("/api/v4/groups/1", + func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodDelete) + w.WriteHeader(http.StatusAccepted) + + // Get the request parameters + parsedParams, err := url.ParseQuery(r.URL.RawQuery) + if err != nil { + t.Errorf("Groups.DeleteGroup returned error when parsing test parameters: %v", err) + } + params = parsedParams + }) + + resp, err := client.Groups.DeleteGroup(1, &DeleteGroupOptions{ + PermanentlyRemove: Ptr(true), + FullPath: Ptr("testPath"), + }) + + if err != nil { + t.Errorf("Groups.DeleteGroup returned error: %v", err) + } + + // Test that our status code matches + if resp.StatusCode != http.StatusAccepted { + t.Errorf("Groups.DeleteGroup returned %d, want %d", resp.StatusCode, http.StatusAccepted) + } + + // Test that "permanently_remove" is set to true + if params.Get("permanently_remove") != "true" { + t.Errorf("Groups.DeleteGroup returned %v, want %v", params.Get("permanently_remove"), true) + } + + // Test that "full_path" is set to "testPath" + if params.Get("full_path") != "testPath" { + t.Errorf("Groups.DeleteGroup returned %v, want %v", params.Get("full_path"), "testPath") + } +} + func TestSearchGroup(t *testing.T) { mux, client := setup(t)