name: Pre Merge Checks

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

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@v2
    - name: Setup Java
      uses: actions/setup-java@v2
      with:
        java-version: ${{ matrix.jdk }}
        distribution: 'temurin'
    - name: Build detekt
      uses: gradle/gradle-build-action@v2
      with:
        arguments: build -x detekt
    - uses: actions/upload-artifact@v2
      with:
        name: heap-dump
        path: '**.hprof'
        if-no-files-found: ignore
    - name: Run detekt-cli --help
      uses: gradle/gradle-build-action@v2
      with:
        arguments: --configuration-cache :detekt-cli:runWithHelpFlag
    - name: Run detekt-cli with argsfile
      uses: gradle/gradle-build-action@v2
      with:
        arguments: --configuration-cache :detekt-cli:runWithArgsFile

  verify-generated-config-file:
    if: ${{ !contains(github.event.head_commit.message, 'ci skip') }}
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Repo
      uses: actions/checkout@v2
    - name: Setup Java
      uses: actions/setup-java@v2
      with:
        java-version: 17
        distribution: 'temurin'
    - name: Verify Generated Detekt Config File
      uses: gradle/gradle-build-action@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@v2
      - name: Setup Java
        uses: actions/setup-java@v2
        with:
          java-version: 17
          distribution: 'temurin'
      - name: Build and compile test snippets
        uses: gradle/gradle-build-action@v2
        with:
          arguments: --configuration-cache test -x :detekt-gradle-plugin:test -Pcompile-test-snippets=true
