diff --git a/src/main/java/me/itzg/helpers/github/DownloadLatestAssetCommand.java b/src/main/java/me/itzg/helpers/github/DownloadLatestAssetCommand.java index 23ad7595..f3d231ed 100644 --- a/src/main/java/me/itzg/helpers/github/DownloadLatestAssetCommand.java +++ b/src/main/java/me/itzg/helpers/github/DownloadLatestAssetCommand.java @@ -13,6 +13,7 @@ import picocli.CommandLine.Command; import picocli.CommandLine.Option; import picocli.CommandLine.Parameters; +import picocli.CommandLine.ParentCommand; @Command(name = "download-latest-asset", description = "From the latest release, downloads the first matching asset, and outputs the downloaded filename" @@ -29,8 +30,8 @@ public class DownloadLatestAssetCommand implements Callable { @Option(names = "--output-directory", defaultValue = ".") Path outputDirectory; - @Option(names = "--api-base-url", defaultValue = GithubClient.DEFAULT_API_BASE_URL) - String apiBaseUrl; + @ParentCommand + GithubCommands parent; @Parameters(arity = "1", paramLabel = "org/repo") public void setOrgRepo(String input) { @@ -50,7 +51,7 @@ public Integer call() throws Exception { try (SharedFetch sharedFetch = Fetch.sharedFetch("github download-latest-asset", sharedFetchArgs.options())) { - final GithubClient client = new GithubClient(sharedFetch, apiBaseUrl); + final GithubClient client = new GithubClient(sharedFetch, parent.getApiBaseUrl(), parent.getToken()); final Path result = client.downloadLatestAsset(organization, repo, namePattern, outputDirectory) .block(); diff --git a/src/main/java/me/itzg/helpers/github/GithubClient.java b/src/main/java/me/itzg/helpers/github/GithubClient.java index 13dff178..3fcfc854 100644 --- a/src/main/java/me/itzg/helpers/github/GithubClient.java +++ b/src/main/java/me/itzg/helpers/github/GithubClient.java @@ -28,10 +28,12 @@ public class GithubClient { private final SharedFetch sharedFetch; private final UriBuilder uriBuilder; + private final String token; - public GithubClient(SharedFetch sharedFetch, String apiBaseUrl) { + public GithubClient(SharedFetch sharedFetch, String apiBaseUrl, @Nullable String token) { this.sharedFetch = sharedFetch; this.uriBuilder = UriBuilder.withBaseUrl(apiBaseUrl); + this.token = token; } public Mono downloadLatestAsset(String org, String repo, @Nullable Pattern namePattern, Path outputDirectory) { @@ -39,6 +41,7 @@ public Mono downloadLatestAsset(String org, String repo, @Nullable Pattern uriBuilder.resolve("/repos/{org}/{repo}/releases/latest", org, repo) ) .acceptContentTypes(Collections.singletonList("application/vnd.github+json")) + .withAuthorization("Bearer", token) .toObject(Release.class) .assemble() .onErrorResume(throwable -> { diff --git a/src/main/java/me/itzg/helpers/github/GithubCommands.java b/src/main/java/me/itzg/helpers/github/GithubCommands.java index 7080ead1..838152dd 100644 --- a/src/main/java/me/itzg/helpers/github/GithubCommands.java +++ b/src/main/java/me/itzg/helpers/github/GithubCommands.java @@ -1,12 +1,22 @@ package me.itzg.helpers.github; +import lombok.Getter; import picocli.CommandLine.Command; +import picocli.CommandLine.Option; @Command(name = "github", subcommands = { DownloadLatestAssetCommand.class } ) +@Getter public class GithubCommands { + @Option(names = "--api-base-url", defaultValue = GithubClient.DEFAULT_API_BASE_URL) + String apiBaseUrl; + + @Option(names = "--token", defaultValue = "${env:GH_TOKEN}", + description = "An access token for GitHub to elevate rate limit vs anonymous access" + ) + String token; } diff --git a/src/main/java/me/itzg/helpers/http/FetchBuilderBase.java b/src/main/java/me/itzg/helpers/http/FetchBuilderBase.java index 63202484..22a92e7e 100644 --- a/src/main/java/me/itzg/helpers/http/FetchBuilderBase.java +++ b/src/main/java/me/itzg/helpers/http/FetchBuilderBase.java @@ -29,6 +29,7 @@ import me.itzg.helpers.errors.GenericException; import me.itzg.helpers.http.SharedFetch.Options; import me.itzg.helpers.json.ObjectMappers; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import reactor.core.publisher.Mono; import reactor.netty.ByteBufMono; @@ -172,6 +173,15 @@ public SELF header(String name, String value) { return self(); } + public SELF withAuthorization(String authorizationType, String credentials) { + if (StringUtils.isNotBlank(authorizationType) && StringUtils.isNotBlank(credentials)) { + return header(AUTHORIZATION.toString(), authorizationType + " " + credentials); + } + else { + return self(); + } + } + /** * Helps with fluent sub-type builder pattern */ diff --git a/src/main/java/me/itzg/helpers/http/SharedFetch.java b/src/main/java/me/itzg/helpers/http/SharedFetch.java index 131133ff..b6343d3a 100644 --- a/src/main/java/me/itzg/helpers/http/SharedFetch.java +++ b/src/main/java/me/itzg/helpers/http/SharedFetch.java @@ -118,12 +118,6 @@ public FetchBuilderBase fetch(URI uri) { return new FetchBuilderBase<>(uri, this); } - @SuppressWarnings("unused") - public SharedFetch addHeader(String name, String value) { - headers.put(name, value); - return this; - } - @Override public void close() { } diff --git a/src/test/java/me/itzg/helpers/github/DownloadLatestAssetCommandTest.java b/src/test/java/me/itzg/helpers/github/DownloadLatestAssetCommandTest.java index 98f9ce42..84b85527 100644 --- a/src/test/java/me/itzg/helpers/github/DownloadLatestAssetCommandTest.java +++ b/src/test/java/me/itzg/helpers/github/DownloadLatestAssetCommandTest.java @@ -31,10 +31,12 @@ class DownloadLatestAssetCommandTest { .configureStaticDsl(true) .build(); + private final RandomStringUtils randomStringUtils = RandomStringUtils.insecure(); + @Test void usingNamePattern(@TempDir Path tempDir, WireMockRuntimeInfo wmInfo) { - final String filename = RandomStringUtils.randomAlphabetic(10) + ".jar"; - final String fileContent = RandomStringUtils.randomAlphabetic(20); + final String filename = randomStringUtils.nextAlphabetic(10) + ".jar"; + final String fileContent = randomStringUtils.nextAlphabetic(20); stubFor(get("/repos/org/repo/releases/latest") .willReturn(ok() @@ -58,9 +60,10 @@ void usingNamePattern(@TempDir Path tempDir, WireMockRuntimeInfo wmInfo) { ) ); - final int exitCode = new CommandLine(new DownloadLatestAssetCommand()) + final int exitCode = new CommandLine(new GithubCommands()) .execute( "--api-base-url", wmInfo.getHttpBaseUrl(), + "download-latest-asset", "--name-pattern", "app-.+?(?