From b3ae581e0353643e659985f638284738a56b6e0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Champeau?= Date: Mon, 18 Jul 2022 10:50:35 +0200 Subject: [PATCH] Cache find baseline for an hour (#369) The previous implementation required fetching the remote list of releases before determining if the task was cacheable or not. This caused rate limiting issues on GitHub (while this was in theory the right thing to do). To workaround limits, we're replacing the JSON input with a timestamp corresponding to the current hour, so we'd fetch at most once per hour. --- .../build/compat/FindBaselineTask.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/io/micronaut/build/compat/FindBaselineTask.java b/src/main/java/io/micronaut/build/compat/FindBaselineTask.java index f5416188..283d8ce5 100644 --- a/src/main/java/io/micronaut/build/compat/FindBaselineTask.java +++ b/src/main/java/io/micronaut/build/compat/FindBaselineTask.java @@ -21,12 +21,15 @@ import org.gradle.api.file.RegularFileProperty; import org.gradle.api.provider.Property; import org.gradle.api.provider.Provider; +import org.gradle.api.provider.ProviderFactory; import org.gradle.api.tasks.CacheableTask; import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.OutputFile; import org.gradle.api.tasks.TaskAction; import org.jetbrains.annotations.NotNull; +import javax.inject.Inject; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.HttpURLConnection; @@ -44,6 +47,9 @@ @CacheableTask public abstract class FindBaselineTask extends DefaultTask { + + public static final int CACHE_IN_SECONDS = 3600; + @Input public abstract Property getGithubSlug(); @@ -51,10 +57,22 @@ public abstract class FindBaselineTask extends DefaultTask { public abstract Property getCurrentVersion(); @Input + protected Provider getTimestamp() { + return getProviders().provider(() -> { + long seconds = System.currentTimeMillis() / 1000; + long base = seconds / CACHE_IN_SECONDS; + return base * CACHE_IN_SECONDS; + }); + } + + @Internal protected Provider getJson() { return getGithubSlug().map(this::fetchReleasesFromGitHub); } + @Inject + protected abstract ProviderFactory getProviders(); + private byte[] fetchReleasesFromGitHub(String slug) { String releasesUrl = "https://api.github.com/repos/" + normalizeSlug(slug) + "/releases"; try {