Skip to content
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
18 changes: 7 additions & 11 deletions github/admin_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,22 @@ import (
"fmt"
)

// createUserRequest is a subset of User and is used internally
// by CreateUser to pass only the known fields for the endpoint.
type createUserRequest struct {
Login *string `json:"login,omitempty"`
Email *string `json:"email,omitempty"`
// CreateUserRequest represents the fields sent to the `CreateUser` endpoint.
// Note that `Login` is a required field.
type CreateUserRequest struct {
Login string `json:"login"`
Email *string `json:"email,omitempty"`
Suspended *bool `json:"suspended,omitempty"`
}

// CreateUser creates a new user in GitHub Enterprise.
//
// GitHub API docs: https://docs.github.com/[email protected]/rest/enterprise-admin/users#create-a-user
//
//meta:operation POST /admin/users
func (s *AdminService) CreateUser(ctx context.Context, login, email string) (*User, *Response, error) {
func (s *AdminService) CreateUser(ctx context.Context, userReq CreateUserRequest) (*User, *Response, error) {
u := "admin/users"

userReq := &createUserRequest{
Login: &login,
Email: &email,
}

req, err := s.client.NewRequest("POST", u, userReq)
if err != nil {
return nil, nil, err
Expand Down
22 changes: 15 additions & 7 deletions github/admin_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ func TestAdminUsers_Create(t *testing.T) {
defer teardown()

mux.HandleFunc("/admin/users", func(w http.ResponseWriter, r *http.Request) {
v := new(createUserRequest)
v := new(CreateUserRequest)
assertNilError(t, json.NewDecoder(r.Body).Decode(v))

testMethod(t, r, "POST")
want := &createUserRequest{Login: String("github"), Email: String("[email protected]")}
want := &CreateUserRequest{Login: "github", Email: String("[email protected]"), Suspended: Bool(false)}
if !cmp.Equal(v, want) {
t.Errorf("Request body = %+v, want %+v", v, want)
}
Expand All @@ -34,7 +34,11 @@ func TestAdminUsers_Create(t *testing.T) {
})

ctx := context.Background()
org, _, err := client.Admin.CreateUser(ctx, "github", "[email protected]")
org, _, err := client.Admin.CreateUser(ctx, CreateUserRequest{
Login: "github",
Email: String("[email protected]"),
Suspended: Bool(false),
})
if err != nil {
t.Errorf("Admin.CreateUser returned error: %v", err)
}
Expand All @@ -46,7 +50,11 @@ func TestAdminUsers_Create(t *testing.T) {

const methodName = "CreateUser"
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Admin.CreateUser(ctx, "github", "[email protected]")
got, resp, err := client.Admin.CreateUser(ctx, CreateUserRequest{
Login: "github",
Email: String("[email protected]"),
Suspended: Bool(false),
})
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand Down Expand Up @@ -177,10 +185,10 @@ func TestUserImpersonation_Delete(t *testing.T) {
}

func TestCreateUserRequest_Marshal(t *testing.T) {
testJSONMarshal(t, &createUserRequest{}, "{}")
testJSONMarshal(t, &CreateUserRequest{}, "{}")

u := &createUserRequest{
Login: String("l"),
u := &CreateUserRequest{
Login: "l",
Email: String("e"),
}

Expand Down
16 changes: 16 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.