name: PR build

on: pull_request

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up JDK 11 for running checks
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - name: Restore cache
        uses: burrunan/gradle-cache-action@v1.6
        with:
          job-id: jdk11

      - name: Start deadlock detector
        run: .github/scripts/deadlock-detector.sh

      - name: Build
        run: ./gradlew check --stacktrace -x :smoke-tests:test

      - name: Upload deadlock detector artifacts
        if: always()
        uses: actions/upload-artifact@v2
        with:
          name: deadlock-detector-build
          path: /tmp/deadlock-detector-*
          if-no-files-found: ignore

  example-distro:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up JDK 11 for running checks
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - name: Restore cache
        uses: burrunan/gradle-cache-action@v1.6
        with:
          job-id: jdk11

      - name: Build
        run: ./gradlew build --stacktrace
        working-directory: examples/distro

  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        java: [ 8, 15 ]
      fail-fast: false
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - id: setup-test-java
        name: Set up JDK ${{ matrix.java }} for running tests
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java }}

      - name: Set up JDK 11 for running Gradle
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - name: Restore cache
        uses: burrunan/gradle-cache-action@v1.6
        with:
          job-id: jdk${{ matrix.java }}

      - name: Start deadlock detector
        run: .github/scripts/deadlock-detector.sh

      - name: Test
        run: ./gradlew test -PtestJavaVersion=${{ matrix.java }} --stacktrace -x :smoke-tests:test -Porg.gradle.java.installations.paths=${{ steps.setup-test-java.outputs.path }} -Porg.gradle.java.installations.auto-download=false

      - name: Upload deadlock detector artifacts
        if: always()
        uses: actions/upload-artifact@v2
        with:
          name: deadlock-detector-test-${{ matrix.java }}
          path: /tmp/deadlock-detector-*
          if-no-files-found: ignore

  smoke-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Set up JDK 11 for running Gradle
        uses: actions/setup-java@v1
        with:
          java-version: 11

      - name: Restore cache
        uses: burrunan/gradle-cache-action@v1.6
        with:
          job-id: smokeTests

      - name: Test
        run: ./gradlew :smoke-tests:test

  setup-muzzle-matrix:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - name: Check out repository
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Set up JDK 11 for running Gradle
        uses: actions/setup-java@v1
        with:
          java-version: 11
      - id: set-matrix
        run: echo "::set-output name=matrix::{\"module\":[\"$(./gradlew -q instrumentation:listInstrumentations | xargs echo | sed 's/ /","/g')\"]}"

  muzzle:
    needs: setup-muzzle-matrix
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJson(needs.setup-muzzle-matrix.outputs.matrix)}}
      fail-fast: false
    steps:
      - name: Check out repository
        uses: actions/checkout@v2
      - name: Set up JDK 11 for running Gradle
        uses: actions/setup-java@v1
        with:
          java-version: 11
      - name: Run  muzzle
        run: ./gradlew ${{ matrix.module }}:muzzle --no-daemon

  accept-pr:
    needs: [ build, test, smoke-test, muzzle ]
    runs-on: ubuntu-latest
    if: always()
    steps:
      # run this action to get workflow conclusion
      # You can get conclusion by env (env.WORKFLOW_CONCLUSION)
      - uses: technote-space/workflow-conclusion-action@v1
      - name: Fail build
        if: env.WORKFLOW_CONCLUSION == 'failure' # notify only if failure
        run: exit 1