name: Muzzle

on:
  workflow_call:

jobs:
  setup-matrix:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v3

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

      - name: List instrumentations to file
        uses: gradle/gradle-build-action@v2
        with:
          arguments: instrumentation:listInstrumentations
          cache-read-only: true

      - id: set-matrix
        run: echo "::set-output name=matrix::{\"module\":[\"$(cat instrumentation-list.txt | xargs echo | sed 's/ /","/g')\"]}"

  run:
    needs: setup-matrix
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJson(needs.setup-matrix.outputs.matrix)}}
      fail-fast: false
    steps:
      - uses: actions/checkout@v3

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

      # don't use gradle-build-action because all the parallel muzzle jobs cause the cache service
      # to respond with 429 (to both muzzle and other jobs running in parallel)

      - name: Run muzzle
        # using retry because of sporadic gradle download failures
        uses: nick-invision/retry@v2.6.0
        with:
          # timing out has not been a problem, these jobs typically finish in 2-3 minutes
          timeout_minutes: 15
          # give maven central some time to hopefully recover
          retry_wait_seconds: 120
          max_attempts: 5
          command: ./gradlew ${{ matrix.module }}:muzzle
