name: SonarCloud Analysis

on:
  push:
    branches:
      - main
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  sonarcloud:
    name: SonarCloud
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          fetch-depth: 0

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

      - name: Cache cargo
        uses: Swatinem/rust-cache@v2

      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov

      - name: Run Clippy
        run: |
          cargo clippy --workspace --all-features --message-format=json > clippy-report.json

      - name: Run tests with coverage
        run: |
          cargo llvm-cov \
            --workspace \
            --all-features \
            --lcov \
            --output-path lcov.info

      - name: SonarCloud scan
        uses: SonarSource/sonarqube-scan-action@v8
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        with:
          args: >
            -Dsonar.rust.clippy.reportPaths=clippy-report.json
            -Dcommunity.rust.lcov.reportPaths=lcov.info
