-
Notifications
You must be signed in to change notification settings - Fork 129
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
base: master
Are you sure you want to change the base?
Conversation
src/test/java/org/jenkinsci/plugins/workflow/steps/EnvStepTest.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A nice feature and a nice set of tests! :)
@Override | ||
public Map<String, Object> customInstantiate(@NonNull Map<String, Object> arguments) { | ||
Map<String, Object> r = new HashMap<>(arguments); | ||
Object overrides = r.get("overrides"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DRY for "overrides" :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to implement customUninstantiate
to guide users of Pipeline Syntax to use the new form, or leave them with the List<String>
syntax?
@@ -150,11 +153,37 @@ | |||
} | |||
} | |||
return b.toString(); | |||
} else if (overrides instanceof Map) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
src/main/java/org/jenkinsci/plugins/workflow/steps/EnvStep.java
Outdated
Show resolved
Hide resolved
src/main/java/org/jenkinsci/plugins/workflow/steps/EnvStep.java
Outdated
Show resolved
Hide resolved
src/test/java/org/jenkinsci/plugins/workflow/steps/EnvStepTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/jenkinsci/plugins/workflow/steps/EnvStepTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/jenkinsci/plugins/workflow/steps/EnvStepTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/jenkinsci/plugins/workflow/steps/EnvStepTest.java
Outdated
Show resolved
Hide resolved
src/test/java/org/jenkinsci/plugins/workflow/steps/EnvStepTest.java
Outdated
Show resolved
Hide resolved
please take another look @jglick I think I listen to your feedback, I honestly don't know what I was thinking about when I wrote that stream collector 🤯 |
src/test/java/org/jenkinsci/plugins/workflow/steps/EnvStepTest.java
Outdated
Show resolved
Hide resolved
.collect(Collectors.toMap(e -> (String)e.getKey(), e -> e.getValue() == null ? "" : e.getValue().toString())) | ||
.entrySet().stream() | ||
.map(m -> m.getKey() + "=" + m.getValue()) | ||
.map(m -> m.getKey() + "=" + (m.getValue() == null ? "" : m.getValue().toString())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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']) {}
There was a problem hiding this comment.
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']) {
There was a problem hiding this comment.
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) {}
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/test/java/org/jenkinsci/plugins/workflow/steps/EnvStepTest.java
Outdated
Show resolved
Hide resolved
@bitwiseman @jglick 🙏 Would be a great addition to get merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! The implementation looks good to me (thanks for adding tests!). I am a little hesitant to add complexity since we will have to support both syntaxes going forward, but I think the trade-off is worth it in this case.
Can you please update EnvStep/help-overrides.html (and maybe also EnvStep/help.html) to mention the new syntax and give an example? Once that is done I am happy to merge the PR.
I'll see what I can do :) |
@dwnusbaum I was looking for this exact feature for years and finally found this PR but it got stalled. I'd be happy to provide the requested documentation but since this code does not apply in the meantime I wondered who would have to adapt it to the changed codebase? |
fixes JENKINS-46124