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

[JENKINS-46124] - support map with EnvStep #105

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package org.jenkinsci.plugins.workflow.steps;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.EnvVars;
import hudson.Extension;
Expand All @@ -34,7 +35,9 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.structs.describable.CustomDescribableModel;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

Expand Down Expand Up @@ -103,7 +106,7 @@ private static final class ExpanderImpl extends EnvironmentExpander {
}
}

@Extension public static class DescriptorImpl extends StepDescriptor {
@Extension public static class DescriptorImpl extends StepDescriptor implements CustomDescribableModel {

@Override public String getFunctionName() {
return "withEnv";
Expand Down Expand Up @@ -150,11 +153,34 @@ private static final class ExpanderImpl extends EnvironmentExpander {
}
}
return b.toString();
} else if (overrides instanceof Map) {
Copy link
Member

Choose a reason for hiding this comment

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

BTW I lobbied for ArgumentsAction[Impl] to persist only the internal, resolved form of arguments, but the design we went with persists the surface form, necessitating extra work like this.

Copy link
Contributor

Choose a reason for hiding this comment

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

@jglick Okay, is there some action to be taken about that now?

Copy link
Member

Choose a reason for hiding this comment

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

Not easily. Just noting some background.

return ((Map<?, ?>) overrides).keySet()
.stream()
.filter(e -> e instanceof String)
.map(Object::toString)
.collect(Collectors.joining(", "));
jglick marked this conversation as resolved.
Show resolved Hide resolved
} else {
return null;
}
}

@NonNull
@Override
public Map<String, Object> customInstantiate(@NonNull Map<String, Object> arguments) {
Map<String, Object> r = new HashMap<>(arguments);
final String key = "overrides";
jglick marked this conversation as resolved.
Show resolved Hide resolved
Object overrides = r.get(key);
if (overrides instanceof Map) {
r.put(key, toKeyValueList((Map<?, ?>) overrides));
}
return r;
}

private static List<String> toKeyValueList(Map<?, ?> map) {
return map.entrySet().stream()
.map(m -> m.getKey() + "=" + (m.getValue() == null ? "" : m.getValue().toString()))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.map(m -> m.getKey() + "=" + (m.getValue() == null ? "" : m.getValue().toString()))
.map(m -> (String) m.getKey() + "=" + (m.getValue() == null ? "" : m.getValue().toString()))

since non-String keys would be bogus

Copy link
Member Author

Choose a reason for hiding this comment

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

IDE consider this a redundant cast and wants to remove it.

Copy link
Member

Choose a reason for hiding this comment

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

Then your IDE is wrong and you should ignore it. You should be able to have a test demonstrating a failure from some bogus thing like

withEnv([new Object(): 'x']) {}

Copy link
Member Author

Choose a reason for hiding this comment

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

This case is not supported:

WorkflowScript: 2: illegal colon after argument expression;
  solution: a complex label expression before a colon must be parenthesized @ line 2, column 72.
  D: true, E: null, new Object(): 'x']) {

Copy link
Member

Choose a reason for hiding this comment

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

Maybe

withEnv([(new Object()): 'x']) {}

then, whatever the syntax is. At worst

def m = [:]
m.put(new Object(), 'x')
withEnv(m) {}

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd argue your asking for trouble

Copy link
Member

Choose a reason for hiding this comment

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

OK forget new Object(), just some arbitrary object key which is not a String. new URL('http://nowhere.net/') for example is whitelisted.

Copy link
Member Author

Choose a reason for hiding this comment

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

Than you get into java.lang.ClassCastException: java.net.URL cannot be cast to java.lang.String 🤔

Properly better to call toString instead of trying to cast the object

Copy link
Member

Choose a reason for hiding this comment

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

Then you get into java.lang.ClassCastException

Exactly what I was requesting with 8d385a2.

Copy link
Member Author

Choose a reason for hiding this comment

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

image

.collect(Collectors.toList());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,52 @@ public class EnvStepTest {
});
}

@Test public void mapArguments() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" withEnv(a: 1, b: 2, c: 'hello world', d: true) {\n" +
jglick marked this conversation as resolved.
Show resolved Hide resolved
" echo \"a=$a b=$b c=$c d=$d\"" +
jglick marked this conversation as resolved.
Show resolved Hide resolved
" }\n" +
"}", true));
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("a=1 b=2 c=hello world d=true", b);
List<FlowNode> coreStepNodes = new DepthFirstScanner().filteredNodes(
b.getExecution(),
Predicates.and(
new NodeStepTypePredicate("withEnv"),
n -> n instanceof StepStartNode && !((StepStartNode) n).isBody()));
assertThat(coreStepNodes, Matchers.hasSize(1));
assertEquals("a, b, c, d", ArgumentsAction.getStepArgumentsAsString(coreStepNodes.get(0)));
}
});
}

@Test public void mapArgumentsAsMap() {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
WorkflowJob p = story.j.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node {\n" +
" withEnv([A: 1, B: 2, C: 'hello world', D: true]) {\n" +
" echo \"A=$A B=$B C=$C D=$D\"\n" +
" }\n" +
"}", true));
WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
story.j.assertLogContains("A=1 B=2 C=hello world D=true", b);
List<FlowNode> coreStepNodes = new DepthFirstScanner().filteredNodes(
b.getExecution(),
Predicates.and(
new NodeStepTypePredicate("withEnv"),
n -> n instanceof StepStartNode && !((StepStartNode) n).isBody()));
assertThat(coreStepNodes, Matchers.hasSize(1));
assertEquals("A, B, C, D", ArgumentsAction.getStepArgumentsAsString(coreStepNodes.get(0)));
}
});
}

@Test public void configRoundTrip() throws Exception {
story.addStep(new Statement() {
@Override public void evaluate() throws Throwable {
Expand Down