Skip to content

Commit 0511421

Browse files
authored
Add RenameBranch support (#2119)
1 parent 07267ab commit 0511421

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

github/repos.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,34 @@ func (s *RepositoriesService) getBranchFromURL(ctx context.Context, u string, fo
10731073
return resp, err
10741074
}
10751075

1076+
// renameBranchRequest represents a request to rename a branch.
1077+
type renameBranchRequest struct {
1078+
NewName string `json:"new_name"`
1079+
}
1080+
1081+
// RenameBranch renames a branch in a repository.
1082+
//
1083+
// To rename a non-default branch: Users must have push access. GitHub Apps must have the `contents:write` repository permission.
1084+
// To rename the default branch: Users must have admin or owner permissions. GitHub Apps must have the `administration:write` repository permission.
1085+
//
1086+
// GitHub API docs: https://docs.github.com/en/rest/reference/repos#rename-a-branch
1087+
func (s *RepositoriesService) RenameBranch(ctx context.Context, owner, repo, branch, newName string) (*Branch, *Response, error) {
1088+
u := fmt.Sprintf("repos/%v/%v/branches/%v/rename", owner, repo, branch)
1089+
r := &renameBranchRequest{NewName: newName}
1090+
req, err := s.client.NewRequest("POST", u, r)
1091+
if err != nil {
1092+
return nil, nil, err
1093+
}
1094+
1095+
b := new(Branch)
1096+
resp, err := s.client.Do(ctx, req, b)
1097+
if err != nil {
1098+
return nil, resp, err
1099+
}
1100+
1101+
return b, resp, nil
1102+
}
1103+
10761104
// GetBranchProtection gets the protection of a given branch.
10771105
//
10781106
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-branch-protection

github/repos_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,51 @@ func TestRepositoriesService_GetBranch_notFound(t *testing.T) {
981981
})
982982
}
983983

984+
func TestRepositoriesService_RenameBranch(t *testing.T) {
985+
client, mux, _, teardown := setup()
986+
defer teardown()
987+
988+
renameBranchReq := "nn"
989+
990+
mux.HandleFunc("/repos/o/r/branches/b/rename", func(w http.ResponseWriter, r *http.Request) {
991+
v := new(renameBranchRequest)
992+
json.NewDecoder(r.Body).Decode(v)
993+
994+
testMethod(t, r, "POST")
995+
want := &renameBranchRequest{NewName: "nn"}
996+
if !cmp.Equal(v, want) {
997+
t.Errorf("Request body = %+v, want %+v", v, want)
998+
}
999+
1000+
fmt.Fprint(w, `{"protected":true,"name":"nn"}`)
1001+
})
1002+
1003+
ctx := context.Background()
1004+
got, _, err := client.Repositories.RenameBranch(ctx, "o", "r", "b", renameBranchReq)
1005+
if err != nil {
1006+
t.Errorf("Repositories.RenameBranch returned error: %v", err)
1007+
}
1008+
1009+
want := &Branch{Name: String("nn"), Protected: Bool(true)}
1010+
if !cmp.Equal(got, want) {
1011+
t.Errorf("Repositories.RenameBranch returned %+v, want %+v", got, want)
1012+
}
1013+
1014+
const methodName = "RenameBranch"
1015+
testBadOptions(t, methodName, func() (err error) {
1016+
_, _, err = client.Repositories.RenameBranch(ctx, "\n", "\n", "\n", renameBranchReq)
1017+
return err
1018+
})
1019+
1020+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
1021+
got, resp, err := client.Repositories.RenameBranch(ctx, "o", "r", "b", renameBranchReq)
1022+
if got != nil {
1023+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
1024+
}
1025+
return resp, err
1026+
})
1027+
}
1028+
9841029
func TestRepositoriesService_GetBranchProtection(t *testing.T) {
9851030
client, mux, _, teardown := setup()
9861031
defer teardown()

0 commit comments

Comments
 (0)