Skip to content

Commit

Permalink
Cache find baseline for an hour (#369)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
melix authored Jul 18, 2022
1 parent 78979c9 commit b3ae581
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/io/micronaut/build/compat/FindBaselineTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,17 +47,32 @@

@CacheableTask
public abstract class FindBaselineTask extends DefaultTask {

public static final int CACHE_IN_SECONDS = 3600;

@Input
public abstract Property<String> getGithubSlug();

@Input
public abstract Property<String> getCurrentVersion();

@Input
protected Provider<Long> getTimestamp() {
return getProviders().provider(() -> {
long seconds = System.currentTimeMillis() / 1000;
long base = seconds / CACHE_IN_SECONDS;
return base * CACHE_IN_SECONDS;
});
}

@Internal
protected Provider<byte[]> 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 {
Expand Down

0 comments on commit b3ae581

Please sign in to comment.