name: SonarQube Analysis
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  sonarqube:
    name: SonarQube Scan
    runs-on: ubuntu-latest
    # This runs the whole job INSIDE the sonar-scanner container
    container:
      image: sonarsource/sonar-scanner-cli:12.0.0.3214_8.0.1
      options: --user root

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

      - name: Run Scan
        run: |
          # Gitea repo name
          REPO_NAME=${{ gitea.event.repository.name }}
          
          # We call the scanner directly since we are already inside its container
          sonar-scanner \
            -Dsonar.projectKey=$REPO_NAME \
            -Dsonar.projectName=$REPO_NAME \
            -Dsonar.sources=. \
            -Dsonar.host.url=${{ secrets.SONARQUBE_HOST }} \
            -Dsonar.token=${{ secrets.SONARQUBE_TOKEN }} \
            -Dsonar.exclusions=node_modules/**,dist/**,coverage/** \
            -Dsonar.qualitygate.wait=true