Skip to content

Commit

Permalink
Added support for group share with group api
Browse files Browse the repository at this point in the history
  • Loading branch information
markuslackner committed Feb 8, 2021
1 parent d6e5222 commit 77043ab
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
51 changes: 51 additions & 0 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,57 @@ func (s *GroupsService) DeleteGroupLDAPLinkForProvider(gid interface{}, provider
return s.client.Do(req, nil)
}

// GroupShareWithGroupOptions represents options to share group with groups
//
// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#share-groups-with-groups
type GroupShareWithGroupOptions struct {
GroupID *int `url:"group_id" json:"group_id"`
GroupAccess *AccessLevelValue `url:"group_access" json:"group_access"`
ExpiresAt *string `url:"expires_at" json:"expires_at"`
}

// ShareGroupWithGroup allows to share a group with a group.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#create-a-link-to-share-a-group-with-another-group
func (s *GroupsService) ShareGroupWithGroup(gid interface{}, opt *GroupShareWithGroupOptions, options ...RequestOptionFunc) (*Group, *Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("groups/%s/share", pathEscape(group))

req, err := s.client.NewRequest(http.MethodPost, 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
}

// DeleteSharedGroupFromGroup allows to unshare a group from a group.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#delete-link-sharing-group-with-another-group
func (s *GroupsService) DeleteSharedGroupFromGroup(gid interface{}, groupID int, options ...RequestOptionFunc) (*Response, error) {
group, err := parseID(gid)
if err != nil {
return nil, err
}
u := fmt.Sprintf("groups/%s/share/%d", pathEscape(group), groupID)

req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
if err != nil {
return nil, err
}

return s.client.Do(req, nil)
}

// GroupPushRules represents a group push rule.
//
// GitLab API docs:
Expand Down
40 changes: 40 additions & 0 deletions groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,43 @@ func TestRestoreGroup(t *testing.T) {
t.Errorf("Groups.RestoreGroup returned %+v, want %+v", group, want)
}
}

func TestShareGroupWithGroup(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/groups/1/share",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(w, `{"id": 1, "name": "g"}`)
})

group, _, err := client.Groups.ShareGroupWithGroup(1, &GroupShareWithGroupOptions{
GroupID: Int(1),
GroupAccess: AccessLevel(DeveloperPermissions),
})
if err != nil {
t.Errorf("Groups.ShareGroupWithGroup returned error: %v", err)
}
want := &Group{ID: 1, Name: "g"}
if !reflect.DeepEqual(want, group) {
t.Errorf("Groups.ShareGroupWithGroup returned %+v, want %+v", group, want)
}
}

func TestDeleteSharedGroupFromGroup(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)
mux.HandleFunc("/api/v4/groups/1/share/2",
func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
w.WriteHeader(204)
})

r, err := client.Groups.DeleteSharedGroupFromGroup(1, 2)
if err != nil {
t.Errorf("Groups.DeleteSharedGroupFromGroup returned error: %v", err)
}
if r.StatusCode != 204 {
t.Errorf("Groups.DeleteSharedGroupFromGroup returned status code %d", r.StatusCode)
}
}

0 comments on commit 77043ab

Please sign in to comment.