Skip to content

Commit

Permalink
Merge pull request #24 from jglick/sanitize
Browse files Browse the repository at this point in the history
Prevent `[ab][0-9]` in commit hashes from being treated as `alpha` or `beta`
  • Loading branch information
jglick committed Dec 28, 2021
2 parents 724ec51 + 5b284ba commit 44f328b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
6 changes: 6 additions & 0 deletions git-changelist-maven-extension/pom.xml
Expand Up @@ -35,5 +35,11 @@
<artifactId>org.eclipse.jgit</artifactId>
<version>4.9.0.201710071750-r</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -130,7 +130,7 @@ public void afterSessionStart(MavenSession session) throws MavenExecutionExcepti
throw new MavenExecutionException("Git operations failed", x);
}
log.debug("Spent " + (System.nanoTime() - start) / 1000 / 1000 + "ms on calculations");
String value = String.format(props.getProperty("changelist.format", "-rc%d.%s"), count, hash);
String value = String.format(props.getProperty("changelist.format", "-rc%d.%s"), count, sanitize(hash));
log.info("Setting: -Dchangelist=" + value + " -DscmTag=" + fullHash);
props.setProperty("changelist", value);
props.setProperty("scmTag", fullHash);
Expand Down Expand Up @@ -172,6 +172,10 @@ public void afterSessionStart(MavenSession session) throws MavenExecutionExcepti
}
}

static String sanitize(String hash) {
return hash.replaceAll("[ab]", "$0_");
}

private static String summarize(RevCommit c) {
return c.getId().name() + " “" + c.getShortMessage() + "” " + DateTimeFormatter.ISO_LOCAL_DATE.format(Instant.ofEpochSecond(c.getCommitTime()).atZone(ZoneId.systemDefault()));
}
Expand Down
@@ -0,0 +1,55 @@
/*
* The MIT License
*
* Copyright 2021 CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.jenkins.tools.incrementals.git_changelist_maven_extension;

import org.apache.maven.artifact.versioning.ComparableVersion;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;

public class MainTest {

// https://maven.apache.org/pom.html#Version_Order_Specification
private static final String[] PRERELEASE = {
// From ComparableVersion.StringItem.QUALIFIERS:
"alpha", "beta", "milestone", "rc", "snapshot",
// ALIASES:
"cr",
// Nonstandard ones in Dependabot? https://github.com/dependabot/dependabot-core/blob/f146743aa400c7913b5e953e1b93c8b40345aaf4/maven/lib/dependabot/maven/version.rb#L24-L25
"pr", "dev",
};
@Test public void alphaBeta() {
String hash = "852b473a2b8c";
String sanitized = Main.sanitize(hash);
assertThat(hash + " has been sanitized to the expected format", sanitized, is("852b_473a_2b_8c"));
String canonical = new ComparableVersion(sanitized).getCanonical();
for (String prerelease : PRERELEASE) {
assertThat(sanitized + " treated as a prerelease", canonical, not(containsString(prerelease)));
}
}

}

0 comments on commit 44f328b

Please sign in to comment.