Skip to content

Commit

Permalink
fix: calculate to get the latest tag
Browse files Browse the repository at this point in the history
  • Loading branch information
sukidayou committed Dec 16, 2024
1 parent 42a1c80 commit 01d6b11
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@
</arguments>
</configuration>
</execution>
<execution>
<id>generate-version</id>
<phase>initialize</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>bash</executable>
<arguments>
<argument>-c</argument>
<argument>
git describe --tags --abbrev=0
</argument>
</arguments>
<outputFile>${project.build.outputDirectory}/META-INF/lastCli.version</outputFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
Expand Down
63 changes: 60 additions & 3 deletions src/main/java/org/casbin/util/VersionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,52 @@
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/***
* Utility class for retrieving version-related information.
*/
public class VersionUtil {

/***
* Calculates the new version based on the current version and the type of commit.
*
* @param currentVersion The current version string in the format "vX.Y.Z".
* @param commitType The type of commit that caused the version change (e.g., "fix", "feat", "breaking change").
* @return The calculated new version string in the format "vX.Y.Z".
*/
public static String calculateNewVersion(String currentVersion, String commitType) {
String[] versionParts = currentVersion.substring(1).split("\\.");
int major = Integer.parseInt(versionParts[0]);
int minor = Integer.parseInt(versionParts[1]);
int patch = Integer.parseInt(versionParts[2]);

switch (commitType.toLowerCase()) {
case "fix":
patch++;
break;
case "feat":
minor++;
patch = 0;
break;
case "breaking change":
major++;
minor = 0;
patch = 0;
break;
default:
return currentVersion;
}
return "v" + major + "." + minor + "." + patch;
}

/**
* Retrieves Cli version information.
*
Expand All @@ -50,10 +87,30 @@ public static String getCliVersion() throws IOException {
String tag = properties.getProperty("git.closest.tag.name", "Unknown");
String commitCount = properties.getProperty("git.closest.tag.commit.count", "Unknown");

if(tag.isEmpty()) {
tag = properties.getProperty("git.tags", "Unknown");
if(tag.isEmpty() || tag.equals("Unknown")) {
String tags = properties.getProperty("git.tags", "Unknown");
tag = tags;
if(tags.isEmpty() || tags.equals("Unknown")) {
String commitMessage = properties.getProperty("git.commit.message.full", "Unknown");
Pattern pattern = Pattern.compile("^(fix|feat|chore|docs|style|refactor|test|perf|build|ci):");
Matcher matcher = pattern.matcher(commitMessage);

InputStream versionInputStream = Client.class.getResourceAsStream("/META-INF/lastCli.version");
if (versionInputStream != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(versionInputStream, StandardCharsets.UTF_8));
String version = "";
String line;
if ((line = reader.readLine()) != null) {
version = line;
if (matcher.find()) {
tag = calculateNewVersion(version, matcher.group(1));
}
}
}
}
}
if ((commitCount.isEmpty() || commitCount.equals("0")) && !tag.isEmpty()) {

if ((commitCount.isEmpty() || commitCount.equals("0")) && (!tag.isEmpty() && !tag.equals("Unknown"))) {
return tag;
}
return commitId;
Expand Down

0 comments on commit 01d6b11

Please sign in to comment.