From 3f8a452c9170fffeb3883b0c720aa90ddf31cec4 Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Thu, 18 Jul 2024 09:13:05 -0400 Subject: [PATCH] Make dep on `matrix-project` optional --- pom.xml | 7 +++ .../plugins/copyartifact/CopyArtifact.java | 20 ++++--- .../copyartifact/OptionalDepsTest.java | 58 +++++++++++++++++++ 3 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 src/test/java/hudson/plugins/copyartifact/OptionalDepsTest.java diff --git a/pom.xml b/pom.xml index ad31e591..6233ae7f 100644 --- a/pom.xml +++ b/pom.xml @@ -75,6 +75,12 @@ jenkins-test-harness-tools 2.2 test + + + org.jenkins-ci.plugins + gradle + + org.jenkins-ci.plugins @@ -84,6 +90,7 @@ org.jenkins-ci.plugins matrix-project + true org.jenkins-ci.plugins.workflow diff --git a/src/main/java/hudson/plugins/copyartifact/CopyArtifact.java b/src/main/java/hudson/plugins/copyartifact/CopyArtifact.java index e0099396..2d4e5e62 100644 --- a/src/main/java/hudson/plugins/copyartifact/CopyArtifact.java +++ b/src/main/java/hudson/plugins/copyartifact/CopyArtifact.java @@ -286,10 +286,14 @@ private static synchronized void setUpgradeNeeded() { // get all CopyArtifacts configured to AbstractProject. This works both for Project and MatrixProject. private static List getCopyArtifactsInProject(AbstractProject project) { - DescribableList> list = - project instanceof Project ? ((Project)project).getBuildersList() - : (project instanceof MatrixProject ? - ((MatrixProject)project).getBuildersList() : null); + DescribableList> list; + if (project instanceof Project) { + list = ((Project)project).getBuildersList(); + } else if (Jenkins.get().getPlugin("matrix-project") != null && project instanceof MatrixProject) { + list = ((MatrixProject)project).getBuildersList(); + } else { + list = null; + } if (list == null) { return Collections.emptyList(); } @@ -543,7 +547,7 @@ public void perform(@NonNull Run build, @NonNull FilePath workspace, @NonN if (!ok) { throw new AbortException(Messages.CopyArtifact_FailedToCopy(expandedProject, expandedFilter)); } - } else if (src instanceof MatrixBuild) { + } else if (jenkins.getPlugin("matrix-project") != null && src instanceof MatrixBuild) { boolean ok = false; // Copy artifacts from all configurations of this matrix build // Use MatrixBuild.getExactRuns if available @@ -854,10 +858,10 @@ public FormValidation doCheckProjectName( if (item != null) { if (jenkins.getPlugin("maven-plugin") != null && item instanceof MavenModuleSet) { result = FormValidation.warning(Messages.CopyArtifact_MavenProject()); + } else if (jenkins.getPlugin("matrix-project") != null && item instanceof MatrixProject) { + result = FormValidation.warning(Messages.CopyArtifact_MatrixProject()); } else { - result = (item instanceof MatrixProject) - ? FormValidation.warning(Messages.CopyArtifact_MatrixProject()) - : FormValidation.ok(); + result = FormValidation.ok(); } } else if (value.indexOf('$') >= 0) { result = FormValidation.warning(Messages.CopyArtifact_ParameterizedName()); diff --git a/src/test/java/hudson/plugins/copyartifact/OptionalDepsTest.java b/src/test/java/hudson/plugins/copyartifact/OptionalDepsTest.java new file mode 100644 index 00000000..d94669ec --- /dev/null +++ b/src/test/java/hudson/plugins/copyartifact/OptionalDepsTest.java @@ -0,0 +1,58 @@ +/* + * The MIT License + * + * Copyright 2024 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 hudson.plugins.copyartifact; + +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.RealJenkinsRule; +import org.jvnet.hudson.test.TailLog; + +/** Verifies that basic functionality works without optional plugin dependencies. */ +public final class OptionalDepsTest { + + @Rule public RealJenkinsRule r = new RealJenkinsRule().omitPlugins("maven-plugin", "matrix-project"); + + @Test public void usePipeline() throws Throwable { + r.then(OptionalDepsTest::_usePipeline); + } + + /** Adapted from {@link CopyArtifactWorkflowTest#testLastCompletedBuildSelector}. */ + private static void _usePipeline(JenkinsRule r) throws Throwable { + var upstream = r.createProject(WorkflowJob.class, "upstream"); + upstream.setDefinition(new CpsFlowDefinition("node {writeFile text: 'upstream content', file: 'x.txt'; archiveArtifacts 'x.txt'}", true)); + try (var tail = new TailLog(r, "upstream", 1)) { + r.buildAndAssertSuccess(upstream); + } + var downstream = r.createProject(WorkflowJob.class, "downstream"); + downstream.setDefinition(new CpsFlowDefinition("node {copyArtifacts(projectName: 'upstream', selector: lastCompleted()); echo readFile('x.txt')}", true)); + try (var tail = new TailLog(r, "downstream", 1)) { + r.assertLogContains("upstream content", r.buildAndAssertSuccess(downstream)); + } + } + +}