name: Heuristic based

on:
  issue_comment:
    types: [created]

jobs:
  test1:
    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 }}

  test2:
    runs-on: ubuntu-latest

    steps:
    - name: Detect branch for PR
      id: vars
      run: |
        PR=$( echo "${{ github.event.comment.issue_url }}" | grep -oE 'issues/([0-9]+)$' | cut -d'/' -f 2 )
        PR_INFO=$( curl \
                 --request GET \
                 --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
                 --header 'content-type: application/json' \
                 --url https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$PR )
        REF=$(echo "${PR_INFO}" | jq -r .head.ref)
        echo "branch=$REF" >> $GITHUB_OUTPUT
    - uses: actions/checkout@v4
      with:
        ref: ${{ steps.vars.outputs.branch }}
