name: SonarQube Analysis

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  workflow_dispatch:

jobs:
  analyze:
    name: Build and Analyze
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:18.4
        env:
          POSTGRES_USER: vaessl
          POSTGRES_PASSWORD: vaessl
          POSTGRES_DB: vaessl_test
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

    steps:
      - name: Checkout Code
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
        with:
          fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis

      - name: Set up JDK 25
        uses: actions/setup-java@c1e323688fd81a25caa38c78aa6df2d33d3e20d9 # v4.8.0
        with:
          java-version: "25"
          distribution: "zulu" # Alternative distribution options are available.

      - name: Make Gradle Executable
        run: chmod +x ./gradlew
        working-directory: backend

      - name: Build and Analyze
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}

          DB_URL: jdbc:postgresql://postgres:5432/vaessl_test
          DB_TEST_URL: jdbc:postgresql://postgres:5432/vaessl_test
          DB_USERNAME: vaessl
          DB_PASSWORD: vaessl
          PG_DRIVER_CLASS_NAME: org.postgresql.Driver

          OPENAI_BASE_URL: https://api.openai.com/v1
          OPENAI_KEY: ${{ secrets.OPENAI_KEY }}
          ALLOWED_ORIGINS: http://localhost:3000
        run: ./gradlew build sonar --info
        working-directory: backend
