From 7255b106126b6012a0c7591c373e46c049c835a5 Mon Sep 17 00:00:00 2001 From: jguttman Date: Wed, 25 Aug 2021 18:36:00 -0500 Subject: [PATCH 1/3] Add edit groups api invocation --- groups.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ groups_test.go | 26 +++++++++++++++++++++++++ types.go | 22 +++++++++++++++++++++ 3 files changed, 101 insertions(+) diff --git a/groups.go b/groups.go index 248e8ae22..724d1cfc8 100644 --- a/groups.go +++ b/groups.go @@ -198,6 +198,59 @@ func (s *GroupsService) CreateGroup(opt *CreateGroupOptions, options ...RequestO return g, resp, err } +// EditGroupOptions represents the available EditGroup() options. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#update-group +type EditGroupOptions struct { + Name *string `url:"name,omitempty" json:"name,omitempty"` + Path *string `url:"path,omitempty" json:"path,omitempty"` + Description *string `url:"description,omitempty" json:"description,omitempty"` + MembershipLock *bool `url:"membership_lock,omitempty" json:"membership_lock,omitempty"` + Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"` + ShareWithGroupLock *bool `url:"share_with_group_lock,omitempty" json:"share_with_group_lock,omitempty"` + RequireTwoFactorAuth *bool `url:"require_two_factor_authentication,omitempty" json:"require_two_factor_authentication,omitempty"` + TwoFactorGracePeriod *int `url:"two_factor_grace_period,omitempty" json:"two_factor_grace_period,omitempty"` + ProjectCreationLevel *ProjectCreationLevelValue `url:"project_creation_level,omitempty" json:"project_creation_level,omitempty"` + AutoDevopsEnabled *bool `url:"auto_devops_enabled,omitempty" json:"auto_devops_enabled,omitempty"` + SubGroupCreationLevel *SubGroupCreationLevelValue `url:"subgroup_creation_level,omitempty" json:"subgroup_creation_level,omitempty"` + EmailsDisabled *bool `url:"emails_disabled,omitempty" json:"emails_disabled,omitempty"` + MentionsDisabled *bool `url:"mentions_disabled,omitempty" json:"mentions_disabled,omitempty"` + LFSEnabled *bool `url:"lfs_enabled,omitempty" json:"lfs_enabled,omitempty"` + RequestAccessEnabled *bool `url:"request_access_enabled,omitempty" json:"request_access_enabled,omitempty"` + DefaultBranchProtection *int `url:"default_branch_protection,omitempty" json:"default_branch_protection,omitempty"` + FileTemplateProjectId *int `url:"file_template_project_id,omitempty" json:"file_template_project_id,omitempty"` + SharedRunnersMinutesLimit *int `url:"shared_runners_minutes_limit,omitempty" json:"shared_runners_minutes_limit,omitempty"` + ExtraSharedRunnersMinutesLimit *int `url:"extra_shared_runners_minutes_limit,omitempty" json:"extra_shared_runners_minutes_limit,omitempty"` + PreventForkingOutsideGroup *bool `url:"prevent_forking_outside_group,omitempty" json:"prevent_forking_outside_group,omitempty"` + SharedRunnersSetting *SharedRunnersSettingValue `url:"shared_runners_setting,omitempty" json:"shared_runners_setting,omitempty"` + PreventSharingGroupsOutsideHierarchy *bool `url:"prevent_sharing_groups_outside_hierarchy,omitempty" json:"prevent_sharing_groups_outside_hierarchy,omitempty"` +} + +// EditGroup creates a new project group. Only available to group owners +// and administrators. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#update-group +func (s *GroupsService) EditGroup(gid interface{}, opt *EditGroupOptions, options ...RequestOptionFunc) (*Group, *Response, error) { + group, err := parseID(gid) + if err != nil { + return nil, nil, err + } + u := fmt.Sprintf("groups/%s", pathEscape(group)) + + req, err := s.client.NewRequest(http.MethodPut, u, opt, options) + if err != nil { + return nil, nil, err + } + + g := new(Group) + resp, err := s.client.Do(req, g) + if err != nil { + return nil, resp, err + } + + return g, resp, err +} + // TransferGroup transfers a project to the Group namespace. Available only // for admin. // diff --git a/groups_test.go b/groups_test.go index e2e2b87b5..d3093e4cf 100644 --- a/groups_test.go +++ b/groups_test.go @@ -75,6 +75,32 @@ func TestCreateGroup(t *testing.T) { } } +func TestEditGroup(t *testing.T) { + mux, server, client := setup(t) + defer teardown(server) + + mux.HandleFunc("/api/v4/groups/1", + func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodPut) + fmt.Fprint(w, `{"id": 1, "name": "g", "path": "g"}`) + }) + + opt := &EditGroupOptions{ + Name: String("g"), + Path: String("g"), + } + + group, _, err := client.Groups.EditGroup(1, opt, nil) + if err != nil { + t.Errorf("Groups.EditGroup returned error: %v", err) + } + + want := &Group{ID: 1, Name: "g", Path: "g"} + if !reflect.DeepEqual(want, group) { + t.Errorf("Groups.EditGroup returned %+v, want %+v", group, want) + } +} + func TestTransferGroup(t *testing.T) { mux, server, client := setup(t) defer teardown(server) diff --git a/types.go b/types.go index 1d99a0d1c..e71419ffc 100644 --- a/types.go +++ b/types.go @@ -421,6 +421,28 @@ func ProjectCreationLevel(v ProjectCreationLevelValue) *ProjectCreationLevelValu return p } +// SharedRunnersSettingValue represents whether shared runners are enabled for a group’s subgroups and projects. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#options-for-shared_runners_setting +type SharedRunnersSettingValue string + +// List of available shared runner settings levels. +// +// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#options-for-shared_runners_setting +const ( + EnabledSharedRunnersSettingValue SharedRunnersSettingValue = "enabled" + DisabledWithOverrideSharedRunnersSettingValue SharedRunnersSettingValue = "disabled_with_override" + DisabledAndUnoverridableSharedRunnersSettingValue SharedRunnersSettingValue = "disabled_and_unoverridable" +) + +// SharedRunnersSetting is a helper routine that allocates a new SharedRunnersSettingValue +// to store v and returns a pointer to it. +func SharedRunnersSetting(v SharedRunnersSettingValue) *SharedRunnersSettingValue { + p := new(SharedRunnersSettingValue) + *p = v + return p +} + // SubGroupCreationLevelValue represents a sub group creation level within GitLab. // // GitLab API docs: https://docs.gitlab.com/ce/api/ From b9d3fde2b95ae964e6cb84da4108032d83de0e15 Mon Sep 17 00:00:00 2001 From: jguttman Date: Wed, 25 Aug 2021 18:38:10 -0500 Subject: [PATCH 2/3] Fix name of new field --- groups.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/groups.go b/groups.go index 724d1cfc8..93461c363 100644 --- a/groups.go +++ b/groups.go @@ -218,7 +218,7 @@ type EditGroupOptions struct { LFSEnabled *bool `url:"lfs_enabled,omitempty" json:"lfs_enabled,omitempty"` RequestAccessEnabled *bool `url:"request_access_enabled,omitempty" json:"request_access_enabled,omitempty"` DefaultBranchProtection *int `url:"default_branch_protection,omitempty" json:"default_branch_protection,omitempty"` - FileTemplateProjectId *int `url:"file_template_project_id,omitempty" json:"file_template_project_id,omitempty"` + FileTemplateProjectID *int `url:"file_template_project_id,omitempty" json:"file_template_project_id,omitempty"` SharedRunnersMinutesLimit *int `url:"shared_runners_minutes_limit,omitempty" json:"shared_runners_minutes_limit,omitempty"` ExtraSharedRunnersMinutesLimit *int `url:"extra_shared_runners_minutes_limit,omitempty" json:"extra_shared_runners_minutes_limit,omitempty"` PreventForkingOutsideGroup *bool `url:"prevent_forking_outside_group,omitempty" json:"prevent_forking_outside_group,omitempty"` From 5c2574c881676eaaa448ff66262013da8cdcb763 Mon Sep 17 00:00:00 2001 From: jguttman Date: Wed, 25 Aug 2021 18:43:34 -0500 Subject: [PATCH 3/3] Update godoc for new function --- groups.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/groups.go b/groups.go index 93461c363..b6bf1f989 100644 --- a/groups.go +++ b/groups.go @@ -226,7 +226,7 @@ type EditGroupOptions struct { PreventSharingGroupsOutsideHierarchy *bool `url:"prevent_sharing_groups_outside_hierarchy,omitempty" json:"prevent_sharing_groups_outside_hierarchy,omitempty"` } -// EditGroup creates a new project group. Only available to group owners +// EditGroup updates an existing group. Only available to group owners // and administrators. // // GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#update-group