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

Prevent [ab][0-9] in commit hashes from being treated as alpha or beta #24

Merged
merged 2 commits into from
Dec 28, 2021
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
6 changes: 6 additions & 0 deletions git-changelist-maven-extension/pom.xml
Original file line number Diff line number Diff line change
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>
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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)));
}
}

}