-
Notifications
You must be signed in to change notification settings - Fork 16
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
Refresh component for January 2023 #37
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As recommended upstream. This means we'll need to change |
||
public class RequireExtensionVersion extends AbstractEnforcerRule { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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; | ||
Comment on lines
+53
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copypasta. |
||
|
||
private final PlexusContainer container; | ||
|
||
@Inject | ||
public RequireExtensionVersion(PlexusContainer container) { | ||
this.container = Objects.requireNonNull(container); | ||
} | ||
Comment on lines
+68
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adapting to the new recommended way of injecting components via constructor ( |
||
|
||
@Override | ||
public void execute(EnforcerRuleHelper erh) throws EnforcerRuleException { | ||
Log log = erh.getLog(); | ||
public void execute() throws EnforcerRuleException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No longer using the deprecated |
||
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) { | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copypasta (this method is now private) |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copypasta (this method is now private) |
||
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; | ||
} | ||
Comment on lines
+156
to
+198
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copypasta |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depending on the public API as recommended upstream rather than on internal implementation details.