---
name: Benchmark CLI - Comment

on:
  issue_comment:
    types: [created, edited]

jobs:
  comment-handler:
    name: Trigger Benchmarks

    runs-on: ubuntu-latest

    steps:
      - name: Handle Incoming Comment
        env:
          DISPATCH_REPO: "benchmarks"
          DISPATCH_OWNER: "npm"
          EVENT_NAME: ${{ github.event_name }}
          EVENT_ACTION: ${{ github.event.action }}
          OWNER: ${{ github.event.repository.owner.login }}
          REPO: ${{ github.event.repository.name }}
          ISSUE_NUMBER: ${{ github.event.issue.number }}
          COMMENT_BODY: ${{ github.event.comment.body }}
          COMMENT_ID: ${{ github.event.comment.id }}
          COMMENT_NODE_ID: ${{ github.event.comment.node_id }}
          COMMENT_ACTIONABLE: ${{ startsWith(github.event.comment.body, 'test this please ✅') }}
          AUTH_TOKEN: ${{ secrets.NPM_DEPLOY_USER_PAT }}
        run: |
          # Comment Handler

          # Creates an exit early condition if there are errors
          # Exit early if `jq` is not present
          set -e
          jq --version

          # Figure out if comment came from pull-request or issue
          IS_PR=$(curl -s https://api.github.com/repos/${OWNER}/${REPO}/issues/${ISSUE_NUMBER} | jq -cr '.pull_request.url')

          if [ "${IS_PR}" != "null" ]; then
            echo "Comment from pull/${ISSUE_NUMBER}."

            # It is a pull-request; check comment body for correct phrase
            if [ "${COMMENT_ACTIONABLE}" == "true" ]; then
              # Fetch pull-request information
              PR_DATA=$(curl -s "${IS_PR}")
              PR_OWNER=$(echo "${PR_DATA}" | jq '.head.repo.owner.login')
              PR_REPO=$(echo "${PR_DATA}" | jq '.head.repo.name')
              PR_COMMIT_SHA=$(curl -s "${IS_PR}/commits" | jq -r '.[0].sha')

              # dispatch request for benchmarks
              echo "Dispatching request..."
              curl \
                -s \
                -X POST https://api.github.com/repos/${DISPATCH_OWNER}/${DISPATCH_REPO}/dispatches \
                -H "Accept: application/vnd.github.everest-preview+json" \
                -H "Authorization: token ${AUTH_TOKEN}" \
                -d \
                '
                {
                  "event_type": "'"${EVENT_NAME}"'",
                  "client_payload": {
                    "pr_id": "'"${ISSUE_NUMBER}"'",
                    "repo": "'"${PR_REPO}"'",
                    "owner": "'"${PR_OWNER}"'",
                    "commit_sha": "'"${PR_COMMIT_SHA}"'"
                  }
                }'

              # Create reaction on comment to confirm dispatch was sent
              curl \
                -s \
                -X POST https://api.github.com/graphql \
                -H "Content-Type: application/json" \
                -H "Authorization: token ${AUTH_TOKEN}" \
                -d \
                '
                {
                  "query": "mutation($inputData:AddReactionInput!) { addReaction(input:$inputData) { reaction { content } } }",
                  "variables": {
                    "inputData": {
                      "subjectId": "'"${COMMENT_NODE_ID}"'",
                      "content": "ROCKET"
                    }
                  }
                }'
            else
              echo "Comment not actionable."
            fi
          else
            echo "Comment not from pull-request."
          fi
