---
name: Benchmark Suite

on:
  pull_request:
    branches:
      - "**"

jobs:
  build:
    name: Trigger Benchmarks

    runs-on: ubuntu-latest

    steps:
      - name: Handle Incoming Pull-Request
        env:
          DISPATCH_REPO: "benchmarks"
          DISPATCH_OWNER: "npm"
          EVENT_NAME: ${{ github.event_name }}
          REPO: ${{ github.event.repository.name }}
          PR_NUMBER: ${{ github.event.pull_request.number }}
          PR_OWNER: ${{ github.event.pull_request.head.repo.owner.login }}
          AUTH_TOKEN: ${{ secrets.NPM_BENCHMARKS_TOKEN }}
        run: |
          # Dispatch Handler

          dispatch_request () {
            echo "Dispatching request..."
            curl \
              -s \
              -X POST https://api.github.com/repos/${DISPATCH_OWNER}/${DISPATCH_REPO}/dispatches \
              -H "Accept: application/vnd.github.v3+json" \
              -H "Authorization: token ${AUTH_TOKEN}" \
              -d \
              '
              {
                "event_type": "'"${EVENT_NAME} ${PR_OWNER}/${REPO}#${PR_NUMBER}"'",
                "client_payload": {
                  "pr_id": "'"${PR_NUMBER}"'",
                  "repo": "'"${REPO}"'",
                  "owner": "'"${PR_OWNER}"'"
                }
              }'
          }

          if [ "${AUTH_TOKEN}" != "" ]; then
            # Dispatch request for benchmarks
            dispatch_request
          else
            echo "NO AUTH - FORK PULL REQUEST"
          fi
