Skip to content

Commit

Permalink
Add methods for GitHub Enterprise endpoints (#1214)
Browse files Browse the repository at this point in the history
  • Loading branch information
fiahil authored and gmlewis committed Jul 8, 2019
1 parent 1bab17e commit cad32f2
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 2 deletions.
43 changes: 43 additions & 0 deletions github/admin_orgs.go
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 47 additions & 0 deletions github/admin_orgs_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 2 additions & 2 deletions github/admin_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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,
Expand Down
61 changes: 61 additions & 0 deletions github/admin_users.go
Original file line number Diff line number Diff line change
@@ -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
}
57 changes: 57 additions & 0 deletions github/admin_users_test.go
Original file line number Diff line number Diff line change
@@ -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 protected]")}
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 protected]")
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)
}
}

0 comments on commit cad32f2

Please sign in to comment.