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

Make dep on matrix-project optional #234

Merged
merged 1 commit into from
Jul 18, 2024
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
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
<artifactId>jenkins-test-harness-tools</artifactId>
<version>2.2</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>gradle</artifactId>
</exclusion>
</exclusions>
Comment on lines +78 to +83
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand All @@ -84,6 +90,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down
20 changes: 12 additions & 8 deletions src/main/java/hudson/plugins/copyartifact/CopyArtifact.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,14 @@

// get all CopyArtifacts configured to AbstractProject. This works both for Project and MatrixProject.
private static List<CopyArtifact> getCopyArtifactsInProject(AbstractProject<?,?> project) {
DescribableList<Builder,Descriptor<Builder>> list =
project instanceof Project ? ((Project<?,?>)project).getBuildersList()
: (project instanceof MatrixProject ?
((MatrixProject)project).getBuildersList() : null);
DescribableList<Builder,Descriptor<Builder>> list;
if (project instanceof Project) {
list = ((Project<?,?>)project).getBuildersList();
} else if (Jenkins.get().getPlugin("matrix-project") != null && project instanceof MatrixProject) {

Check warning on line 292 in src/main/java/hudson/plugins/copyartifact/CopyArtifact.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 292 is only partially covered, 2 branches are missing
list = ((MatrixProject)project).getBuildersList();
} else {
list = null;

Check warning on line 295 in src/main/java/hudson/plugins/copyartifact/CopyArtifact.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 295 is not covered by tests
}
if (list == null) {
return Collections.emptyList();
}
Expand Down Expand Up @@ -543,7 +547,7 @@
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) {
Comment on lines -546 to +550
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compare #14 (amending 4fd5c3e). Without this, the new test fails:

Started
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in …/copyartifact-plugin/target/tmp/j h11474560698679713783/workspace/downstream
[Pipeline] {
[Pipeline] copyArtifacts
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
java.lang.ClassNotFoundException: hudson.matrix.MatrixBuild
	at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)
	at jenkins.util.URLClassLoader2.findClass(URLClassLoader2.java:35)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:592)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
Also:   org.jenkinsci.plugins.workflow.actions.ErrorAction$ErrorId: af406f0c-e047-466c-874b-a56e04bfb18a
Caused: java.lang.NoClassDefFoundError: hudson/matrix/MatrixBuild
	at hudson.plugins.copyartifact.CopyArtifact.perform(CopyArtifact.java:546)
	at jenkins.tasks.SimpleBuildStep.perform(SimpleBuildStep.java:123)
	at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:101)
	at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:71)
	at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:840)
Finished: FAILURE

boolean ok = false;
// Copy artifacts from all configurations of this matrix build
// Use MatrixBuild.getExactRuns if available
Expand Down Expand Up @@ -854,10 +858,10 @@
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) {

Check warning on line 861 in src/main/java/hudson/plugins/copyartifact/CopyArtifact.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 861 is only partially covered, one branch is missing
result = FormValidation.warning(Messages.CopyArtifact_MatrixProject());

Check warning on line 862 in src/main/java/hudson/plugins/copyartifact/CopyArtifact.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 862 is not covered by tests
} 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());
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/hudson/plugins/copyartifact/OptionalDepsTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}

}