GithubReleaseInfo.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package com.provectus.kafka.ui.util;
  2. import com.google.common.annotations.VisibleForTesting;
  3. import java.time.Duration;
  4. import lombok.extern.slf4j.Slf4j;
  5. import reactor.core.publisher.Mono;
  6. @Slf4j
  7. public class GithubReleaseInfo {
  8. private static final String GITHUB_LATEST_RELEASE_RETRIEVAL_URL =
  9. "https://api.github.com/repos/provectus/kafka-ui/releases/latest";
  10. private static final Duration GITHUB_API_MAX_WAIT_TIME = Duration.ofSeconds(2);
  11. public record GithubReleaseDto(String html_url, String tag_name, String published_at) {
  12. static GithubReleaseDto empty() {
  13. return new GithubReleaseDto(null, null, null);
  14. }
  15. }
  16. private volatile GithubReleaseDto release = GithubReleaseDto.empty();
  17. private final Mono<Void> refreshMono;
  18. public GithubReleaseInfo() {
  19. this(GITHUB_LATEST_RELEASE_RETRIEVAL_URL);
  20. }
  21. @VisibleForTesting
  22. GithubReleaseInfo(String url) {
  23. this.refreshMono = new WebClientConfigurator().build()
  24. .get()
  25. .uri(url)
  26. .exchangeToMono(resp -> resp.bodyToMono(GithubReleaseDto.class))
  27. .timeout(GITHUB_API_MAX_WAIT_TIME)
  28. .doOnError(th -> log.trace("Error getting latest github release info", th))
  29. .onErrorResume(th -> true, th -> Mono.just(GithubReleaseDto.empty()))
  30. .doOnNext(release -> this.release = release)
  31. .then();
  32. }
  33. public GithubReleaseDto get() {
  34. return release;
  35. }
  36. public Mono<Void> refresh() {
  37. return refreshMono;
  38. }
  39. }