Skip to content

Commit 92c141c

Browse files
committed
Add support for string head
Also support all changes already merged.
1 parent fd1a1a1 commit 92c141c

File tree

27 files changed

+522
-154
lines changed

27 files changed

+522
-154
lines changed

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

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.Collection;
1010
import java.util.Objects;
1111

12+
import javax.annotation.CheckForNull;
13+
1214
/**
1315
* A branch in a repository.
1416
*
@@ -166,28 +168,56 @@ public void enableProtection(EnforcementLevel level, Collection<String> contexts
166168
}
167169

168170
/**
169-
* Merge branches.
171+
* Merge a branch into this branch.
170172
*
171173
* @param headBranch
172-
* the branch whose head is being merged
174+
* the branch whose head will be merged
173175
*
174176
* @param commitMessage
175177
* the commit message
176178
*
177-
* @return GHCommit the merge commit created
179+
* @return the merge {@link GHCommit} created, or {@code null} if the base already contains the head (nothing to
180+
* merge).
178181
*
179182
* @throws IOException
180183
* if merging fails
181184
*/
185+
@CheckForNull
182186
public GHCommit merge(GHBranch headBranch, String commitMessage) throws IOException {
183-
return root.createRequest()
187+
return merge(headBranch.getName(), commitMessage);
188+
}
189+
190+
/**
191+
* Merge a ref into this branch.
192+
*
193+
* @param head
194+
* the ref name that will be merged into this branch. Follows the usual ref naming rules, could be a
195+
* branch name, tag, or commit sha.
196+
*
197+
* @param commitMessage
198+
* the commit message
199+
*
200+
* @return the merge {@link GHCommit} created, or {@code null} if the base already contains the head (nothing to
201+
* merge).
202+
*
203+
* @throws IOException
204+
* if merging fails
205+
*/
206+
@CheckForNull
207+
public GHCommit merge(String head, String commitMessage) throws IOException {
208+
GHCommit result = root.createRequest()
184209
.withUrlPath(owner.getApiTailUrl("merges"))
185210
.method("POST")
186211
.with("commit_message", commitMessage)
187212
.with("base", this.name)
188-
.with("head", headBranch.getName())
189-
.fetch(GHCommit.class)
190-
.wrapUp(owner);
213+
.with("head", head)
214+
.fetch(GHCommit.class);
215+
216+
if (result != null) {
217+
result.wrapUp(owner);
218+
}
219+
220+
return result;
191221
}
192222

