Giter VIP home page Giter VIP logo

github-sonarcloud-integration's Introduction

github-sonarcloud-integration

Quality Gate Status Bugs Code Smells Coverage Duplicated Lines (%) Lines of Code Reliability Rating Security Rating Technical Debt Maintainability Rating Vulnerabilities

Scan and analyze GitHub repository with SonarCloud

Setup

Automatic Analysis

Important

With Automatic Analysis for .Net, certain rules for .Net source code are automatically deactivated. This includes security rules, all rules that come from outside the Sonar Way quality profile, as well as certain rules from within it.

Note

Automatic analysis is only supported for GitHub repositories. If you are using a different version control system, you will need to use a different method to analyze your code.

  1. Follow the docs under SonarCloud - Getting Started with GitHub to setup SonarCloud with GitHub
    1. Sign up at SonarCloud
    2. Click Import another organization
    3. Select your personal GitHub account or the organization that contains the repository you want to scan
    4. When reaching the Create your SonarCloud organization page adjust/update data and click Create organization
    5. On Analyze projects page select the repository you want to scan and click Set Up
    6. On Set up project for Clean as You Code page select the desired code definition and click Create project
  2. After completing the setup, the repository will be scanned automatically and you will see the results on the SonarCloud dashboard

CI-based analysis

Important

Assumption: Automatic analysis is already set up for the repository

To set up CI-based analysis with GitHub actions follow the instructions (guided wizard) under https://sonarcloud.io/project/configuration/GitHubActions?id=SONAR_CLOUD_PROJECT_ID

Note

Replace SONAR_CLOUD_PROJECT_ID with the id of the SonarCloud project

Include languages other than C#

To include i.e. terraform files in the analysis of SonarScanner for .NET, the following adjustments are required.

  1. Extend the dotnet-sonarscanner begin command with project base dir argument /d:sonar.projectBaseDir="D:\a\GITHUB_PROJECT_NAME\GITHUB_PROJECT_NAME" where GITHUB_PROJECT_NAME is the name of the GitHub project

  2. Include the corresponding source files/folders in one of the projects csproj file

    <ItemGroup>
       <!-- This is required to include terraform files in SonarCloud analysis -->
       <Content Include="..\..\deploy\**\*.tf" Visible="false">
          <CopyToOutputDirectory>Never</CopyToOutputDirectory>
       </Content>
    </ItemGroup>

    For more details see here

Include test coverage

To include test coverage in the analysis of SonarScanner for .NET, the following adjustments are required in the GitHub actions workflow (.github/workflows/quality.yml).

# Install dotnet-coverage
- name: Install dotnet-coverage
  shell: powershell
  run: |
    dotnet tool install --global dotnet-coverage
- name: Build and analyze
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
    SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
  shell: powershell
  run: |
    # Add /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml
    .\.sonar\scanner\dotnet-sonarscanner begin /k:"rufer7_github-sonarcloud-integration" /o:"rufer7" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.projectBaseDir="D:\a\github-sonarcloud-integration\github-sonarcloud-integration" /d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml
    dotnet build .\src\ArbitrarySolution.sln --configuration Release
    # Execute tests and collect coverage
    dotnet-coverage collect 'dotnet test .\src\ArbitraryProject.Tests\ArbitraryProject.Tests.csproj' -f xml  -o 'coverage.xml'
    .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"

Include test coverage

To include test coverage in the analysis of SonarScanner for .NET, the following adjustments are required in the

Scan Results

SonarCloud

The scan results can be viewed on the SonarCloud dashboard

GitHub

Security hotspots detected by SonarCloud can be viewed directly on the GitHub repository under Security tab in the Code scanning section

Example

Code scanning alert

Pull Request (GitHub)

Pull request analysis results can be found directly on the pull requests.

For an example, see here

Useful Links

github-sonarcloud-integration's People

Contributors

rufer7 avatar damienbod avatar

Stargazers

Ioannis Michail avatar gerudaeta avatar Roland Leitenberger avatar Lee Seung Hu avatar  avatar Luca Milan avatar  avatar

Watchers

 avatar

Forkers

damienbod

github-sonarcloud-integration's Issues

Scan repository with SonarCloud

  • Set up SonarCloud account - see SonarCloud - Getting Started with GitHub link in README
  • Scan repo with SonarCloud (automatic scan)
  • Ensure automatic scan considers terraform files
  • Ensure automatic scan considers .NET solution
  • Check code scanning alerts in GitHub
  • Scan repo with SonarCloud (CI-based scan)
  • Ensure CI-based scan considers terraform files
  • Ensure CI-based scan considers .NET solution
  • Check code scanning alerts in GitHub
  • Update README
    • Document setup (automatic scan)
    • Document setup (CI-based)
    • Add links to code scanning results
      • SonarCloud project
      • PR analysis
      • Code scanning alerts in GitHub
    • Add badges
  • Test pull request analysis by creating a pull request that introduces a security issue/hotspot
  • Try to make code coverage work

Add .NET Web API project

  • Create .NET Web API project based on VS template (src folder)
  • Push to GitHub repo
  • Add security hotspot to be detected by SonarCloud
  • Add some unit tests

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.