GitHub pull request analysis using SonarQube

Sonarqube

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/

About the Author

Sandeep Agrawal
working @ Kalpavruksh Technologies as Software Engineer. I am a simple and straightforward person adaptable to any environment and work culture. Like to work in a creative and challenging environment using cutting edge technologies where I could constantly learn and successfully deliver solutions to problems.

24 Comments on "GitHub pull request analysis using SonarQube"

  1. This is really cool. This plugin will really help me. Thanks Sandeep for sharing it. Will definitely try this out. 🙂

    • Yes, Its really cool.
      we should do more and more research and see how it can be tweaked to make it review based on other rules like php codesniffer rules to be integrated with it so as to make it Project/Website based review as per requirements.

  2. I am not sure why , but i get this error .”Unable to perform GitHub WS operation: Server returned HTTP response code: -1, message: ‘null’ for URL: https://api.github.com/user

    I have configured the sonar.github.oauth as well.

    • Did you also allowed the scope for that access token in your github account as per screenshot attached in the main post? or may be if you can try selecting other relevant scope to allow and see if that work?

      • Yes infact just to be sure i have selected all the scopes available.

        • I have locally set up sonarQube and my sonar-project.properties has
          # must be unique in a given SonarQube instance
          sonar.projectKey=transactions
          # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
          sonar.projectName=transactions-api
          sonar.projectVersion=1.0

          # Path is relative to the sonar-project.properties file. Replace “\” by “/” on Windows.
          # Since SonarQube 4.2, this property is optional if sonar.modules is set.
          # If not set, SonarQube starts looking for source code from the directory containing
          # the sonar-project.properties file.
          sonar.sources=.

          # Encoding of the source code. Default is default system encoding
          #sonar.sourceEncoding=UTF-8

          # This is to allow sonar to access your github repository. Personal access token you generated above in Github.
          sonar.github.oauth=XXXXXXX
          # Give repository identification in the format of like SonarSource/sonarqube
          sonar.github.repository=XXXX/transactions.api.XX.XX.XX
          # Pull request number of git pull request so that sonar will analysis and pull comment for that pull request.
          sonar.github.pullRequest=10
          sonar.analysis.mode=preview
          sonar.login=XXXX

          • The configuration you posted looks fine.
            I am still trying to understand what could be the issue.

            The latest version i am using is Version 6.0. Are you using the same or the latest one that is 6.2 ?

            if you have set all permissions for github repo to be accessible for sonar and sonar configuration properly, then just thinking if the error has something has to do with version if you are using latest?

          • Yes i am using the latest version 6.2

      • Yes i am using the latest version 6.2

        • I am connecting externally from my organization so i have also added proxy setting to sonar.properties . Is this the only location i add proxy
          http.proxyHost=XXXXX
          http.proxyPort=8080
          # HTTPS proxy (defaults are values of http.proxyHost and http.proxyPort)
          https.proxyHost=XXXX
          https.proxyPort=8080

          • It works when i connect from an external network , maybe something to do with the way its accessing the proxy settings.Thanks a lot for all the help sandeep.It a great article helped me a lot.

          • Its great that you figured it out related to proxy setting.

            and its good that my article helped you a lot.
            You’re welcome.

  3. Hi,
    I followed your post as above. but the issue/ comments inline are not appearing in GIT hub console.

    Please help

    • When you run sonar-runner on your branch pull request via command line then what output it shows at the end? Does it says anything about posting the pull request analysis comment to github or something? or it fails to post?
      also i hope you are running sonar runner on the active branch with ‘sonar.github.pullRequest=‘in configuration file for which pull request is to be analysed.
      If you can give some more information about what is the output at the end when you run sonar runner.

      • Hi Sandeep,

        Thanks for quick response. Below is sonar-project.properties configuration.
        $ cat sonar-project.properties
        sonar.projectKey=org.sonarqube:mk
        sonar.projectName=PHP :: PHPUnit :: php_mk
        sonar.projectVersion=1.0

        sonar.sources=/home/usharpr/site/docroot/
        sonar.exclusions=/home/usharpr/site/docroot/core/modules/*

        sonar.language=php

        sonar.sourceEncoding=UTF-8

        # Reusing PHPUnit reports
        sonar.php.coverage.reportPath=reports/phpunit.coverage.xml
        sonar.php.tests.reportPath=reports/phpunit.xml

        sonar.analysis.mode=preview
        sonar.github.oauth=
        sonar.github.repository=
        sonar.github.pullRequest=1
        sonar.github.endpoint=https://api.github.com
        ==========================================

        Below is output after execution.

        INFO: 18022/18022 source files have been analyzed
        INFO: PHPUnit xml test report not found: reports/phpunit.xml
        INFO: PHPUnit xml unit test coverage report not found: reports/phpunit.coverage.xml
        INFO: No PHPUnit integration test coverage report provided (see ‘sonar.php.coverage.itReportPath’ property)
        INFO: No PHPUnit overall coverage report provided (see ‘sonar.php.coverage.overallReportPath’ property)
        INFO: Sensor PHP sensor (done) | time=475054ms
        INFO: Sensor Analyzer for “php.ini” files
        INFO: Sensor Analyzer for “php.ini” files (done) | time=26ms
        INFO: Performing issue tracking
        INFO: 21876/21876 components tracked
        INFO: ANALYSIS SUCCESSFUL
        INFO: Executing post-job GitHub Pull Request Issue Publisher
        INFO: ————————————————————————
        INFO: EXECUTION SUCCESS
        INFO: ————————————————————————
        INFO: Total time: 8:14.975s
        INFO: Final Memory: 48M/736M
        INFO: —————————————————————–

        Let me know if any other information is required.

  4. Sandeep, thank you for publishing this. It was very helpful!
    Mark

  5. Do we need to post the pull request ID manually all the time?

    • Yes until you automate the process via any tool like Jenkins to make that sonar-project.properties file with update information.
      but yes, if you are running the pull request analysis on particular pull request then you have to update the sonar-project.properties with that pull request id so that the comment will get added to that particular pull request for which you are running the analysis.

      • Hi Sandeep,

        How do you update the sonar.github.pullRequest in jenkins job if we are automating this and would like to have sonar for every new pull request without manual intervention.

        “-Dsonar.github.pullRequest=$PULL_REQUEST_ID”

  6. Hi Sandeep!
    I’m using sonarqube 5.5 and I’m unable to install the girhub plugins. I’m getting like ‘Error while downloading plugin ‘github’ with version ‘1.4.1’. No compatible plugin found.’ Please do suggest me a solution. Thanks in advance.

    Karpagavalli

  7. Thank you for writing the blogpost. However I’ve tried to accomplish this on SonarQube Version 6.7.6 (build 38781). It doesn’t work at all. I’ve checked the official docs and it says this “This plugin is deprecated, and its functionality more than replaced by the Developer Edition for SonarQube 7.2+.” The developer edition is not for free.

    Would really appreciate if you could respond. Thank you!

    • ok. It was working with the previous version but now SonarQube team has made the changes to include it only in the latest version of SonarQube developer edition which is not a free version now. 😐
      I think we can not use it for free anymore.
      Thanks for visiting the post 🙂 I will update here if I find anything.

Leave a comment

Your email address will not be published.


*