Skip to content

Commit

Permalink
Refresh component for January 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
basil committed Feb 16, 2023
1 parent 947f1d6 commit c395a88
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 54 deletions.
10 changes: 8 additions & 2 deletions enforcer-rules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
</parent>
<artifactId>incrementals-enforcer-rules</artifactId>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.enforcer</groupId>
<artifactId>enforcer-rules</artifactId>
<version>3.0.0-M1</version>
<artifactId>enforcer-api</artifactId>
<version>3.2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,60 @@
package io.jenkins.tools.incrementals.enforcer;

import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.maven.AbstractMavenLifecycleParticipant;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.enforcer.rule.api.AbstractEnforcerRule;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.enforcer.AbstractVersionEnforcer;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.classworlds.realm.ClassRealm;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.util.StringUtils;

/**
* Verifies that {@code git-changelist-maven-extension}, if present, is sufficiently new.
*/
public class RequireExtensionVersion extends AbstractVersionEnforcer {
@Named("requireExtensionVersion")
public class RequireExtensionVersion extends AbstractEnforcerRule {

private static final Pattern ID_PATTERN = Pattern.compile("\\QcoreExtension>io.jenkins.tools.incrementals:git-changelist-maven-extension:\\E(.+)");

/**
* Specify the required version. Some examples are:
* <ul>
* <li><code>2.0.4</code> Version 2.0.4 and higher (different from Maven meaning)</li>
* <li><code>[2.0,2.1)</code> Versions 2.0 (included) to 2.1 (not included)</li>
* <li><code>[2.0,2.1]</code> Versions 2.0 to 2.1 (both included)</li>
* <li><code>[2.0.5,)</code> Versions 2.0.5 and higher</li>
* <li><code>(,2.0.5],[2.1.1,)</code> Versions up to 2.0.5 (included) and 2.1.1 or higher</li>
* </ul>
*
* @see {@link #setVersion(String)}
* @see {@link #getVersion()}
*/
private String version;

private final PlexusContainer container;

@Inject
public RequireExtensionVersion(PlexusContainer container) {
this.container = Objects.requireNonNull(container);
}

@Override
public void execute(EnforcerRuleHelper erh) throws EnforcerRuleException {
Log log = erh.getLog();
public void execute() throws EnforcerRuleException {
List<AbstractMavenLifecycleParticipant> participants;
try {
participants = erh.getContainer().lookupList(AbstractMavenLifecycleParticipant.class);
participants = container.lookupList(AbstractMavenLifecycleParticipant.class);
} catch (ComponentLookupException x) {
log.warn(x);
getLog().warn(x.getMessage());
return;
}
for (AbstractMavenLifecycleParticipant participant : participants) {
Expand All @@ -59,10 +87,113 @@ public void execute(EnforcerRuleHelper erh) throws EnforcerRuleException {
if (loader instanceof ClassRealm) {
Matcher m = ID_PATTERN.matcher(((ClassRealm) loader).getId());
if (m.matches()) {
enforceVersion(log, "git-changelist-maven-extension", getVersion(), new DefaultArtifactVersion(m.group(1)));
enforceVersion("git-changelist-maven-extension", getVersion(), new DefaultArtifactVersion(m.group(1)));
}
}
}
}

/**
* Compares the specified version to see if it is allowed by the defined version range.
*
* @param variableName name of variable to use in messages (Example: "Maven" or "Java" etc).
* @param requiredVersionRange range of allowed versions.
* @param actualVersion the version to be checked.
* @throws EnforcerRuleException the enforcer rule exception
*/
// CHECKSTYLE_OFF: LineLength
private void enforceVersion(String variableName, String requiredVersionRange, ArtifactVersion actualVersion)
throws EnforcerRuleException
// CHECKSTYLE_ON: LineLength
{
if (StringUtils.isEmpty(requiredVersionRange)) {
throw new EnforcerRuleException(variableName + " version can't be empty.");
} else {

VersionRange vr;
String msg = "Detected " + variableName + " Version: " + actualVersion;

// short circuit check if the strings are exactly equal
if (actualVersion.toString().equals(requiredVersionRange)) {
getLog().debug(msg + " is allowed in the range " + requiredVersionRange + ".");
} else {
try {
vr = VersionRange.createFromVersionSpec(requiredVersionRange);

if (containsVersion(vr, actualVersion)) {
getLog().debug(msg + " is allowed in the range " + toString(vr) + ".");
} else {
throw new EnforcerRuleException(msg + " is not in the allowed range " + toString(vr) + ".");
}
} catch (InvalidVersionSpecificationException e) {
throw new EnforcerRuleException(
"The requested " + variableName + " version " + requiredVersionRange + " is invalid.", e);
}
}
}
}

/**
* Copied from Artifact.VersionRange. This is tweaked to handle singular ranges properly. Currently the default
* containsVersion method assumes a singular version means allow everything. This method assumes that "2.0.4" ==
* "[2.0.4,)"
*
* @param allowedRange range of allowed versions.
* @param theVersion the version to be checked.
* @return true if the version is contained by the range.
*/
private static boolean containsVersion(VersionRange allowedRange, ArtifactVersion theVersion) {
ArtifactVersion recommendedVersion = allowedRange.getRecommendedVersion();
if (recommendedVersion == null) {
return allowedRange.containsVersion(theVersion);
} else {
// only singular versions ever have a recommendedVersion
int compareTo = recommendedVersion.compareTo(theVersion);
return (compareTo <= 0);
}
}

private static String toString(VersionRange vr) {
// as recommended version is used as lower bound in this context modify the string representation
if (vr.getRecommendedVersion() != null) {
return "[" + vr.getRecommendedVersion().toString() + ",)";
} else {
return vr.toString();
}
}

@Override
public String getCacheId() {
if (StringUtils.isNotEmpty(version)) {
// return the hashcodes of the parameter that matters
return "" + version.hashCode();
} else {
return "0";
}
}

/**
* Gets the required version.
*
* @return the required version
*/
public final String getVersion() {
return this.version;
}

/**
* Specify the required version. Some examples are:
* <ul>
* <li><code>2.0.4</code> Version 2.0.4 and higher (different from Maven meaning)</li>
* <li><code>[2.0,2.1)</code> Versions 2.0 (included) to 2.1 (not included)</li>
* <li><code>[2.0,2.1]</code> Versions 2.0 to 2.1 (both included)</li>
* <li><code>[2.0.5,)</code> Versions 2.0.5 and higher</li>
* <li><code>(,2.0.5],[2.1.1,)</code> Versions up to 2.0.5 (included) and 2.1.1 or higher</li>
* </ul>
*
* @param theVersion the required version to set
*/
public void setVersion(String theVersion) {
this.version = theVersion;
}
}
2 changes: 1 addition & 1 deletion git-changelist-maven-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.7.1</version>
<version>2.1.1</version>
<executions>
<execution>
<goals>
Expand Down
14 changes: 10 additions & 4 deletions lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
<artifactId>lib</artifactId>
<dependencies>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<scope>provided</scope>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<version>4.7.3</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.CheckForNull;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.maven.artifact.versioning.ComparableVersion;
import org.kohsuke.github.GHCompare;
Expand Down
52 changes: 43 additions & 9 deletions maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
</parent>
<artifactId>incrementals-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<properties>
<maven-plugin-tools.version>3.7.1</maven-plugin-tools.version>
</properties>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
Expand All @@ -17,30 +20,61 @@
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.5</version>
<version>2.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5.1</version>
<version>${maven-plugin-tools.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.twdata.maven</groupId>
<artifactId>mojo-executor</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<scope>provided</scope>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<version>4.7.3</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
Expand All @@ -49,7 +83,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.5.1</version>
<version>${maven-plugin-tools.version}</version>
</plugin>
</plugins>
</pluginManagement>
Expand Down
Loading

0 comments on commit c395a88

Please sign in to comment.