diff --git a/github/admin_orgs.go b/github/admin_orgs.go new file mode 100644 index 00000000000..f063831085b --- /dev/null +++ b/github/admin_orgs.go @@ -0,0 +1,43 @@ +// Copyright 2019 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import "context" + +// createOrgRequest is a subset of Organization and is used internally +// by CreateOrg to pass only the known fields for the endpoint. +type createOrgRequest struct { + Login *string `json:"login,omitempty"` + Admin *string `json:"admin,omitempty"` +} + +// CreateOrg creates a new organization in GitHub Enterprise. +// +// Note that only a subset of the org fields are used and org must +// not be nil. +// +// GitHub Enterprise API docs: https://developer.github.com/enterprise/v3/enterprise-admin/orgs/#create-an-organization +func (s *AdminService) CreateOrg(ctx context.Context, org *Organization, admin string) (*Organization, *Response, error) { + u := "admin/organizations" + + orgReq := &createOrgRequest{ + Login: org.Login, + Admin: &admin, + } + + req, err := s.client.NewRequest("POST", u, orgReq) + if err != nil { + return nil, nil, err + } + + o := new(Organization) + resp, err := s.client.Do(ctx, req, o) + if err != nil { + return nil, resp, err + } + + return o, resp, nil +} diff --git a/github/admin_orgs_test.go b/github/admin_orgs_test.go new file mode 100644 index 00000000000..6966a179c05 --- /dev/null +++ b/github/admin_orgs_test.go @@ -0,0 +1,47 @@ +// Copyright 2019 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "reflect" + "testing" +) + +func TestAdminOrgs_Create(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := &Organization{ + Login: String("github"), + } + + mux.HandleFunc("/admin/organizations", func(w http.ResponseWriter, r *http.Request) { + v := new(createOrgRequest) + json.NewDecoder(r.Body).Decode(v) + + testMethod(t, r, "POST") + want := &createOrgRequest{Login: String("github"), Admin: String("ghAdmin")} + if !reflect.DeepEqual(v, want) { + t.Errorf("Request body = %+v, want %+v", v, want) + } + + fmt.Fprint(w, `{"login":"github","id":1}`) + }) + + org, _, err := client.Admin.CreateOrg(context.Background(), input, "ghAdmin") + if err != nil { + t.Errorf("Admin.CreateOrg returned error: %v", err) + } + + want := &Organization{ID: Int64(1), Login: String("github")} + if !reflect.DeepEqual(org, want) { + t.Errorf("Admin.CreateOrg returned %+v, want %+v", org, want) + } +} diff --git a/github/admin_stats.go b/github/admin_stats.go index b5645f8c176..dabefde3e7b 100644 --- a/github/admin_stats.go +++ b/github/admin_stats.go @@ -10,7 +10,7 @@ import ( "fmt" ) -// AdminStats represents a variety of stats of a Github Enterprise +// AdminStats represents a variety of stats of a GitHub Enterprise // installation. type AdminStats struct { Issues *IssueStats `json:"issues,omitempty"` @@ -147,7 +147,7 @@ func (s RepoStats) String() string { return Stringify(s) } -// GetAdminStats returns a variety of metrics about a Github Enterprise +// GetAdminStats returns a variety of metrics about a GitHub Enterprise // installation. // // Please note that this is only available to site administrators, diff --git a/github/admin_users.go b/github/admin_users.go new file mode 100644 index 00000000000..ea7a47d31ce --- /dev/null +++ b/github/admin_users.go @@ -0,0 +1,61 @@ +// Copyright 2019 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" +) + +// 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"` +} + +// CreateUser creates a new user in GitHub Enterprise. +// +// GitHub Enterprise API docs: https://developer.github.com/enterprise/v3/enterprise-admin/users/#create-a-new-user +func (s *AdminService) CreateUser(ctx context.Context, login, email string) (*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 + } + + var user User + resp, err := s.client.Do(ctx, req, &user) + if err != nil { + return nil, resp, err + } + + return &user, resp, nil +} + +// DeleteUser deletes a user in GitHub Enterprise. +// +// GitHub Enterprise API docs: https://developer.github.com/enterprise/v3/enterprise-admin/users/#delete-a-user +func (s *AdminService) DeleteUser(ctx context.Context, username string) (*Response, error) { + u := "admin/users/" + username + + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} diff --git a/github/admin_users_test.go b/github/admin_users_test.go new file mode 100644 index 00000000000..ad5379e2f2b --- /dev/null +++ b/github/admin_users_test.go @@ -0,0 +1,57 @@ +// Copyright 2019 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "reflect" + "testing" +) + +func TestAdminUsers_Create(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/admin/users", func(w http.ResponseWriter, r *http.Request) { + v := new(createUserRequest) + json.NewDecoder(r.Body).Decode(v) + + testMethod(t, r, "POST") + want := &createUserRequest{Login: String("github"), Email: String("email@domain.com")} + if !reflect.DeepEqual(v, want) { + t.Errorf("Request body = %+v, want %+v", v, want) + } + + fmt.Fprint(w, `{"login":"github","id":1}`) + }) + + org, _, err := client.Admin.CreateUser(context.Background(), "github", "email@domain.com") + if err != nil { + t.Errorf("Admin.CreateUser returned error: %v", err) + } + + want := &User{ID: Int64(1), Login: String("github")} + if !reflect.DeepEqual(org, want) { + t.Errorf("Admin.CreateUser returned %+v, want %+v", org, want) + } +} + +func TestAdminUsers_Delete(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/admin/users/github", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + }) + + _, err := client.Admin.DeleteUser(context.Background(), "github") + if err != nil { + t.Errorf("Admin.DeleteUser returned error: %v", err) + } +}