name: SonarQube

on:
  schedule:
    - cron: '0 2 * * *'
  workflow_dispatch:

jobs:
  sonarqube:
    runs-on: ubuntu-latest
    container:
      image: gitea.siegeln.net/cameleer/cameleer-build:1
      credentials:
        username: cameleer
        password: ${{ secrets.REGISTRY_TOKEN }}
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Configure Gitea Maven Registry
        run: |
          mkdir -p ~/.m2
          cat > ~/.m2/settings.xml << 'SETTINGS'
          <settings>
            <servers>
              <server>
                <id>gitea</id>
                <username>cameleer</username>
                <password>${env.REGISTRY_TOKEN}</password>
              </server>
            </servers>
          </settings>
          SETTINGS
        env:
          REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}

      - name: Cache Maven dependencies
        uses: actions/cache@v4
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-maven-

      - name: Install UI dependencies
        working-directory: ui
        run: |
          echo '//gitea.siegeln.net/api/packages/cameleer/npm/:_authToken=${REGISTRY_TOKEN}' >> .npmrc
          npm ci
        env:
          REGISTRY_TOKEN: ${{ secrets.REGISTRY_TOKEN }}

      - name: Lint UI
        working-directory: ui
        run: npm run lint -- --format json --output-file eslint-report.json || true

      - name: Build, Test and Analyze
        run: |
          mvn clean verify sonar:sonar -DskipITs -U --batch-mode \
            -Dsonar.host.url=${{ secrets.SONAR_HOST_URL }} \
            -Dsonar.token=${{ secrets.SONAR_TOKEN }} \
            -Dsonar.projectKey=cameleer-server \
            -Dsonar.projectName="Cameleer Server"
