diff --git a/github/admin_users.go b/github/admin_users.go index 0d3fe9fafc..e134ff34db 100644 --- a/github/admin_users.go +++ b/github/admin_users.go @@ -10,11 +10,12 @@ 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. @@ -22,14 +23,9 @@ type createUserRequest struct { // GitHub API docs: https://docs.github.com/enterprise-server@3.11/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 diff --git a/github/admin_users_test.go b/github/admin_users_test.go index 2dac611709..d29a7e672f 100644 --- a/github/admin_users_test.go +++ b/github/admin_users_test.go @@ -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@domain.com")} + want := &CreateUserRequest{Login: "github", Email: String("email@domain.com"), Suspended: Bool(false)} if !cmp.Equal(v, want) { t.Errorf("Request body = %+v, want %+v", v, want) } @@ -34,7 +34,11 @@ func TestAdminUsers_Create(t *testing.T) { }) ctx := context.Background() - org, _, err := client.Admin.CreateUser(ctx, "github", "email@domain.com") + org, _, err := client.Admin.CreateUser(ctx, CreateUserRequest{ + Login: "github", + Email: String("email@domain.com"), + Suspended: Bool(false), + }) if err != nil { t.Errorf("Admin.CreateUser returned error: %v", err) } @@ -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@domain.com") + got, resp, err := client.Admin.CreateUser(ctx, CreateUserRequest{ + Login: "github", + Email: String("email@domain.com"), + Suspended: Bool(false), + }) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -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"), } diff --git a/github/github-accessors.go b/github/github-accessors.go index 5a763df1e2..3b1a662966 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -4478,6 +4478,22 @@ func (c *CreateUserProjectOptions) GetBody() string { return *c.Body } +// GetEmail returns the Email field if it's non-nil, zero value otherwise. +func (c *CreateUserRequest) GetEmail() string { + if c == nil || c.Email == nil { + return "" + } + return *c.Email +} + +// GetSuspended returns the Suspended field if it's non-nil, zero value otherwise. +func (c *CreateUserRequest) GetSuspended() bool { + if c == nil || c.Suspended == nil { + return false + } + return *c.Suspended +} + // GetCreated returns the Created field if it's non-nil, zero value otherwise. func (c *CreationInfo) GetCreated() Timestamp { if c == nil || c.Created == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 5c291306ab..f4c71742ef 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -5262,6 +5262,26 @@ func TestCreateUserProjectOptions_GetBody(tt *testing.T) { c.GetBody() } +func TestCreateUserRequest_GetEmail(tt *testing.T) { + var zeroValue string + c := &CreateUserRequest{Email: &zeroValue} + c.GetEmail() + c = &CreateUserRequest{} + c.GetEmail() + c = nil + c.GetEmail() +} + +func TestCreateUserRequest_GetSuspended(tt *testing.T) { + var zeroValue bool + c := &CreateUserRequest{Suspended: &zeroValue} + c.GetSuspended() + c = &CreateUserRequest{} + c.GetSuspended() + c = nil + c.GetSuspended() +} + func TestCreationInfo_GetCreated(tt *testing.T) { var zeroValue Timestamp c := &CreationInfo{Created: &zeroValue}