Skip to content

Commit f1720b7

Browse files
committed
Move archive readers to use new functional interfaces
1 parent 7a79a18 commit f1720b7

16 files changed

Lines changed: 731 additions & 26 deletions

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

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import edu.umd.cs.findbugs.annotations.NonNull;
3131
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
3232
import org.apache.commons.lang3.StringUtils;
33+
import org.kohsuke.github.function.InputStreamConsumer;
3334

3435
import java.io.FileNotFoundException;
3536
import java.io.IOException;
@@ -49,7 +50,6 @@
4950
import java.util.LinkedHashSet;
5051
import java.util.List;
5152
import java.util.Map;
52-
import java.util.Optional;
5353
import java.util.Set;
5454
import java.util.TreeMap;
5555
import java.util.WeakHashMap;
@@ -2968,50 +2968,39 @@ public GHTagObject createTag(String tag, String message, String object, String t
29682968
* Streams a zip archive of the repository, optionally at a given <code>ref</code>.
29692969
*
29702970
* @param sink
2971-
* The {@link StreamConsumer} that will consume the stream
2971+
* The {@link InputStreamConsumer} that will consume the stream
29722972
* @param ref
29732973
* if <code>null</code> the repository's default branch, usually <code>master</code>,
29742974
* @throws IOException
29752975
* The IO exception.
29762976
*/
2977-
public void zipball(StreamConsumer sink, String ref) throws IOException {
2978-
downloadArchive("zip", Optional.ofNullable(ref), sink);
2977+
public void readZip(InputStreamConsumer sink, String ref) throws IOException {
2978+
downloadArchive("zip", ref, sink);
29792979
}
29802980

29812981
/**
29822982
* Streams a tar archive of the repository, optionally at a given <code>ref</code>.
29832983
*
29842984
* @param sink
2985-
* The {@link StreamConsumer} that will consume the stream
2985+
* The {@link InputStreamConsumer} that will consume the stream
29862986
* @param ref
29872987
* if <code>null</code> the repository's default branch, usually <code>master</code>,
29882988
* @throws IOException
29892989
* The IO exception.
29902990
*/
2991-
public void tarball(StreamConsumer sink, String ref) throws IOException {
2992-
downloadArchive("tar", Optional.ofNullable(ref), sink);
2991+
public void readTar(InputStreamConsumer sink, String ref) throws IOException {
2992+
downloadArchive("tar", ref, sink);
29932993
}
29942994

2995-
/**
2996-
* A functional interface, equivalent to {@link java.util.function.Consumer} but that allows throwing
2997-
* {@link IOException}
2998-
*/
2999-
@FunctionalInterface
3000-
public interface StreamConsumer {
3001-
void accept(InputStream stream) throws IOException;
3002-
}
3003-
3004-
private void downloadArchive(String type, Optional<String> ref, StreamConsumer sink) throws IOException {
2995+
private void downloadArchive(@Nonnull String type, @CheckForNull String ref, @Nonnull InputStreamConsumer sink)
2996+
throws IOException {
30052997
requireNonNull(sink, "Sink must not be null");
3006-
final String base = getApiTailUrl(requireNonNull(type, "Type must not be null") + "ball");
3007-
final String url = ref.map(base::concat).orElse(base);
3008-
final Requester builder = root.createRequest().method("GET").withUrlPath(url);
3009-
builder.client.sendRequest(builder.build(), response -> {
3010-
try (final InputStream body = response.bodyStream()) {
3011-
sink.accept(body);
3012-
}
3013-
return null;
3014-
});
2998+
String tailUrl = getApiTailUrl(type + "ball");
2999+
if (ref != null) {
3000+
tailUrl += "/" + ref;
3001+
}
3002+
final Requester builder = root.createRequest().method("GET").withUrlPath(tailUrl);
3003+
builder.fetchStream(sink);
30153004
}
30163005

30173006
/**

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import org.apache.commons.io.IOUtils;
55
import org.junit.Test;
66

7+
import java.io.ByteArrayInputStream;
78
import java.io.FileNotFoundException;
89
import java.io.IOException;
10+
import java.io.InputStream;
911
import java.net.URL;
1012
import java.util.ArrayList;
1113
import java.util.List;
@@ -28,6 +30,20 @@ private GHRepository getRepository(GitHub gitHub) throws IOException {
2830
return gitHub.getOrganization("hub4j-test-org").getRepository("github-api");
2931
}
3032

33+
@Test
34+
public void testZipball() throws IOException {
35+
getTempRepository().readZip((InputStream inputstream) -> {
36+
InputStream i = new ByteArrayInputStream(IOUtils.toByteArray(inputstream));
37+
}, null);
38+
}
39+
40+
@Test
41+
public void testTarball() throws IOException {
42+
getTempRepository().readTar((InputStream inputstream) -> {
43+
InputStream i = new ByteArrayInputStream(IOUtils.toByteArray(inputstream));
44+
}, null);
45+
}
46+
3147
@Test
3248
public void testGetters() throws IOException {
3349
GHRepository r = getTempRepository();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"id": 326623381,
3+
"node_id": "MDEwOlJlcG9zaXRvcnkzMjY2MjMzODE=",
4+
"name": "temp-testTarball",
5+
"full_name": "hub4j-test-org/temp-testTarball",
6+
"private": false,
7+
"owner": {
8+
"login": "hub4j-test-org",
9+
"id": 7544739,
10+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
11+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/hub4j-test-org",
14+
"html_url": "https://github.com/hub4j-test-org",
15+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
16+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
20+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
21+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
22+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
24+
"type": "Organization",
25+
"site_admin": false
26+
},
27+
"html_url": "https://github.com/hub4j-test-org/temp-testTarball",
28+
"description": "A test repository for testing the github-api project: temp-testTarball",
29+
"fork": false,
30+
"url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball",
31+
"forks_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/forks",
32+
"keys_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/keys{/key_id}",
33+
"collaborators_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/collaborators{/collaborator}",
34+
"teams_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/teams",
35+
"hooks_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/hooks",
36+
"issue_events_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/issues/events{/number}",
37+
"events_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/events",
38+
"assignees_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/assignees{/user}",
39+
"branches_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/branches{/branch}",
40+
"tags_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/tags",
41+
"blobs_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/git/blobs{/sha}",
42+
"git_tags_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/git/tags{/sha}",
43+
"git_refs_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/git/refs{/sha}",
44+
"trees_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/git/trees{/sha}",
45+
"statuses_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/statuses/{sha}",
46+
"languages_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/languages",
47+
"stargazers_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/stargazers",
48+
"contributors_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/contributors",
49+
"subscribers_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/subscribers",
50+
"subscription_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/subscription",
51+
"commits_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/commits{/sha}",
52+
"git_commits_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/git/commits{/sha}",
53+
"comments_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/comments{/number}",
54+
"issue_comment_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/issues/comments{/number}",
55+
"contents_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/contents/{+path}",
56+
"compare_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/compare/{base}...{head}",
57+
"merges_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/merges",
58+
"archive_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/{archive_format}{/ref}",
59+
"downloads_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/downloads",
60+
"issues_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/issues{/number}",
61+
"pulls_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/pulls{/number}",
62+
"milestones_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/milestones{/number}",
63+
"notifications_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/notifications{?since,all,participating}",
64+
"labels_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/labels{/name}",
65+
"releases_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/releases{/id}",
66+
"deployments_url": "https://api.github.com/repos/hub4j-test-org/temp-testTarball/deployments",
67+
"created_at": "2021-01-04T08:53:50Z",
68+
"updated_at": "2021-01-04T08:53:54Z",
69+
"pushed_at": "2021-01-04T08:53:52Z",
70+
"git_url": "git://github.com/hub4j-test-org/temp-testTarball.git",
71+
"ssh_url": "git@github.com:hub4j-test-org/temp-testTarball.git",
72+
"clone_url": "https://github.com/hub4j-test-org/temp-testTarball.git",
73+
"svn_url": "https://github.com/hub4j-test-org/temp-testTarball",
74+
"homepage": "http://github-api.kohsuke.org/",
75+
"size": 0,
76+
"stargazers_count": 0,
77+
"watchers_count": 0,
78+
"language": null,
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": "main",
94+
"permissions": {
95+
"admin": true,
96+
"push": true,
97+
"pull": true
98+
},
99+
"temp_clone_token": "",
100+
"allow_squash_merge": true,
101+
"allow_merge_commit": true,
102+
"allow_rebase_merge": true,
103+
"delete_branch_on_merge": false,
104+
"organization": {
105+
"login": "hub4j-test-org",
106+
"id": 7544739,
107+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
108+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
109+
"gravatar_id": "",
110+
"url": "https://api.github.com/users/hub4j-test-org",
111+
"html_url": "https://github.com/hub4j-test-org",
112+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
113+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
114+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
115+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
116+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
117+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
118+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
119+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
120+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
121+
"type": "Organization",
122+
"site_admin": false
123+
},
124+
"network_count": 0,
125+
"subscribers_count": 9
126+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"login": "bitwiseman",
3+
"id": 1958953,
4+
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
5+
"avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/bitwiseman",
8+
"html_url": "https://github.com/bitwiseman",
9+
"followers_url": "https://api.github.com/users/bitwiseman/followers",
10+
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
14+
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
15+
"repos_url": "https://api.github.com/users/bitwiseman/repos",
16+
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Liam Newman",
21+
"company": "Cloudbees, Inc.",
22+
"blog": "",
23+
"location": "Seattle, WA, USA",
24+
"email": "bitwiseman@gmail.com",
25+
"hireable": null,
26+
"bio": null,
27+
"twitter_username": "bitwiseman",
28+
"public_repos": 199,
29+
"public_gists": 7,
30+
"followers": 175,
31+
"following": 11,
32+
"created_at": "2012-07-11T20:38:33Z",
33+
"updated_at": "2020-12-23T22:23:08Z",
34+
"private_gists": 19,
35+
"total_private_repos": 17,
36+
"owned_private_repos": 0,
37+
"disk_usage": 33700,
38+
"collaborators": 0,
39+
"two_factor_authentication": true,
40+
"plan": {
41+
"name": "free",
42+
"space": 976562499,
43+
"collaborators": 0,
44+
"private_repos": 10000
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"id": "9223893a-a7e9-427e-adb0-e3759a83884d",
3+
"name": "repos_hub4j-test-org_temp-testtarball",
4+
"request": {
5+
"url": "/repos/hub4j-test-org/temp-testTarball",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "repos_hub4j-test-org_temp-testtarball-2.json",
16+
"headers": {
17+
"Date": "Mon, 04 Jan 2021 08:53:56 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "200 OK",
21+
"Cache-Control": "private, max-age=60, s-maxage=60",
22+
"Vary": [
23+
"Accept, Authorization, Cookie, X-GitHub-OTP",
24+
"Accept-Encoding, Accept, X-Requested-With",
25+
"Accept-Encoding"
26+
],
27+
"ETag": "W/\"0efc5253a075be861b75de091f949051cbb8d1edd1d9f5a2671361506750599f\"",
28+
"Last-Modified": "Mon, 04 Jan 2021 08:53:54 GMT",
29+
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, workflow, write:discussion",
30+
"X-Accepted-OAuth-Scopes": "repo",
31+
"X-GitHub-Media-Type": "unknown, github.v3",
32+
"X-RateLimit-Limit": "5000",
33+
"X-RateLimit-Remaining": "4956",
34+
"X-RateLimit-Reset": "1609753350",
35+
"X-RateLimit-Used": "44",
36+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
37+
"X-Frame-Options": "deny",
38+
"X-Content-Type-Options": "nosniff",
39+
"X-XSS-Protection": "1; mode=block",
40+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
41+
"Content-Security-Policy": "default-src 'none'",
42+
"X-GitHub-Request-Id": "E328:327C:74BB33:94FCDB:5FF2D79D"
43+
}
44+
},
45+
"uuid": "9223893a-a7e9-427e-adb0-e3759a83884d",
46+
"persistent": true,
47+
"insertionIndex": 2
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"id": "28a8f5bf-a72d-40cc-86a1-df9c1489c3ee",
3+
"name": "repos_hub4j-test-org_temp-testtarball_tarball",
4+
"request": {
5+
"url": "/repos/hub4j-test-org/temp-testTarball/tarball",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 302,
15+
"headers": {
16+
"Date": "Mon, 04 Jan 2021 08:53:56 GMT",
17+
"Content-Type": "text/html;charset=utf-8",
18+
"Server": "GitHub.com",
19+
"Status": "302 Found",
20+
"X-RateLimit-Limit": "5000",
21+
"X-RateLimit-Remaining": "4956",
22+
"X-RateLimit-Reset": "1609753350",
23+
"X-RateLimit-Used": "44",
24+
"Cache-Control": "public, must-revalidate, max-age=0",
25+
"Expires": "Mon, 04 Jan 2021 08:53:56 GMT",
26+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
27+
"X-Frame-Options": "deny",
28+
"X-Content-Type-Options": "nosniff",
29+
"X-XSS-Protection": "1; mode=block",
30+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
31+
"Content-Security-Policy": "default-src 'none'",
32+
"Vary": [
33+
"Accept-Encoding, Accept, X-Requested-With",
34+
"Accept-Encoding"
35+
],
36+
"X-GitHub-Request-Id": "E328:327C:74BB34:94FCF3:5FF2D7A4",
37+
"Location": "https://codeload.github.com/hub4j-test-org/temp-testTarball/legacy.tar.gz/main"
38+
}
39+
},
40+
"uuid": "28a8f5bf-a72d-40cc-86a1-df9c1489c3ee",
41+
"persistent": true,
42+
"insertionIndex": 3
43+
}

0 commit comments

Comments
 (0)