Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suspended as option to AdminService.CreateUser() #3049

Merged
merged 3 commits into from Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 10 additions & 11 deletions github/admin_users.go
Expand Up @@ -10,26 +10,25 @@ 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"`
ttomsu marked this conversation as resolved.
Show resolved Hide resolved
Email *string `json:"email,omitempty"`
Suspended *bool `json:"suspended,omitempty"`
}

type CreateUserOptions struct {
ttomsu marked this conversation as resolved.
Show resolved Hide resolved
}

// CreateUser creates a new user in GitHub Enterprise.
//
// 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) {
ttomsu marked this conversation as resolved.
Show resolved Hide resolved
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
20 changes: 14 additions & 6 deletions github/admin_users_test.go
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@domain.com")}
want := &CreateUserRequest{Login: String("github"), Email: String("email@domain.com"), 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@domain.com")
org, _, err := client.Admin.CreateUser(ctx, &CreateUserRequest{
Login: String("github"),
Email: String("email@domain.com"),
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@domain.com")
got, resp, err := client.Admin.CreateUser(ctx, &CreateUserRequest{
Login: String("github"),
Email: String("email@domain.com"),
Suspended: Bool(false),
})
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
Expand Down Expand Up @@ -177,9 +185,9 @@ func TestUserImpersonation_Delete(t *testing.T) {
}

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

u := &createUserRequest{
u := &CreateUserRequest{
Login: String("l"),
Email: String("e"),
}
Expand Down
24 changes: 24 additions & 0 deletions github/github-accessors.go

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

30 changes: 30 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.