diff --git a/testdata/create_service_account_user.json b/testdata/create_service_account_user.json new file mode 100644 index 000000000..e5354e550 --- /dev/null +++ b/testdata/create_service_account_user.json @@ -0,0 +1,9 @@ +{ + "id": 999, + "username": "service_account_94e556c44d40d5a710ca59e3a0f40a3d", + "name": "Service account user", + "state": "active", + "locked": false, + "avatar_url": "http://localhost:3000/uploads/user/avatar/999/cd8.jpeg", + "web_url": "http://localhost:3000/service_account_94e556c44d40d5a710ca59e3a0f40a3d" +} diff --git a/users.go b/users.go index 4489034e1..5ad87ca00 100644 --- a/users.go +++ b/users.go @@ -103,6 +103,7 @@ type User struct { UsingLicenseSeat bool `json:"using_license_seat"` CustomAttributes []*CustomAttribute `json:"custom_attributes"` NamespaceID int `json:"namespace_id"` + Locked bool `json:"locked"` } // UserIdentity represents a user identity. @@ -1488,3 +1489,21 @@ 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. +// +// 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 { + return nil, nil, err + } + + usr := new(User) + resp, err := s.client.Do(req, usr) + if err != nil { + return nil, resp, err + } + + return usr, resp, nil +} diff --git a/users_test.go b/users_test.go index 2fca55d0c..9a2afcc46 100644 --- a/users_test.go +++ b/users_test.go @@ -716,3 +716,28 @@ func TestCreatePersonalAccessTokenForCurrentUser(t *testing.T) { } require.Equal(t, want, user) } + +func TestCreateServiceAccountUser(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.MethodPost) + mustWriteHTTPResponse(t, w, "testdata/create_service_account_user.json") + }) + + user, _, err := client.Users.CreateServiceAccountUser() + require.NoError(t, err) + + want := &User{ + ID: 999, + Username: "service_account_94e556c44d40d5a710ca59e3a0f40a3d", + Name: "Service account user", + State: "active", + Locked: false, + AvatarURL: "http://localhost:3000/uploads/user/avatar/999/cd8.jpeg", + WebURL: "http://localhost:3000/service_account_94e556c44d40d5a710ca59e3a0f40a3d", + } + require.Equal(t, want, user) +}