Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1967 Adding rename method #1968

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
17 changes: 17 additions & 0 deletions src/main/java/org/kohsuke/github/GHBranch.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,23 @@ public GHCommit merge(String head, String commitMessage) throws IOException {
return result;
}

/**
* Rename this branch.
*
* @param name
* the name
* @throws IOException
* the io exception
* @see https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#rename-a-branch
*/
public GHBranch rename(String name) throws IOException {
return root().createRequest()
.method("POST")
.with("new_name", name)
.withUrlPath(getApiRoute() + "/rename")
.fetch(GHBranch.class);
}

/**
* Gets the api route.
*
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/kohsuke/github/GHBranchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public GHBranchTest() {

private static final String BRANCH_1 = "testBranch1";
private static final String BRANCH_2 = "testBranch2";
private static final String BRANCH_3 = "testBranch3";

private GHRepository repository;

Expand Down Expand Up @@ -74,4 +75,26 @@ public void testMergeBranch() throws Exception {
// Should be null since all changes already merged
assertThat(mergeCommit, nullValue());
}

/**
* Test merge rename.
*
* @throws Exception
* the exception
*/
@Test
public void testBranchRename() throws Exception {
repository = getTempRepository();
String mainHead = repository.getRef("heads/main").getObject().getSha();

String branchName = "refs/heads/" + BRANCH_3;
repository.createRef(branchName, mainHead);
repository.createContent().content(branchName).message(branchName).path(branchName).branch(branchName).commit();

GHBranch otherBranch = repository.getBranch(BRANCH_3);
otherBranch.rename(BRANCH_3 + "_renamed");
GHBranch otherBranchRenamed = repository.getBranch(BRANCH_3 + "_renamed");
assertThat(otherBranchRenamed, notNullValue());
assertThat(otherBranchRenamed.getName(), equalTo(BRANCH_3 + "_renamed"));
}
}
Loading