Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: null latest release information #577

Closed
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -73,7 +73,7 @@ private List<EnabledFeaturesEnum> getEnabledFeatures() {
// updating on startup and every hour
@Scheduled(fixedRateString = "${github-release-info-update-rate:3600000}")
public void updateGithubReleaseInfo() {
githubReleaseInfo.refresh().subscribe();
githubReleaseInfo.refresh();
}

@VisibleForTesting
Expand Down
43 changes: 21 additions & 22 deletions api/src/main/java/io/kafbat/ui/util/GithubReleaseInfo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.kafbat.ui.util;

import com.google.common.annotations.VisibleForTesting;
import java.time.Duration;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -9,48 +8,48 @@
@Slf4j
public class GithubReleaseInfo {
public static final String GITHUB_RELEASE_INFO_TIMEOUT = "github.release.info.timeout";

private static final String GITHUB_LATEST_RELEASE_RETRIEVAL_URL =
"https://api.github.com/repos/kafbat/kafka-ui/releases/latest";

public record GithubReleaseDto(String html_url, String tag_name, String published_at) {

static GithubReleaseDto empty() {
return new GithubReleaseDto(null, null, null);
}
}

private volatile GithubReleaseDto release = GithubReleaseDto.empty();

private final Mono<Void> refreshMono;

@Getter
private final int githubApiMaxWaitTime;
private volatile GithubReleaseDto release;

public GithubReleaseInfo(int githubApiMaxWaitTime) {
this(GITHUB_LATEST_RELEASE_RETRIEVAL_URL, githubApiMaxWaitTime);
this.githubApiMaxWaitTime = githubApiMaxWaitTime;
}

@VisibleForTesting
GithubReleaseInfo(String url, int githubApiMaxWaitTime) {
this.githubApiMaxWaitTime = githubApiMaxWaitTime;
this.refreshMono = new WebClientConfigurator().build()
public GithubReleaseDto get() {
if (release != null) {
return release;
}

refresh();
return release == null ? GithubReleaseDto.empty() : release;
}

public void refresh() {
refresh(GITHUB_LATEST_RELEASE_RETRIEVAL_URL);
}

public void refresh(String url) {
new WebClientConfigurator().build()
.get()
.uri(url)
.exchangeToMono(resp -> resp.bodyToMono(GithubReleaseDto.class))
.timeout(Duration.ofSeconds(this.githubApiMaxWaitTime))
.doOnError(th -> log.trace("Error getting latest github release info", th))
.timeout(Duration.ofSeconds(githubApiMaxWaitTime))
.doOnError(th -> log.error("Failed to retrieve latest release info", th))
.onErrorResume(th -> true, th -> Mono.just(GithubReleaseDto.empty()))
.doOnNext(release -> this.release = release)
.then();
.doOnNext(r -> this.release = r)
.block();
}

public GithubReleaseDto get() {
return release;
}

public Mono<Void> refresh() {
return refreshMono;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ void stopMockServer() throws IOException {

@Test
void test() {
var infoHolder = new GithubReleaseInfo(10);
var url = mockWebServer.url("repos/kafbat/kafka-ui/releases/latest").toString();

mockWebServer.enqueue(new MockResponse()
.addHeader("content-type: application/json")
.setBody("""
Expand All @@ -35,10 +38,8 @@ void test() {
"some_unused_prop": "ololo"
}
"""));
var url = mockWebServer.url("repos/kafbat/kafka-ui/releases/latest").toString();

var infoHolder = new GithubReleaseInfo(url, 10);
infoHolder.refresh().block();
infoHolder.refresh(url);

var i = infoHolder.get();
assertThat(i.html_url())
Expand Down
Loading