fix(api): expose version from jar at /v1/version endpoint (#1224)
This commit is contained in:
@@ -1,23 +1,26 @@
|
|||||||
package org.booklore.controller;
|
package org.booklore.controller;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.booklore.model.dto.HealthcheckResponse;
|
import org.booklore.model.dto.HealthcheckResponse;
|
||||||
import org.booklore.model.dto.response.SuccessResponse;
|
import org.booklore.model.dto.response.SuccessResponse;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||||
|
import org.booklore.service.VersionService;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@RequiredArgsConstructor
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v1/healthcheck")
|
@RequestMapping("/api/v1/healthcheck")
|
||||||
@Tag(name = "Healthcheck", description = "Endpoints for checking the health of the application")
|
@Tag(name = "Healthcheck", description = "Endpoints for checking the health of the application")
|
||||||
public class HealthcheckController {
|
public class HealthcheckController {
|
||||||
|
private final VersionService versionService;
|
||||||
@Value("${app.version}")
|
|
||||||
private String appVersion;
|
|
||||||
|
|
||||||
@Operation(summary = "Get a ping response", description = "Check if the application is responding")
|
@Operation(summary = "Get a ping response", description = "Check if the application is responding")
|
||||||
@ApiResponse(responseCode = "200", description = "Health status returned successfully")
|
@ApiResponse(responseCode = "200", description = "Health status returned successfully")
|
||||||
@@ -27,7 +30,7 @@ public class HealthcheckController {
|
|||||||
HealthcheckResponse healthData = HealthcheckResponse.builder()
|
HealthcheckResponse healthData = HealthcheckResponse.builder()
|
||||||
.status("UP")
|
.status("UP")
|
||||||
.message("Application is running smoothly.")
|
.message("Application is running smoothly.")
|
||||||
.version(appVersion) // ex) 'development' Insert
|
.version(versionService.getAppVersion()) // ex) 'development' Insert
|
||||||
.timestamp(LocalDateTime.now())
|
.timestamp(LocalDateTime.now())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
|||||||
@@ -13,14 +13,13 @@ import java.time.LocalDateTime;
|
|||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class VersionService {
|
public class VersionService {
|
||||||
|
private static final Pattern VERSION_PATTERN = Pattern.compile("^\\d+\\.\\d+\\.\\d+$");
|
||||||
@Value("${app.version:unknown}")
|
private static final String DEVELOPMENT_VERSION = "development";
|
||||||
String appVersion;
|
|
||||||
|
|
||||||
private static final String GITHUB_REPO = "grimmory-tools/grimmory";
|
private static final String GITHUB_REPO = "grimmory-tools/grimmory";
|
||||||
private static final String BASE_URI = "https://api.github.com/repos/" + GITHUB_REPO;
|
private static final String BASE_URI = "https://api.github.com/repos/" + GITHUB_REPO;
|
||||||
private static final int MAX_RELEASES = 15;
|
private static final int MAX_RELEASES = 15;
|
||||||
@@ -30,6 +29,20 @@ public class VersionService {
|
|||||||
.build();
|
.build();
|
||||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||||
|
|
||||||
|
public String getAppVersion() {
|
||||||
|
String appVersion = getClass().getPackage().getImplementationVersion();
|
||||||
|
|
||||||
|
if (appVersion == null) {
|
||||||
|
return DEVELOPMENT_VERSION;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If in X.Y.Z format, prefix with a `v` to match the tag.
|
||||||
|
if (VERSION_PATTERN.matcher(appVersion).matches()) {
|
||||||
|
appVersion = "v" + appVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
return appVersion;
|
||||||
|
}
|
||||||
|
|
||||||
public VersionInfo getVersionInfo() {
|
public VersionInfo getVersionInfo() {
|
||||||
String latest = "unknown";
|
String latest = "unknown";
|
||||||
@@ -38,11 +51,12 @@ public class VersionService {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Error fetching latest release version");
|
log.warn("Error fetching latest release version");
|
||||||
}
|
}
|
||||||
return new VersionInfo(appVersion, latest);
|
|
||||||
|
return new VersionInfo(getAppVersion(), latest);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ReleaseNote> getChangelogSinceCurrentVersion() {
|
public List<ReleaseNote> getChangelogSinceCurrentVersion() {
|
||||||
return fetchReleaseNotesSince(appVersion);
|
return fetchReleaseNotesSince(getAppVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -63,7 +77,7 @@ public class VersionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<ReleaseNote> fetchReleaseNotesSince(String currentVersion) {
|
public List<ReleaseNote> fetchReleaseNotesSince(String currentVersion) {
|
||||||
if ("development".equals(currentVersion)) {
|
if (DEVELOPMENT_VERSION.equals(currentVersion)) {
|
||||||
log.warn("Skipping fetch of release notes because current version is '{}', which is a local development build.", currentVersion);
|
log.warn("Skipping fetch of release notes because current version is '{}', which is a local development build.", currentVersion);
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
app:
|
app:
|
||||||
path-config: '/app/data'
|
path-config: '/app/data'
|
||||||
bookdrop-folder: '/bookdrop'
|
bookdrop-folder: '/bookdrop'
|
||||||
version: ${APP_VERSION:development}
|
|
||||||
api-docs:
|
api-docs:
|
||||||
enabled: ${API_DOCS_ENABLED:false}
|
enabled: ${API_DOCS_ENABLED:false}
|
||||||
|
|
||||||
|
|||||||
@@ -115,7 +115,21 @@ class VersionServiceTest {
|
|||||||
VersionInfo info = spyService.getVersionInfo();
|
VersionInfo info = spyService.getVersionInfo();
|
||||||
|
|
||||||
assertThat(info.getCurrent())
|
assertThat(info.getCurrent())
|
||||||
.isEqualTo(service.appVersion);
|
.isEqualTo(service.getAppVersion());
|
||||||
|
assertThat(info.getLatest())
|
||||||
|
.isEqualTo("v9.9.9");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void usesFallbackIfMissingPackageVersion() {
|
||||||
|
Mockito.doReturn("v9.9.9")
|
||||||
|
.when(spyService)
|
||||||
|
.fetchLatestGitHubReleaseVersion();
|
||||||
|
|
||||||
|
VersionInfo info = spyService.getVersionInfo();
|
||||||
|
|
||||||
|
assertThat(info.getCurrent())
|
||||||
|
.isEqualTo(service.getAppVersion());
|
||||||
assertThat(info.getLatest())
|
assertThat(info.getLatest())
|
||||||
.isEqualTo("v9.9.9");
|
.isEqualTo("v9.9.9");
|
||||||
}
|
}
|
||||||
@@ -144,7 +158,7 @@ class VersionServiceTest {
|
|||||||
|
|
||||||
Mockito.doReturn(List.of(note))
|
Mockito.doReturn(List.of(note))
|
||||||
.when(spyService)
|
.when(spyService)
|
||||||
.fetchReleaseNotesSince(service.appVersion);
|
.fetchReleaseNotesSince(service.getAppVersion());
|
||||||
|
|
||||||
List<ReleaseNote> result = spyService.getChangelogSinceCurrentVersion();
|
List<ReleaseNote> result = spyService.getChangelogSinceCurrentVersion();
|
||||||
assertThat(result).hasSize(1).containsExactly(note);
|
assertThat(result).hasSize(1).containsExactly(note);
|
||||||
@@ -154,7 +168,7 @@ class VersionServiceTest {
|
|||||||
void returnsEmptyListWhenNoNewReleases() {
|
void returnsEmptyListWhenNoNewReleases() {
|
||||||
Mockito.doReturn(List.of())
|
Mockito.doReturn(List.of())
|
||||||
.when(spyService)
|
.when(spyService)
|
||||||
.fetchReleaseNotesSince(service.appVersion);
|
.fetchReleaseNotesSince(service.getAppVersion());
|
||||||
|
|
||||||
var result = spyService.getChangelogSinceCurrentVersion();
|
var result = spyService.getChangelogSinceCurrentVersion();
|
||||||
assertThat(result).isEmpty();
|
assertThat(result).isEmpty();
|
||||||
|
|||||||
Reference in New Issue
Block a user