Getting code inspections
Is it possible to get the Code Inspections for a build programatically? i.e. I have a SRunningBuild object in my plugin, and I want to write some code that looks at the current inspections and changes the build status to failed.
Please sign in to leave a comment.
Simon,
There is no open API to access inspection data.
You can probably use non-open AP, provided, it's OK to adapt/rewrite your codewith future TeamCity releases when the API can change.
You can try to use something like: new InspectionInfo(myServer, buildId).getStatistics()
Where the returned array contains the values displayed on "Code Inspection" tab on build results in TeamCIty web UI as follows:
Total: ${stats[0]} (+${stats[1]} -${stats[2]})
Errors: ${stats[3]} (+${stats[4]} -${stats[5]})
Can you please describe what kind of logic do you want to implement?
Hi Yegor,
Thanks for the response.
Initially I want to write a plugin that causes a build failure if there are any new inspections added. This is becuase our legacy code base has a large number of existing issues, and we don't want them to get worse. The details you have given will allow me to do this.
The next step would then be to go through the inspections and try to work out if they are really new, or just due to a line change. Currently if a new line of code is inserted before a line with an inspection we get +1 -1, but really it's the same inspection. For these I need to go through all of the text etc. for each inspection. I assume this info is also available from the InspectionInfo object.
Many thanks, Simon
Hi Yegor,
I've hit a problem with my inspections plugin.
I want to cause the build to fail if the inspections goes up. I've got all the code I need in the in the buildFinished() function within my buildServerListener class. However calling runningBuild.setBuildStatus(Status.FAILURE) appears to have no effect there.
I've tried moving the code to beforeBuildFinish() which then allows runningBuild.setBuildStatus(Status.FAILURE) to work, however inspectionInfo.getStatistics() returns null.
How can I retrieve the inspections from the running build? or change the status of the finished build?
Simon
Try this approach: catch buildStarted event and register new listener with beforeBuildFinish event processing. In this case beforeBuildFinish will be called after the processing of inspections results. Upon registration the listener must be tied to a build, and react to events related to this build only. Do not forget to unregister this listener in beforeBuildFinished event, to prevent memory leak.
It's a bit hacky, but should solve your problem.
That's clever. It worked perfectly. My plugin now finds when a build finishes, and if it was going to pass, it looks back to find the last successful build and checks the inspection totals against that, and if there is an increase it adds a build message and fails the build.
When finding the reference build I look for the last one that passed OR had a tag of "INSPECTIONS_BASELINE" so that we can set an acceptable point manually if required.
Many thanks for your help.
Great! Do you have any plans to open source this plugin?
Yes and no. I don't really want to host it anywhere, I've not really got the time to sort it out and it could use some more features, like making builds that fail due to new inspection errors get marked as passed, and maybe a parameter somewhere to say whether this configuration is using it or not.
However, I'm happy to attach the source here so someone else can take it over, or people can use it for their own plugins.
I hope people find it useful.
Simon
Attachment(s):
inspections-monitor_v1.zip
A link to the related feature request in our issue tracker to vote for or watch: TW-2539
Hi Yegor,
Is it possible to update inspection statistics from a plugin? Let's say an external plugin has a better knowledge of fixed and newly detected defects in changed code. How can I push this information into TeamCity database so build result message would show correct numbers?
Regards,
Evgeny
Evgeny,
In theory, it is possible to publish statistics values from the build which will match those used by TeamCity to display inspections data.
However, it is recommended to use own statistics values, see details at the corresponding documentaiton section.
Well, I used custom metrics reports for other project and it worked just fine, but they don't affect the final build message. I.e. TeamCity core will still report "..inspections total: n (+n -k)..." at the end of the build. And k will be the number of defects detected in previous build. With default statistics values I still can affect only newly detected defects, but not the number of defectes fixed in last commit.
I could probably overwrite the whole build status message using SRunningBuild.setBuildStatus call, but it's deprecated now. Suggested workaround to use SBuild.muteBuildProblems requires not null User context, which is not available at build time.
I'm currently got stuck here. There're plenty of differnt solutions available, but non of them completely solves the problem. :(