Display changelog from current version to latest upon clicking the update link
This commit is contained in:
committed by
Aditya Chandel
parent
b708976c1b
commit
6af3ee7019
+13
-29
@@ -1,46 +1,30 @@
|
||||
package com.adityachandel.booklore.controller;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.ReleaseNote;
|
||||
import com.adityachandel.booklore.model.dto.VersionInfo;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import com.adityachandel.booklore.service.VersionService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@AllArgsConstructor
|
||||
@RestController
|
||||
@RequestMapping("/api/v1/version")
|
||||
public class VersionController {
|
||||
|
||||
@Value("${app.version:unknown}")
|
||||
private String appVersion;
|
||||
private final VersionService versionService;
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<VersionInfo> getVersions() {
|
||||
String latestVersion = fetchLatestGitHubReleaseVersion();
|
||||
return ResponseEntity.ok(new VersionInfo(appVersion, latestVersion));
|
||||
public ResponseEntity<VersionInfo> getVersionInfo() {
|
||||
return ResponseEntity.ok(versionService.getVersionInfo());
|
||||
}
|
||||
|
||||
private String fetchLatestGitHubReleaseVersion() {
|
||||
try {
|
||||
RestClient restClient = RestClient.builder()
|
||||
.defaultHeader("Accept", "application/vnd.github+json")
|
||||
.defaultHeader("User-Agent", "BookLore-Version-Checker")
|
||||
.build();
|
||||
|
||||
String response = restClient.get()
|
||||
.uri("https://api.github.com/repos/adityachandelgit/BookLore/releases/latest")
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
JsonNode root = objectMapper.readTree(response);
|
||||
return root.has("tag_name") ? root.get("tag_name").asText() : "unknown";
|
||||
|
||||
} catch (Exception e) {
|
||||
return "unknown";
|
||||
}
|
||||
@GetMapping("/changelog")
|
||||
public ResponseEntity<List<ReleaseNote>> getChangelogSinceCurrent() {
|
||||
return ResponseEntity.ok(versionService.getChangelogSinceCurrentVersion());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.adityachandel.booklore.model.dto;
|
||||
|
||||
public record ReleaseNote(String version, String name, String changelog, String url) {
|
||||
}
|
||||
@@ -1,11 +1,5 @@
|
||||
package com.adityachandel.booklore.model.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class VersionInfo {
|
||||
private String current;
|
||||
private String latest;
|
||||
public record VersionInfo(String current, String latest) {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.adityachandel.booklore.service;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.ReleaseNote;
|
||||
import com.adityachandel.booklore.model.dto.VersionInfo;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestClient;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class VersionService {
|
||||
|
||||
@Value("${app.version:unknown}")
|
||||
private String appVersion;
|
||||
|
||||
private static final String GITHUB_REPO = "adityachandelgit/BookLore";
|
||||
|
||||
public VersionInfo getVersionInfo() {
|
||||
String latestVersion = fetchLatestGitHubReleaseVersion();
|
||||
return new VersionInfo(appVersion, latestVersion);
|
||||
}
|
||||
|
||||
public List<ReleaseNote> getChangelogSinceCurrentVersion() {
|
||||
return fetchReleaseNotesSince(appVersion);
|
||||
}
|
||||
|
||||
private String fetchLatestGitHubReleaseVersion() {
|
||||
try {
|
||||
RestClient restClient = RestClient.builder()
|
||||
.defaultHeader("Accept", "application/vnd.github+json")
|
||||
.defaultHeader("User-Agent", "BookLore-Version-Checker")
|
||||
.build();
|
||||
|
||||
String response = restClient.get()
|
||||
.uri("https://api.github.com/repos/" + GITHUB_REPO + "/releases/latest")
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
|
||||
JsonNode root = new ObjectMapper().readTree(response);
|
||||
return root.has("tag_name") ? root.get("tag_name").asText() : "unknown";
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to fetch latest release version", e);
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
private List<ReleaseNote> fetchReleaseNotesSince(String currentVersion) {
|
||||
List<ReleaseNote> updates = new ArrayList<>();
|
||||
try {
|
||||
RestClient restClient = RestClient.builder()
|
||||
.defaultHeader("Accept", "application/vnd.github+json")
|
||||
.defaultHeader("User-Agent", "BookLore-Version-Checker")
|
||||
.build();
|
||||
|
||||
String response = restClient.get()
|
||||
.uri("https://api.github.com/repos/" + GITHUB_REPO + "/releases")
|
||||
.retrieve()
|
||||
.body(String.class);
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode releases = mapper.readTree(response);
|
||||
|
||||
for (JsonNode release : releases) {
|
||||
String tag = release.get("tag_name").asText();
|
||||
if (isVersionGreater(tag, currentVersion)) {
|
||||
String url = "https://github.com/" + GITHUB_REPO + "/releases/tag/" + tag;
|
||||
updates.add(new ReleaseNote(
|
||||
tag,
|
||||
release.get("name").asText(),
|
||||
release.get("body").asText(),
|
||||
url
|
||||
));
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to fetch release notes", e);
|
||||
}
|
||||
return updates;
|
||||
}
|
||||
|
||||
private boolean isVersionGreater(String version1, String version2) {
|
||||
try {
|
||||
String[] v1 = version1.replace("v", "").split("\\.");
|
||||
String[] v2 = version2.replace("v", "").split("\\.");
|
||||
for (int i = 0; i < Math.max(v1.length, v2.length); i++) {
|
||||
int num1 = i < v1.length ? Integer.parseInt(v1[i]) : 0;
|
||||
int num2 = i < v2.length ? Integer.parseInt(v2[i]) : 0;
|
||||
if (num1 > num2) return true;
|
||||
if (num1 < num2) return false;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Version comparison failed", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
app:
|
||||
path-book: '/app/books'
|
||||
path-config: '/app/data'
|
||||
version: 'development'
|
||||
version: 'v0.0.40'
|
||||
|
||||
spring:
|
||||
servlet:
|
||||
|
||||
Reference in New Issue
Block a user