193223
String getApiRoute() {

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,14 @@ class GitHubResponse<T> {
7878
@CheckForNull
7979
static <T> T parseBody(ResponseInfo responseInfo, Class<T> type) throws IOException {
8080

81-
if (responseInfo.statusCode() == HttpURLConnection.HTTP_NO_CONTENT && type != null && type.isArray()) {
82-
// no content
83-
return type.cast(Array.newInstance(type.getComponentType(), 0));
81+
if (responseInfo.statusCode() == HttpURLConnection.HTTP_NO_CONTENT) {
82+
if (type != null && type.isArray()) {
83+
// no content for array should be empty array
84+
return type.cast(Array.newInstance(type.getComponentType(), 0));
85+
} else {
86+
// no content for object should be null
87+
return null;
88+
}
8489
}
8590

8691
String data = responseInfo.getBodyAsString();

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ public void testMergeBranch() throws Exception {
2525
GHCommit mergeCommit = repository.getBranch(BRANCH_1).merge(otherBranch, commitMessage);
2626
assertThat(mergeCommit, notNullValue());
2727
assertThat(mergeCommit.getCommitShortInfo().getMessage(), equalTo(commitMessage));
28+
29+
// Merging commit sha should work
30+
commitMessage = "merging from " + mergeCommit.getSHA1();
31+
GHBranch master = repository.getBranch("master");
32+
mergeCommit = master.merge(mergeCommit.getSHA1(), commitMessage);
33+
34+
assertThat(mergeCommit, notNullValue());
35+
assertThat(mergeCommit.getCommitShortInfo().getMessage(), equalTo(commitMessage));
36+
37+
mergeCommit = master.merge(mergeCommit.getSHA1(), commitMessage);
38+
// Should be null since all changes already merged
39+
assertThat(mergeCommit, nullValue());
2840
}
2941

3042
private void createRefAndPostContent(String branchName, String sha) throws IOException {

src/test/resources/org/kohsuke/github/GHBranchTest/wiremock/testMergeBranch/__files/repos_hub4j-test-org_temp-testmergebranch-2.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"id": 283293221,
3-
"node_id": "MDEwOlJlcG9zaXRvcnkyODMyOTMyMjE=",
2+
"id": 283313593,
3+
"node_id": "MDEwOlJlcG9zaXRvcnkyODMzMTM1OTM=",
44
"name": "temp-testMergeBranch",
55
"full_name": "hub4j-test-org/temp-testMergeBranch",
66
"private": false,
@@ -64,9 +64,9 @@
6464
"labels_url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/labels{/name}",
6565
"releases_url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/releases{/id}",
6666
"deployments_url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/deployments",
67-
"created_at": "2020-07-28T18:18:44Z",
68-
"updated_at": "2020-07-28T18:18:49Z",
69-
"pushed_at": "2020-07-28T18:18:46Z",
67+
"created_at": "2020-07-28T19:54:49Z",
68+
"updated_at": "2020-07-28T19:54:53Z",
69+
"pushed_at": "2020-07-28T19:54:51Z",
7070
"git_url": "git://github.com/hub4j-test-org/temp-testMergeBranch.git",
7171
"ssh_url": "[email protected]:hub4j-test-org/temp-testMergeBranch.git",
7272
"clone_url": "https://github.com/hub4j-test-org/temp-testMergeBranch.git",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"name": "master",
3+
"commit": {
4+
"sha": "688a1c3f8bc67deb767f68560ed8c7350d70d50f",
5+
"node_id": "MDY6Q29tbWl0MjgzMzEzNTkzOjY4OGExYzNmOGJjNjdkZWI3NjdmNjg1NjBlZDhjNzM1MGQ3MGQ1MGY=",
6+
"commit": {
7+
"author": {
8+
"name": "Liam Newman",
9+
"email": "[email protected]",
10+
"date": "2020-07-28T19:54:50Z"
11+
},
12+
"committer": {
13+
"name": "GitHub",
14+
"email": "[email protected]",
15+
"date": "2020-07-28T19:54:50Z"
16+
},
17+
"message": "Initial commit",
18+
"tree": {
19+
"sha": "c6da5eb430eb876e5a03323a62ea01365a753d3b",
20+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/trees/c6da5eb430eb876e5a03323a62ea01365a753d3b"
21+
},
22+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/commits/688a1c3f8bc67deb767f68560ed8c7350d70d50f",
23+
"comment_count": 0,
24+
"verification": {
25+
"verified": true,
26+
"reason": "valid",
27+
"signature": "-----BEGIN PGP SIGNATURE-----\n\nwsBcBAABCAAQBQJfIIKKCRBK7hj4Ov3rIwAAdHIIAH1NiSMFO1907U4QdfRgdqCP\nQHgc6KsflfLPxeP/Tt02Q+BJQeN8e1/IMXziwOp5dt5+CSuGFLN9U41fsBOxIRk6\nagqX5q4KleZaUEjOik2PfmXkwO+mQU64CX2QdpfHuqZNvCl6H1G4dnEAWdM1E3oA\nvLMIOHdjy6Fc1VJzmcc8+RVzUvKv/9Fq0uU6WR9Jl6TfMnfGqtVwQ1USuerhnIs0\nPSI9Yo12Wy8b+8psD3bwBcjbX8r6ItM//DjzU3XcKRZxv54u7tgQuLELqxh9dQSu\nCDSV9/LyHpMC3WMRAupusGsb4rMKeN+C7NYC4ZMmoRmwvWAhRKjTtCHBP8Ksapo=\n=OLMl\n-----END PGP SIGNATURE-----\n",
28+
"payload": "tree c6da5eb430eb876e5a03323a62ea01365a753d3b\nauthor Liam Newman <[email protected]> 1595966090 -0700\ncommitter GitHub <[email protected]> 1595966090 -0700\n\nInitial commit"
29+
}
30+
},
31+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/688a1c3f8bc67deb767f68560ed8c7350d70d50f",
32+
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/688a1c3f8bc67deb767f68560ed8c7350d70d50f",
33+
"comments_url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/688a1c3f8bc67deb767f68560ed8c7350d70d50f/comments",
34+
"author": {
35+
"login": "bitwiseman",
36+
"id": 1958953,
37+
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
38+
"avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
39+
"gravatar_id": "",
40+
"url": "https://api.github.com/users/bitwiseman",
41+
"html_url": "https://github.com/bitwiseman",
42+
"followers_url": "https://api.github.com/users/bitwiseman/followers",
43+
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
44+
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
45+
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
46+
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
47+
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
48+
"repos_url": "https://api.github.com/users/bitwiseman/repos",
49+
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
50+
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
51+
"type": "User",
52+
"site_admin": false
53+
},
54+
"committer": {
55+
"login": "web-flow",
56+
"id": 19864447,
57+
"node_id": "MDQ6VXNlcjE5ODY0NDQ3",
58+
"avatar_url": "https://avatars3.githubusercontent.com/u/19864447?v=4",
59+
"gravatar_id": "",
60+
"url": "https://api.github.com/users/web-flow",
61+
"html_url": "https://github.com/web-flow",
62+
"followers_url": "https://api.github.com/users/web-flow/followers",
63+
"following_url": "https://api.github.com/users/web-flow/following{/other_user}",
64+
"gists_url": "https://api.github.com/users/web-flow/gists{/gist_id}",
65+
"starred_url": "https://api.github.com/users/web-flow/starred{/owner}{/repo}",
66+
"subscriptions_url": "https://api.github.com/users/web-flow/subscriptions",
67+
"organizations_url": "https://api.github.com/users/web-flow/orgs",
68+
"repos_url": "https://api.github.com/users/web-flow/repos",
69+
"events_url": "https://api.github.com/users/web-flow/events{/privacy}",
70+
"received_events_url": "https://api.github.com/users/web-flow/received_events",
71+
"type": "User",
72+
"site_admin": false
73+
},
74+
"parents": []
75+
},
76+
"_links": {
77+
"self": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/branches/master",
78+
"html": "https://github.com/hub4j-test-org/temp-testMergeBranch/tree/master"
79+
},
80+
"protected": false,
81+
"protection": {
82+
"enabled": false,
83+
"required_status_checks": {
84+
"enforcement_level": "off",
85+
"contexts": []
86+
}
87+
},
88+
"protection_url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/branches/master/protection"
89+
}

src/test/resources/org/kohsuke/github/GHBranchTest/wiremock/testMergeBranch/__files/repos_hub4j-test-org_temp-testmergebranch_branches_testbranch1-9.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
{
22
"name": "testBranch1",
33
"commit": {
4-
"sha": "1daaedca738fa6117848f3128a41058ae6c48277",
5-
"node_id": "MDY6Q29tbWl0MjgzMjkzMjIxOjFkYWFlZGNhNzM4ZmE2MTE3ODQ4ZjMxMjhhNDEwNThhZTZjNDgyNzc=",
4+
"sha": "beac07bbfdeb8a5d261739cd65c4caaaca48ac79",
5+
"node_id": "MDY6Q29tbWl0MjgzMzEzNTkzOmJlYWMwN2JiZmRlYjhhNWQyNjE3MzljZDY1YzRjYWFhY2E0OGFjNzk=",
66
"commit": {
77
"author": {
88
"name": "Liam Newman",
99
"email": "[email protected]",
10-
"date": "2020-07-28T18:18:51Z"
10+
"date": "2020-07-28T19:54:55Z"
1111
},
1212
"committer": {
1313
"name": "Liam Newman",
1414
"email": "[email protected]",
15-
"date": "2020-07-28T18:18:51Z"
15+
"date": "2020-07-28T19:54:55Z"
1616
},
1717
"message": "testBranch1",
1818
"tree": {
1919
"sha": "540c4a3c093ad09a869163c60d44218774ff5474",
2020
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/trees/540c4a3c093ad09a869163c60d44218774ff5474"
2121
},
22-
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/commits/1daaedca738fa6117848f3128a41058ae6c48277",
22+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/commits/beac07bbfdeb8a5d261739cd65c4caaaca48ac79",
2323
"comment_count": 0,
2424
"verification": {
2525
"verified": false,
@@ -28,9 +28,9 @@
2828
"payload": null
2929
}
3030
},
31-
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/1daaedca738fa6117848f3128a41058ae6c48277",
32-
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/1daaedca738fa6117848f3128a41058ae6c48277",
33-
"comments_url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/1daaedca738fa6117848f3128a41058ae6c48277/comments",
31+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/beac07bbfdeb8a5d261739cd65c4caaaca48ac79",
32+
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/beac07bbfdeb8a5d261739cd65c4caaaca48ac79",
33+
"comments_url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/beac07bbfdeb8a5d261739cd65c4caaaca48ac79/comments",
3434
"author": {
3535
"login": "bitwiseman",
3636
"id": 1958953,
@@ -73,9 +73,9 @@
7373
},
7474
"parents": [
7575
{
76-
"sha": "486418cfccc351dc18b68d0d8c699eefa504aec6",
77-
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/486418cfccc351dc18b68d0d8c699eefa504aec6",
78-
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/486418cfccc351dc18b68d0d8c699eefa504aec6"
76+
"sha": "688a1c3f8bc67deb767f68560ed8c7350d70d50f",
77+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/688a1c3f8bc67deb767f68560ed8c7350d70d50f",
78+
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/688a1c3f8bc67deb767f68560ed8c7350d70d50f"
7979
}
8080
]
8181
},

src/test/resources/org/kohsuke/github/GHBranchTest/wiremock/testMergeBranch/__files/repos_hub4j-test-org_temp-testmergebranch_branches_testbranch2-8.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
{
22
"name": "testBranch2",
33
"commit": {
4-
"sha": "2294f9e512939576357baf7556ff3cb87d75c4bd",
5-
"node_id": "MDY6Q29tbWl0MjgzMjkzMjIxOjIyOTRmOWU1MTI5Mzk1NzYzNTdiYWY3NTU2ZmYzY2I4N2Q3NWM0YmQ=",
4+
"sha": "506cfc5bb47ac087b26c6d95d0ad305c5917c4b2",
5+
"node_id": "MDY6Q29tbWl0MjgzMzEzNTkzOjUwNmNmYzViYjQ3YWMwODdiMjZjNmQ5NWQwYWQzMDVjNTkxN2M0YjI=",
66
"commit": {
77
"author": {
88
"name": "Liam Newman",
99
"email": "[email protected]",
10-
"date": "2020-07-28T18:18:53Z"
10+
"date": "2020-07-28T19:54:57Z"
1111
},
1212
"committer": {
1313
"name": "Liam Newman",
1414
"email": "[email protected]",
15-
"date": "2020-07-28T18:18:53Z"
15+
"date": "2020-07-28T19:54:57Z"
1616
},
1717
"message": "testBranch2",
1818
"tree": {
1919
"sha": "a183aaff617bc52ba1bdcea74faf24687194faf1",
2020
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/trees/a183aaff617bc52ba1bdcea74faf24687194faf1"
2121
},
22-
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/commits/2294f9e512939576357baf7556ff3cb87d75c4bd",
22+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/commits/506cfc5bb47ac087b26c6d95d0ad305c5917c4b2",
2323
"comment_count": 0,
2424
"verification": {
2525
"verified": false,
@@ -28,9 +28,9 @@
2828
"payload": null
2929
}
3030
},
31-
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/2294f9e512939576357baf7556ff3cb87d75c4bd",
32-
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/2294f9e512939576357baf7556ff3cb87d75c4bd",
33-
"comments_url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/2294f9e512939576357baf7556ff3cb87d75c4bd/comments",
31+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/506cfc5bb47ac087b26c6d95d0ad305c5917c4b2",
32+
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/506cfc5bb47ac087b26c6d95d0ad305c5917c4b2",
33+
"comments_url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/506cfc5bb47ac087b26c6d95d0ad305c5917c4b2/comments",
3434
"author": {
3535
"login": "bitwiseman",
3636
"id": 1958953,
@@ -73,9 +73,9 @@
7373
},
7474
"parents": [
7575
{
76-
"sha": "486418cfccc351dc18b68d0d8c699eefa504aec6",
77-
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/486418cfccc351dc18b68d0d8c699eefa504aec6",
78-
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/486418cfccc351dc18b68d0d8c699eefa504aec6"
76+
"sha": "688a1c3f8bc67deb767f68560ed8c7350d70d50f",
77+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/commits/688a1c3f8bc67deb767f68560ed8c7350d70d50f",
78+
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/688a1c3f8bc67deb767f68560ed8c7350d70d50f"
7979
}
8080
]
8181
},

src/test/resources/org/kohsuke/github/GHBranchTest/wiremock/testMergeBranch/__files/repos_hub4j-test-org_temp-testmergebranch_contents_testbranch1-5.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616
}
1717
},
1818
"commit": {
19-
"sha": "1daaedca738fa6117848f3128a41058ae6c48277",
20-
"node_id": "MDY6Q29tbWl0MjgzMjkzMjIxOjFkYWFlZGNhNzM4ZmE2MTE3ODQ4ZjMxMjhhNDEwNThhZTZjNDgyNzc=",
21-
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/commits/1daaedca738fa6117848f3128a41058ae6c48277",
22-
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/1daaedca738fa6117848f3128a41058ae6c48277",
19+
"sha": "beac07bbfdeb8a5d261739cd65c4caaaca48ac79",
20+
"node_id": "MDY6Q29tbWl0MjgzMzEzNTkzOmJlYWMwN2JiZmRlYjhhNWQyNjE3MzljZDY1YzRjYWFhY2E0OGFjNzk=",
21+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/commits/beac07bbfdeb8a5d261739cd65c4caaaca48ac79",
22+
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/beac07bbfdeb8a5d261739cd65c4caaaca48ac79",
2323
"author": {
2424
"name": "Liam Newman",
2525
"email": "[email protected]",
26-
"date": "2020-07-28T18:18:51Z"
26+
"date": "2020-07-28T19:54:55Z"
2727
},
2828
"committer": {
2929
"name": "Liam Newman",
3030
"email": "[email protected]",
31-
"date": "2020-07-28T18:18:51Z"
31+
"date": "2020-07-28T19:54:55Z"
3232
},
3333
"tree": {
3434
"sha": "540c4a3c093ad09a869163c60d44218774ff5474",
@@ -37,9 +37,9 @@
3737
"message": "testBranch1",
3838
"parents": [
3939
{
40-
"sha": "486418cfccc351dc18b68d0d8c699eefa504aec6",
41-
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/commits/486418cfccc351dc18b68d0d8c699eefa504aec6",
42-
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/486418cfccc351dc18b68d0d8c699eefa504aec6"
40+
"sha": "688a1c3f8bc67deb767f68560ed8c7350d70d50f",
41+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testMergeBranch/git/commits/688a1c3f8bc67deb767f68560ed8c7350d70d50f",
42+
"html_url": "https://github.com/hub4j-test-org/temp-testMergeBranch/commit/688a1c3f8bc67deb767f68560ed8c7350d70d50f"
4343
}
4444
],
4545
"verification": {

0 commit comments

Comments
 (0)