SonarQube provides Github plugin using which it can publish inline comments in the git pull request for issues found in the modified/new codes of the pull request. So you do not have to manually review Github pull request and add comments, SonarQube will do it for you. Below are the steps to set up the Github plugin in SonarQube.

1. Installation:
To install Github Plugin, follow below process:

  1. Login to your SonarQube as Administrator,
  2. Go to tab Administrator -> System -> Update Center -> Available,
  3. Search GitHub in the search box which will then list the plugin by searching SonarQube plugin repository.
  4. On the right side of the plugin list, click Install button to install it.
  5. Once installed, restart your SonarQube. You can confirm installation by going to ‘Installed’ tab from step 2 above and check if Github is displaying as installed without any issue.

Below is the screen attached for reference:

Github-plugin-installation-process
2. Generate Github OAuth access token:
Generate your Github OAuth access token to be used in SonarQube Github plugin configuration so that SonarQube can access your Github repository and specified pull request to analyze code and publish the comments for issues found.

To Generate Github access token, follow below process:

  1. Login to your Github account.
  2. Go to the account settings page.
  3. On the left side of the page in ‘Personal settings’ list, click ‘Personal access tokens’ option which will show its page on right side.
  4. Click ‘Generate new token’ button on the top right of ‘Personal access tokens’ page.
  5. Give any suitable name for ‘Token description’ like SonarQube and then check repo box to allow access to your repositories.
    NOTE: If you do not want to allow private repository access and just using the public repository for SonarQube analysis then just select ‘public_repo’ checkbox.
  6. Click ‘Generate token’ button at the bottom of the page and which will generate a token. save this token so as to use in SonarQUbe Github plugin configuration.

Below is the screen attached for reference:
github-sonar-generate-token-linkgithub-sonar-access-token-generate-page
3. Configure the Github plugin:
Now you have installed Github plugin and generated Github access token to be used in SonarQube configuration.

So the only process left is to add configuration code your ‘sonar-project.properties’ file which you are using for SonarQube analysis in your codebase/project.

Follow below step to add configuration:

  1. Open your sonar-project.properties file which you have inside your codebase/project using which you run sonar-runner to analyze code.
  2. Add below configuration code at the end of the file. (see inline hashed comments to know what value to give for each property)
    # This is to allow sonar to access your GitHub repository. Personal access token you generated above in Github.
    sonar.github.oauth= <your oauthid generated in Github>
    # Give repository identification in the format of <organisation/repo> like SonarSource/SonarQube
    sonar.github.repository= <organisation/repo>
    # Pull request number of git pull request so that sonar will analysis and pull comment for that pull request.
    sonar.github.pullRequest= <Pull request number>

NOTE:

  • Pull request number you can get from pull request list or url like for “https://github.com/SonarSource/sonar-jira/pull/10” pull request number is 10.
    This value you have to give/change every time before running sonar-runner so that SonarWub
  • <organisation/repo> value you can get from “Repositories you contribute to” section appears on the right side of your home page at https://github.com

That’s it. Now git checkout to the branch of the pull request for which you want to do analyze and run sonar-runner.

Once the analysis is done by Sonar runner then it will post comments to the Github pull request for the modified/new codes for issues if there is any. You can open your git pull request on the browser and see inline comments for related codes having issues.

References:

  • http://docs.sonarqube.org/display/PLUG/GitHub+Plugin
  • http://www.sonarqube.org/github-pull-request-analysis-helps-fix-the-leak/