name: Benchmark - CLI

on:
  pull_request:
    branches:
      - '*'
  issue_comment:
    types:
      - created
      - edited

jobs:
  pull-request:
    runs-on: ubuntu-latest
    steps:
      - name: Incoming Pull Request
        if: |
          secrets.NPM_BENCHMARKS_TOKEN && (
          github.event_name == 'pull_request' ||
          (github.event_name == 'issue_comment' && github.event.issue.pull_request &&
          startsWith(github.event.comment.body, 'test this please ✅')))
        env:
          # gh cli uses these env vars for owner/repo/token
          GH_REPO: "npm/benchmarks"
          GITHUB_TOKEN: ${{ secrets.NPM_BENCHMARKS_TOKEN }}
        run: |
          EVENT_NAME="${{ github.event_name }}"
          OWNER="${{ github.event.repository.owner.login }}"
          REPO="${{ github.event.repository.name }}"

          if [[ "$EVENT_NAME" == "pull_request" ]]; then
            PR="${{ github.event.pull_request.number }}"
          else
            PR="${{ github.event.issue.number }}"
          fi

          EVENT="${EVENT_NAME} ${OWNER}/${REPO}#${PR}"
          echo '{
            "event_type": "'"$EVENT"'",
            "client_payload": {
              "pr_id": "'"$PR"'",
              "repo": "'"$REPO"'",
              "owner": "'"$OWNER"'"
            }
          }' | gh api repos/{owner}/{repo}/dispatches --input -

  comment:
    runs-on: ubuntu-latest
    steps:
      - name: Incoming Comment
        if: |
          secrets.NPM_BENCHMARKS_TOKEN &&
          github.event_name == 'issue_comment' &&
          github.event.issue.pull_request &&
          startsWith(github.event.comment.body, 'test this please ✅')
        env:
          # gh cli uses this env var as the token
          GITHUB_TOKEN: ${{ secrets.NPM_BENCHMARKS_TOKEN }}
        run: |
          COMMENT_NODE_ID="${{ github.event.comment.node_id }}"
          QUERY='mutation ($inputData:AddReactionInput!) {
            addReaction (input:$inputData) {
              reaction { content }
            }
          }'
          echo '{
            "query": "'${QUERY}'",
            "variables": {
              "inputData": {
                "subjectId": "'"${COMMENT_NODE_ID}"'",
                "content": "ROCKET"
              }
            }
          }' | gh api graphql --input -

