首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在代理上在Groovy脚本中查找文件中的行数

在代理上在Groovy脚本中查找文件中的行数
EN

Stack Overflow用户
提问于 2022-03-10 07:52:29
回答 1查看 136关注 0票数 1

我编写了一个groovy脚本,用于使用运行静态代码分析,并希望将所有发现的问题存储在.txt文件中。然后需要在该.txt文件中找到行数,如果它大于1,则需要向相关人员发送电子邮件。我为这个groovy脚本编写的代码如下:

代码语言:javascript
复制
def call(Map coverityParams){
  echo "${coverityParams.defectsFileName}"
  bat """
        cov-analyze --dir ${coverityParams.tempDir} --all --security ${coverityParams.securitySwitch}
        cov-commit-defects --dir ${coverityParams.tempDir} --stream ${coverityParams.stream} --host 172.16.15.34 --auth-key-file ${coverityParams.authKeyFile}
        cov-manage-im --mode defects --show --host 172.16.15.34 --stream ${coverityParams.stream} --user=${coverityParams.userName} --password=${coverityParams.password} --status New > ${coverityParams.defectsFileName}
     """
     int DEFECTS_COUNT = bat(
                script: find /v \"\" /c < \"${coverityParams.defectsFileName}\",
                returnStdout: true
            ).trim().toInteger()
            DEFECTS_COUNT = DEFECTS_COUNT - 1
            echo "Defects Count is ${DEFECTS_COUNT}"
            if(DEFECTS_COUNT > 0){
                echo "Defects Count is ${DEFECTS_COUNT}"
                mail (body: 'You have ' + DEFECTS_COUNT + ' new coverity defects introduced. Please go to ' + "${coverityParams.viewName}" + ' view on http://172.16.15.34:8080/ ', subject: DEFECTS_COUNT + ' new coverity issues detected.', to: "${coverityParams.mailTo}")
            }
}

运行时,Jenkins将抛出以下错误

代码语言:javascript
复制
java.lang.NumberFormatException: For input string: "C:\JenkinsWS\workspace\gwp-master>find /v "" /c  0<"GWDefects.txt" 
1"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.valueOf(Integer.java:766)
    at org.codehaus.groovy.runtime.StringGroovyMethods.toInteger(StringGroovyMethods.java:3312)
    at org.codehaus.groovy.runtime.dgm$1160.doMethodInvoke(Unknown Source)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1213)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
    at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:47)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.methodCall(DefaultInvoker.java:20)
    at coverityCommitDefectsAndMailWin.call(coverityCommitDefectsAndMailWin.groovy:8)
    at WorkflowScript.run(WorkflowScript:197)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.delegateAndExecute(ModelInterpreter.groovy:137)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:666)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:395)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.catchRequiredContextForNode(ModelInterpreter.groovy:393)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.executeSingleStage(ModelInterpreter.groovy:665)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:288)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:544)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.toolsBlock(ModelInterpreter.groovy:543)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.evaluateStage(ModelInterpreter.groovy:276)
    at org.jenkinsci.plugins.pipeline.modeldefinition.ModelInterpreter.withEnvBlock(ModelInterpreter.groovy:438)
    at ___cps.transform___(Native Method)
    at com.cloudbees.groovy.cps.impl.ContinuationGroup.methodCall(ContinuationGroup.java:86)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.dispatchOrArg(FunctionCallBlock.java:113)
    at com.cloudbees.groovy.cps.impl.FunctionCallBlock$ContinuationImpl.fixName(FunctionCallBlock.java:78)
    at sun.reflect.GeneratedMethodAccessor1021.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
    at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
    at com.cloudbees.groovy.cps.Next.step(Next.java:83)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
    at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
    at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:129)
    at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:268)
    at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$001(SandboxContinuable.java:18)
    at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:51)
    at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:185)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:402)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$400(CpsThreadGroup.java:96)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:314)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:278)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:67)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:139)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:68)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:750)
Finished: FAILURE

据我所知,这与将字符串转换为数字有关。我认为问题在于我的脚本正在返回1"而不是1,这就是为什么toInteger()无法将它转换为数字。但我无法找到解决这一问题的办法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-11 09:52:37

因此,下面的代码片段可以用于您的案例

代码语言:javascript
复制
script {
    // some block
    def DEFECTS_COUNT_STR = bat(
                script: '''find /v "" /c < "somefile.txt"''',
                returnStdout: true
            ).trim().split("\n")
    int DEFECTS_COUNT = DEFECTS_COUNT_STR[1].toInteger()
    echo "$DEFECTS_COUNT"
}

现在是什么问题,\m。在您的例子中的问题是,您正在得到一个字符串,如下所示:

代码语言:javascript
复制
"C:\JenkinsWS\workspace\gwp-master>find /v "" /c  0<"GWDefects.txt" 
1"

这是两条线。1蝙蝠在哪里和什么地方被执行,第二次你的结果。你需要结果。所以最好的方法是拆分它,然后丢弃第一个输出,使用第二个输出,然后转换为整数。

编辑:我应该更新你的代码。下面是您的代码的样子

代码语言:javascript
复制
def call(Map coverityParams){
  echo "${coverityParams.defectsFileName}"
  bat """
        cov-analyze --dir ${coverityParams.tempDir} --all --security ${coverityParams.securitySwitch}
        cov-commit-defects --dir ${coverityParams.tempDir} --stream ${coverityParams.stream} --host 172.16.15.34 --auth-key-file ${coverityParams.authKeyFile}
        cov-manage-im --mode defects --show --host 172.16.15.34 --stream ${coverityParams.stream} --user=${coverityParams.userName} --password=${coverityParams.password} --status New > ${coverityParams.defectsFileName}
     """
     def DEFECTS_COUNT_STR = bat(
                script: find /v \"\" /c < \"${coverityParams.defectsFileName}\",
                returnStdout: true
            ).trim().split("\n")
     int DEFECTS_COUNT = DEFECTS_COUNT_STR[1].toInteger()
            DEFECTS_COUNT = DEFECTS_COUNT - 1
            echo "Defects Count is ${DEFECTS_COUNT}"
            if(DEFECTS_COUNT > 0){
                echo "Defects Count is ${DEFECTS_COUNT}"
                mail (body: 'You have ' + DEFECTS_COUNT + ' new coverity defects introduced. Please go to ' + "${coverityParams.viewName}" + ' view on http://172.16.15.34:8080/ ', subject: DEFECTS_COUNT + ' new coverity issues detected.', to: "${coverityParams.mailTo}")
            }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71420513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档