Skip to content

Commit

Permalink
[JENKINS-41759] Pick specific describables when choosing env method
Browse files Browse the repository at this point in the history
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 8609c0f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 19 deletions.
Expand Up @@ -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)
}
}
}
Expand Up @@ -47,7 +47,7 @@ public String getPath() {
return path;
}

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

}
Expand Down
Expand Up @@ -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)
}

Expand Down
Expand Up @@ -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<>();
Expand Down
Expand Up @@ -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 {
Expand Down

0 comments on commit 8609c0f

Please sign in to comment.