在 Jenkins/Groovy 中替代 `list.findIndexOf`

Alternative to `list.findIndexOf` in Jenkins/Groovy

在 Jenkins 中执行以下操作时:

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

我收到以下错误:

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.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.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)

我已记下此处描述的解决方案

但是,禁用沙箱或禁用脚本安全性不是一种选择。因此我想知道是否有 list.findIndexOf

的替代方案

只需删除 .value 部分:

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

findIndexOf()函数已经returns一个整数。