Skip to content

Commit 56fcbaa

Browse files
authored
Merge pull request #525 from SpectraLogic/3_5_git_commit_hash
Fix for https://jira.spectralogic.com/browse/JSDK-268.
2 parents 6180b3d + 6aff0ff commit 56fcbaa

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ buildscript {
2929
}
3030

3131

32+
plugins {
33+
id "com.palantir.git-version" version "0.10.0"
34+
}
35+
3236
allprojects {
3337
group = 'com.spectralogic.ds3'
3438
version = '3.5.3'

ds3-sdk/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@ task genConfigProperties {
5858
Files.createDirectories(configPath)
5959
}
6060

61+
def gitInfo = versionDetails()
62+
6163
configFile.withWriter{out ->
6264
out.writeLine("productionBuild=" + productionBuild.toString())
6365
out.writeLine("version=" + version.toString())
6466
out.writeLine("build.date=" + new Date().toString())
67+
out.writeLine("git.commitHash=" + gitInfo.gitHashFull)
6568
}
6669
}
6770
}

ds3-sdk/src/main/java/com/spectralogic/ds3client/utils/PropertyUtils.java

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ public final class PropertyUtils {
2626
private static final Logger LOG = LoggerFactory.getLogger(PropertyUtils.class);
2727

2828
private static final String SDK_VERSION_PROPERTY_NAME = "version";
29+
private static final String GIT_COMMIT_HASH_PROPERTY_NAME = "git.commitHash";
2930
private static final String PROPERTIES_FILE_NAME = "ds3_sdk.properties";
3031

3132
private static final AtomicReference<String> versionProperty = new AtomicReference<>("");
33+
private static final AtomicReference<String> gitCommitHashProperty = new AtomicReference<>("");
3234

3335
private PropertyUtils() {}
3436

@@ -37,29 +39,47 @@ private PropertyUtils() {}
3739
* @return The sdk version, if we can find one, an empty string otherwise.
3840
*/
3941
public static String getSdkVersion() {
40-
if ( ! Guard.isStringNullOrEmpty(versionProperty.get())) {
41-
return versionProperty.get();
42+
return getPropertyFromPropertiesFile(SDK_VERSION_PROPERTY_NAME, versionProperty);
43+
}
44+
45+
private static String getPropertyFromPropertiesFile(final String propertyName, final AtomicReference<String> propertyValue) {
46+
if ( ! Guard.isStringNullOrEmpty(propertyValue.get())) {
47+
return propertyValue.get();
48+
}
49+
50+
final String propertyFromPropertiesFile = getPropertyFromPropertiesFile(propertyName);
51+
52+
if ( ! Guard.isStringNullOrEmpty(propertyFromPropertiesFile)) {
53+
propertyValue.set(propertyFromPropertiesFile);
4254
}
4355

56+
return propertyValue.get();
57+
}
58+
59+
private static String getPropertyFromPropertiesFile(final String propertyName) {
4460
final Properties properties = new Properties();
4561

4662
try (final InputStream propertiesStream = PropertyUtils.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE_NAME)) {
4763
if (propertiesStream != null) {
4864
properties.load(propertiesStream);
49-
final Object versionPropertyObject = properties.get(SDK_VERSION_PROPERTY_NAME);
50-
51-
if (versionPropertyObject != null) {
52-
final String versionFromPropFile = properties.get(SDK_VERSION_PROPERTY_NAME).toString();
65+
final Object propertyObject = properties.get(propertyName);
5366

54-
if ( ! Guard.isStringNullOrEmpty(versionFromPropFile)) {
55-
versionProperty.set(versionFromPropFile);
56-
}
67+
if (propertyObject != null) {
68+
return properties.get(propertyName).toString();
5769
}
5870
}
5971
} catch (final Throwable t) {
6072
LOG.warn("Could not read properties file.", t);
6173
}
6274

63-
return versionProperty.get();
75+
return "";
76+
}
77+
78+
/**
79+
* Get the git commit hash from a properties file.
80+
* @return The git commit hash, if we can find it, an empty string otherwise.
81+
*/
82+
public static String getGitCommitHash() {
83+
return getPropertyFromPropertiesFile(GIT_COMMIT_HASH_PROPERTY_NAME, gitCommitHashProperty);
6484
}
6585
}

ds3-sdk/src/test/java/com/spectralogic/ds3client/Ds3Client_Test.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.spectralogic.ds3client.networking.FailedRequestUsingMgmtPortException;
3131
import com.spectralogic.ds3client.networking.HttpVerb;
3232
import com.spectralogic.ds3client.utils.ByteArraySeekableByteChannel;
33+
import com.spectralogic.ds3client.utils.PropertyUtils;
3334
import com.spectralogic.ds3client.utils.ResourceUtils;
3435
import org.junit.Before;
3536
import org.junit.Test;
@@ -50,6 +51,7 @@
5051
import static org.hamcrest.CoreMatchers.*;
5152
import static org.hamcrest.MatcherAssert.assertThat;
5253
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
54+
import static org.junit.Assert.assertEquals;
5355

5456
public class Ds3Client_Test {
5557
private static final UUID MASTER_OBJECT_LIST_JOB_ID = UUID.fromString("1a85e743-ec8f-4789-afec-97e587a26936");
@@ -923,6 +925,13 @@ public void testGettingDefaultUserAgent() {
923925
assertThat(matcher.find(), is(true));
924926
}
925927

928+
@Test
929+
public void testGettingGitCommitHash() {
930+
final String gitCommitHash = PropertyUtils.getGitCommitHash();
931+
932+
assertEquals(40, gitCommitHash.length());
933+
}
934+
926935
@Test
927936
public void VerifySystemHealthSpectraS3() throws IOException {
928937
final String responsePayload = "<Data><MsRequiredToVerifyDataPlannerHealth>0</MsRequiredToVerifyDataPlannerHealth></Data>";

0 commit comments

Comments
 (0)