From b1829e57ac8cdf9fb6ca1e8b7e0a462ff9588e56 Mon Sep 17 00:00:00 2001 From: Quentin Leffray Date: Thu, 24 Jan 2019 13:25:17 +0100 Subject: [PATCH 1/3] Add admin methods for GHE endpoints add create organization method for Admin API add GHE admin create user add GHE admin create impersonation token add DeleteUser endpoint to go-github Co-authored-by: Quentin Leffray Co-authored-by: David Dumas Co-authored-by: Clement Roche --- github/admin_orgs.go | 44 +++++++++++++++++++++++++++ github/admin_orgs_test.go | 47 +++++++++++++++++++++++++++++ github/admin_users.go | 62 ++++++++++++++++++++++++++++++++++++++ github/admin_users_test.go | 57 +++++++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+) create mode 100644 github/admin_orgs.go create mode 100644 github/admin_orgs_test.go create mode 100644 github/admin_users.go create mode 100644 github/admin_users_test.go diff --git a/github/admin_orgs.go b/github/admin_orgs.go new file mode 100644 index 00000000000..ea04d6f90af --- /dev/null +++ b/github/admin_orgs.go @@ -0,0 +1,44 @@ +// Copyright 2013 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 Create to pass only the known fields for the endpoint +// +type createOrgRequest struct { + Login *string `json:"login"` + Admin *string `json:"admin"` +} + +// 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/2.16/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..bc829e7ac44 --- /dev/null +++ b/github/admin_orgs_test.go @@ -0,0 +1,47 @@ +// Copyright 2013 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_users.go b/github/admin_users.go new file mode 100644 index 00000000000..cc3f1015e76 --- /dev/null +++ b/github/admin_users.go @@ -0,0 +1,62 @@ +// Copyright 2013 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 Create to pass only the known fields for the endpoint +// +type createUserRequest struct { + Login *string `json:"login"` + Email *string `json:"email"` +} + +// CreateUser creates a new user in Github Enterprise. +// +// Github Enterprise API docs: https://developer.github.com/enterprise/2.16/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. +// +// https://developer.github.com/enterprise/2.16/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..37a5dc1d099 --- /dev/null +++ b/github/admin_users_test.go @@ -0,0 +1,57 @@ +// Copyright 2013 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) + } +} From c9a287e4ae3b76908c785247bff4815c4a139443 Mon Sep 17 00:00:00 2001 From: Quentin Leffray Date: Wed, 3 Jul 2019 11:46:30 +0200 Subject: [PATCH 2/3] fix go docs --- github/admin_orgs.go | 15 +++++++-------- github/admin_orgs_test.go | 2 +- github/admin_stats.go | 4 ++-- github/admin_users.go | 17 ++++++++--------- github/admin_users_test.go | 2 +- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/github/admin_orgs.go b/github/admin_orgs.go index ea04d6f90af..f063831085b 100644 --- a/github/admin_orgs.go +++ b/github/admin_orgs.go @@ -1,4 +1,4 @@ -// Copyright 2013 The go-github AUTHORS. All rights reserved. +// 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. @@ -8,19 +8,18 @@ package github import "context" // createOrgRequest is a subset of Organization and is used internally -// by Create to pass only the known fields for the endpoint -// +// by CreateOrg to pass only the known fields for the endpoint. type createOrgRequest struct { - Login *string `json:"login"` - Admin *string `json:"admin"` + Login *string `json:"login,omitempty"` + Admin *string `json:"admin,omitempty"` } -// CreateOrg creates a new organization in Github Enterprise. +// 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 +// not be nil. // -// Github Enterprise API docs: https://developer.github.com/enterprise/2.16/v3/enterprise-admin/orgs/#create-an-organization +// 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" diff --git a/github/admin_orgs_test.go b/github/admin_orgs_test.go index bc829e7ac44..6966a179c05 100644 --- a/github/admin_orgs_test.go +++ b/github/admin_orgs_test.go @@ -1,4 +1,4 @@ -// Copyright 2013 The go-github AUTHORS. All rights reserved. +// 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. 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 index cc3f1015e76..51ddbeae01a 100644 --- a/github/admin_users.go +++ b/github/admin_users.go @@ -1,4 +1,4 @@ -// Copyright 2013 The go-github AUTHORS. All rights reserved. +// 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. @@ -10,16 +10,15 @@ import ( ) // createUserRequest is a subset of User and is used internally -// by Create to pass only the known fields for the endpoint -// +// by CreateUser to pass only the known fields for the endpoint. type createUserRequest struct { - Login *string `json:"login"` - Email *string `json:"email"` + Login *string `json:"login,omitempty"` + Email *string `json:"email,omitempty"` } -// CreateUser creates a new user in Github Enterprise. +// CreateUser creates a new user in GitHub Enterprise. // -// Github Enterprise API docs: https://developer.github.com/enterprise/2.16/v3/enterprise-admin/users/#create-a-new-user +// 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" @@ -42,9 +41,9 @@ func (s *AdminService) CreateUser(ctx context.Context, login, email string) (*Us return &user, resp, nil } -// DeleteUser deletes a user in Github Enterprise. +// DeleteUser deletes a user in GitHub Enterprise. // -// https://developer.github.com/enterprise/2.16/v3/enterprise-admin/users/#delete-a-user +// 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 diff --git a/github/admin_users_test.go b/github/admin_users_test.go index 37a5dc1d099..ad5379e2f2b 100644 --- a/github/admin_users_test.go +++ b/github/admin_users_test.go @@ -1,4 +1,4 @@ -// Copyright 2013 The go-github AUTHORS. All rights reserved. +// 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. From 52819cbd07e61f90ede3133a9a03599de22e88f6 Mon Sep 17 00:00:00 2001 From: Quentin Leffray Date: Thu, 4 Jul 2019 15:09:54 +0200 Subject: [PATCH 3/3] fix doc --- github/admin_users.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/admin_users.go b/github/admin_users.go index 51ddbeae01a..ea7a47d31ce 100644 --- a/github/admin_users.go +++ b/github/admin_users.go @@ -43,7 +43,7 @@ func (s *AdminService) CreateUser(ctx context.Context, login, email string) (*Us // DeleteUser deletes a user in GitHub Enterprise. // -// https://developer.github.com/enterprise/v3/enterprise-admin/users/#delete-a-user +// 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