name: CI

on:
  push:
    branches:
      - main
      - renovate/**

  pull_request:
    branches:
      - main

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  check:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
      with:
        fetch-depth: 0

    - name: Set up JDK
      uses: actions/setup-java@387ac29b308b003ca37ba93a6cab5eb57c8f5f93 # v4.0.0
      with:
        distribution: temurin
        java-version: 18

    - name: Run unit tests
      uses: gradle/gradle-build-action@982da8e78c05368c70dac0351bb82647a9e9a5d2 # v2.11.1
      with:
        arguments: --no-configuration-cache --stacktrace check -PslimTests
        gradle-home-cache-cleanup: true
        cache-read-only: ${{ github.ref != 'refs/heads/main' }}
        dependency-graph: generate-and-submit

    - name: (Fail-only) Upload test report
      if: failure()
      uses: actions/upload-artifact@1eb3cb2b3e0f29609092a73eb033bb759a334595 # v4.1.0
      with:
          name: Test report
          path: '**/build/reports/tests/**'

  dependency-review:
    runs-on: ubuntu-latest
    needs:
      - check
    steps:
      - name: Checkout repository
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

      - name: Dependency Review
        uses: actions/dependency-review-action@4cd9eb2d23752464a87e00499c30d256a59a01b4 # v4
        with:
          base-ref: refs/heads/main
          head-ref: ${{ github.ref }}

  validate-changelog:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

      - name: Check changelog format
        uses: mindsers/changelog-reader-action@b97ce03a10d9bdbb07beb491c76a5a01d78cd3ef # v2.2.2
        with:
          path: ./CHANGELOG.md
          validation_level: error

  deploy-release-snapshot:
    runs-on: ubuntu-latest
    if: "github.event_name == 'push' && github.event.ref == 'refs/heads/main'"
    needs:
      - check
      - validate-changelog
      - dependency-review
    steps:
    - name: Checkout repository
      uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

    - name: Set up JDK
      uses: actions/setup-java@387ac29b308b003ca37ba93a6cab5eb57c8f5f93 # v4.0.0
      with:
        distribution: temurin
        java-version: 18

    - name: Decrypt secrets
      run: |
        ./scripts/setup-age.sh
        ./scripts/signing-setup.sh "$AGE_SECRET_KEY"
      env:
        AGE_SECRET_KEY: ${{ secrets.AGE_SECRET_KEY }}

    - name: Build release app
      uses: gradle/gradle-build-action@982da8e78c05368c70dac0351bb82647a9e9a5d2 # v2.11.1
      env:
        SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
      with:
        arguments: --no-configuration-cache --stacktrace collectReleaseApks
        gradle-home-cache-cleanup: true
        cache-read-only: ${{ github.ref != 'refs/heads/main' }}

    - name: Clean secrets
      run: scripts/signing-cleanup.sh

    - name: Deploy snapshot
      run: scripts/deploy-snapshot.sh
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
