|
50 | 50 | import com.spotify.github.v3.repos.Status;
|
51 | 51 | import com.spotify.github.v3.repos.requests.AuthenticatedUserRepositoriesFilter;
|
52 | 52 | import com.spotify.github.v3.repos.requests.RepositoryCreateStatus;
|
| 53 | +import java.io.InputStream; |
53 | 54 | import java.lang.invoke.MethodHandles;
|
54 | 55 | import java.util.Iterator;
|
55 | 56 | import java.util.List;
|
@@ -91,6 +92,8 @@ public class RepositoryClient {
|
91 | 92 | private static final String REPOSITORY_COLLABORATOR = "/repos/%s/%s/collaborators/%s";
|
92 | 93 | private static final String REPOSITORY_INVITATION = "/repos/%s/%s/invitations/%s";
|
93 | 94 | 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"; |
94 | 97 | private final String owner;
|
95 | 98 | private final String repo;
|
96 | 99 | private final GitHubClient github;
|
@@ -242,6 +245,56 @@ public CompletableFuture<List<RepositoryInvitation>> listInvitations() {
|
242 | 245 | return github.request(path, LIST_REPOSITORY_INVITATION);
|
243 | 246 | }
|
244 | 247 |
|
| 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 | + |
245 | 298 | /**
|
246 | 299 | * Create a webhook.
|
247 | 300 | *
|
|
0 commit comments