Skip to content

Commit

Permalink
[FIXED JENKINS-37397] Allow use of @symbols from tools as tool types.
Browse files Browse the repository at this point in the history
Fall back to allowing descriptor IDs as well.
  • Loading branch information
abayer committed Aug 14, 2016
1 parent b1703f0 commit 20f2223
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
32 changes: 29 additions & 3 deletions src/main/java/org/jenkinsci/plugins/workflow/steps/ToolStep.java
Expand Up @@ -39,6 +39,8 @@
import javax.annotation.CheckForNull;
import javax.inject.Inject;

import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.structs.SymbolLookup;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -87,16 +89,21 @@ public ListBoxModel doFillTypeItems() {
ListBoxModel r = new ListBoxModel();
r.add("<any>", "");
for (ToolDescriptor<?> desc : ToolInstallation.all()) {
r.add(desc.getDisplayName(), desc.getId());
String idOrSymbol = symbolForDescriptor(desc);
if (idOrSymbol == null) {
idOrSymbol = desc.getId();
}
r.add(desc.getDisplayName(), idOrSymbol);
}
return r;
}

public ListBoxModel doFillNameItems(@QueryParameter String type) {
type = Util.fixEmpty(type);
ListBoxModel r = new ListBoxModel();

for (ToolDescriptor<?> desc : ToolInstallation.all()) {
if (type != null && !desc.getId().equals(type)) {
if (type != null && !desc.getId().equals(type) && !type.equals(symbolForDescriptor(desc))) {
continue;
}
for (ToolInstallation tool : desc.getInstallations()) {
Expand All @@ -119,7 +126,7 @@ public static final class Execution extends AbstractSynchronousNonBlockingStepEx
String name = step.getName();
String type = step.getType();
for (ToolDescriptor<?> desc : ToolInstallation.all()) {
if (type != null && !desc.getId().equals(type)) {
if (type != null && !desc.getId().equals(type) && !type.equals(symbolForDescriptor(desc))) {
continue;
}
for (ToolInstallation tool : desc.getInstallations()) {
Expand All @@ -141,4 +148,23 @@ public static final class Execution extends AbstractSynchronousNonBlockingStepEx

}

/**
* Finds the {@code @Symbol} on the given {@link ToolDescriptor}, if it exists.
* TODO: This should probably go somewhere else - maybe {@link SymbolLookup}?
*
* @param desc A {@link ToolDescriptor}
* @return The {@code @Symbol} value for that descriptor, or null if not present.
*/
public static String symbolForDescriptor(ToolDescriptor<?> desc) {
Symbol s = desc.getClass().getAnnotation(Symbol.class);
if (s != null) {
for (String t : s.value()) {
if (t != null) {
return t;
}
}
}
return null;
}

}
Expand Up @@ -25,6 +25,7 @@
package org.jenkinsci.plugins.workflow.steps;

import hudson.tasks.Maven;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.ClassRule;
Expand All @@ -42,8 +43,19 @@ public class ToolStepRunTest {
@Test public void build() throws Exception {
Maven.MavenInstallation tool = ToolInstallations.configureMaven3();
String name = tool.getName();
Maven.MavenInstallation.DescriptorImpl desc = Jenkins.getInstance().getDescriptorByType(Maven.MavenInstallation.DescriptorImpl.class);

String type = ToolStep.symbolForDescriptor(desc);

// Defensive - Maven doesn't have a symbol before 2.x, and other tools may still not have symbols after that.
if (type == null) {
type = desc.getId();
}

WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("node {def home = tool '" + name + "'; sh \"M2_HOME=${home} ${home}/bin/mvn -version\"}"));
p.setDefinition(new CpsFlowDefinition("node {def home = tool name: '" + name + "', type: '" + type + "'; sh \"M2_HOME=${home} ${home}/bin/mvn -version\"}",
true));

r.assertLogContains("Apache Maven 3", r.assertBuildStatusSuccess(p.scheduleBuild2(0)));
}

Expand Down

0 comments on commit 20f2223

Please sign in to comment.