Skip to content

Commit fe8bdb7

Browse files
author
Joe
committed
Merge branches method
1 parent 67dc6d2 commit fe8bdb7

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/main/java/org/kohsuke/github/GHBranch.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,31 @@ public void enableProtection(EnforcementLevel level, Collection<String> contexts
165165
}
166166
}
167167

168+
/**
169+
* Merge branches.
170+
*
171+
* @param headBranch
172+
* the branch whose head is being merged
173+
*
174+
* @param commit_message
175+
* the commit message
176+
*
177+
* @return GHCommit the merge commit created
178+
*
179+
* @throws IOException
180+
* if merging fails
181+
*/
182+
public GHCommit merge(GHBranch headBranch, String commit_message) throws IOException {
183+
return root.createRequest()
184+
.withUrlPath(owner.getApiTailUrl("merges"))
185+
.method("POST")
186+
.with("commit_message", commit_message)
187+
.with("base", this.name)
188+
.with("head", headBranch.getName())
189+
.fetch(GHCommit.class)
190+
.wrapUp(owner);
191+
}
192+
168193
String getApiRoute() {
169194
return owner.getApiTailUrl("/branches/" + name);
170195
}

src/test/java/org/kohsuke/github/GHBranchProtectionTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,35 @@ public void testGetProtection() throws Exception {
112112
assertTrue(protectionTest instanceof GHBranchProtection);
113113
assertTrue(repo.getBranch(BRANCH).isProtected());
114114
}
115+
116+
@Test
117+
public void testMergeBranch() throws Exception {
118+
String name = "testMergeBranch";
119+
String branchName = "test/" + name;
120+
121+
GHRepository repository = gitHub.getOrganization("hub4j-test-org").getRepository("github-api");
122+
GHRef masterRef = repository.getRef("heads/master");
123+
repository.createRef("refs/heads/" + branchName, masterRef.getObject().getSha());
124+
125+
GHContentUpdateResponse response = repository.createContent()
126+
.content(name)
127+
.message(name)
128+
.path(name)
129+
.branch(branchName)
130+
.commit();
131+
132+
Thread.sleep(1000);
133+
134+
repository.createContent()
135+
.content(name + name)
136+
.path(name)
137+
.branch(branchName)
138+
.message(name)
139+
.sha(response.getContent().getSha())
140+
.commit();
141+
142+
GHBranch masterBranch = repository.getBranch("master");
143+
GHCommit mergeCommit = repository.getBranch(branchName).merge(masterBranch, "merging master into testBranch");
144+
assertNotNull(mergeCommit);
145+
}
115146
}

0 commit comments

Comments
 (0)