I writing a groovy script for running a static code analysis using Synopsys Coverity and want to store all found issues in a .txt file. Then need to find the number of lines in that .txt file and if it is greater than 1 then need to shoot an email to concerned person. My code for this groovy script is as follows: -
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}")
}
}
When this runs Jenkins is throwing following error
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
I understand that this is related to casting string to number. I think problem is that my script is returning 1"
instead of 1
and that's why toInteger()
is not able to convert it to number. But I am not able to find out how to resolve this.
CodePudding user response:
So following snippet you can use for your case
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"
}
Now what was the proble,\m. The problem in your case is that you are getting a string written of follows:
"C:\JenkinsWS\workspace\gwp-master>find /v "" /c 0<"GWDefects.txt"
1"
These are 2 lines. 1 where and what bat is executed and the second your result. You need the result. So best way is to split it and then discard the first output and use the second output and then convert to integer.
EDIT: I should have updated your code. Here is what your code would look like
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}")
}
}