Skip to content
Permalink
Browse files
[JENKINS-41759] Pick specific describables when choosing env method
Utilizing the new function in Utils to find an UninstantiatedDescribable
of a specific type enables us to call the function plain 'properties'
  • Loading branch information
rsandell committed Feb 13, 2017
1 parent d0a70ca commit 8609c0f6416602aa24a2cf7d764abc003e74b096
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
@@ -436,10 +436,12 @@ public class Utils {

if (a.size()==1 && a.get(0) instanceof Map && !((Map)a.get(0)).containsKey('$class')) {
return (Map) a.get(0)
} else if (a.size() == 1 && !(a.get(0) instanceof Map)) {
return Collections.singletonMap(UninstantiatedDescribable.ANONYMOUS_KEY, a.get(0))
}
throw new IllegalArgumentException("Expected named arguments but got "+a)
} else {
return Collections.singletonMap(UninstantiatedDescribable.ANONYMOUS_KEY, _args);
return Collections.singletonMap(UninstantiatedDescribable.ANONYMOUS_KEY, _args)
}
}
}
@@ -47,7 +47,7 @@ public String getPath() {
return path;
}

@Extension @Symbol("fromPropertiesFile")
@Extension @Symbol("properties")
public static class DescriptorImpl extends DeclarativeEnvironmentContributorDescriptor<TrustedProperties> {

}
@@ -50,28 +50,20 @@ public class PropertiesToMapTranslator implements MethodMissingWrapper, Serializ
}

def methodMissing(String s, args) {
if (resolveEnvironmentContributors) {
def describable = Utils.getDescribable(s, DeclarativeEnvironmentContributor.class, args)
if (describable != null) {
return describable.instantiate()
}
}

def argValue
if (args.length > 1) {
argValue = args
} else if (args.length == 1) {
argValue = args[0]
}

if (resolveEnvironmentContributors) {
def retVal
if (argValue != null) {
retVal = script."${s}"(argValue)
} else {
retVal = script."${s}"()
}

if (retVal instanceof UninstantiatedDescribable && isOfType(retVal, DeclarativeEnvironmentContributor.class)) {
return retVal.instantiate()
} else {
return retVal
}
}

return script."${s}"(argValue)
}

@@ -61,6 +61,16 @@ public void getDescribableSingleArgument() throws Exception {
assertEquals("credId", instance.getCredentialsId());
}

@Test
public void getDescribableSingleArgumentArray() throws Exception {
//Apparently it can be passed like this as well
UninstantiatedDescribable d = Utils.getDescribable("credentials", DeclarativeEnvironmentContributor.class, new Object[] {"credId"});
assertNotNull(d);
Credentials instance = (Credentials) d.instantiate();
assertNotNull(instance);
assertEquals("credId", instance.getCredentialsId());
}

@Test
public void getDescribableTwoMustHaveArguments() throws Exception {
Map<String,Object> p = new HashMap<>();
@@ -26,15 +26,15 @@
pipeline {
environment {
FOO = "BAZ"
PROP = fromPropertiesFile("marker.properties")
PROP = properties("marker.properties")
}
agent any

stages {
stage("foo") {
environment {
FOO = "BAR"
P_ = fromPropertiesFile("stage/marker.properties")
P_ = properties("stage/marker.properties")
}

steps {

0 comments on commit 8609c0f

Please sign in to comment.