Repository: google/go-github
Pull request #1480 (2020-04-03 06:51:07)

Add RenameOrg endpoint for Org Admin API
Adds RenameOrg and RenameOrgByName functions for the Github Enterprise Organization Admin API. Addresses #1479.

codecov[bot]: # [Codecov](https://codecov.io/gh/google/go-github/pull/1480?src=pr&el=h1) Report
> Merging [#1480](https://codecov.io/gh/google/go-github/pull/1480?src=pr&el=desc) into [master](https://codecov.io/gh/google/go-github/commit/34cb1d623f03e277545da01608448d9fea80dc3b&el=desc) will **increase** coverage by `5.09%`.
> The diff coverage is `71.42%`.

[![Impacted file tree graph](https://codecov.io/gh/google/go-github/pull/1480/graphs/tree.svg?width=650&height=150&src=pr&token=EgYjGjZlI0)](https://codecov.io/gh/google/go-github/pull/1480?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1480      +/-   ##
==========================================
+ Coverage   68.15%   73.25%   +5.09%     
==========================================
  Files          94       94              
  Lines        8417     6794    -1623     
==========================================
- Hits         5737     4977     -760     
+ Misses       1819      954     -865     
- Partials      861      863       +2     
```


| [Impacted Files](https://codecov.io/gh/google/go-github/pull/1480?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [github/admin\_orgs.go](https://codecov.io/gh/google/go-github/pull/1480/diff?src=pr&el=tree#diff-Z2l0aHViL2FkbWluX29yZ3MuZ28=) | `70.37% <71.42%> (+3.70%)` | :arrow_up: |
| [github/github.go](https://codecov.io/gh/google/go-github/pull/1480/diff?src=pr&el=tree#diff-Z2l0aHViL2dpdGh1Yi5nbw==) | `89.62% <0.00%> (-0.03%)` | :arrow_down: |
| [github/admin.go](https://codecov.io/gh/google/go-github/pull/1480/diff?src=pr&el=tree#diff-Z2l0aHViL2FkbWluLmdv) | `100.00% <0.00%> (ø)` | |
| [github/strings.go](https://codecov.io/gh/google/go-github/pull/1480/diff?src=pr&el=tree#diff-Z2l0aHViL3N0cmluZ3MuZ28=) | `100.00% <0.00%> (ø)` | |
| [github/activity.go](https://codecov.io/gh/google/go-github/pull/1480/diff?src=pr&el=tree#diff-Z2l0aHViL2FjdGl2aXR5Lmdv) | `100.00% <0.00%> (ø)` | |
| [github/timestamp.go](https://codecov.io/gh/google/go-github/pull/1480/diff?src=pr&el=tree#diff-Z2l0aHViL3RpbWVzdGFtcC5nbw==) | `100.00% <0.00%> (ø)` | |
| [github/admin\_stats.go](https://codecov.io/gh/google/go-github/pull/1480/diff?src=pr&el=tree#diff-Z2l0aHViL2FkbWluX3N0YXRzLmdv) | `100.00% <0.00%> (ø)` | |
| [github/repos\_traffic.go](https://codecov.io/gh/google/go-github/pull/1480/diff?src=pr&el=tree#diff-Z2l0aHViL3JlcG9zX3RyYWZmaWMuZ28=) | `100.00% <0.00%> (ø)` | |
| [github/pulls\_reviewers.go](https://codecov.io/gh/google/go-github/pull/1480/diff?src=pr&el=tree#diff-Z2l0aHViL3B1bGxzX3Jldmlld2Vycy5nbw==) | `100.00% <0.00%> (ø)` | |
| [github/git\_commits.go](https://codecov.io/gh/google/go-github/pull/1480/diff?src=pr&el=tree#diff-Z2l0aHViL2dpdF9jb21taXRzLmdv) | `91.30% <0.00%> (+1.30%)` | :arrow_up: |
| ... and [82 more](https://codecov.io/gh/google/go-github/pull/1480/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/google/go-github/pull/1480?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/google/go-github/pull/1480?src=pr&el=footer). Last update [34cb1d6...4e6a321](https://codecov.io/gh/google/go-github/pull/1480?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

nightlark: I'm not sure why either. I think it was late and wanted CI test results before looking over it again today.
gmlewis: Thank you, @wesleimp!
Merging.

Patch:
diff --git a/github/admin_orgs.go b/github/admin_orgs.go
index f0638310..448e51f6 100644
--- a/github/admin_orgs.go
+++ b/github/admin_orgs.go
@@ -5,7 +5,10 @@
 
 package github
 
-import "context"
+import (
+	"context"
+	"fmt"
+)
 
 // createOrgRequest is a subset of Organization and is used internally
 // 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
 
 	return o, resp, nil
 }
+
+// renameOrgRequest is a subset of Organization and is used internally
+// by RenameOrg and RenameOrgByName to pass only the known fields for the endpoint.
+type renameOrgRequest struct {
+	Login *string `json:"login,omitempty"`
+}
+
+// RenameOrgResponse is the response given when renaming an Organization.
+type RenameOrgResponse struct {
+	Message *string `json:"message,omitempty"`
+	URL     *string `json:"url,omitempty"`
+}
+
+// RenameOrg renames an organization in GitHub Enterprise.
+//
+// GitHub Enterprise API docs: https://developer.github.com/enterprise/v3/enterprise-admin/orgs/#rename-an-organization
+func (s *AdminService) RenameOrg(ctx context.Context, org *Organization, newName string) (*RenameOrgResponse, *Response, error) {
+	return s.RenameOrgByName(ctx, *org.Login, newName)
+}
+
+// RenameOrgByName renames an organization in GitHub Enterprise using its current name.
+//
+// GitHub Enterprise API docs: https://developer.github.com/enterprise/v3/enterprise-admin/orgs/#rename-an-organization
+func (s *AdminService) RenameOrgByName(ctx context.Context, org, newName string) (*RenameOrgResponse, *Response, error) {
+	u := fmt.Sprintf("admin/organizations/%v", org)
+
+	orgReq := &renameOrgRequest{
+		Login: &newName,
+	}
+
+	req, err := s.client.NewRequest("PATCH", u, orgReq)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	o := new(RenameOrgResponse)
+	resp, err := s.client.Do(ctx, req, o)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return o, resp, nil
+}
diff --git a/github/github-accessors.go b/github/github-accessors.go
index 27e8b136..4141b9cd 100644
--- a/github/github-accessors.go
+++ b/github/github-accessors.go
@@ -9996,6 +9996,22 @@ func (r *Rename) GetTo() string {
 	return *r.To
 }
 
+// GetMessage returns the Message field if it's non-nil, zero value otherwise.
+func (r *RenameOrgResponse) GetMessage() string {
+	if r == nil || r.Message == nil {
+		return ""
+	}
+	return *r.Message
+}
+
+// GetURL returns the URL field if it's non-nil, zero value otherwise.
+func (r *RenameOrgResponse) GetURL() string {
+	if r == nil || r.URL == nil {
+		return ""
+	}
+	return *r.URL
+}
+
 // GetIncompleteResults returns the IncompleteResults field if it's non-nil, zero value otherwise.
 func (r *RepositoriesSearchResult) GetIncompleteResults() bool {
 	if r == nil || r.IncompleteResults == nil {


Test patch:
diff --git a/github/admin_orgs_test.go b/github/admin_orgs_test.go
index 6966a179..4e0d6e0f 100644
--- a/github/admin_orgs_test.go
+++ b/github/admin_orgs_test.go
@@ -45,3 +45,63 @@ func TestAdminOrgs_Create(t *testing.T) {
 		t.Errorf("Admin.CreateOrg returned %+v, want %+v", org, want)
 	}
 }
+
+func TestAdminOrgs_Rename(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	input := &Organization{
+		Login: String("o"),
+	}
+
+	mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) {
+		v := new(renameOrgRequest)
+		json.NewDecoder(r.Body).Decode(v)
+
+		testMethod(t, r, "PATCH")
+		want := &renameOrgRequest{Login: String("the-new-octocats")}
+		if !reflect.DeepEqual(v, want) {
+			t.Errorf("Request body = %+v, want %+v", v, want)
+		}
+
+		fmt.Fprint(w, `{"message":"Job queued to rename organization. It may take a few minutes to complete.","url":"https://<hostname>/api/v3/organizations/1"}`)
+	})
+
+	resp, _, err := client.Admin.RenameOrg(context.Background(), input, "the-new-octocats")
+	if err != nil {
+		t.Errorf("Admin.RenameOrg returned error: %v", err)
+	}
+
+	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")}
+	if !reflect.DeepEqual(resp, want) {
+		t.Errorf("Admin.RenameOrg returned %+v, want %+v", resp, want)
+	}
+}
+
+func TestAdminOrgs_RenameByName(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/admin/organizations/o", func(w http.ResponseWriter, r *http.Request) {
+		v := new(renameOrgRequest)
+		json.NewDecoder(r.Body).Decode(v)
+
+		testMethod(t, r, "PATCH")
+		want := &renameOrgRequest{Login: String("the-new-octocats")}
+		if !reflect.DeepEqual(v, want) {
+			t.Errorf("Request body = %+v, want %+v", v, want)
+		}
+
+		fmt.Fprint(w, `{"message":"Job queued to rename organization. It may take a few minutes to complete.","url":"https://<hostname>/api/v3/organizations/1"}`)
+	})
+
+	resp, _, err := client.Admin.RenameOrgByName(context.Background(), "o", "the-new-octocats")
+	if err != nil {
+		t.Errorf("Admin.RenameOrg returned error: %v", err)
+	}
+
+	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")}
+	if !reflect.DeepEqual(resp, want) {
+		t.Errorf("Admin.RenameOrg returned %+v, want %+v", resp, want)
+	}
+}