diff --git a/src/main/java/org/eclipse/xpanse/terraform/boot/terraform/tool/TerraformVersionsFetcher.java b/src/main/java/org/eclipse/xpanse/terraform/boot/terraform/tool/TerraformVersionsFetcher.java index 5f0b509..0246777 100644 --- a/src/main/java/org/eclipse/xpanse/terraform/boot/terraform/tool/TerraformVersionsFetcher.java +++ b/src/main/java/org/eclipse/xpanse/terraform/boot/terraform/tool/TerraformVersionsFetcher.java @@ -5,16 +5,20 @@ package org.eclipse.xpanse.terraform.boot.terraform.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.terraform.boot.models.exceptions.InvalidTerraformToolException; 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; @@ -47,7 +51,10 @@ public class TerraformVersionsFetcher { backoff = @Backoff(delayExpression = "${spring.retry.delay-millions}")) public Set fetchAvailableVersionsFromTerraformWebsite() throws Exception { Set allVersions = new HashSet<>(); - GitHub gitHub = new GitHubBuilder().withEndpoint(terraformGithubApiEndpoint).build(); + GitHub gitHub = new GitHubBuilder() + .withEndpoint(terraformGithubApiEndpoint) + .withRateLimitHandler(getGithubRateLimitHandler()) + .build(); GHRepository repository = gitHub.getRepository(terraformGithubRepository); 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); + } + }; + } + }