-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods for GitHub Enterprise endpoints (#1214)
- Loading branch information
Showing
5 changed files
with
210 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |