-
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 all commits
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
12 changes: 12 additions & 0 deletions
12
integrations/src/test/resources/io/jenkins/plugins/casc/GlobalNodePropertiesTest.yml
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 |
|---|---|---|
| @@ -1,6 +1,18 @@ | ||
| jenkins: | ||
| globalNodeProperties: | ||
| - diskSpaceMonitor: | ||
| freeDiskSpaceThreshold: "1GiB" | ||
| freeDiskSpaceWarningThreshold: "2GiB" | ||
| freeTempSpaceThreshold: "1GiB" | ||
| freeTempSpaceWarningThreshold: "2GiB" | ||
| - envVars: | ||
| env: | ||
| - key: FOO | ||
| value: BAR | ||
| - key: FOO2 | ||
| value: "" | ||
| - key: FOO3 | ||
| - toolLocation: | ||
| locations: | ||
| - home: "/home/user/bin/git" | ||
| key: "hudson.plugins.git.GitTool$DescriptorImpl@Default" |
11 changes: 11 additions & 0 deletions
11
integrations/src/test/resources/io/jenkins/plugins/casc/GlobalNodePropertiesTestExpected.yml
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 |
|---|---|---|
| @@ -1,4 +1,15 @@ | ||
| - diskSpaceMonitor: | ||
| freeDiskSpaceThreshold: "1GiB" | ||
| freeDiskSpaceWarningThreshold: "2GiB" | ||
| freeTempSpaceThreshold: "1GiB" | ||
| freeTempSpaceWarningThreshold: "2GiB" | ||
| - envVars: | ||
| env: | ||
| - key: "FOO" | ||
| value: "BAR" | ||
| - key: "FOO2" | ||
| - key: "FOO3" | ||
| - toolLocation: | ||
| locations: | ||
| - home: "/home/user/bin/git" | ||
| key: "hudson.plugins.git.GitTool$DescriptorImpl@Default" |
61 changes: 61 additions & 0 deletions
61
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,61 @@ | ||
| package io.jenkins.plugins.casc.core; | ||
|
|
||
| import edu.umd.cs.findbugs.annotations.NonNull; | ||
| import hudson.Extension; | ||
| import hudson.slaves.EnvironmentVariablesNodeProperty; | ||
| import hudson.slaves.EnvironmentVariablesNodeProperty.Entry; | ||
| import io.jenkins.plugins.casc.ConfigurationContext; | ||
| import io.jenkins.plugins.casc.ConfiguratorException; | ||
| import io.jenkins.plugins.casc.impl.configurators.DataBoundConfigurator; | ||
| import io.jenkins.plugins.casc.model.CNode; | ||
| import io.jenkins.plugins.casc.model.Mapping; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Extension | ||
| public class GlobalNodePropertiesConfigurator extends DataBoundConfigurator<EnvironmentVariablesNodeProperty> { | ||
|
|
||
| public GlobalNodePropertiesConfigurator() { | ||
| this(EnvironmentVariablesNodeProperty.class); | ||
| } | ||
|
|
||
| public GlobalNodePropertiesConfigurator(Class<?> clazz) { | ||
| super(EnvironmentVariablesNodeProperty.class); | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public String getName() { | ||
| return "globalNodeProperties"; | ||
| } | ||
|
|
||
| @NonNull | ||
| @Override | ||
| public EnvironmentVariablesNodeProperty configure(CNode c, ConfigurationContext context) | ||
| throws ConfiguratorException { | ||
| Mapping mapping = c.asMapping(); | ||
| List<Entry> variables = getVarsAsList(mapping); | ||
| return new EnvironmentVariablesNodeProperty(variables); | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
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.
IMO this is the wrong fix and it is
configuration-as-code-plugin/plugin/src/main/java/io/jenkins/plugins/casc/ConfigurationAsCode.java
Lines 660 to 662 in 148f87d
regardless of where it is. If some config class neglects to call
Util.fixEmpty(orfixEmptyAndTrimetc.) when processing a parameter to a@DataBoundSetterwhich cannot logically be the empty string, then fix that setter.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.
that might have an huge impact on the export. Any single property with an empty String as value will be exported when nowadays is not
which might mean to fix how many plugins? Although I agree with that being the 100% correct solution, that might be little realistic as users using the export nowadays will see unexpected changes in the export without a (from the user perspective) good reason
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 sure, but they need to be fixed anyway even without JCasC.