name: Pre Merge Checks

on:
  push:
    branches:
      - master
  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, 14]
    runs-on: ${{ matrix.os }}
    env:
      JDK_VERSION:  ${{ matrix.jdk }}
      GRADLE_OPTS: -Dorg.gradle.daemon=false
    steps:
    - name: Checkout Repo
      uses: actions/checkout@v2

      # Let's cleanup the gradle cache folders to make sure
      # we don't accidentally cache stale files.
    - name: Cleanup Gradle Folders
      shell: bash
      run: |
        rm -rf ~/.gradle/caches/ && \
        rm -rf ~/.gradle/wrapper/


    - name: Cache Gradle Folders
      uses: actions/cache@v2
      with:
        path: |
          ~/.gradle/caches/
          ~/.gradle/wrapper/
        key: cache-gradle-${{ matrix.os }}-${{ matrix.jdk }}-${{ hashFiles('detekt-bom/build.gradle.kts') }}
        restore-keys: |
          cache-gradle-${{ matrix.os }}-${{ matrix.jdk }}-
          cache-gradle-${{ matrix.os }}-
          cache-gradle-


    - name: Setup Java
      uses: actions/setup-java@v1
      with:
        java-version: ${{ matrix.jdk }}


    - name: Build detekt
      run: ./gradlew build moveJarForIntegrationTest shadowJarExecutable --parallel -x detekt
    - name: Run detekt-cli --help
      run: java -jar ./detekt-cli/build/run/detekt --help
    - name: Run detekt-cli with argsfile
      run: java -jar ./detekt-cli/build/run/detekt "@./config/detekt/argsfile"


  verify-documentation:
    if: ${{ !contains(github.event.head_commit.message, 'ci skip') }}
    runs-on: ubuntu-latest
    env:
      GRADLE_OPTS: -Dorg.gradle.daemon=false
    steps:
    - name: Checkout Repo
      uses: actions/checkout@v2
    - name: Cache Gradle Folders
      uses: actions/cache@v2
      with:
        path: |
          ~/.gradle/caches/
          ~/.gradle/wrapper/
        key: cache-gradle-ubuntu-latest-14-verifygenerator-${{ hashFiles('detekt-bom/build.gradle.kts') }}
        restore-keys: |
          cache-gradle-ubuntu-latest-14-verifygenerator-
          cache-gradle-ubuntu-latest-14-
          cache-gradle-ubuntu-latest-
          cache-gradle-
    - name: Setup Java
      uses: actions/setup-java@v1
      with:
        java-version: 14
    - name: Verify Generator Output
      run: ./gradlew verifyGeneratorOutput


  compile-test-snippets:
    if: ${{ !contains(github.event.head_commit.message, 'ci skip') }}
    runs-on: ubuntu-latest
    env:
      GRADLE_OPTS: -Dorg.gradle.daemon=false
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v2
      - name: Cache Gradle Folders
        uses: actions/cache@v2
        with:
          path: |
            ~/.gradle/caches/
            ~/.gradle/wrapper/
          key: cache-gradle-ubuntu-latest-14-compiletestsnippets-${{ hashFiles('detekt-bom/build.gradle.kts') }}
          restore-keys: |
            cache-gradle-ubuntu-latest-14-compiletestsnippets-
            cache-gradle-ubuntu-latest-14-
            cache-gradle-ubuntu-latest-
            cache-gradle-
      - name: Setup Java
        uses: actions/setup-java@v1
        with:
          java-version: 14
      - name: Build and compile test snippets
        run: ./gradlew test -x ":detekt-gradle-plugin:test" -Pcompile-test-snippets=true --parallel
