GithubReleaseInfo.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 org.springframework.web.reactive.function.client.WebClient;
  6. import reactor.core.publisher.Mono;
  7. @Slf4j
  8. public class GithubReleaseInfo {
  9. private static final String GITHUB_LATEST_RELEASE_RETRIEVAL_URL =
  10. "https://api.github.com/repos/provectus/kafka-ui/releases/latest";
  11. private static final Duration GITHUB_API_MAX_WAIT_TIME = Duration.ofSeconds(2);
  12. public record GithubReleaseDto(String html_url, String tag_name, String published_at) {
  13. static GithubReleaseDto empty() {
  14. return new GithubReleaseDto(null, null, null);
  15. }
  16. }
  17. private volatile GithubReleaseDto release = GithubReleaseDto.empty();
  18. private final Mono<Void> refreshMono;
  19. public GithubReleaseInfo() {
  20. this(GITHUB_LATEST_RELEASE_RETRIEVAL_URL);
  21. }
  22. @VisibleForTesting
  23. GithubReleaseInfo(String url) {
  24. this.refreshMono = WebClient.create()
  25. .get()
  26. .uri(url)
  27. .exchangeToMono(resp -> resp.bodyToMono(GithubReleaseDto.class))
  28. .timeout(GITHUB_API_MAX_WAIT_TIME)
  29. .doOnError(th -> log.trace("Error getting latest github release info", th))
  30. .onErrorResume(th -> true, th -> Mono.just(GithubReleaseDto.empty()))
  31. .doOnNext(release -> this.release = release)
  32. .then();
  33. }
  34. public GithubReleaseDto get() {
  35. return release;
  36. }
  37. public Mono<Void> refresh() {
  38. return refreshMono;
  39. }
  40. }