on:
  # Trigger analysis when pushing in master or pull requests, and when creating
  # a pull request. 
  push:
    branches:
      - master
    paths-ignore:
      - '.azure/**'
      - '.circleci/**'
      - '.github/workflows/linux.yml'
      - '.cirrus.yml'
      - 'README.md'
  pull_request:
      types: [opened, synchronize, reopened]

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  # Use Debug build for better code coverage results
  BUILD_TYPE: Debug

name: SonarCloud Workflow
jobs:
  sonarcloud:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
      with:
        # Disabling shallow clone is recommended for improving relevancy of reporting
        fetch-depth: 0
    
    - name: Add apt-get repositories
      run: |
        sudo add-apt-repository ppa:ubuntu-toolchain-r/test

    - name: Update apt
      run: sudo apt-get update -y

    - name: Install Dependencies
      run: sudo -E apt-get -yq --no-install-suggests --no-install-recommends install cmake-data cmake libglib2.0-0 libsndfile-dev libasound2-dev libjack-dev portaudio19-dev libsdl2-dev libpulse-dev libdbus-1-dev libsystemd-dev libinstpatch-dev libreadline-dev lcov gcovr ggcov

    - name: Create Build Environment
      # Some projects don't allow in-source building, so create a separate build directory
      # We'll use this as our working directory for all subsequent commands
      run: |
        cmake -E make_directory ${{github.workspace}}/build
        echo $PATH

    - name: Get Sonar Build Wrapper
      shell: bash
      working-directory: ${{github.workspace}}/build
      run: |
        set -ex
        wget https://sonarcloud.io/static/cpp/build-wrapper-linux-x86.zip
        unzip build-wrapper-linux-x86.zip
        mv build-wrapper-linux-x86/* .
      
    - name: Configure CMake
      # Use a bash shell so we can use the same syntax for environment variable
      # access regardless of the host operating system
      shell: bash
      working-directory: ${{github.workspace}}/build
      run: cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -Werror=dev -Denable-portaudio=1 -Denable-ladspa=1 -Denable-coverage=1 -DNO_GUI=1 $GITHUB_WORKSPACE

    - name: Build
      working-directory: ${{github.workspace}}/build
      shell: bash
      # Execute the build.  You can specify a specific target with "--target <NAME>"
      run: |
        ./build-wrapper-linux-x86-64 --out-dir bw-output make

    - name: Test
      working-directory: ${{github.workspace}}/build
      shell: bash
      # Execute tests defined by the CMake configuration.
      run: |
        ./build-wrapper-linux-x86-64 --out-dir bw-output make coverage

# sonar-scanner does not like utf8 filenames
    - name: Prepare for Sonar
      run: |
       rm -rf ${{ github.workspace }}/sf2
       gcovr --version
       gcovr --help
       ls -la ${{ github.workspace }}
       ls -la ${{ github.workspace }}/build
       ls -la ${{ github.workspace }}/build/coverage

# The official sonarsource/sonarcloud-github-action@v1.5 action does not work properly.
# It keeps complaining that the build-wrapper.json cannot be found.
# Hence, use a third party action to download and add sonar-scanner to PATH and then run it manually.
    - name: Setup sonarqube
      uses: warchant/setup-sonar-scanner@v3
      
    - name: Run sonarqube
      env:
        # to get access to secrets.SONAR_TOKEN, provide GITHUB_TOKEN
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: sonar-scanner
           -Dsonar.login=${{ secrets.SONAR_TOKEN }}
           -Dsonar.cfamily.build-wrapper-output=${{ github.workspace }}/build/bw-output
           -Dsonar.coverageReportPaths=build/coverage/sonarqube.report
           -Dsonar.verbose=false
           -Dsonar.host.url=https://sonarcloud.io/
