name: Pre Merge Checks

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - '**'

env:
  GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
  GRADLE_CACHE_USERNAME: ${{ secrets.GRADLE_CACHE_USERNAME }}
  GRADLE_CACHE_PASSWORD: ${{ secrets.GRADLE_CACHE_PASSWORD }}

permissions:
  contents: read

jobs:
  gradle:
    if: ${{ !contains(github.event.head_commit.message, 'ci skip') }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        jdk: [8, 11, 17]
        exclude: # windows with JDK8 are *really* flaky
          - os: windows-latest
            jdk: 8
    runs-on: ${{ matrix.os }}
    steps:
    - name: Checkout Repo
      uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3
    - name: Setup Java
      uses: actions/setup-java@16cca5479d7c6b6843f6a6515640ba33c6501543 # tag=v3
      with:
        java-version: ${{ matrix.jdk }}
        distribution: 'temurin'
    - name: Build detekt
      uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 # tag=v2
      with:
        arguments: build -x detekt
    - uses: actions/upload-artifact@3cea5372237819ed00197afe530f5a7ea3e805c8 # tag=v3
      with:
        name: heap-dump
        path: '**.hprof'
        if-no-files-found: ignore
    - name: Run detekt-cli --help
      uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 # tag=v2
      with:
        arguments: :detekt-cli:runWithHelpFlag
    - name: Run detekt-cli with argsfile
      uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 # tag=v2
      with:
        arguments: :detekt-cli:runWithArgsFile
    - name: Try to publish to Maven Local
      uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 # tag=v2
      with:
        arguments: publishToMavenLocal

  verify-generated-config-file:
    if: ${{ !contains(github.event.head_commit.message, 'ci skip') }}
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Repo
      uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3
    - name: Setup Java
      uses: actions/setup-java@16cca5479d7c6b6843f6a6515640ba33c6501543 # tag=v3
      with:
        java-version: 17
        distribution: 'temurin'
    - name: Verify Generated Detekt Config File
      uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 # tag=v2
      with:
        arguments: verifyGeneratorOutput

  compile-test-snippets:
    if: ${{ !contains(github.event.head_commit.message, 'ci skip') }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3
      - name: Setup Java
        uses: actions/setup-java@16cca5479d7c6b6843f6a6515640ba33c6501543 # tag=v3
        with:
          java-version: 17
          distribution: 'temurin'
      - name: Build and compile test snippets
        uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 # tag=v2
        with:
          arguments: test -Pcompile-test-snippets=true

  warnings-as-errors:
    if: ${{ !contains(github.event.head_commit.message, 'ci skip') }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3
      - name: Setup Java
        uses: actions/setup-java@16cca5479d7c6b6843f6a6515640ba33c6501543 # tag=v3
        with:
          java-version: 17
          distribution: 'temurin'
      - name: Run with allWarningsAsErrors
        uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 # tag=v2
        with:
          arguments: build -x detekt -PwarningsAsErrors=true
