Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -15,7 +15,9 @@
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import io.jenkins.plugins.casc.model.CNode;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import jenkins.model.Jenkins;
import org.junit.jupiter.api.Test;
Expand All @@ -33,11 +35,20 @@ void configure(JenkinsConfiguredWithCodeRule j) {
Set<Map.Entry<String, String>> entries = ((EnvironmentVariablesNodeProperty) nodeProperties.get(0))
.getEnvVars()
.entrySet();
assertEquals(1, entries.size());
assertEquals(3, entries.size());

Map.Entry<String, String> envVar = entries.iterator().next();
Iterator<Entry<String, String>> iterator = entries.iterator();
Map.Entry<String, String> envVar = iterator.next();
assertEquals("FOO", envVar.getKey());
assertEquals("BAR", envVar.getValue());

envVar = iterator.next();
assertEquals("FOO2", envVar.getKey());
assertEquals("", envVar.getValue());

envVar = iterator.next();
assertEquals("FOO3", envVar.getKey());
assertEquals("", envVar.getValue());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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";
Comment thread
timja marked this conversation as resolved.
Outdated
}

@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>(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, if not using it env is not exported

- \"envVars\"
- toolLocation:    
  - locations:\n    
    - home: "/home/user/bin/git"
    [....]

If I'm not wrong it's because we're extending BaseConfigurator which will take EnvVars as an scalar, while before this configurator it was read as a HeteroDescribableConfigurator.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would extending HeteroDescribableConfigurator work instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't test that, seems more complex than this approach.
I can do a test and check if we would be simplifying things

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

worth a quick go

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry, my fault, not Hetero, but DataBoundConfigurator.
Just changed it to use it, slightly simpler.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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;
}
}
Loading