Skip to content

Commit

Permalink
feat: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
lfvjimisola committed May 27, 2024
1 parent 9b03590 commit ecc6146
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 2 deletions.
14 changes: 12 additions & 2 deletions versions-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -111,6 +113,14 @@
<artifactId>commons-lang3</artifactId>
</dependency>

<!-- other -->

<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>6.9.0.202403050737-r</version>
</dependency>

<!-- testing -->
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
Expand Down Expand Up @@ -298,4 +308,4 @@
</build>
</profile>
</profiles>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
try {
// smoke test if we have a needed tools
def gitVersion = "git --version".execute()
gitVersion.consumeProcessOutput(System.out, System.out)
gitVersion.waitFor()
return gitVersion.exitValue() == 0
} catch (Exception e) {
// some error occurs - we skip a test
return false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
void exec(String command) {
def proc = command.execute(null, basedir)
proc.consumeProcessOutput(System.out, System.out)
proc.waitFor()
assert proc.exitValue() == 0 : "command '${command}' return status: " + proc.exitValue()
}

def testFile = new File(basedir, 'test.txt')
testFile << 'content'

exec('git init')
exec('git add test.txt')
exec('git status')
exec('git commit -m first-commit')
exec('git tag first-tag')

exec('mvn se.lfv.maven.plugins:ci-friendly-versions-maven-plugin:set-version-from-tag')
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// check that property was set
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package org.codehaus.mojo.versions;

/*
* Copyright MojoHaus and Contributors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.io.File;
import java.io.IOException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.maven.artifact.versioning.ComparableVersion;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit;

@Mojo(name = "set-version-from-tag", defaultPhase = LifecyclePhase.PACKAGE)
public class CIFriendlyVersionsMojo extends AbstractMojo {

@Parameter(property = "propertyName", defaultValue = "revision")
private String propertyName;

private static final Pattern TAG_VERSION_PATTERN = Pattern.compile("refs/tags/(?:v)?((\\d+\\.\\d+\\.\\d+)(.*))");

public void execute() throws MojoExecutionException {
VersionInformation vi = getVersion();

getLog().info("Setting property '" + propertyName + "' to: " + vi);
System.setProperty(propertyName, vi.toString());
}

private VersionInformation getVersion() throws MojoExecutionException {
try (Git git = Git.open(new File("."))) {

Iterator<RevCommit> latestCommitIterator =
git.log().setMaxCount(1).call().iterator();
// no commits in repository
if (!latestCommitIterator.hasNext()) {
getLog().debug("No commits");

return addSnapshotQualifier(new VersionInformation("0.0.1-0"));
}

// latest commit has version tag
RevCommit latestCommit = latestCommitIterator.next();
List<String> versionTags = getVersionedTagsForCommit(git, latestCommit);

getLog().debug("Latest commit: " + latestCommit);
getLog().debug("current branch: " + git.getRepository().getFullBranch());

Optional<VersionInformation> ovi = findHighestVersion(versionTags);

if (ovi.isPresent()) {
VersionInformation vi = ovi.get();
getLog().debug("tag ref MATCHES: " + vi);

return vi;
}

if (versionTags.size() == 1) {
Matcher tagVersionMatcher = TAG_VERSION_PATTERN.matcher(versionTags.get(0));
if (tagVersionMatcher.matches()) {
String version = tagVersionMatcher.group(1);
VersionInformation vi = new VersionInformation(version);

return vi;
}
}

getLog().debug("\n\nstart looking for version tagged commit");

Iterable<RevCommit> commits = git.log().call();
int count = 1;
for (RevCommit commit : commits) {
count++;
versionTags = getVersionedTagsForCommit(git, commit);

ovi = findHighestVersion(versionTags);

if (ovi.isPresent()) {
VersionInformation vi = ovi.get();

vi.setPatch(vi.getPatch() + 1);
vi.setBuildNumber(vi.getBuildNumber() + 1);

return addSnapshotQualifier(vi);
}
}

// no version tags in repository
return addSnapshotQualifier(new VersionInformation("0.0.1-" + count));

} catch (IOException | GitAPIException e) {
throw new MojoExecutionException("Error reading Git information", e);
}
}

private static Optional<VersionInformation> findHighestVersion(List<String> versionTags) {
Optional<String> highestVersionString = versionTags.stream().max(new VersionComparator());

return highestVersionString.map(VersionInformation::new);
}

private List<String> getVersionedTagsForCommit(Git git, RevCommit commit) throws GitAPIException {
// get tags directly associated with the commit
List<String> tagNames = git.tagList().call().stream()
.filter(tag -> tag.getObjectId().equals(commit.getId()))
.map(Ref::getName)
.filter(tagName -> {
Matcher matcher = TAG_VERSION_PATTERN.matcher(tagName);
return matcher.matches() && matcher.groupCount() > 0;
})
.map(tagName -> {
Matcher matcher = TAG_VERSION_PATTERN.matcher(tagName);
matcher.matches();
return matcher.group(1);
})
.collect(Collectors.toList());

getLog().debug("VERSIONED Tags directly on commit " + commit.getName() + ": " + tagNames);

return tagNames;
}

static VersionInformation addSnapshotQualifier(VersionInformation vi) {
vi.setQualifier("SNAPSHOT");

return vi;
}

static class VersionComparator implements Comparator<String> {

@Override
public int compare(String version1, String version2) {
return new ComparableVersion(version1).compareTo(new ComparableVersion(version2));
}
}
}
Loading

0 comments on commit ecc6146

Please sign in to comment.