Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VersionPlace: if version is release, do not add date #1004

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/main/java/emissary/place/VersionPlace.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VersionPlace extends ServiceProviderPlace {

Expand All @@ -15,6 +17,7 @@ public class VersionPlace extends ServiceProviderPlace {
private boolean useAbbrevHash;
private String formattedVersion;
private String versionHash;
protected Pattern regexPatternForVersion;

/**
* Create the place from the specified config file or resource
Expand Down Expand Up @@ -63,6 +66,10 @@ private void configurePlace() {

includeDate = configG.findBooleanEntry("INCLUDE_DATE", true);
useAbbrevHash = configG.findBooleanEntry("USE_ABBREV_HASH", true);

String cfgRegex = configG.findStringEntry("VERSION_REGEX_FOR_NO_DATE", "^(\\d+\\.)?(\\d+\\.)?(\\d+)$");
DonResnik marked this conversation as resolved.
Show resolved Hide resolved
regexPatternForVersion = Pattern.compile(cfgRegex);

formattedVersion = getVersion(gitRepositoryState);
versionHash = getVersionHash(gitRepositoryState);
}
Expand All @@ -78,16 +85,23 @@ GitRepositoryState initGitRepositoryState() {
}

protected String getVersion(GitRepositoryState gitRepositoryState) {
String version = gitRepositoryState.getBuildVersion();
Matcher matcher = regexPatternForVersion.matcher(version);
// if a release version, return just the version, even if includeDate is true
if (matcher.matches()) {
return version;
}

if (includeDate) {
// version with date & time information
// changes format of date from 2024-09-23T10:41:18-0400, to 20240923104118
String buildTime = gitRepositoryState.getBuildTime();
int cutEndMark = buildTime.lastIndexOf(":") + 3;
String formattedDate = buildTime.substring(0, cutEndMark).replaceAll("\\D", "");
return gitRepositoryState.getBuildVersion() + "-" + formattedDate;
return version + "-" + formattedDate;
} else {
// adds just version
return gitRepositoryState.getBuildVersion();
return version;
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/test/java/emissary/place/VersionPlaceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -76,6 +77,19 @@ void testAddVersionHash() throws ResourceException, IOException {
"EMISSARY_VERSION_HASH should contain (at least) the abbreviated hash");
}

@Test
void testVersionRegex() throws IOException, URISyntaxException {
// uses different test gitRepoState properties file with version number that matches default regex
// passes this file to getVersion() to verify version returned has no date
Path regexTestFile = Paths.get(new ResourceReader().getResource("emissary/util/test.version.regex.git.properties").toURI());
GitRepositoryState regexRepoState = GitRepositoryState.getRepositoryState(regexTestFile);

// create the place, using the normal class cfg
place = new MyVersionPlace();
assertFalse(place.getVersion(regexRepoState).contains("-20240828141716"),
"the date should not be added to the version due to the matching release (default) regex");
}

class MyVersionPlace extends VersionPlace {
MyVersionPlace() throws IOException {
super(new ResourceReader().getConfigDataAsStream(VersionPlace.class));
Expand Down
26 changes: 26 additions & 0 deletions src/test/resources/emissary/util/test.version.regex.git.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# for VersionPlaceTest testVersionRegex()
git.branch=main
git.build.host=localhost
git.build.time=2024-08-28T14\:17\:16+0000
[email protected]
git.build.user.name=DEV
git.build.version=1.2.0
git.closest.tag.commit.count=363
git.closest.tag.name=emissary-7.19.0
git.commit.author.time=2024-08-27T19\:11\:01+0000
git.commit.committer.time=2024-08-27T19\:11\:01+0000
git.commit.id.abbrev=5e9a143
git.commit.id.describe=emissary-7.19.0-363-g5e9a143
git.commit.id.describe-short=emissary-7.19.0-363
git.commit.id.full=5e9a143fbe70de4143acba5c0a8763f5b811fee2
git.commit.message.full=VersionPlace - add EMISSARY_VERSION_HASH to metadata
git.commit.message.short=VersionPlace - add EMISSARY_VERSION_HASH to metadata
git.commit.time=2024-08-27T19\:11\:01+0000
[email protected]
git.commit.user.name=DEV
git.dirty=false
git.local.branch.ahead=NO_REMOTE
git.local.branch.behind=NO_REMOTE
[email protected]\:fork/emissary.git
git.tags=
git.total.commit.count=869