name: Feature CI

on:
  push:
    branches-ignore:
      - master

permissions:
  contents: read

jobs:
  sonarqube:
    name: SonarQube Cloud scan
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Compute Sonar project key
        run: echo "SONAR_PROJECT_KEY=${GITHUB_REPOSITORY/\//_}" >> "$GITHUB_ENV"

      - name: Install scan prerequisites
        run: >
          sudo apt-get update &&
          sudo apt-get install -y
          cmake
          ninja-build
          gcovr
          pkg-config
          libgtk-3-dev
          libwxgtk3.2-dev

      - name: Configure with coverage instrumentation
        run: >
          cmake -S . -B build -G Ninja
          -DCMAKE_C_COMPILER=gcc
          -DCMAKE_CXX_COMPILER=g++
          -DCCM_BUILD_TESTS=ON
          -DCCM_USE_SYSTEM_WX=ON
          -DCMAKE_BUILD_TYPE=Debug
          -DCMAKE_C_FLAGS=--coverage
          -DCMAKE_CXX_FLAGS=--coverage
          -DCMAKE_EXPORT_COMPILE_COMMANDS=ON

      - name: Build and run tests for coverage
        run: >
          cmake --build build --parallel &&
          ctest --test-dir build --output-on-failure

      - name: Generate Sonar coverage report
        run: >
          gcovr -r .
          --sonarqube build/sonarqube-coverage.xml
          --exclude "build/_deps/"
          --exclude-directories "build/_deps"

      - name: SonarQube Cloud scan
        uses: SonarSource/sonarqube-scan-action@v5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        with:
          args: >
            -Dsonar.host.url=https://sonarcloud.io
            -Dsonar.organization=${{ github.repository_owner }}
            -Dsonar.projectKey=${{ env.SONAR_PROJECT_KEY }}
            -Dsonar.sources=core,ui_wx,app
            -Dsonar.cfamily.compile-commands=build/compile_commands.json
            -Dsonar.coverageReportPaths=build/sonarqube-coverage.xml

  linux:
    name: Linux build + tests
    uses: ./.github/workflows/feature-linux.yml

  windows:
    name: Windows build + tests
    uses: ./.github/workflows/feature-windows.yml
