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

Commit

Permalink
Merge pull request #1848 from mycrEEpy/service-user
Browse files Browse the repository at this point in the history
Add CreateServiceAccountUser
  • Loading branch information
svanharmelen authored Dec 9, 2023
2 parents 151995e + 7c3f449 commit f344220
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
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)
}

0 comments on commit f344220

Please sign in to comment.