Skip to content

Commit 4a7cf4a

Browse files
authored
Add RenameOrg endpoint for Org Admin API (google#1480)
Fixes google#1479.
1 parent 34cb1d6 commit 4a7cf4a

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

github/admin_orgs.go

+47-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
package github
77

8-
import "context"
8+
import (
9+
"context"
10+
"fmt"
11+
)
912

1013
// createOrgRequest is a subset of Organization and is used internally
1114
// by CreateOrg to pass only the known fields for the endpoint.
@@ -41,3 +44,46 @@ func (s *AdminService) CreateOrg(ctx context.Context, org *Organization, admin s
4144

4245
return o, resp, nil
4346
}
47+
48+
// renameOrgRequest is a subset of Organization and is used internally
49+
// by RenameOrg and RenameOrgByName to pass only the known fields for the endpoint.
50+
type renameOrgRequest struct {
51+
Login *string `json:"login,omitempty"`
52+
}
53+
54+
// RenameOrgResponse is the response given when renaming an Organization.
55+
type RenameOrgResponse struct {
56+
Message *string `json:"message,omitempty"`
57+
URL *string `json:"url,omitempty"`
58+
}
59+
60+
// RenameOrg renames an organization in GitHub Enterprise.
61+
//
62+
// GitHub Enterprise API docs: https://developer.github.com/enterprise/v3/enterprise-admin/orgs/#rename-an-organization
63+
func (s *AdminService) RenameOrg(ctx context.Context, org *Organization, newName string) (*RenameOrgResponse, *Response, error) {
64+
return s.RenameOrgByName(ctx, *org.Login, newName)
65+
}
66+
67+
// RenameOrgByName renames an organization in GitHub Enterprise using its current name.
68+
//
69+
// GitHub Enterprise API docs: https://developer.github.com/enterprise/v3/enterprise-admin/orgs/#rename-an-organization
70+
func (s *AdminService) RenameOrgByName(ctx context.Context, org, newName string) (*RenameOrgResponse, *Response, error) {
71+
u := fmt.Sprintf("admin/organizations/%v", org)
72+
73+
orgReq := &renameOrgRequest{
74+
Login: &newName,
75+
}
76+
77+
req, err := s.client.NewRequest("PATCH", u, orgReq)
78+
if err != nil {
79+
return nil, nil, err
80+
}
81+
82+
o := new(RenameOrgResponse)
83+
resp, err := s.client.Do(ctx, req, o)
84+
if err != nil {
85+
return nil, resp, err
86+
}
87+
88+
return o, resp, nil
89+
}

github/admin_orgs_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,63 @@ func TestAdminOrgs_Create(t *testing.T) {
4545
t.Errorf("Admin.CreateOrg returned %+v, want %+v", org, want)
4646
}
4747
}
48+
49+
func TestAdminOrgs_Rename(t *testing.T) {
50+
client, mux, _, teardown := setup()
51+
defer teardown()
52+
53+
input := &Organization{
54+
Login: String("o"),
55+
}
56+
57+
mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) {
58+
v := new(renameOrgRequest)
59+
json.NewDecoder(r.Body).Decode(v)
60+
61+
testMethod(t, r, "PATCH")
62+
want := &renameOrgRequest{Login: String("the-new-octocats")}
63+
if !reflect.DeepEqual(v, want) {
64+
t.Errorf("Request body = %+v, want %+v", v, want)
65+
}
66+
67+
fmt.Fprint(w, `{"message":"Job queued to rename organization. It may take a few minutes to complete.","url":"https://<hostname>/api/v3/organizations/1"}`)
68+
})
69+
70+
resp, _, err := client.Admin.RenameOrg(context.Background(), input, "the-new-octocats")
71+
if err != nil {
72+
t.Errorf("Admin.RenameOrg returned error: %v", err)
73+
}
74+
75+
want := &RenameOrgResponse{Message: String("Job queued to rename organization. It may take a few minutes to complete."), URL: String("https://<hostname>/api/v3/organizations/1")}
76+
if !reflect.DeepEqual(resp, want) {
77+
t.Errorf("Admin.RenameOrg returned %+v, want %+v", resp, want)
78+
}
79+
}
80+
81+
func TestAdminOrgs_RenameByName(t *testing.T) {
82+
client, mux, _, teardown := setup()
83+
defer teardown()
84+
85+
mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) {
86+
v := new(renameOrgRequest)
87+
json.NewDecoder(r.Body).Decode(v)
88+
89+
testMethod(t, r, "PATCH")
90+
want := &renameOrgRequest{Login: String("the-new-octocats")}
91+
if !reflect.DeepEqual(v, want) {
92+
t.Errorf("Request body = %+v, want %+v", v, want)
93+
}
94+
95+
fmt.Fprint(w, `{"message":"Job queued to rename organization. It may take a few minutes to complete.","url":"https://<hostname>/api/v3/organizations/1"}`)
96+
})
97+
98+
resp, _, err := client.Admin.RenameOrgByName(context.Background(), "o", "the-new-octocats")
99+
if err != nil {
100+
t.Errorf("Admin.RenameOrg returned error: %v", err)
101+
}
102+
103+
want := &RenameOrgResponse{Message: String("Job queued to rename organization. It may take a few minutes to complete."), URL: String("https://<hostname>/api/v3/organizations/1")}
104+
if !reflect.DeepEqual(resp, want) {
105+
t.Errorf("Admin.RenameOrg returned %+v, want %+v", resp, want)
106+
}
107+
}

github/github-accessors.go

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)