From 55a72443229660bc96d242fb71ba26c57624602b Mon Sep 17 00:00:00 2001 From: Aivars Sterns Date: Thu, 12 Sep 2024 11:18:45 +0300 Subject: [PATCH 1/5] Add ListServiceAccounts function to list all service accounts --- testdata/get_serviceaccounts.json | 12 ++++++++++++ users.go | 27 +++++++++++++++++++++++++++ users_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 testdata/get_serviceaccounts.json diff --git a/testdata/get_serviceaccounts.json b/testdata/get_serviceaccounts.json new file mode 100644 index 000000000..e3350bcc7 --- /dev/null +++ b/testdata/get_serviceaccounts.json @@ -0,0 +1,12 @@ +[ + { + "id": 114, + "username": "service_account_33", + "name": "Service account user" + }, + { + "id": 137, + "username": "service_account_34", + "name": "john doe" + } +] \ No newline at end of file diff --git a/users.go b/users.go index f463952ac..dcaf3ba8c 100644 --- a/users.go +++ b/users.go @@ -1589,3 +1589,30 @@ func (s *UsersService) UploadAvatar(avatar io.Reader, filename string, options . return usr, resp, nil } + +// ServiceAccount represents the available ServiceAccount options. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/user_service_accounts.html#list-all-service-account-users + +type ServiceAccount struct { + ID int `json:"id"` + Username string `json:"username"` + Name string `json:"name"` +} + +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 serviceaccounts []*ServiceAccount + resp, err := s.client.Do(req, &serviceaccounts) + if err != nil { + return nil, resp, err + } + + return serviceaccounts, resp, nil +} diff --git a/users_test.go b/users_test.go index 79cede7a9..02dfec49d 100644 --- a/users_test.go +++ b/users_test.go @@ -893,3 +893,29 @@ func TestUploadAvatarUser(t *testing.T) { t.Fatalf("Users.UploadAvatar returns an error: %v", err) } } +func TestGetServiceAccount(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) +} From 4e3e990ea457396ce8275951565cd888e49043a7 Mon Sep 17 00:00:00 2001 From: Aivars Sterns Date: Thu, 12 Sep 2024 11:23:16 +0300 Subject: [PATCH 2/5] fix newline missing --- testdata/get_serviceaccounts.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testdata/get_serviceaccounts.json b/testdata/get_serviceaccounts.json index e3350bcc7..dc63ac48e 100644 --- a/testdata/get_serviceaccounts.json +++ b/testdata/get_serviceaccounts.json @@ -9,4 +9,4 @@ "username": "service_account_34", "name": "john doe" } -] \ No newline at end of file +] From cda09ebf4e6baaf3580b367c392bd0dcf1771f71 Mon Sep 17 00:00:00 2001 From: Aivars Sterns Date: Thu, 12 Sep 2024 11:26:53 +0300 Subject: [PATCH 3/5] add info in readme, move function closer to create function --- README.md | 1 + users.go | 58 +++++++++++++++++++++++++++++-------------------------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index fa5a049a3..dfaba32c4 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ to add new and/or missing endpoints. Currently, the following services are suppo - [x] Todos - [x] Topics - [x] Users +- [x] Users (ServiceAccounts) - [x] Validate CI Configuration - [x] Version - [x] Wikis diff --git a/users.go b/users.go index dcaf3ba8c..667f8e9b5 100644 --- a/users.go +++ b/users.go @@ -1561,6 +1561,37 @@ func (s *UsersService) CreateServiceAccountUser(options ...RequestOptionFunc) (* return usr, resp, nil } +// ServiceAccount represents the available ServiceAccount options. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/user_service_accounts.html#list-all-service-account-users + +type ServiceAccount struct { + ID int `json:"id"` + Username string `json:"username"` + Name string `json:"name"` +} + +// ListServiceAccounts lists all service accounts. Note only administrators can list 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 serviceaccounts []*ServiceAccount + resp, err := s.client.Do(req, &serviceaccounts) + if err != nil { + return nil, resp, err + } + + return serviceaccounts, resp, nil +} + // UploadAvatar uploads an avatar to the current user. // // GitLab API docs: @@ -1589,30 +1620,3 @@ func (s *UsersService) UploadAvatar(avatar io.Reader, filename string, options . return usr, resp, nil } - -// ServiceAccount represents the available ServiceAccount options. -// -// GitLab API docs: -// https://docs.gitlab.com/ee/api/user_service_accounts.html#list-all-service-account-users - -type ServiceAccount struct { - ID int `json:"id"` - Username string `json:"username"` - Name string `json:"name"` -} - -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 serviceaccounts []*ServiceAccount - resp, err := s.client.Do(req, &serviceaccounts) - if err != nil { - return nil, resp, err - } - - return serviceaccounts, resp, nil -} From 7fc874b3dff889b62d4a290957029285b6078f61 Mon Sep 17 00:00:00 2001 From: Aivars Sterns Date: Thu, 12 Sep 2024 11:27:48 +0300 Subject: [PATCH 4/5] fix test function name --- users_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/users_test.go b/users_test.go index 02dfec49d..280959289 100644 --- a/users_test.go +++ b/users_test.go @@ -893,7 +893,7 @@ func TestUploadAvatarUser(t *testing.T) { t.Fatalf("Users.UploadAvatar returns an error: %v", err) } } -func TestGetServiceAccount(t *testing.T) { +func TestListServiceAccounts(t *testing.T) { mux, client := setup(t) path := "/api/v4/service_accounts" From 0c8599a2807c184b26cde0678c29964e4402a372 Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Wed, 25 Sep 2024 10:21:53 +0200 Subject: [PATCH 5/5] Make a few small tweaks --- README.md | 1 - users.go | 37 ++++++++++++++++++------------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index dfaba32c4..fa5a049a3 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,6 @@ to add new and/or missing endpoints. Currently, the following services are suppo - [x] Todos - [x] Topics - [x] Users -- [x] Users (ServiceAccounts) - [x] Validate CI Configuration - [x] Version - [x] Wikis diff --git a/users.go b/users.go index 667f8e9b5..f85667802 100644 --- a/users.go +++ b/users.go @@ -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 @@ -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 { @@ -1561,35 +1572,23 @@ func (s *UsersService) CreateServiceAccountUser(options ...RequestOptionFunc) (* return usr, resp, nil } -// ServiceAccount represents the available ServiceAccount options. +// ListServiceAccounts lists all service accounts. // // GitLab API docs: -// https://docs.gitlab.com/ee/api/user_service_accounts.html#list-all-service-account-users - -type ServiceAccount struct { - ID int `json:"id"` - Username string `json:"username"` - Name string `json:"name"` -} - -// ListServiceAccounts lists all service accounts. Note only administrators can list service accounts. -// -// GitLab API docs: https://docs.gitlab.com/ee/api/users.html#create-service-account-user - +// 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 serviceaccounts []*ServiceAccount - resp, err := s.client.Do(req, &serviceaccounts) + var sas []*ServiceAccount + resp, err := s.client.Do(req, &sas) if err != nil { return nil, resp, err } - return serviceaccounts, resp, nil + return sas, resp, nil } // UploadAvatar uploads an avatar to the current user.