Skip to content

Commit

Permalink
[FIXED JENKINS-34818] expand global node properties during script eva…
Browse files Browse the repository at this point in the history
…luation
  • Loading branch information
kinow committed Nov 5, 2016
1 parent 3afccea commit dcea3b7
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 1 deletion.
Expand Up @@ -154,7 +154,10 @@ public Map<Object, Object> getParameters() {
* @return Map with helper parameters
*/
private Map<Object, Object> getHelperParameters() {
// map with parameters
final Map<Object, Object> helperParameters = new LinkedHashMap<Object, Object>();

// First, if the project name is set, we then find the project by its name, and inject into the map
Project<?, ?> project = null;
if (StringUtils.isNotBlank(this.projectName)) {
// first we try to get the item given its name, which is more efficient
Expand All @@ -170,6 +173,10 @@ private Map<Object, Object> getHelperParameters() {
helperParameters.put(JENKINS_BUILD_VARIABLE_NAME, build);
}
}

// Here we inject the global node properties
final Map<String, Object> globalNodeProperties = Utils.getGlobalNodeProperties();
helperParameters.putAll(globalNodeProperties);
return helperParameters;
}

Expand Down
27 changes: 26 additions & 1 deletion src/main/java/org/biouno/unochoice/util/Utils.java
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) <2014-2015> <Ioannis Moutsatsos, Bruno P. Kinoshita>
* Copyright (c) 2014-2016 Ioannis Moutsatsos, Bruno P. Kinoshita
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,6 +25,7 @@
package org.biouno.unochoice.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -42,6 +43,7 @@
import hudson.model.ParameterDefinition;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Project;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.util.DescribableList;
Expand Down Expand Up @@ -216,4 +218,27 @@ private static boolean isParameterDefintionOf(@Nonnull String parameterUUID, @No
return Collections.EMPTY_LIST;
}

/**
* Get a map with the global node properties.
*
* @since 1.6
* @return map with global node properties
*/
public static @Nonnull Map<String, Object> getGlobalNodeProperties() {
Map<String, Object> map = new HashMap<String, Object>();
Jenkins instance = Jenkins.getInstance();
if (instance != null) {
DescribableList<NodeProperty<?>, NodePropertyDescriptor> globalNodeProperties = instance
.getGlobalNodeProperties();
if (globalNodeProperties != null) {
for (NodeProperty<?> nodeProperty : globalNodeProperties) {
if (nodeProperty instanceof EnvironmentVariablesNodeProperty) {
EnvironmentVariablesNodeProperty envNodeProperty = (EnvironmentVariablesNodeProperty) nodeProperty;
map.putAll(envNodeProperty.getEnvVars());
}
}
}
}
return map;
}
}
@@ -0,0 +1,80 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016 Ioannis Moutsatsos, Bruno P. Kinoshita
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.biouno.unochoice.issue35101;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.biouno.unochoice.CascadeChoiceParameter;
import org.biouno.unochoice.ChoiceParameter;
import org.biouno.unochoice.model.GroovyScript;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
import org.jenkinsci.plugins.scriptsecurity.scripts.languages.GroovyLanguage;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import hudson.slaves.EnvironmentVariablesNodeProperty;

/**
* Test that scripts can access global node properties.
*
* @since 1.6
*/
public class TestGlobalNodePropertiesScript {

@Rule
public JenkinsRule j = new JenkinsRule();

private final String SCRIPT = "return ['a', 'b', \"$NODE_TIME\"]";
private final String FALLBACK_SCRIPT = "return ['EMPTY!']";

@Before
public void setUp() throws Exception {
ScriptApproval.get().preapprove(SCRIPT, GroovyLanguage.get());
ScriptApproval.get().preapprove(FALLBACK_SCRIPT, GroovyLanguage.get());
}

@Test
public void testScriptAccessingGlobalProperties() {
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("time", "20:13:13");
EnvironmentVariablesNodeProperty.Entry entry = new EnvironmentVariablesNodeProperty.Entry("NODE_TIME",
testMap.get("time"));
EnvironmentVariablesNodeProperty envVarsNodeProp = new EnvironmentVariablesNodeProperty(entry);
j.jenkins.getGlobalNodeProperties().add(envVarsNodeProp);
GroovyScript script = new GroovyScript(new SecureGroovyScript(SCRIPT, Boolean.FALSE, null),
new SecureGroovyScript(FALLBACK_SCRIPT, Boolean.FALSE, null));
ChoiceParameter param = new ChoiceParameter("param000", "description", "randomName", script,
CascadeChoiceParameter.ELEMENT_TYPE_FORMATTED_HIDDEN_HTML, true);

assertEquals(Arrays.asList("a", "b", "20:13:13").toString(), param.getChoices().values().toString());
}

}
29 changes: 29 additions & 0 deletions src/test/java/org/biouno/unochoice/issue35101/package-info.java
@@ -0,0 +1,29 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016 Ioannis Moutsatsos, Bruno P. Kinoshita
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

/**
* Tests for global node properties issue.
* @since 1.6
*/
package org.biouno.unochoice.issue35101;
20 changes: 20 additions & 0 deletions src/test/java/org/biouno/unochoice/util/TestUtils.java
Expand Up @@ -29,20 +29,28 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jenkinsci.plugins.scriptler.config.Script;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.JenkinsRule;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import hudson.slaves.EnvironmentVariablesNodeProperty;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Utils.class})
public class TestUtils {

@Rule JenkinsRule jenkins = new JenkinsRule();

@Test
public void testGetAllScriptlerScripts() {
Set<Script> fakeScripts = new HashSet<Script>();
Expand Down Expand Up @@ -95,4 +103,16 @@ public void testRandomParameterName() {
assertFalse(paramName.endsWith("param"));
}

@Test
public void testGetGlobalNodeProperties() {
Map<?,?> map = Utils.getGlobalNodeProperties();
Map<String, String> testMap = new HashMap<String, String>();
testMap.put("time", "20:13:13");
EnvironmentVariablesNodeProperty.Entry entry = new EnvironmentVariablesNodeProperty.Entry("time", testMap.get("time"));
EnvironmentVariablesNodeProperty envVarsNodeProp = new EnvironmentVariablesNodeProperty(entry);
jenkins.jenkins.getGlobalNodeProperties().add(envVarsNodeProp);
map = Utils.getGlobalNodeProperties();
assertEquals("20:13:13", map.values().iterator().next());
}

}

0 comments on commit dcea3b7

Please sign in to comment.