Skip to content

Commit

Permalink
Merge pull request #2012 from Atoms/serviceaccountlist
Browse files Browse the repository at this point in the history
Add ListServiceAccounts function to list all service accounts
  • Loading branch information
svanharmelen authored Sep 25, 2024
2 parents 03a68ed + 0c8599a commit 27d9adb
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
12 changes: 12 additions & 0 deletions testdata/get_serviceaccounts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": 114,
"username": "service_account_33",
"name": "Service account user"
},
{
"id": 137,
"username": "service_account_34",
"name": "john doe"
}
]
34 changes: 32 additions & 2 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ type BasicUser struct {
WebURL string `json:"web_url"`
}

// ServiceAccount represents a GitLab service account.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/user_service_accounts.html
type ServiceAccount struct {
ID int `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
}

// User represents a GitLab user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html
Expand Down Expand Up @@ -1543,9 +1553,10 @@ func (s *UsersService) CreateUserRunner(opts *CreateUserRunnerOptions, options .
return r, resp, nil
}

// CreateServiceAccountUser creates a new service account user. Note only administrators can create new service account users.
// CreateServiceAccountUser creates a new service account user.
//
// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#create-service-account-user
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-service-account-user
func (s *UsersService) CreateServiceAccountUser(options ...RequestOptionFunc) (*User, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "service_accounts", nil, options)
if err != nil {
Expand All @@ -1561,6 +1572,25 @@ func (s *UsersService) CreateServiceAccountUser(options ...RequestOptionFunc) (*
return usr, resp, nil
}

// ListServiceAccounts lists all service accounts.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/users.html#create-service-account-user
func (s *UsersService) ListServiceAccounts(opt *ListServiceAccountsOptions, options ...RequestOptionFunc) ([]*ServiceAccount, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "service_accounts", opt, options)
if err != nil {
return nil, nil, err
}

var sas []*ServiceAccount
resp, err := s.client.Do(req, &sas)
if err != nil {
return nil, resp, err
}

return sas, resp, nil
}

// UploadAvatar uploads an avatar to the current user.
//
// GitLab API docs:
Expand Down
26 changes: 26 additions & 0 deletions users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,3 +893,29 @@ func TestUploadAvatarUser(t *testing.T) {
t.Fatalf("Users.UploadAvatar returns an error: %v", err)
}
}
func TestListServiceAccounts(t *testing.T) {
mux, client := setup(t)

path := "/api/v4/service_accounts"

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
mustWriteHTTPResponse(t, w, "testdata/get_serviceaccounts.json")
})

serviceaccounts, _, err := client.Users.ListServiceAccounts(&ListServiceAccountsOptions{})
require.NoError(t, err)
want := []*ServiceAccount{
{
ID: 114,
Username: "service_account_33",
Name: "Service account user",
},
{
ID: 137,
Username: "service_account_34",
Name: "john doe",
},
}
require.Equal(t, want, serviceaccounts)
}

0 comments on commit 27d9adb

Please sign in to comment.