Skip to content
Snippets Groups Projects
Commit 335dbbf6 authored by Clayton, Brandon Scott's avatar Clayton, Brandon Scott
Browse files

build version info for nshm

parent 674c332c
No related branches found
No related tags found
1 merge request!757Model version
package gov.usgs.earthquake.nshmp.www;
import java.nio.file.Path;
import java.util.ArrayList;
import org.eclipse.jgit.api.Git;
import com.google.common.io.Resources;
import gov.usgs.earthquake.nshmp.internal.AppVersion;
......@@ -7,17 +12,61 @@ import gov.usgs.earthquake.nshmp.internal.LibVersion;
public class HazVersion implements AppVersion {
public static VersionInfo[] appVersions() {
VersionInfo[] versions = {
new HazVersion().getVersionInfo(),
new LibVersion().getVersionInfo(),
new WsUtilsVersion().getVersionInfo(),
};
return versions;
public static VersionInfo[] appVersions(Path modelPath) {
var versions = new ArrayList<VersionInfo>();
versions.add(new HazVersion().getVersionInfo());
versions.add(new LibVersion().getVersionInfo());
versions.add(new WsUtilsVersion().getVersionInfo());
var nshmVersion = getNshmVersion(modelPath);
if (nshmVersion != null) {
versions.add(nshmVersion);
}
return versions.toArray(new VersionInfo[0]);
}
/**
* Returns the version info from resources file.
*/
public VersionInfo getVersionInfo() {
var resource = Resources.getResource("version/nshmp-haz-version.json");
return AppVersion.versionInfo(resource);
}
/**
* Returns the version info of a repository.
*
* If the model path is not a git repository, returns null.
*
* @param modelPath The path to the model
*/
private static VersionInfo getNshmVersion(Path modelPath) {
try {
var git = Git.open(modelPath.toFile());
var repo = git.getRepository();
var hash = repo.getRefDatabase().findRef("HEAD").getObjectId().name();
var url = repo
.getConfig()
.getString("remote", "origin", "url")
.split(".git")[0];
var slash = url.split("/");
var projectName = slash[slash.length - 1];
return VersionInfo.builder()
.projectName(projectName)
.url(repo.getConfig().getString("remote", "origin", "url"))
.version(git.describe().call())
.branchName(repo.getBranch())
.commitDistance(0)
.gitHash(hash.substring(0, 10))
.gitHashFull(hash)
.isCleanTag(git.status().call().isClean())
.lastTag("")
.build();
} catch (Exception e) {
return null;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment