Skip to content

Commit

Permalink
Make dep on matrix-project optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Jul 18, 2024
1 parent fdd4cc7 commit 3f8a452
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 8 deletions.
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>
</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 @@ private static synchronized void setUpgradeNeeded() {

// 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 @@ 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
Expand Down Expand Up @@ -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) {

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));
}
}

}

0 comments on commit 3f8a452

Please sign in to comment.