{"id":11840,"date":"2014-05-26T11:03:00","date_gmt":"2014-05-26T10:03:00","guid":{"rendered":"http:\/\/touk.pl\/blog\/?guid=8a857f3e0dc8d107675ea789b858c309"},"modified":"2022-08-01T15:38:57","modified_gmt":"2022-08-01T13:38:57","slug":"meet-sputnik-static-code-analyser-for-gerrit","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2014\/05\/26\/meet-sputnik-static-code-analyser-for-gerrit\/","title":{"rendered":"Meet Sputnik &#8211; static code analyser for Gerrit"},"content":{"rendered":"<h3 id=\"sputnik-runs-checkstyle-pmd-and-findbugs-for-your-gerrit-patchsets\">Sputnik runs Checkstyle, PMD and FindBugs for your Gerrit patchsets<\/h3>\n<p>I am happy to announce a first release of <strong>Sputnik<\/strong>! It is a static code analyzer that runs Checkstyle, PMD and FindBugs for your Gerrit patchsets. Its main advantage over my previous project <a href=\"http:\/\/refaktor.blogspot.com\/2013\/12\/sonar-gerrit-plugin-release.html\">Sonar Gerrit plugin<\/a> is that Sputnik is a small, lightweight and standalone Java application. You don&#8217;t need any other software to run it. It bundles Checkstyle, PMD and FindBugs jars within distribution zip.<\/p>\n<h3 id=\"workflow\">Workflow<\/h3>\n<p>Sputnik is intended to use with Gerrit and Continous Integration server, i. e. Jenkins. It works like this:<\/p>\n<div class=\"separator\" style=\"clear: both;text-align: center\"><a style=\"margin-left: 1em;margin-right: 1em\" href=\"http:\/\/1.bp.blogspot.com\/-iBrQTZnzk2I\/U2ECnfJWZUI\/AAAAAAAAAyM\/fDxtCa2-r3Q\/s1600\/Sputnik+and+Jenkins+2.png\"><img decoding=\"async\" src=\"http:\/\/1.bp.blogspot.com\/-iBrQTZnzk2I\/U2ECnfJWZUI\/AAAAAAAAAyM\/fDxtCa2-r3Q\/s1600\/Sputnik+and+Jenkins+2.png\" border=\"0\" \/><\/a><\/div>\n<p>Your CI server is updated by ssh that a new patch is submitted to Gerrit. CI fetches this patch and builds a while project. After a build, CI server reports its result to Gerrit. It&#8217;s time for Sputnik now.<\/p>\n<p>Sputnik runs regardless of build result (you can change that in your CI configuration). Sputnik fetches patchset&#8217;s file list from Gerrit over HTTP REST API. Then <strong>it runs an analysis only on these files!<\/strong> Even if your project is huge, analysis on several files takes only seconds. Sputnik collects comments from all three analysers: Checkstyle, PMD and FindBugs. It sends back all comments to Gerrit via HTTP REST API back. It&#8217;s very simple and very fast!<\/p>\n<h3 id=\"installation-and-configuration\">Installation and configuration<\/h3>\n<p>First, you need to build <a href=\"https:\/\/github.com\/TouK\/sputnik\">https:\/\/github.com\/TouK\/sputnik<\/a> master or download distribution zip from here: <a href=\"https:\/\/github.com\/TouK\/sputnik\/releases\/download\/v1.0\/sputnik-1.0.zip\">sputnik-1.0.zip<\/a>. Go to you CI server and extract it to a directory of your choice. Remember that a user you run CI builds needs to have an access rights to this directory (in my case it&#8217;s simply a <code>jenkins<\/code> user). Then you need to prepare your configuration file and write this file to the same directory as unzipped distribution. It is a simple Java properties file, which is pretty self-explanatory. Here is an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">gerrit.host=gerrit.yourcompany.com\r\ngerrit.port=8080\r\ngerrit.username=sputnik\r\ngerrit.password=Pa$$wo4d\r\ncheckstyle.enabled=true\r\ncheckstyle.configurationFile=\/opt\/jenkins\/sputnik\/checkstyle.xml\r\ncheckstyle.propertiesFile=\r\npmd.enabled=true\r\npmd.ruleSets=\/opt\/jenkins\/sputnik\/pmd.xml\r\nfindbugs.enabled=true\r\nfindbugs.includeFilter=\/opt\/jenkins\/sputnik\/findbugs.xml\r\nfindbugs.excludeFilter=<\/pre>\n<p>Now you need to configure you CI server to actually run Sputnik after a build. It is very simple for Jenkins, just add a Post-Build Step. You can adjust if Sputnik runs only on successful build or for every build &#8211; use radio buttons for this:<\/p>\n<div class=\"separator\" style=\"clear: both;text-align: center\"><a style=\"margin-left: 1em;margin-right: 1em\" href=\"http:\/\/1.bp.blogspot.com\/-kx3y-alHeHg\/U2D8Gzs_PJI\/AAAAAAAAAyA\/rjmCHdpVWM8\/s1600\/sputnik-jenkins.png\"><img decoding=\"async\" src=\"http:\/\/1.bp.blogspot.com\/-kx3y-alHeHg\/U2D8Gzs_PJI\/AAAAAAAAAyA\/rjmCHdpVWM8\/s1600\/sputnik-jenkins.png\" border=\"0\" \/><\/a><\/div>\n<p>Last line with <code>exit 0<\/code> is a workaround for a clean exit, even if Sputnik fails for some reason. Exit 0 guarantees you that result of this step doesn&#8217;t affect overall build result.<\/p>\n<h3 id=\"summary\">Summary<\/h3>\n<p>This is an example screenshot of Sputnik&#8217;s comments:<\/p>\n<div class=\"separator\" style=\"clear: both;text-align: center\"><a style=\"margin-left: 1em;margin-right: 1em\" href=\"http:\/\/4.bp.blogspot.com\/-BK7gj9jhg8M\/U4MMCjJQ1SI\/AAAAAAAAAyk\/DPljjjwLVrg\/s1600\/sputnik-jenkins2.png\"><img decoding=\"async\" src=\"http:\/\/4.bp.blogspot.com\/-BK7gj9jhg8M\/U4MMCjJQ1SI\/AAAAAAAAAyk\/DPljjjwLVrg\/s1600\/sputnik-jenkins2.png\" border=\"0\" \/><\/a><\/div>\n<p>Sputnik always reports +1 as a result. It can be lacking in some network and authorisation configuration. But it&#8217;s open source so please submit issues and patches to its github page: <a href=\"https:\/\/github.com\/TouK\/sputnik\">https:\/\/github.com\/TouK\/sputnik<\/a>.<\/p>\n<p>Your feedback and pull requests are heartly welcome!<\/p>\n","protected":false},"excerpt":{"rendered":"Sputnik runs Checkstyle, PMD and FindBugs for your Gerrit patchsets I am happy to announce a first release of Sputnik! It is a static code analyzer that runs Checkstyle, PMD and FindBugs for your Gerrit patchsets. Its main advantage over my previous pr&#8230;\n","protected":false},"author":37,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[461,353,212,482],"class_list":{"0":"post-11840","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-ci-cd","8":"tag-code-review","9":"tag-git","10":"tag-quality"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=11840"}],"version-history":[{"count":2,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11840\/revisions"}],"predecessor-version":[{"id":14790,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/11840\/revisions\/14790"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=11840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=11840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=11840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}