Converting a string parameter to a List of Maps

1,269 views
Skip to first unread message

niristotle okram

unread,
Mar 26, 2017, 3:06:16 PM3/26/17
to jenkins...@googlegroups.com
hi, 

Can anyone let me know me, what is wrong here. I have the pipeline script, and i am trying to pass a string parameter when the job is triggered. The parameter will then be converted into a List of maps, so that i can iterate through it. 

Here is what i am trying:


String input = "$objectListParameter"  //This is from the job's input String parameter
println input 
def inputMap = Eval.me("$input")  
def objectList=[]     //initialize an empty List
objectList << inputMap  
println objectList
println objectList.getClass()
//call the method
generateXML(objectList)  // This is the method marked with @NonCPS



The input parameter, ($objectListParameter) looks something like this: 

[[name: 'a', file: 'fileA' , objectName: 'wf_A' , objectType: 'workflow', sourceRepository: 'DEV2', folderNames: [srcFolder1: 'TgtFolder1', srcFolder2: 'TgtFolder2']],[ name: 'B' , file: 'fileB' , objectName: 'wf_B' , objectType: 'workflow', sourceRepository: 'DEV2', folderNames: [srcFolder4: 'TgtFolder4', srcFolder3: 'TgtFolder3']]]


In the jenkins console Log, this the snippet 





[Pipeline] echo
[[name: 'a', file: 'fileA' , objectName: 'wf_A' , objectType: 'workflow', sourceRepository: 'DEV2', folderNames: [srcFolder1: 'TgtFolder1', srcFolder2: 'TgtFolder2']],[ name: 'B' , file: 'fileB' , objectName: 'wf_B' , objectType: 'workflow', sourceRepository: 'DEV2', folderNames: [srcFolder4: 'TgtFolder4', srcFolder3: 'TgtFolder3']]]
[Pipeline] echo
[[{name=a, file=fileA, objectName=wf_A, objectType=workflow, sourceRepository=DEV2, folderNames={srcFolder1=TgtFolder1, srcFolder2=TgtFolder2}}, {name=B, file=fileB, objectName=wf_B, objectType=workflow, sourceRepository=DEV2, folderNames={srcFolder4=TgtFolder4, srcFolder3=TgtFolder3}}]]
[Pipeline] echo
class java.util.ArrayList
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: WorkflowScript$_generateXML_closure1$_closure2$_closure3$_closure4$_closure5$_closure6$_closure9.doCall() is applicable for argument types: (java.util.LinkedHashMap) values: [[srcFolder1:TgtFolder1, srcFolder2:TgtFolder2]]
Possible solutions: doCall(java.lang.Object, java.lang.Object), findAll(), findAll(), isCase(java.lang.Object), isCase(java.lang.Object)
	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:286)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
	at groovy.lang.Closure.call(Closure.java:414)
	at groovy.lang.Closure.call(Closure.java:430)
	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2030)
	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2015)
	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2056)
	at org.codehaus.groovy.runtime.dgm$162.invoke(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)





Bill Dennis

unread,
Mar 27, 2017, 8:31:14 PM3/27/17
to Jenkins Users
I think it is failing on the call to the generateXML() method - is it defined in your job and how is it defined, what parameters does it expect?

BTW, be aware that the Groovy 'Eval' will allow anyone to execute whatever Groovy code they would like to put in the input parameter. This line:

def inputMap = Eval.me("$input") 

Your local IT security people may not like this :-)

Have you considered passing the List/Map as a JSON structure on the input? Seems safer, unless there is a better way to secure the groovy eval.

--Bill

niristotle okram

unread,
Apr 6, 2017, 10:50:47 PM4/6/17
to jenkins...@googlegroups.com
Hey Bill:

yes, got things to work with the input being parsed from a json. BUT only in the master. Things get messy and dark, when its a distributed env (master-slave) 

problem:
 jenkins workflow script doesn't like java api's. So IO things like 

  // def fileReader = new BufferedReader(
  //   new FileReader("${currentws}/objects.json"))  


  def writer = new FileWriter("${currentws}/okram.XML")

DOESN"T WORK.. 

I think they want me to use 

  def fileReader = readFile "${currentws}/objects.json"
    def parsedData = jsonSlurper.parse(fileReader)


def xmlwriter = writeFile file: '${currentws}/okram.XML', text: ''


But the problem here is that nothing is shows up although the execution of the NonCPS method completes. There is no trace of the file. 









--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/ba367247-dca5-43e4-8f8f-b6e4700ce6f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages