name: Octokit (heuristics)

on:
  issue_comment:
    types: [created]

jobs:
  test1: 
    if: github.event.comment.body == '@metabase-bot run visual tests'
    runs-on: ubuntu-24.04
    steps:
      - name: Fetch issue
        uses: octokit/request-action@v2.x
        id: fetch_issue
        with:
          route: GET ${{ github.event.issue.url }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Fetch PR
        uses: octokit/request-action@v2.x
        id: fetch_pr
        with:
          route: GET ${{ fromJson(steps.fetch_issue.outputs.data).pull_request.url }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - uses: actions/checkout@v4
        with:
          ref: ${{ fromJson(steps.fetch_pr.outputs.data).head.ref }}
          token: ${{ secrets.GITHUB_TOKEN }}
      - uses: actions/checkout@v4
        with:
          ref: ${{ fromJson(steps.fetch_pr.outputs.data).head.sha }}
          token: ${{ secrets.GITHUB_TOKEN }}

  test2:
    runs-on: ubuntu-latest
    steps:
      - name: Get Info from comment
        uses: actions/github-script@v7
        id: get-pr-info
        with:
          script: |
            const request = {
                owner: context.repo.owner,
                repo: context.repo.repo,
                pull_number: ${{ github.event.issue.number }},
            }
            core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`)
            const pr = await github.rest.pulls.get(request);
            return pr.data;

      - name: Debug
        id: get-sha
        run: |
          echo "sha=${{ fromJSON(steps.get-pr-info.outputs.result).head.sha }}" >> $GITHUB_OUTPUT

      - name: "Checkout ${{ github.ref }} ( ${{ github.sha }} : ${{steps.get-sha.outputs.sha}} )"
        uses: actions/checkout@v4
        with:
          ref: ${{ steps.get-sha.outputs.sha }}

  test3:
    if: github.event.comment.body == '@excalibot trigger release' && github.event.issue.pull_request
    runs-on: ubuntu-latest
    steps:
      - name: Get PR SHA
        id: sha
        uses: actions/github-script@v4
        with:
          result-encoding: string
          script: |
            const { owner, repo, number } = context.issue;
            const pr = await github.pulls.get({
              owner,
              repo,
              pull_number: number,
            });
            return pr.data.head.sha
      - uses: actions/checkout@v2
        with:
          ref: ${{ steps.sha.outputs.result }}

  test4:
    if: github.event.issue.pull_request && contains(github.event.comment.body, '!bench_parser')
    runs-on: ubuntu-latest
    steps:
      - name: Get PR SHA
        id: sha
        uses: actions/github-script@v6
        with:
          result-encoding: string
          script: |
            const response = await github.request(context.payload.issue.pull_request.url);
            return response.data.head.sha;
      - name: Checkout PR Branch
        uses: actions/checkout@v3
        with:
          ref: ${{ steps.sha.outputs.result }}

  test5:
    runs-on: ubuntu-20.04
    steps:
      - id: request
        uses: octokit/request-action@v2.0.2
        with:
          route: ${{ github.event.issue.pull_request.url }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Checkout PR Branch
        uses: actions/checkout@v3
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          repository: ${{fromJson(steps.request.outputs.data).head.repo.full_name}}
          ref: ${{fromJson(steps.request.outputs.data).head.ref}}
