Skip to content

Commit 3616d36

Browse files
stklcodegermanosin
andauthored
BE: Allow disabling GitHub release info (#1108)
Co-authored-by: German Osin <[email protected]>
1 parent d4bd299 commit 3616d36

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

api/src/main/java/io/kafbat/ui/service/ApplicationInfoService.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static io.kafbat.ui.api.model.AuthType.DISABLED;
44
import static io.kafbat.ui.api.model.AuthType.OAUTH2;
55
import static io.kafbat.ui.model.ApplicationInfoDTO.EnabledFeaturesEnum;
6+
import static io.kafbat.ui.util.GithubReleaseInfo.GITHUB_RELEASE_INFO_ENABLED;
67
import static io.kafbat.ui.util.GithubReleaseInfo.GITHUB_RELEASE_INFO_TIMEOUT;
78

89
import com.google.common.annotations.VisibleForTesting;
@@ -15,12 +16,14 @@
1516
import io.kafbat.ui.model.OAuthProviderDTO;
1617
import io.kafbat.ui.util.DynamicConfigOperations;
1718
import io.kafbat.ui.util.GithubReleaseInfo;
19+
import jakarta.annotation.Nullable;
1820
import java.time.format.DateTimeFormatter;
1921
import java.util.ArrayList;
2022
import java.util.Collections;
2123
import java.util.List;
2224
import java.util.Optional;
2325
import java.util.Properties;
26+
import lombok.extern.slf4j.Slf4j;
2427
import org.springframework.beans.factory.annotation.Autowired;
2528
import org.springframework.beans.factory.annotation.Value;
2629
import org.springframework.boot.info.BuildProperties;
@@ -33,7 +36,9 @@
3336
import org.springframework.stereotype.Service;
3437

3538
@Service
39+
@Slf4j
3640
public class ApplicationInfoService {
41+
@Nullable
3742
private final GithubReleaseInfo githubReleaseInfo;
3843
private final ApplicationContext applicationContext;
3944
private final DynamicConfigOperations dynamicConfigOperations;
@@ -44,36 +49,52 @@ public ApplicationInfoService(DynamicConfigOperations dynamicConfigOperations,
4449
ApplicationContext applicationContext,
4550
@Autowired(required = false) BuildProperties buildProperties,
4651
@Autowired(required = false) GitProperties gitProperties,
52+
@Value("${" + GITHUB_RELEASE_INFO_ENABLED + ":true}") boolean githubInfoEnabled,
4753
@Value("${" + GITHUB_RELEASE_INFO_TIMEOUT + ":10}") int githubApiMaxWaitTime) {
4854
this.applicationContext = applicationContext;
4955
this.dynamicConfigOperations = dynamicConfigOperations;
5056
this.buildProperties = Optional.ofNullable(buildProperties).orElse(new BuildProperties(new Properties()));
5157
this.gitProperties = Optional.ofNullable(gitProperties).orElse(new GitProperties(new Properties()));
52-
githubReleaseInfo = new GithubReleaseInfo(githubApiMaxWaitTime);
58+
if (githubInfoEnabled) {
59+
this.githubReleaseInfo = new GithubReleaseInfo(githubApiMaxWaitTime);
60+
} else {
61+
this.githubReleaseInfo = null;
62+
log.warn("Check for latest release is disabled."
63+
+ " Note that old versions are not supported, please make sure that your system is up to date.");
64+
}
5365
}
5466

5567
public ApplicationInfoDTO getApplicationInfo() {
56-
var releaseInfo = githubReleaseInfo.get();
68+
var releaseInfo = githubReleaseInfo != null ? githubReleaseInfo.get() : null;
5769
return new ApplicationInfoDTO()
5870
.build(getBuildInfo(releaseInfo))
5971
.enabledFeatures(getEnabledFeatures())
6072
.latestRelease(convert(releaseInfo));
6173
}
6274

75+
@Nullable
6376
private ApplicationInfoLatestReleaseDTO convert(GithubReleaseInfo.GithubReleaseDto releaseInfo) {
77+
if (releaseInfo == null) {
78+
return null;
79+
}
6480
return new ApplicationInfoLatestReleaseDTO()
6581
.htmlUrl(releaseInfo.html_url())
6682
.publishedAt(releaseInfo.published_at())
6783
.versionTag(releaseInfo.tag_name());
6884
}
6985

7086
private ApplicationInfoBuildDTO getBuildInfo(GithubReleaseInfo.GithubReleaseDto release) {
71-
return new ApplicationInfoBuildDTO()
72-
.isLatestRelease(release.tag_name() != null && release.tag_name().equals(buildProperties.getVersion()))
87+
var buildInfo = new ApplicationInfoBuildDTO()
7388
.commitId(gitProperties.getShortCommitId())
7489
.version(buildProperties.getVersion())
7590
.buildTime(buildProperties.getTime() != null
7691
? DateTimeFormatter.ISO_INSTANT.format(buildProperties.getTime()) : null);
92+
if (release != null) {
93+
buildInfo = buildInfo.isLatestRelease(
94+
release.tag_name() != null && release.tag_name().equals(buildProperties.getVersion())
95+
);
96+
}
97+
return buildInfo;
7798
}
7899

79100
private List<EnabledFeaturesEnum> getEnabledFeatures() {
@@ -119,10 +140,13 @@ private List<OAuthProviderDTO> getOAuthProviders() {
119140
// updating on startup and every hour
120141
@Scheduled(fixedRateString = "${github-release-info-update-rate:3600000}")
121142
public void updateGithubReleaseInfo() {
122-
githubReleaseInfo.refresh().subscribe();
143+
if (githubReleaseInfo != null) {
144+
githubReleaseInfo.refresh().subscribe();
145+
}
123146
}
124147

125148
@VisibleForTesting
149+
@Nullable
126150
GithubReleaseInfo githubReleaseInfo() {
127151
return githubReleaseInfo;
128152
}

api/src/main/java/io/kafbat/ui/util/GithubReleaseInfo.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
@Slf4j
1010
public class GithubReleaseInfo {
11+
public static final String GITHUB_RELEASE_INFO_ENABLED = "github.release.info.enabled";
1112
public static final String GITHUB_RELEASE_INFO_TIMEOUT = "github.release.info.timeout";
1213

1314
private static final String GITHUB_LATEST_RELEASE_RETRIEVAL_URL =
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,43 @@
11
package io.kafbat.ui.service;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
46

57
import io.kafbat.ui.AbstractIntegrationTest;
8+
import io.kafbat.ui.util.DynamicConfigOperations;
69
import org.junit.jupiter.api.Test;
710
import org.springframework.beans.factory.annotation.Autowired;
811

912
class ApplicationInfoServiceTest extends AbstractIntegrationTest {
1013
@Autowired
1114
private ApplicationInfoService service;
1215

16+
@Autowired
17+
private DynamicConfigOperations dynamicConfigOperations;
18+
1319
@Test
1420
void testCustomGithubReleaseInfoTimeout() {
1521
assertEquals(100, service.githubReleaseInfo().getGithubApiMaxWaitTime());
1622
}
23+
24+
@Test
25+
void testDisabledReleaseInfo() {
26+
var service2 = new ApplicationInfoService(
27+
dynamicConfigOperations,
28+
null,
29+
null,
30+
null,
31+
false,
32+
101
33+
);
34+
35+
assertNull(service2.githubReleaseInfo(), "unexpected GitHub release info when disabled");
36+
var appInfo = service2.getApplicationInfo();
37+
assertNotNull(appInfo, "application info must not be NULL");
38+
assertNull(appInfo.getLatestRelease(), "latest release should be NULL when disabled");
39+
assertNotNull(appInfo.getBuild(), "build info must not be NULL");
40+
assertNotNull(appInfo.getEnabledFeatures(), "enabled features must not be NULL");
41+
}
42+
1743
}

frontend/src/components/Version/Version.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const Version: React.FC = () => {
2424

2525
return (
2626
<S.Wrapper>
27-
{!isLatestRelease && (
27+
{isLatestRelease === false && (
2828
<S.OutdatedWarning
2929
title={`Your app version is outdated. Latest version is ${
3030
versionTag || 'UNKNOWN'

0 commit comments

Comments
 (0)