diff --git a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsFetcher.java b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsFetcher.java index 46235be..59e7760 100644 --- a/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsFetcher.java +++ b/src/main/java/org/eclipse/xpanse/tofu/maker/opentofu/tool/OpenTofuVersionsFetcher.java @@ -5,16 +5,20 @@ package org.eclipse.xpanse.tofu.maker.opentofu.tool; +import java.io.IOException; import java.util.HashSet; import java.util.Set; import java.util.regex.Pattern; +import javax.annotation.Nonnull; import lombok.extern.slf4j.Slf4j; import org.eclipse.xpanse.tofu.maker.models.exceptions.InvalidOpenTofuToolException; import org.kohsuke.github.GHRepository; import org.kohsuke.github.GHTag; import org.kohsuke.github.GitHub; import org.kohsuke.github.GitHubBuilder; +import org.kohsuke.github.GitHubRateLimitHandler; import org.kohsuke.github.PagedIterable; +import org.kohsuke.github.connector.GitHubConnectorResponse; import org.springframework.beans.factory.annotation.Value; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Retryable; @@ -46,7 +50,10 @@ public class OpenTofuVersionsFetcher { backoff = @Backoff(delayExpression = "${spring.retry.delay-millions}")) public Set fetchAvailableVersionsFromOpenTofuWebsite() throws Exception { Set allVersions = new HashSet<>(); - GitHub gitHub = new GitHubBuilder().withEndpoint(openTofuGithubApiEndpoint).build(); + GitHub gitHub = new GitHubBuilder() + .withEndpoint(openTofuGithubApiEndpoint) + .withRateLimitHandler(getGithubRateLimitHandler()) + .build(); GHRepository repository = gitHub.getRepository(openTofuGithubRepository); PagedIterable tags = repository.listTags(); tags.forEach(tag -> { @@ -78,4 +85,18 @@ public Set getDefaultVersionsFromConfig() { return defaultVersions; } + private GitHubRateLimitHandler getGithubRateLimitHandler() { + return new GitHubRateLimitHandler() { + @Override + public void onError(@Nonnull GitHubConnectorResponse response) throws IOException { + String limit = response.header("X-RateLimit-Limit"); + String remaining = response.header("X-RateLimit-Remaining"); + String reset = response.header("X-RateLimit-Reset"); + String errorMsg = String.format("GitHub API rate limit exceeded. " + + "Rate limit: %s, remaining: %s, reset time: %s", limit, remaining, reset); + throw new IOException(errorMsg); + } + }; + } + }