name: PR sonar report

on:
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  sonar:
    name: Run SonarQube analysis
    runs-on: ubuntu-latest

    permissions:
      contents: read
      pull-requests: write
      issues: write

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "npm"

      - name: Install dependencies
        run: npm ci

      - name: Install sonar-scanner
        run: npm install -g sonar-scanner

      - name: Run Sonar analysis and post to PR
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
          GITHUB_REPOSITORY: ${{ github.repository }}
        run: |
          bash .wednesday/scripts/pr-sonar.sh --post ${{ github.base_ref }}
