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

2.2234 serial form incompatibility #770

Merged
merged 8 commits into from
Mar 4, 2025
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
44 changes: 44 additions & 0 deletions pipeline-model-definition/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,49 @@
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>old-releases</id>
<goals>
<goal>copy</goal>
</goals>
<phase>generate-test-resources</phase>
<configuration>
<outputDirectory>${project.build.testOutputDirectory}/old-releases</outputDirectory>
<stripVersion>true</stripVersion>
<artifactItems>
<artifactItem>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-stage-tags-metadata</artifactId>
<version>${old-release.version}</version>
<type>hpi</type>
</artifactItem>
<artifactItem>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-api</artifactId>
<version>${old-release.version}</version>
<type>hpi</type>
</artifactItem>
<artifactItem>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-extensions</artifactId>
<version>${old-release.version}</version>
<type>hpi</type>
</artifactItem>
<artifactItem>
<groupId>org.jenkinsci.plugins</groupId>
<artifactId>pipeline-model-definition</artifactId>
<version>${old-release.version}</version>
<type>hpi</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -252,5 +295,6 @@
<properties>
<no-test-jar>false</no-test-jar>
<useBeta>true</useBeta>
<old-release.version>2.2221.vc657003fb_d93</old-release.version>
</properties>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* The MIT License
*
* Copyright 2025 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 org.jenkinsci.plugins.pipeline.modeldefinition;

import groovy.lang.GroovyShell;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.util.VersionNumber;
import java.io.File;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.XMLConstants;
import javax.xml.parsers.SAXParserFactory;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript2;
import org.jenkinsci.plugins.pipeline.modeldefinition.parser.CompatibilityLoader;
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
import org.jenkinsci.plugins.workflow.cps.GroovyShellDecorator;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
* Detects old builds (predating for example {@link DeclarativeAgentScript2}) and loads old Groovy resources to match.
*/
@Extension public final class Upgrade extends GroovyShellDecorator {

private static final Logger LOGGER = Logger.getLogger(Upgrade.class.getName());

@Override public void configureShell(CpsFlowExecution context, GroovyShell shell) {
if (context == null) {
return;
}
var owner = context.getOwner();
if (owner == null) {
return;
}
try {
if (!isOld(owner)) {
LOGGER.fine(() -> context + " does not seem to be old");
return;
}
} catch (Exception x) {
LOGGER.log(Level.WARNING, "failed to check " + context, x);
return;
}
var cl = shell.getClassLoader();
var base = cl.getResourceLoader();
cl.setResourceLoader(filename -> {
for (var loader : ExtensionList.lookup(CompatibilityLoader.class)) {
var url = loader.loadGroovySource(filename);
if (url != null) {
LOGGER.fine(() -> "for " + context + " loading " + filename + " via " + loader + " ⇒ " + url);
return url;
}
}
var url = base.loadGroovySource(filename);
if (url != null) {
LOGGER.finer(() -> "for " + context + " loading " + filename + " ⇒ " + url);
}
return url;
});
}

@Override public GroovyShellDecorator forTrusted() {
return this;
}

@Extension public static final class Loader implements CompatibilityLoader {
private static final Set<String> CLASSES = Set.of(
"org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter",
"org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript",
"org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.AnyScript",
"org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.LabelScript",
"org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl.NoneScript");
@Override public URL loadGroovySource(String clazz) {
if (CLASSES.contains(clazz)) {
return Upgrade.class.getResource("compat/" + clazz.replaceFirst(".+[.]", "") + ".groovy");
} else {
return null;
}
}
}

private static boolean isOld(FlowExecutionOwner owner) throws Exception {
var factory = SAXParserFactory.newDefaultInstance();
// TODO XMLUtils does not support SAX parsing:

Check warning on line 112 in pipeline-model-definition/src/main/java/org/jenkinsci/plugins/pipeline/modeldefinition/Upgrade.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: XMLUtils does not support SAX parsing:
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
var parser = factory.newSAXParser();
var old = new AtomicBoolean();
var buildXml = new File(owner.getRootDir(), "build.xml");
parser.parse(buildXml, new DefaultHandler() {
@Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
var plugin = attributes.getValue("plugin");
if (plugin != null) {
int at = plugin.indexOf('@');
if (at != -1 && plugin.substring(0, at).equals("pipeline-model-definition")) {
var version = new VersionNumber(plugin.substring(at + 1));
LOGGER.fine(() -> "got " + version + " off " + qName + " in " + buildXml);
if (version.isOlderThan(new VersionNumber("2.2234"))) {
old.set(true);
}
}
}
}
});
return old.get();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* The MIT License
*
* Copyright (c) 2016, 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 org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl

import org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
import org.jenkinsci.plugins.workflow.cps.CpsScript

class AnyScript extends DeclarativeAgentScript<Any> {

AnyScript(CpsScript s, Any a) {
super(s, a)
}

@Override
Closure run(Closure body) {
return {
script.node {
CheckoutScript.doCheckout(script, describable, null, body).call()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* The MIT License
*
* Copyright (c) 2017, 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 org.jenkinsci.plugins.pipeline.modeldefinition.agent

import org.jenkinsci.plugins.pipeline.modeldefinition.SyntheticStageNames
import org.jenkinsci.plugins.workflow.cps.CpsScript

class CheckoutScript implements Serializable {

static Closure doCheckout(CpsScript script, DeclarativeAgent agent, String customWorkspace = null, Closure body) {
return {
if (customWorkspace) {
script.ws(customWorkspace) {
checkoutAndRun(script, agent, body).call()
}
} else {
checkoutAndRun(script, agent, body).call()
}
}
}

private static Closure checkoutAndRun(CpsScript script, DeclarativeAgent agent, Closure body) {
return {
def checkoutMap = [:]

if (agent.isDoCheckout() && agent.hasScmContext(script)) {
String subDir = agent.subdirectory
if (subDir != null && subDir != "") {
script.dir(subDir) {
checkoutMap.putAll(performCheckout(script, agent))
}
} else {
checkoutMap.putAll(performCheckout(script, agent))
}
}
if (checkoutMap) {
script.withEnv(checkoutMap.collect { k, v -> "${k}=${v}" }) {
body.call()
}
} else {
body.call()
}
}
}

private static Map performCheckout(CpsScript script, DeclarativeAgent agent) {
def checkoutMap = [:]
if (!agent.inStage) {
script.stage(SyntheticStageNames.checkout()) {
checkoutMap.putAll(script.checkout(script.scm) ?: [:])
}
} else {
// No stage when we're in a nested stage already
checkoutMap.putAll(script.checkout(script.scm) ?: [:])
}

return checkoutMap
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* The MIT License
*
* Copyright (c) 2016, 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 org.jenkinsci.plugins.pipeline.modeldefinition.agent.impl


import org.jenkinsci.plugins.pipeline.modeldefinition.agent.CheckoutScript
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentScript
import org.jenkinsci.plugins.workflow.cps.CpsScript

class LabelScript extends DeclarativeAgentScript<Label> {

LabelScript(CpsScript s, Label a) {
super(s, a)
}

@Override
Closure run(Closure body) {
Closure run = {
script.node(describable?.label) {
CheckoutScript.doCheckout(script, describable, describable.customWorkspace, body).call()
}
}
if (describable.retries > 1) {
return {
script.retry(count: describable.retries, conditions: [script.agent(), script.nonresumable()]) {
run.call()
}
}
} else {
run
}
}
}
Loading
Loading