Skip to content

Commit 0a40dc5

Browse files
authored
Merge pull request #620 from siordache-forks/issue#504
fix and unit tests for issue #504
2 parents 7a4870c + 71862d7 commit 0a40dc5

File tree

48 files changed

+2094
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2094
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
44

55
import java.io.IOException;
6+
import java.nio.charset.StandardCharsets;
67
import java.util.ArrayList;
78
import java.util.List;
89

@@ -61,7 +62,9 @@ public GHTreeBuilder baseTree(String baseTree) {
6162
* @param content
6263
* the content
6364
* @return the gh tree builder
65+
* @deprecated use {@link #add(String, String, boolean)} or {@link #add(String, byte[], boolean)} instead.
6466
*/
67+
@Deprecated
6568
public GHTreeBuilder entry(String path, String mode, String type, String sha, String content) {
6669
TreeEntry entry = new TreeEntry(path, mode, type);
6770
entry.sha = sha;
@@ -81,7 +84,9 @@ public GHTreeBuilder entry(String path, String mode, String type, String sha, St
8184
* @param executable
8285
* the executable
8386
* @return the gh tree builder
87+
* @deprecated use {@link #add(String, String, boolean)} or {@link #add(String, byte[], boolean)} instead.
8488
*/
89+
@Deprecated
8590
public GHTreeBuilder shaEntry(String path, String sha, boolean executable) {
8691
TreeEntry entry = new TreeEntry(path, executable ? "100755" : "100644", "blob");
8792
entry.sha = sha;
@@ -100,14 +105,51 @@ public GHTreeBuilder shaEntry(String path, String sha, boolean executable) {
100105
* @param executable
101106
* the executable
102107
* @return the gh tree builder
108+
* @deprecated use {@link #add(String, String, boolean)} or {@link #add(String, byte[], boolean)} instead.
103109
*/
110+
@Deprecated
104111
public GHTreeBuilder textEntry(String path, String content, boolean executable) {
105112
TreeEntry entry = new TreeEntry(path, executable ? "100755" : "100644", "blob");
106113
entry.content = content;
107114
treeEntries.add(entry);
108115
return this;
109116
}
110117

118+
/**
119+
* Adds a new entry with the given binary content to the tree.
120+
*
121+
* @param path
122+
* the file path in the tree
123+
* @param content
124+
* the file content as byte array
125+
* @param executable
126+
* true, if the file should be executable
127+
* @return this GHTreeBuilder
128+
*/
129+
public GHTreeBuilder add(String path, byte[] content, boolean executable) {
130+
try {
131+
String dataSha = repo.createBlob().binaryContent(content).create().getSha();
132+
return shaEntry(path, dataSha, executable);
133+
} catch (IOException e) {
134+
throw new GHException("Cannot create binary content of '" + path + "'", e);
135+
}
136+
}
137+
138+
/**
139+
* Adds a new entry with the given text content to the tree.
140+
*
141+
* @param path
142+
* the file path in the tree
143+
* @param content
144+
* the file content as UTF-8 encoded string
145+
* @param executable
146+
* true, if the file should be executable
147+
* @return this GHTreeBuilder
148+
*/
149+
public GHTreeBuilder add(String path, String content, boolean executable) {
150+
return add(path, content.getBytes(StandardCharsets.UTF_8), executable);
151+
}
152+
111153
private String getApiTail() {
112154
return String.format("/repos/%s/%s/git/trees", repo.getOwnerName(), repo.getName());
113155
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.kohsuke.github;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Ignore;
6+
import org.junit.Test;
7+
8+
import java.io.IOException;
9+
import java.util.Arrays;
10+
11+
public class GHTreeBuilderTest extends AbstractGitHubWireMockTest {
12+
private static String REPO_NAME = "sandboxx/GHTreeBuilderTest";
13+
14+
private static String PATH_SCRIPT = "app/run.sh";
15+
private static String CONTENT_SCRIPT = "#!/bin/bash\necho Hello\n";
16+
17+
private static String PATH_README = "doc/readme.txt";
18+
private static String CONTENT_README = "Thanks for using our application!\n";
19+
20+
private static String PATH_DATA1 = "data/val1.dat";
21+
private static byte[] CONTENT_DATA1 = { 0x01, 0x02, 0x03 };
22+
23+
private static String PATH_DATA2 = "data/val2.dat";
24+
private static byte[] CONTENT_DATA2 = { 0x04, 0x05, 0x06, 0x07 };
25+
26+
private GHRepository repo;
27+
private GHRef masterRef;
28+
private GHTreeBuilder treeBuilder;
29+
30+
@Before
31+
@After
32+
public void cleanup() throws Exception {
33+
if (mockGitHub.isUseProxy()) {
34+
repo = gitHubBeforeAfter.getRepository(REPO_NAME);
35+
Arrays.asList(PATH_SCRIPT, PATH_README, PATH_DATA1, PATH_DATA2).forEach(path -> {
36+
try {
37+
GHContent content = repo.getFileContent(path);
38+
if (content != null) {
39+
content.delete("Cleanup");
40+
}
41+
} catch (IOException e) {
42+
}
43+
});
44+
}
45+
}
46+
47+
@Before
48+
public void setUp() throws Exception {
49+
repo = gitHub.getRepository(REPO_NAME);
50+
masterRef = repo.getRef("heads/master");
51+
String masterTreeSha = repo.getTreeRecursive("master", 1).getSha();
52+
treeBuilder = repo.createTree().baseTree(masterTreeSha);
53+
}
54+
55+
@Test
56+
@Ignore("It seems that GitHub no longer supports the 'content' parameter")
57+
public void testTextEntry() throws Exception {
58+
treeBuilder.textEntry(PATH_SCRIPT, CONTENT_SCRIPT, true);
59+
treeBuilder.textEntry(PATH_README, CONTENT_README, false);
60+
61+
updateTree();
62+
63+
assertEquals(CONTENT_SCRIPT.length(), getFileSize(PATH_SCRIPT));
64+
assertEquals(CONTENT_README.length(), getFileSize(PATH_README));
65+
}
66+
67+
@Test
68+
public void testShaEntry() throws Exception {
69+
String dataSha1 = new GHBlobBuilder(repo).binaryContent(CONTENT_DATA1).create().getSha();
70+
treeBuilder.shaEntry(PATH_DATA1, dataSha1, false);
71+
72+
String dataSha2 = new GHBlobBuilder(repo).binaryContent(CONTENT_DATA2).create().getSha();
73+
treeBuilder.shaEntry(PATH_DATA2, dataSha2, false);
74+
75+
updateTree();
76+
77+
assertEquals(CONTENT_DATA1.length, getFileSize(PATH_DATA1));
78+
assertEquals(CONTENT_DATA2.length, getFileSize(PATH_DATA2));
79+
}
80+
81+
@Test
82+
public void testAdd() throws Exception {
83+
treeBuilder.add(PATH_SCRIPT, CONTENT_SCRIPT, true);
84+
treeBuilder.add(PATH_README, CONTENT_README, false);
85+
treeBuilder.add(PATH_DATA1, CONTENT_DATA1, false);
86+
treeBuilder.add(PATH_DATA2, CONTENT_DATA2, false);
87+
88+
updateTree();
89+
90+
assertEquals(CONTENT_SCRIPT.length(), getFileSize(PATH_SCRIPT));
91+
assertEquals(CONTENT_README.length(), getFileSize(PATH_README));
92+
assertEquals(CONTENT_DATA1.length, getFileSize(PATH_DATA1));
93+
assertEquals(CONTENT_DATA2.length, getFileSize(PATH_DATA2));
94+
}
95+
96+
private void updateTree() throws IOException {
97+
String treeSha = treeBuilder.create().getSha();
98+
String commitSha = new GHCommitBuilder(repo).message("Add files")
99+
.tree(treeSha)
100+
.parent(masterRef.getObject().getSha())
101+
.create()
102+
.getSHA1();
103+
masterRef.updateTo(commitSha);
104+
}
105+
106+
private long getFileSize(String path) throws IOException {
107+
GHContent content = repo.getFileContent(path);
108+
if (content == null)
109+
throw new IOException("File not found: " + path);
110+
return content.getSize();
111+
}
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{
2+
"id": 222874396,
3+
"node_id": "MDEwOlJlcG9zaXRvcnkyMjI4NzQzOTY=",
4+
"name": "GHTreeBuilderTest",
5+
"full_name": "sandboxx/GHTreeBuilderTest",
6+
"private": false,
7+
"owner": {
8+
"login": "sandboxx",
9+
"id": 7226712,
10+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjcyMjY3MTI=",
11+
"avatar_url": "https://avatars3.githubusercontent.com/u/7226712?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/sandboxx",
14+
"html_url": "https://github.com/sandboxx",
15+
"followers_url": "https://api.github.com/users/sandboxx/followers",
16+
"following_url": "https://api.github.com/users/sandboxx/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/sandboxx/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/sandboxx/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/sandboxx/subscriptions",
20+
"organizations_url": "https://api.github.com/users/sandboxx/orgs",
21+
"repos_url": "https://api.github.com/users/sandboxx/repos",
22+
"events_url": "https://api.github.com/users/sandboxx/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/sandboxx/received_events",
24+
"type": "Organization",
25+
"site_admin": false
26+
},
27+
"html_url": "https://github.com/sandboxx/GHTreeBuilderTest",
28+
"description": null,
29+
"fork": false,
30+
"url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest",
31+
"forks_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/forks",
32+
"keys_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/keys{/key_id}",
33+
"collaborators_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/collaborators{/collaborator}",
34+
"teams_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/teams",
35+
"hooks_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/hooks",
36+
"issue_events_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/issues/events{/number}",
37+
"events_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/events",
38+
"assignees_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/assignees{/user}",
39+
"branches_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/branches{/branch}",
40+
"tags_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/tags",
41+
"blobs_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/blobs{/sha}",
42+
"git_tags_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/tags{/sha}",
43+
"git_refs_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/refs{/sha}",
44+
"trees_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/trees{/sha}",
45+
"statuses_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/statuses/{sha}",
46+
"languages_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/languages",
47+
"stargazers_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/stargazers",
48+
"contributors_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/contributors",
49+
"subscribers_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/subscribers",
50+
"subscription_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/subscription",
51+
"commits_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/commits{/sha}",
52+
"git_commits_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/commits{/sha}",
53+
"comments_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/comments{/number}",
54+
"issue_comment_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/issues/comments{/number}",
55+
"contents_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/contents/{+path}",
56+
"compare_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/compare/{base}...{head}",
57+
"merges_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/merges",
58+
"archive_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/{archive_format}{/ref}",
59+
"downloads_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/downloads",
60+
"issues_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/issues{/number}",
61+
"pulls_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/pulls{/number}",
62+
"milestones_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/milestones{/number}",
63+
"notifications_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/notifications{?since,all,participating}",
64+
"labels_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/labels{/name}",
65+
"releases_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/releases{/id}",
66+
"deployments_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/deployments",
67+
"created_at": "2019-11-20T07:14:14Z",
68+
"updated_at": "2019-11-20T22:28:18Z",
69+
"pushed_at": "2019-11-20T22:28:16Z",
70+
"git_url": "git://github.com/sandboxx/GHTreeBuilderTest.git",
71+
"ssh_url": "[email protected]:sandboxx/GHTreeBuilderTest.git",
72+
"clone_url": "https://github.com/sandboxx/GHTreeBuilderTest.git",
73+
"svn_url": "https://github.com/sandboxx/GHTreeBuilderTest",
74+
"homepage": null,
75+
"size": 10,
76+
"stargazers_count": 0,
77+
"watchers_count": 0,
78+
"language": "Shell",
79+
"has_issues": true,
80+
"has_projects": true,
81+
"has_downloads": true,
82+
"has_wiki": true,
83+
"has_pages": false,
84+
"forks_count": 0,
85+
"mirror_url": null,
86+
"archived": false,
87+
"disabled": false,
88+
"open_issues_count": 0,
89+
"license": null,
90+
"forks": 0,
91+
"open_issues": 0,
92+
"watchers": 0,
93+
"default_branch": "master",
94+
"permissions": {
95+
"admin": true,
96+
"push": true,
97+
"pull": true
98+
},
99+
"allow_squash_merge": true,
100+
"allow_merge_commit": true,
101+
"allow_rebase_merge": true,
102+
"organization": {
103+
"login": "sandboxx",
104+
"id": 7226712,
105+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjcyMjY3MTI=",
106+
"avatar_url": "https://avatars3.githubusercontent.com/u/7226712?v=4",
107+
"gravatar_id": "",
108+
"url": "https://api.github.com/users/sandboxx",
109+
"html_url": "https://github.com/sandboxx",
110+
"followers_url": "https://api.github.com/users/sandboxx/followers",
111+
"following_url": "https://api.github.com/users/sandboxx/following{/other_user}",
112+
"gists_url": "https://api.github.com/users/sandboxx/gists{/gist_id}",
113+
"starred_url": "https://api.github.com/users/sandboxx/starred{/owner}{/repo}",
114+
"subscriptions_url": "https://api.github.com/users/sandboxx/subscriptions",
115+
"organizations_url": "https://api.github.com/users/sandboxx/orgs",
116+
"repos_url": "https://api.github.com/users/sandboxx/repos",
117+
"events_url": "https://api.github.com/users/sandboxx/events{/privacy}",
118+
"received_events_url": "https://api.github.com/users/sandboxx/received_events",
119+
"type": "Organization",
120+
"site_admin": false
121+
},
122+
"network_count": 0,
123+
"subscribers_count": 1
124+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "run.sh",
3+
"path": "app/run.sh",
4+
"sha": "e9ba7ba07276a794d493db90e0384940ce2b757b",
5+
"size": 23,
6+
"url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/contents/app/run.sh?ref=master",
7+
"html_url": "https://github.com/sandboxx/GHTreeBuilderTest/blob/master/app/run.sh",
8+
"git_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/blobs/e9ba7ba07276a794d493db90e0384940ce2b757b",
9+
"download_url": "https://raw.githubusercontent.com/sandboxx/GHTreeBuilderTest/master/app/run.sh",
10+
"type": "file",
11+
"content": "IyEvYmluL2Jhc2gKZWNobyBIZWxsbwo=\n",
12+
"encoding": "base64",
13+
"_links": {
14+
"self": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/contents/app/run.sh?ref=master",
15+
"git": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/blobs/e9ba7ba07276a794d493db90e0384940ce2b757b",
16+
"html": "https://github.com/sandboxx/GHTreeBuilderTest/blob/master/app/run.sh"
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "val1.dat",
3+
"path": "data/val1.dat",
4+
"sha": "aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74",
5+
"size": 3,
6+
"url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/contents/data/val1.dat?ref=master",
7+
"html_url": "https://github.com/sandboxx/GHTreeBuilderTest/blob/master/data/val1.dat",
8+
"git_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/blobs/aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74",
9+
"download_url": "https://raw.githubusercontent.com/sandboxx/GHTreeBuilderTest/master/data/val1.dat",
10+
"type": "file",
11+
"content": "AQID\n",
12+
"encoding": "base64",
13+
"_links": {
14+
"self": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/contents/data/val1.dat?ref=master",
15+
"git": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/blobs/aed2973e4b8a7ff1b30ff5c4751e5a2b38989e74",
16+
"html": "https://github.com/sandboxx/GHTreeBuilderTest/blob/master/data/val1.dat"
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "val2.dat",
3+
"path": "data/val2.dat",
4+
"sha": "5bd8bb897b13225c93a1d26baa88c96b7bd5d817",
5+
"size": 4,
6+
"url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/contents/data/val2.dat?ref=master",
7+
"html_url": "https://github.com/sandboxx/GHTreeBuilderTest/blob/master/data/val2.dat",
8+
"git_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/blobs/5bd8bb897b13225c93a1d26baa88c96b7bd5d817",
9+
"download_url": "https://raw.githubusercontent.com/sandboxx/GHTreeBuilderTest/master/data/val2.dat",
10+
"type": "file",
11+
"content": "BAUGBw==\n",
12+
"encoding": "base64",
13+
"_links": {
14+
"self": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/contents/data/val2.dat?ref=master",
15+
"git": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/blobs/5bd8bb897b13225c93a1d26baa88c96b7bd5d817",
16+
"html": "https://github.com/sandboxx/GHTreeBuilderTest/blob/master/data/val2.dat"
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "readme.txt",
3+
"path": "doc/readme.txt",
4+
"sha": "fbbc875b17d1e17da06b4ee8fda46e2596c41f3c",
5+
"size": 34,
6+
"url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/contents/doc/readme.txt?ref=master",
7+
"html_url": "https://github.com/sandboxx/GHTreeBuilderTest/blob/master/doc/readme.txt",
8+
"git_url": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/blobs/fbbc875b17d1e17da06b4ee8fda46e2596c41f3c",
9+
"download_url": "https://raw.githubusercontent.com/sandboxx/GHTreeBuilderTest/master/doc/readme.txt",
10+
"type": "file",
11+
"content": "VGhhbmtzIGZvciB1c2luZyBvdXIgYXBwbGljYXRpb24hCg==\n",
12+
"encoding": "base64",
13+
"_links": {
14+
"self": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/contents/doc/readme.txt?ref=master",
15+
"git": "https://api.github.com/repos/sandboxx/GHTreeBuilderTest/git/blobs/fbbc875b17d1e17da06b4ee8fda46e2596c41f3c",
16+
"html": "https://github.com/sandboxx/GHTreeBuilderTest/blob/master/doc/readme.txt"
17+
}
18+
}

0 commit comments

Comments
 (0)