fix(api): expose version from jar at /v1/version endpoint (#1224)

This commit is contained in:
James Ward
2026-05-09 15:17:04 -04:00
committed by GitHub
parent e8c56e2f8f
commit 90547dfc52
4 changed files with 45 additions and 15 deletions
@@ -1,23 +1,26 @@
package org.booklore.controller;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.booklore.model.dto.HealthcheckResponse;
import org.booklore.model.dto.response.SuccessResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.booklore.service.VersionService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/healthcheck")
@Tag(name = "Healthcheck", description = "Endpoints for checking the health of the application")
public class HealthcheckController {
@Value("${app.version}")
private String appVersion;
private final VersionService versionService;
@Operation(summary = "Get a ping response", description = "Check if the application is responding")
@ApiResponse(responseCode = "200", description = "Health status returned successfully")
@@ -27,7 +30,7 @@ public class HealthcheckController {
HealthcheckResponse healthData = HealthcheckResponse.builder()
.status("UP")
.message("Application is running smoothly.")
.version(appVersion) // ex) 'development' Insert
.version(versionService.getAppVersion()) // ex) 'development' Insert
.timestamp(LocalDateTime.now())
.build();
@@ -13,14 +13,13 @@ import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
@Slf4j
@Service
public class VersionService {
@Value("${app.version:unknown}")
String appVersion;
private static final Pattern VERSION_PATTERN = Pattern.compile("^\\d+\\.\\d+\\.\\d+$");
private static final String DEVELOPMENT_VERSION = "development";
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 int MAX_RELEASES = 15;
@@ -30,6 +29,20 @@ public class VersionService {
.build();
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() {
String latest = "unknown";
@@ -38,11 +51,12 @@ public class VersionService {
} catch (Exception e) {
log.warn("Error fetching latest release version");
}
return new VersionInfo(appVersion, latest);
return new VersionInfo(getAppVersion(), latest);
}
public List<ReleaseNote> getChangelogSinceCurrentVersion() {
return fetchReleaseNotesSince(appVersion);
return fetchReleaseNotesSince(getAppVersion());
}
@@ -63,7 +77,7 @@ public class VersionService {
}
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);
return new ArrayList<>();
}
@@ -1,7 +1,6 @@
app:
path-config: '/app/data'
bookdrop-folder: '/bookdrop'
version: ${APP_VERSION:development}
api-docs:
enabled: ${API_DOCS_ENABLED:false}
@@ -115,7 +115,21 @@ class VersionServiceTest {
VersionInfo info = spyService.getVersionInfo();
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())
.isEqualTo("v9.9.9");
}
@@ -144,7 +158,7 @@ class VersionServiceTest {
Mockito.doReturn(List.of(note))
.when(spyService)
.fetchReleaseNotesSince(service.appVersion);
.fetchReleaseNotesSince(service.getAppVersion());
List<ReleaseNote> result = spyService.getChangelogSinceCurrentVersion();
assertThat(result).hasSize(1).containsExactly(note);
@@ -154,7 +168,7 @@ class VersionServiceTest {
void returnsEmptyListWhenNoNewReleases() {
Mockito.doReturn(List.of())
.when(spyService)
.fetchReleaseNotesSince(service.appVersion);
.fetchReleaseNotesSince(service.getAppVersion());
var result = spyService.getChangelogSinceCurrentVersion();
assertThat(result).isEmpty();