-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
695761b
Using configurator for global props
PereBueno 3f5d776
Accept EnvVars wihtout value
fcojfernandez be60c5a
Testing additional global props
PereBueno e7055ab
Removing not used describe
PereBueno d1c08d9
Switching to use DataBoundConstructor
PereBueno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,5 @@ jenkins: | |
| env: | ||
| - key: FOO | ||
| value: BAR | ||
| - key: FOO2 | ||
| value: "" | ||
93 changes: 93 additions & 0 deletions
93
plugin/src/main/java/io/jenkins/plugins/casc/core/GlobalNodePropertiesConfigurator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.EnvVars; | ||
| import hudson.EnvVars; | ||
| import hudson.Extension; | ||
| import hudson.slaves.EnvironmentVariablesNodeProperty; | ||
| import hudson.slaves.EnvironmentVariablesNodeProperty.DescriptorImpl; | ||
| import hudson.slaves.EnvironmentVariablesNodeProperty.Entry; | ||
| import hudson.util.DescribableList; | ||
| import hudson.util.PersistedList; | ||
| 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.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.commons.lang.StringUtils; | ||
|
|
||
| @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>("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); | ||
| // Clean empty vars | ||
| Sequence values = new Sequence(); | ||
| value.asSequence().stream().filter(entry -> StringUtils.isNotBlank(entry.asMapping().get("value").toString())) | ||
| .forEach(values::add); | ||
|
PereBueno marked this conversation as resolved.
Outdated
|
||
| if (value != null) { | ||
| 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 -> new Entry(pair.asMapping().get("key").asScalar().getValue(), | ||
| pair.asMapping().get("value").asScalar().getValue())) | ||
| .toList(); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.