name: Gameyfin CI Pipeline

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

jobs:
  build:
    name: Build, Test & Scan
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, '[ci skip]')"
    steps:        
      - name: Git checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
          
      - name: Set up JDK
        uses: actions/setup-java@v3
        with:
          java-version: '18'
          distribution: 'temurin'

      - name: Cache SonarCloud packages
        uses: actions/cache@v3
        with:
          path: ~/.sonar/cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar

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

      - name: Extract Maven project version
        id: project
        run: echo "GAMEYFIN_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)" >> $GITHUB_OUTPUT
        
      - name: Build and analyze
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=grimsi_gameyfin

      - name: Upload build artifact
        uses: actions/upload-artifact@v3
        with:
          name: gameyfin-${{ steps.project.outputs.GAMEYFIN_VERSION }}.jar
          path: backend/target/gameyfin-*.jar
