-
Notifications
You must be signed in to change notification settings - Fork 748
Allowing empty valued global variables in config file #2657
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
Changes from 2 commits
695761b
3f5d776
be60c5a
e7055ab
d1c08d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,6 @@ jenkins: | |
| env: | ||
| - key: FOO | ||
| value: BAR | ||
| - key: FOO2 | ||
| value: "" | ||
| - key: FOO3 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,5 @@ | |
| env: | ||
| - key: "FOO" | ||
| value: "BAR" | ||
| - key: "FOO2" | ||
| - key: "FOO3" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package io.jenkins.plugins.casc.core; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
| import edu.umd.cs.findbugs.annotations.NonNull; | ||
| import hudson.Extension; | ||
| import hudson.slaves.EnvironmentVariablesNodeProperty; | ||
| import hudson.slaves.EnvironmentVariablesNodeProperty.Entry; | ||
| import io.jenkins.plugins.casc.Attribute; | ||
| import io.jenkins.plugins.casc.BaseConfigurator; | ||
| import io.jenkins.plugins.casc.ConfigurationContext; | ||
| import io.jenkins.plugins.casc.ConfiguratorException; | ||
| import io.jenkins.plugins.casc.impl.attributes.MultivaluedAttribute; | ||
| import io.jenkins.plugins.casc.model.CNode; | ||
| import io.jenkins.plugins.casc.model.Mapping; | ||
| import io.jenkins.plugins.casc.model.Sequence; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
|
|
||
| @Extension | ||
| public class GlobalNodePropertiesConfigurator extends BaseConfigurator<EnvironmentVariablesNodeProperty> { | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public String getName() { | ||
| return "globalNodeProperties"; | ||
| } | ||
|
|
||
| @Override | ||
| protected EnvironmentVariablesNodeProperty instance(Mapping mapping, ConfigurationContext context) | ||
| throws ConfiguratorException { | ||
| List<Entry> vars = getVarsAsList(mapping); | ||
| return new EnvironmentVariablesNodeProperty(vars); | ||
| } | ||
|
|
||
| @Override | ||
| public Class<EnvironmentVariablesNodeProperty> getTarget() { | ||
| return EnvironmentVariablesNodeProperty.class; | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public Set<Attribute<EnvironmentVariablesNodeProperty, ?>> describe() { | ||
| Set<Attribute<EnvironmentVariablesNodeProperty, ?>> attrs = super.describe(); | ||
| attrs.add(new MultivaluedAttribute<EnvironmentVariablesNodeProperty, EnvironmentVariablesNodeProperty.Entry>( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, if not using it If I'm not wrong it's because we're extending
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would extending
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't test that, seems more complex than this approach.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. worth a quick go
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, my fault, not Hetero, but DataBoundConfigurator.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks, much simpler and more like what I expected, thanks for sorting. |
||
| "env", Entry.class)); | ||
| return attrs; | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public EnvironmentVariablesNodeProperty configure(CNode c, ConfigurationContext context) | ||
| throws ConfiguratorException { | ||
| Mapping mapping = c.asMapping(); | ||
| List<Entry> variables = getVarsAsList(mapping); | ||
| return new EnvironmentVariablesNodeProperty(variables); | ||
| } | ||
|
|
||
| @CheckForNull | ||
| public CNode describe(EnvironmentVariablesNodeProperty instance, ConfigurationContext context) throws Exception { | ||
| Mapping mapping = new Mapping(); | ||
| for (Attribute attribute : getAttributes()) { | ||
| CNode value = attribute.describe(instance, context); | ||
| if (value != null) { | ||
| Sequence values = new Sequence(); | ||
| value.asSequence().stream().forEach(values::add); | ||
| mapping.put(attribute.getName(), values); | ||
| } | ||
| } | ||
| return mapping; | ||
| } | ||
|
|
||
| private List<Entry> getVarsAsList(Mapping m) { | ||
| List<Entry> result = new ArrayList<>(); | ||
| if (m.get("env") != null) { | ||
| result = m.get("env").asSequence().stream() | ||
| .map(pair -> { | ||
| if (pair.asMapping().get("key") == null) { | ||
| return null; | ||
| } | ||
| final String key = | ||
| pair.asMapping().get("key").asScalar().getValue(); | ||
| final String value = pair.asMapping().get("value") == null | ||
| ? "" | ||
| : pair.asMapping().get("value").asScalar().getValue(); | ||
|
|
||
| return new Entry(key, value); | ||
| }) | ||
| .toList(); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.