From 90547dfc52f9d9151413d8de583d5dabdc1d28cb Mon Sep 17 00:00:00 2001 From: James Ward Date: Sat, 9 May 2026 15:17:04 -0400 Subject: [PATCH] fix(api): expose version from jar at `/v1/version` endpoint (#1224) --- .../controller/HealthcheckController.java | 11 +++++--- .../org/booklore/service/VersionService.java | 28 ++++++++++++++----- backend/src/main/resources/application.yaml | 1 - .../booklore/service/VersionServiceTest.java | 20 +++++++++++-- 4 files changed, 45 insertions(+), 15 deletions(-) diff --git a/backend/src/main/java/org/booklore/controller/HealthcheckController.java b/backend/src/main/java/org/booklore/controller/HealthcheckController.java index 4758ccc69..0a0ce07e8 100644 --- a/backend/src/main/java/org/booklore/controller/HealthcheckController.java +++ b/backend/src/main/java/org/booklore/controller/HealthcheckController.java @@ -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(); diff --git a/backend/src/main/java/org/booklore/service/VersionService.java b/backend/src/main/java/org/booklore/service/VersionService.java index 761de5998..303f87084 100644 --- a/backend/src/main/java/org/booklore/service/VersionService.java +++ b/backend/src/main/java/org/booklore/service/VersionService.java @@ -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 getChangelogSinceCurrentVersion() { - return fetchReleaseNotesSince(appVersion); + return fetchReleaseNotesSince(getAppVersion()); } @@ -63,7 +77,7 @@ public class VersionService { } public List 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<>(); } diff --git a/backend/src/main/resources/application.yaml b/backend/src/main/resources/application.yaml index e82a78aa4..94a8311cb 100644 --- a/backend/src/main/resources/application.yaml +++ b/backend/src/main/resources/application.yaml @@ -1,7 +1,6 @@ app: path-config: '/app/data' bookdrop-folder: '/bookdrop' - version: ${APP_VERSION:development} api-docs: enabled: ${API_DOCS_ENABLED:false} diff --git a/backend/src/test/java/org/booklore/service/VersionServiceTest.java b/backend/src/test/java/org/booklore/service/VersionServiceTest.java index d95758bd8..aec49e9bd 100644 --- a/backend/src/test/java/org/booklore/service/VersionServiceTest.java +++ b/backend/src/test/java/org/booklore/service/VersionServiceTest.java @@ -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 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();