Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
import java.util.function.ToIntFunction;
import java.util.regex.Matcher;
import java.util.stream.Stream;
import java.util.concurrent.CompletableFuture;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Slash command (/github-search) used to search for an issue in one of the repositories listed in
Expand All @@ -40,11 +44,12 @@ public final class GitHubCommand extends SlashCommandAdapter {
};

private static final String TITLE_OPTION = "title";
private static final Logger logger = LoggerFactory.getLogger(GitHubCommand.class);

private final GitHubReference reference;

private Instant lastCacheUpdate;
private List<String> autocompleteGHIssueCache;
private Instant lastCacheUpdate = Instant.EPOCH;
private List<String> autocompleteGHIssueCache = List.of();

/**
* Constructs an instance of GitHubCommand.
Expand All @@ -65,7 +70,14 @@ public GitHubCommand(GitHubReference reference) {
getData().addOption(OptionType.STRING, TITLE_OPTION,
"Title of the issue you're looking for", true, true);

updateCache();
CompletableFuture.runAsync(() -> {
try {
updateCache();
} catch (Exception e) {
logger.error("Unknown error updating the GitHub cache", e);
}
});

}

@Override
Expand Down Expand Up @@ -110,7 +122,7 @@ public void onAutoComplete(CommandAutoCompleteInteractionEvent event) {
event.replyChoiceStrings(choices).queue();
}

if (lastCacheUpdate.isAfter(Instant.now().minus(CACHE_EXPIRES_AFTER))) {
if (isCacheExpired()) {
updateCache();
}
}
Expand All @@ -120,12 +132,20 @@ private ToIntFunction<String> suggestionScorer(String title) {
return s -> StringDistances.editDistance(title, s.replaceFirst("\\[#\\d+] ", ""));
}

private boolean isCacheExpired() {
Instant cacheExpiresAt = lastCacheUpdate.plus(CACHE_EXPIRES_AFTER);
return Instant.now().isAfter(cacheExpiresAt);
}

private void updateCache() {
logger.debug("GitHub Autocomplete cache update started");

autocompleteGHIssueCache = reference.getRepositories().stream().map(repo -> {
try {
return repo.queryIssues().pageSize(1000).list().toList();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
throw new UncheckedIOException("Error fetching issues from repo " + repo.getName(),
ex);
}
})
.flatMap(List::stream)
Expand All @@ -134,5 +154,8 @@ private void updateCache() {
.toList();

lastCacheUpdate = Instant.now();

logger.debug("GitHub autocomplete cache update completed successfully. Cached {} issues.",
autocompleteGHIssueCache.size());
}
}
Loading