Automating Pylint via Jenkins and Gerrit
Static analysis and sticking to a style guide helps everyone stay on the same page, especially when your team comes from diverse code backgrounds. Choosing a static analysis and style guide is the easy part, of course–sticking to it is another story altogether.
To get everyone onboard, I integrated our Jenkins server with Gerrit to automatically run pylint (our choice for static analysis - not perfect, but it helps) on incoming reviews and post results. There’re a handful of guides around the web that teach how to setup Jenkins to run a job on Gerrit events, but not a lot of information on custom messages back to Gerrit, or automating static analysis.
Let’s get started.
Integrating Jenkins and Gerrit
Install plugins: Gerrit Trigger, GIT Plugin
Configure Gerrit Trigger plugin:
- Click ‘Manage Jenkins’ on the left at the Jenkins Dashboard
- Click ‘Gerrit Trigger’
- Click ‘Add New Server’ on the left
- Type in a name for your server, select ‘Gerrit Server with Default Config’ and click “Ok”
- Fill out the fields with your Gerrit host information.
- You’ll need to copy over your Jenkins ssh key into Gerrit (and provide proper access rights in gerrit! ‘Stream Events: ALLOW for Event Streaming Users’)
- Major parts to fill out: ‘Hostname’, ‘Frontend URL’, ‘Username’
- Click ‘Test Connection’
- If connection fails, try connecting via terminal to your gerrit host (using SSH).
Configure your Jenkins Job to use Gerrit Trigger:
- Create a new Jenkins Job
- Under Source Code Management, select ‘Git’
- Set ‘repository URL’ to your Gerrit host, including your project (protocol://username@hostname:port/project/path) ex: ssh://tester@gerrit_host:29418/Auto/automation_test
- Select your credentials that you use to connect to gerrit (we use SSH)
- Click ‘Advanced’
- Set ‘Refspec’ to ‘$GERRIT_REFSPEC’
- Set ‘Branch Specifier’ to ‘$GERRIT_BRANCH’
- Click ‘Add’, then select ‘choosing strategy’, and change it to ‘Gerrit Trigger’
- Select ‘Gerrit Event’ as a Build Trigger
Under ‘Gerrit Trigger’ Section:
Still in the same jenkins job configuration 1. Select your server you created earlier under 'Choose a Server' 2. Select 'Silent Mode' 3. Choose you want to trigger on (We use Patchset Created, excluding trivial rebases and 'no code changes'). ![](http://i.imgur.com/tR08BFM.png) 4. Beneath 'Dynamic Trigger Configuration' --- DO NOT SELECT THE CHECKBOX:
Choose 'Path' On the left dropdown, in the box put '**'. On the right, select 'Path' again, and '**' in the text box. ![](http://i.imgur.com/AkzmSCV.png)
In the Build section:
Use 'execute shell': 'gpylinter.py'
Lint
Lint is a python package to assist with automatic code reviews. It provides the following tools:
- Get a list of files changed between the active gerrit branch and the specified gerrit review.
- Lint the original files in the active gerrit branch.
- Checkout the current review ID
- Lint the changed files.
- Analyze the results according to specified validators.
- Post the results of the validation to gerrit via SSH (+1/-1 score assigned, including a message).
Lint uses the environmental variables set by Gerrit Trigger to do almost all of the configuration. You can still use it via command-line (with options!) for manual testing.
You can also add a checkers to validators, which are simple functions to compare lint data that are passed to the validator.