Search code examples
jenkinsgroovyjenkins-pipelinepipelinejenkins-groovy

Alternative to `list.findIndexOf` in Jenkins/Groovy


When executing the following in Jenkins:

def platformIndex = list.findIndexOf { it.key ==  "linux" }.value
if (platformIndex == -1) { // params.PLATFORM not found
    platformIndex = env.BUILD_NUMBER % platformMaps.size() 
}

I get the following error:

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use field java.lang.Integer value
08:50:44    at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectField(StaticWhitelist.java:284)
08:50:44    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor$10.reject(SandboxInterceptor.java:344)
08:50:44    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:409)
08:50:44    at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
08:50:44    at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:357)
08:50:44    at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
08:50:44    at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
08:50:44    at WorkflowScript.run(WorkflowScript:60)
08:50:44    at argusGitPipeline.call(argusGitPipeline.groovy:64)
08:50:44    at argusPipeline.call(argusPipeline.groovy:70)

I've taken note of solution described here Jenkins CI Pipeline Scripts not permitted to use method groovy.lang.GroovyObject

However, disabling sandbox or disabling script security is not an option. Therefore I was wondering if there's an alternative to list.findIndexOf


Solution

  • Just remove the .value part:

    def platformIndex = list.findIndexOf { it.key ==  "linux" }
    if (platformIndex == -1) { // params.PLATFORM not found
        platformIndex = env.BUILD_NUMBER % platformMaps.size() 
    }
    

    The findIndexOf() function already returns an integer.