Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Commit afb6163

Browse files
authored
Merge pull request #2005 from mXtone/main
Add support for groups service accounts
2 parents 658c4fd + 21134f2 commit afb6163

File tree

2 files changed

+73
-8
lines changed

2 files changed

+73
-8
lines changed

group_serviceaccounts.go

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,60 @@ type GroupServiceAccount struct {
3131
UserName string `json:"username"`
3232
}
3333

34-
// CreateServiceAccount create a new service account user for a group.
34+
// ListServiceAccountsOptions represents the available ListServiceAccounts() options.
3535
//
36-
// GitLab API docs:
37-
// https://docs.gitlab.com/ee/api/groups.html#create-service-account-user
38-
func (s *GroupsService) CreateServiceAccount(gid interface{}, options ...RequestOptionFunc) (*GroupServiceAccount, *Response, error) {
36+
// GitLab API docs: https://docs.gitlab.com/ee/api/group_service_accounts.html#list-service-account-users
37+
type ListServiceAccountsOptions struct {
38+
ListOptions
39+
OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
40+
Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
41+
}
42+
43+
// ListServiceAccounts gets a list of service acxcounts.
44+
//
45+
// GitLab API docs: https://docs.gitlab.com/ee/api/group_service_accounts.html#list-service-account-users
46+
func (s *GroupsService) ListServiceAccounts(gid interface{}, opt *ListServiceAccountsOptions, options ...RequestOptionFunc) ([]*GroupServiceAccount, *Response, error) {
3947
group, err := parseID(gid)
4048
if err != nil {
4149
return nil, nil, err
4250
}
4351
u := fmt.Sprintf("groups/%s/service_accounts", PathEscape(group))
4452

45-
req, err := s.client.NewRequest(http.MethodPost, u, nil, options)
53+
req, err := s.client.NewRequest(http.MethodGet, u, opt, options)
54+
if err != nil {
55+
return nil, nil, err
56+
}
57+
58+
var sa []*GroupServiceAccount
59+
resp, err := s.client.Do(req, &sa)
60+
if err != nil {
61+
return nil, resp, err
62+
}
63+
64+
return sa, resp, nil
65+
}
66+
67+
// CreateServiceAccountOptions represents the available CreateServiceAccount() options.
68+
//
69+
// GitLab API docs: https://docs.gitlab.com/ee/api/group_service_accounts.html#create-a-service-account-user
70+
type CreateServiceAccountOptions struct {
71+
Name *string `url:"name,omitempty" json:"name,omitempty"`
72+
Username *string `url:"username,omitempty" json:"username,omitempty"`
73+
}
74+
75+
// Creates a service account user.
76+
//
77+
// This API endpoint works on top-level groups only. It does not work on subgroups.
78+
//
79+
// GitLab API docs: https://docs.gitlab.com/ee/api/groups.html#create-service-account-user
80+
func (s *GroupsService) CreateServiceAccount(gid interface{}, opt *CreateServiceAccountOptions, options ...RequestOptionFunc) (*GroupServiceAccount, *Response, error) {
81+
group, err := parseID(gid)
82+
if err != nil {
83+
return nil, nil, err
84+
}
85+
u := fmt.Sprintf("groups/%s/service_accounts", PathEscape(group))
86+
87+
req, err := s.client.NewRequest(http.MethodPost, u, opt, options)
4688
if err != nil {
4789
return nil, nil, err
4890
}
@@ -60,7 +102,7 @@ func (s *GroupsService) CreateServiceAccount(gid interface{}, options ...Request
60102
// CreateServiceAccountPersonalAccessToken() options.
61103
//
62104
// GitLab API docs:
63-
// https://docs.gitlab.com/ee/api/groups.html#create-personal-access-token-for-service-account-user
105+
// https://docs.gitlab.com/ee/api/group_service_accounts.html#create-a-personal-access-token-for-a-service-account-user
64106
type CreateServiceAccountPersonalAccessTokenOptions struct {
65107
Scopes *[]string `url:"scopes,omitempty" json:"scopes,omitempty"`
66108
Name *string `url:"name,omitempty" json:"name,omitempty"`
@@ -71,7 +113,7 @@ type CreateServiceAccountPersonalAccessTokenOptions struct {
71113
// service account user for a group.
72114
//
73115
// GitLab API docs:
74-
// https://docs.gitlab.com/ee/api/groups.html#create-personal-access-token-for-service-account-user
116+
// https://docs.gitlab.com/ee/api/group_service_accounts.html#create-a-personal-access-token-for-a-service-account-user
75117
func (s *GroupsService) CreateServiceAccountPersonalAccessToken(gid interface{}, serviceAccount int, opt *CreateServiceAccountPersonalAccessTokenOptions, options ...RequestOptionFunc) (*PersonalAccessToken, *Response, error) {
76118
group, err := parseID(gid)
77119
if err != nil {
@@ -117,3 +159,23 @@ func (s *GroupsService) RotateServiceAccountPersonalAccessToken(gid interface{},
117159

118160
return pat, resp, nil
119161
}
162+
163+
// DeleteServiceAccount Deletes a service account user.
164+
//
165+
// This API endpoint works on top-level groups only. It does not work on subgroups.
166+
//
167+
// GitLab API docs: https://docs.gitlab.com/ee/api/group_service_accounts.html#delete-a-service-account-user
168+
func (s *GroupsService) DeleteServiceAccount(gid interface{}, serviceAccount int, options ...RequestOptionFunc) (*Response, error) {
169+
group, err := parseID(gid)
170+
if err != nil {
171+
return nil, err
172+
}
173+
u := fmt.Sprintf("groups/%s/service_accounts/%d", PathEscape(group), serviceAccount)
174+
175+
req, err := s.client.NewRequest(http.MethodDelete, u, nil, options)
176+
if err != nil {
177+
return nil, err
178+
}
179+
180+
return s.client.Do(req, nil)
181+
}

group_serviceaccounts_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ func TestCreateServiceAccount(t *testing.T) {
3939
}`)
4040
})
4141

42-
sa, _, err := client.Groups.CreateServiceAccount(1)
42+
sa, _, err := client.Groups.CreateServiceAccount(1, &CreateServiceAccountOptions{
43+
Name: Ptr("Service account user"),
44+
Username: Ptr("service_account_group_345_6018816a18e515214e0c34c2b33523fc"),
45+
})
4346
if err != nil {
4447
t.Error(err)
4548
}

0 commit comments

Comments
 (0)