Skip to content

Commit 3f07b24

Browse files
authored
feat: add ability to download repository content as zip/tar (#152)
* separate methods to download the tar or zip archive * methods return an Optional InputStream that can be consumed
1 parent 87dbc5e commit 3f07b24

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/main/java/com/spotify/github/v3/clients/RepositoryClient.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.spotify.github.v3.repos.Status;
5151
import com.spotify.github.v3.repos.requests.AuthenticatedUserRepositoriesFilter;
5252
import com.spotify.github.v3.repos.requests.RepositoryCreateStatus;
53+
import java.io.InputStream;
5354
import java.lang.invoke.MethodHandles;
5455
import java.util.Iterator;
5556
import java.util.List;
@@ -91,6 +92,8 @@ public class RepositoryClient {
9192
private static final String REPOSITORY_COLLABORATOR = "/repos/%s/%s/collaborators/%s";
9293
private static final String REPOSITORY_INVITATION = "/repos/%s/%s/invitations/%s";
9394
private static final String REPOSITORY_INVITATIONS = "/repos/%s/%s/invitations";
95+
private static final String REPOSITORY_DOWNLOAD_TARBALL = "/repos/%s/%s/tarball/%s";
96+
private static final String REPOSITORY_DOWNLOAD_ZIPBALL = "/repos/%s/%s/zipball/%s";
9497
private final String owner;
9598
private final String repo;
9699
private final GitHubClient github;
@@ -242,6 +245,56 @@ public CompletableFuture<List<RepositoryInvitation>> listInvitations() {
242245
return github.request(path, LIST_REPOSITORY_INVITATION);
243246
}
244247

248+
/**
249+
* Downloads a tar archive of the repository’s default branch (usually main).
250+
*
251+
* @return a CompletableFuture that resolves to an Optional InputStream
252+
*/
253+
public CompletableFuture<Optional<InputStream>> downloadTarball() {
254+
return downloadRepository(REPOSITORY_DOWNLOAD_TARBALL, Optional.empty());
255+
}
256+
257+
/**
258+
* Downloads a tar archive of the repository. Use :ref to specify a branch or tag to download.
259+
*
260+
* @return a CompletableFuture that resolves to an Optional InputStream
261+
*/
262+
public CompletableFuture<Optional<InputStream>> downloadTarball(final String ref) {
263+
return downloadRepository(REPOSITORY_DOWNLOAD_TARBALL, Optional.of(ref));
264+
}
265+
266+
/**
267+
* Downloads a zip archive of the repository’s default branch (usually main).
268+
*
269+
* @return a CompletableFuture that resolves to an Optional InputStream
270+
*/
271+
public CompletableFuture<Optional<InputStream>> downloadZipball() {
272+
return downloadRepository(REPOSITORY_DOWNLOAD_ZIPBALL, Optional.empty());
273+
}
274+
275+
/**
276+
* Downloads a zip archive of the repository. Use :ref to specify a branch or tag to download.
277+
*
278+
* @return a CompletableFuture that resolves to an Optional InputStream
279+
*/
280+
public CompletableFuture<Optional<InputStream>> downloadZipball(final String ref) {
281+
return downloadRepository(REPOSITORY_DOWNLOAD_ZIPBALL, Optional.of(ref));
282+
}
283+
284+
private CompletableFuture<Optional<InputStream>> downloadRepository(final String path, final Optional<String> maybeRef) {
285+
final var repoRef = maybeRef.orElse("");
286+
final var repoPath = String.format(path, owner, repo, repoRef);
287+
return github.request(repoPath).thenApply(response -> {
288+
var body = response.body();
289+
290+
if (body == null) {
291+
return Optional.empty();
292+
}
293+
294+
return Optional.of(body.byteStream());
295+
});
296+
}
297+
245298
/**
246299
* Create a webhook.
247300
*

src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
import com.spotify.github.v3.repos.Status;
6666
import com.spotify.github.v3.repos.requests.ImmutableAuthenticatedUserRepositoriesFilter;
6767
import java.io.IOException;
68+
import java.io.InputStream;
69+
import java.nio.charset.StandardCharsets;
6870
import java.util.List;
6971
import java.util.Optional;
7072
import java.util.concurrent.CompletableFuture;
@@ -537,4 +539,63 @@ public void mergeNoop() {
537539
final Optional<CommitItem> maybeCommit = repoClient.merge("basebranch", "headbranch").join();
538540
assertThat(maybeCommit, is(Optional.empty()));
539541
}
542+
543+
@Test
544+
public void shouldDownloadTarball() throws Exception {
545+
CompletableFuture<Response> fixture = completedFuture(
546+
new Response.Builder()
547+
.request(new Request.Builder().url("https://example.com/whatever").build())
548+
.protocol(Protocol.HTTP_1_1)
549+
.message("")
550+
.code(200)
551+
.body(
552+
ResponseBody.create(
553+
"some bytes".getBytes(StandardCharsets.UTF_8),
554+
MediaType.get("application/gzip")
555+
))
556+
.build());
557+
when(github.request("/repos/someowner/somerepo/tarball/")).thenReturn(fixture);
558+
559+
try(InputStream response = repoClient.downloadTarball().get().orElseThrow()) {
560+
String result = new String(response.readAllBytes(), StandardCharsets.UTF_8);
561+
assertThat(result, is("some bytes"));
562+
}
563+
}
564+
565+
@Test
566+
public void shouldDownloadZipball() throws Exception {
567+
CompletableFuture<Response> fixture = completedFuture(
568+
new Response.Builder()
569+
.request(new Request.Builder().url("https://example.com/whatever").build())
570+
.protocol(Protocol.HTTP_1_1)
571+
.message("")
572+
.code(200)
573+
.body(
574+
ResponseBody.create(
575+
"some bytes".getBytes(StandardCharsets.UTF_8),
576+
MediaType.get("application/gzip")
577+
))
578+
.build());
579+
when(github.request("/repos/someowner/somerepo/zipball/")).thenReturn(fixture);
580+
581+
try (InputStream response = repoClient.downloadZipball().get().orElseThrow()) {
582+
String result = new String(response.readAllBytes(), StandardCharsets.UTF_8);
583+
assertThat(result, is("some bytes"));
584+
}
585+
}
586+
587+
@Test
588+
public void shouldReturnEmptyOptionalWhenResponseBodyNotPresent() throws Exception {
589+
CompletableFuture<Response> fixture = completedFuture(
590+
new Response.Builder()
591+
.request(new Request.Builder().url("https://example.com/whatever").build())
592+
.protocol(Protocol.HTTP_1_1)
593+
.message("")
594+
.code(204) // No Content
595+
.build());
596+
when(github.request("/repos/someowner/somerepo/zipball/master")).thenReturn(fixture);
597+
598+
Optional<InputStream> response = repoClient.downloadZipball("master").get();
599+
assertThat(response, is(Optional.empty()));
600+
}
540601
}

0 commit comments

Comments
 (0)