From 315e6e5297aad07d2bf5b36965346ef598882411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Bouchex=20Bellomi=C3=A9?= Date: Thu, 3 Oct 2024 15:35:52 +0200 Subject: [PATCH 1/6] #1967 Adding rename method --- src/main/java/org/kohsuke/github/GHBranch.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/org/kohsuke/github/GHBranch.java b/src/main/java/org/kohsuke/github/GHBranch.java index f803c67e70..e9e1f4f9f9 100644 --- a/src/main/java/org/kohsuke/github/GHBranch.java +++ b/src/main/java/org/kohsuke/github/GHBranch.java @@ -192,6 +192,19 @@ public GHCommit merge(String head, String commitMessage) throws IOException { return result; } + /** + * Rename this branch. + * + * @param name + * the name + * @throws IOException + * the io exception + */ + @CheckForNull + public void rename(String name) throws IOException { + root().createRequest().method("POST").with("new_name", name).withUrlPath(getApiRoute() + "/rename").send(); + } + /** * Gets the api route. * From fd8a21db04d9f4b31afbc23360a4a46f2051bd19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Bouchex=20Bellomi=C3=A9?= Date: Thu, 3 Oct 2024 15:46:02 +0200 Subject: [PATCH 2/6] #1967 Adding rename method test --- .../java/org/kohsuke/github/GHBranchTest.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/test/java/org/kohsuke/github/GHBranchTest.java b/src/test/java/org/kohsuke/github/GHBranchTest.java index 117da049f7..b9b6270cd4 100644 --- a/src/test/java/org/kohsuke/github/GHBranchTest.java +++ b/src/test/java/org/kohsuke/github/GHBranchTest.java @@ -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; @@ -74,4 +75,31 @@ 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")); + } } From 4f7892591cf3b99e8769787144c1658d1ae4e5b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Bouchex=20Bellomi=C3=A9?= Date: Thu, 3 Oct 2024 15:46:02 +0200 Subject: [PATCH 3/6] #1967 Adding rename method test --- .../java/org/kohsuke/github/GHBranchTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/java/org/kohsuke/github/GHBranchTest.java b/src/test/java/org/kohsuke/github/GHBranchTest.java index 117da049f7..8e6e17fc7d 100644 --- a/src/test/java/org/kohsuke/github/GHBranchTest.java +++ b/src/test/java/org/kohsuke/github/GHBranchTest.java @@ -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; @@ -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")); + } } From a93e21c50afe9e7aaa919320f09e81799e338e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Bouchex=20Bellomi=C3=A9?= Date: Tue, 15 Oct 2024 08:25:32 +0200 Subject: [PATCH 4/6] Update src/main/java/org/kohsuke/github/GHBranch.java Co-authored-by: Liam Newman --- src/main/java/org/kohsuke/github/GHBranch.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHBranch.java b/src/main/java/org/kohsuke/github/GHBranch.java index e9e1f4f9f9..0f648be857 100644 --- a/src/main/java/org/kohsuke/github/GHBranch.java +++ b/src/main/java/org/kohsuke/github/GHBranch.java @@ -199,9 +199,9 @@ public GHCommit merge(String head, String commitMessage) throws IOException { * the name * @throws IOException * the io exception + * @see https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#rename-a-branch */ - @CheckForNull - public void rename(String name) throws IOException { + public GHBranch rename(String name) throws IOException { root().createRequest().method("POST").with("new_name", name).withUrlPath(getApiRoute() + "/rename").send(); } From 3209cf4835ccbe250ff6c0d5d7cd843db9426740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Bouchex=20Bellomi=C3=A9?= Date: Tue, 15 Oct 2024 08:30:48 +0200 Subject: [PATCH 5/6] #1967 Modified rename method --- src/main/java/org/kohsuke/github/GHBranch.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/kohsuke/github/GHBranch.java b/src/main/java/org/kohsuke/github/GHBranch.java index 0f648be857..1db9801eb5 100644 --- a/src/main/java/org/kohsuke/github/GHBranch.java +++ b/src/main/java/org/kohsuke/github/GHBranch.java @@ -202,7 +202,9 @@ public GHCommit merge(String head, String commitMessage) throws IOException { * @see https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#rename-a-branch */ public GHBranch rename(String name) throws IOException { - root().createRequest().method("POST").with("new_name", name).withUrlPath(getApiRoute() + "/rename").send(); + return root().createRequest().method("POST").with("new_name", name) + .withUrlPath(getApiRoute() + "/rename") + .fetch(GHBranch.class); } /** From fd8399cf20c5b7717b225248b8af05aa5d8ba340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Bouchex=20Bellomi=C3=A9?= Date: Thu, 17 Oct 2024 17:00:38 +0200 Subject: [PATCH 6/6] #1967 Modified rename method --- src/main/java/org/kohsuke/github/GHBranch.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/kohsuke/github/GHBranch.java b/src/main/java/org/kohsuke/github/GHBranch.java index 1db9801eb5..7a41b82f37 100644 --- a/src/main/java/org/kohsuke/github/GHBranch.java +++ b/src/main/java/org/kohsuke/github/GHBranch.java @@ -202,7 +202,9 @@ public GHCommit merge(String head, String commitMessage) throws IOException { * @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) + return root().createRequest() + .method("POST") + .with("new_name", name) .withUrlPath(getApiRoute() + "/rename") .fetch(GHBranch.class); }