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

Add CreateServiceAccountUser #1848

Merged
merged 1 commit into from
Dec 9, 2023
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
9 changes: 9 additions & 0 deletions testdata/create_service_account_user.json
Original file line number Diff line number Diff line change
@@ -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"
}
19 changes: 19 additions & 0 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
25 changes: 25 additions & 0 deletions users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading