Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ListServiceAccounts function to list all service accounts #2012

Merged
merged 5 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}