Add chip sha

This commit is contained in:
2026-01-06 01:18:31 +01:00
parent e167b00471
commit 7ef51e206f
6 changed files with 175 additions and 1 deletions

View File

@@ -10,6 +10,7 @@ import dev.kruhlmann.imgfloat.util.LogSanitizer;
import dev.kruhlmann.imgfloat.service.AuthorizationService;
import dev.kruhlmann.imgfloat.service.ChannelDirectoryService;
import dev.kruhlmann.imgfloat.service.SettingsService;
import dev.kruhlmann.imgfloat.service.GitInfoService;
import dev.kruhlmann.imgfloat.service.VersionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -25,6 +26,7 @@ public class ViewController {
private final ChannelDirectoryService channelDirectoryService;
private final VersionService versionService;
private final SettingsService settingsService;
private final GitInfoService gitInfoService;
private final ObjectMapper objectMapper;
private final AuthorizationService authorizationService;
private final long uploadLimitBytes;
@@ -33,6 +35,7 @@ public class ViewController {
ChannelDirectoryService channelDirectoryService,
VersionService versionService,
SettingsService settingsService,
GitInfoService gitInfoService,
ObjectMapper objectMapper,
AuthorizationService authorizationService,
long uploadLimitBytes
@@ -40,6 +43,7 @@ public class ViewController {
this.channelDirectoryService = channelDirectoryService;
this.versionService = versionService;
this.settingsService = settingsService;
this.gitInfoService = gitInfoService;
this.objectMapper = objectMapper;
this.authorizationService = authorizationService;
this.uploadLimitBytes = uploadLimitBytes;
@@ -129,5 +133,7 @@ public class ViewController {
private void addVersionAttributes(Model model) {
model.addAttribute("version", versionService.getVersion());
model.addAttribute("releaseVersion", versionService.getReleaseVersion());
model.addAttribute("buildCommitShort", gitInfoService.getShortCommitSha());
model.addAttribute("buildCommitUrl", gitInfoService.getCommitUrl());
}
}

View File

@@ -0,0 +1,126 @@
package dev.kruhlmann.imgfloat.service;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class GitInfoService {
private static final Logger LOG = LoggerFactory.getLogger(GitInfoService.class);
private static final String COMMIT_URL_PREFIX = "https://github.com/Kruhlmann/imgfloat-j/commit/";
private final String commitSha;
private final String shortCommitSha;
public GitInfoService() {
CommitInfo commitInfo = resolveFromGitProperties();
if (commitInfo == null) {
commitInfo = resolveFromGitBinary();
}
String full = commitInfo != null ? commitInfo.fullSha() : null;
String abbreviated = commitInfo != null ? commitInfo.shortSha() : null;
if (abbreviated == null && full != null) {
abbreviated = abbreviate(full);
} else if (full == null && abbreviated != null) {
full = abbreviated;
}
this.commitSha = defaultValue(full);
this.shortCommitSha = defaultValue(abbreviated);
}
public String getCommitSha() {
return commitSha;
}
public String getShortCommitSha() {
return shortCommitSha;
}
public String getCommitUrl() {
if (commitSha == null || commitSha.isBlank() || "unknown".equalsIgnoreCase(commitSha)) {
return null;
}
return COMMIT_URL_PREFIX + commitSha;
}
private CommitInfo resolveFromGitProperties() {
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("git.properties")) {
if (inputStream == null) {
return null;
}
Properties properties = new Properties();
properties.load(inputStream);
String fullSha = normalize(properties.getProperty("git.commit.id"));
String shortSha = normalize(properties.getProperty("git.commit.id.abbrev"));
if (fullSha == null && shortSha == null) {
return null;
}
return new CommitInfo(fullSha, shortSha);
} catch (IOException e) {
LOG.warn("Unable to read git.properties from classpath", e);
return null;
}
}
private CommitInfo resolveFromGitBinary() {
String fullSha = runGitCommand("rev-parse", "HEAD");
String shortSha = runGitCommand("rev-parse", "--short", "HEAD");
if (fullSha == null && shortSha == null) {
return null;
}
return new CommitInfo(fullSha, shortSha);
}
private String runGitCommand(String... command) {
try {
Process process = new ProcessBuilder(command).redirectErrorStream(true).start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String output = reader.readLine();
int exitCode = process.waitFor();
if (exitCode == 0 && output != null && !output.isBlank()) {
return output.trim();
}
LOG.debug("Git command {} failed with exit code {}", String.join(" ", command), exitCode);
return null;
}
} catch (InterruptedException e) {
LOG.debug("Git command interrupted {}", String.join(" ", command), e);
return null;
} catch (IOException e) {
LOG.debug("Git command IO error command {}", String.join(" ", command), e);
return null;
}
}
private String abbreviate(String value) {
if (value == null) {
return null;
}
return value.length() > 7 ? value.substring(0, 7) : value;
}
private String normalize(String value) {
if (value == null || value.isBlank()) {
return null;
}
return value.trim();
}
private String defaultValue(String value) {
if (value == null || value.isBlank()) {
return "unknown";
}
return "unknown".equalsIgnoreCase(value) ? "unknown" : value;
}
private record CommitInfo(String fullSha, String shortSha) {}
}

View File

@@ -58,6 +58,13 @@ body {
letter-spacing: 0.2px;
font-size: 13px;
}
.version-link {
color: inherit;
text-decoration: none;
}
.version-link:hover {
text-decoration: underline;
}
.channels-body,
.settings-body {

View File

@@ -39,9 +39,22 @@
<span class="version-badge">MIT</span>
</div>
<div class="build-chip">
<span class="muted">Build</span>
<span class="muted">Version</span>
<span class="version-badge" th:text="${version}">unknown</span>
</div>
<div class="build-chip">
<span class="muted">Build</span>
<a
class="version-badge version-link"
th:if="${buildCommitUrl}"
th:href="${buildCommitUrl}"
th:text="${buildCommitShort}"
rel="noreferrer"
target="_blank"
>unknown</a
>
<span class="version-badge" th:unless="${buildCommitUrl}" th:text="${buildCommitShort}">unknown</span>
</div>
</footer>
</div>
<script src="/js/cookie-consent.js"></script>