Checkout: Any way to checkout PR from `issue_comment` event?

Created on 16 Aug 2020  路  3Comments  路  Source: actions/checkout

The example in the docs here assumes a pull request event, but it does not work when operating on Pull Request comments, since those come through the issue_comment event.

I've spent several hours on this now and I don't see any way to checkout the branch associated with the issue_event.

Complications:

  • The event is the comment and it links to an issue, which only indirectly seems to have data on the pull request url.
  • Checking out the associate sha doesn't give push ability, since the branch name itself is not discoverable (at least not as well as I can tell).
  • Since issue_comment events have to be triggered by the workflow file on the default branch, it seems that is always the branch name provided. I haven't yet found any way to get the actual PR branch, although it seems this should be a straightforward and frequent use case for operating on PR comments.

Most helpful comment

As the required information isn't present in the event payload, you could use the GitHub API to fetch it. The github-script action allows you to write actions in your workflow file, which is pretty convenient:

name: Checkout PR on comment

on:
  issue_comment:
    # triggers on created, edited and deleted by default, you may wanna restrict it, e.g.:
    #types: [created]
jobs:
  pr-commented:
    name: PR commented
    if: github.event.issue.pull_request
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v3
        id: get-pr
        with:
          script: |
            const request = {
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: context.issue.number
            }
            core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`)
            try {
              const result = await github.pulls.get(request)
              return result.data
            } catch (err) {
              core.setFailed(`Request failed with error ${err}`)
            }
      - uses: actions/checkout@v2
        with:
          repository: ${{ fromJSON(steps.get-pr.outputs.result).head.repo.full_name }}
          ref: ${{ fromJSON(steps.get-pr.outputs.result).head.sha }} # or .head.ref for branch name

All 3 comments

The only solution I found was to first use the checkout event and then separately use hub CLI, which is apparently installed by default and which can run hub pr checkout ${pr_num} in order to checkout whatever branch is associated with the PR (after parsing the PR num from the URL).

name: Slash Command CI

on: issue_comment

jobs:
  check_comments:
    runs-on: ubuntu-latest
    name: Check comments for /format
    steps:
      - name: Clone git repo
        uses: actions/checkout@v2
      - name: Checkout Pull Request
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          PR_URL="${{ github.event.issue.pull_request.url }}"
          PR_NUM=${PR_URL##*/}
          echo "Checking out from PR #$PR_NUM based on URL: $PR_URL"
          hub pr checkout $PR_NUM
      - name: Configure Git Agent
        run: |
          git config --global user.email "[email protected]"
          git config --global user.name "Me, Not Really (CI bot)"
      - uses: hashicorp/setup-terraform@v1
        with:
          terraform_version: 0.13.0
      - run: terraform fmt -recursive
      - run: git commit -a -m "GitOps Bot (Autoformat)"
      - run: git push

Is there a better way?

I faced the same problem with one more restriction that github account was not owned by us, I don't know the context that other users will face but in my case I can just send in the branch name through the comment. so, here is what I did:

Basically pull out branch name from comment where branch name is enclosed in [], i.e [branch_name] then extract out the name to pass it to the checkout action. I would love to find better way than this but for now it works ok.

steps:
    - name: Get Branch name
      id: branchName
      run: |
        echo ::set-output name=branch::$(echo $PR_COMMENT | cut -d "[" -f2 | cut -d "]" -f1)
      env:
        PR_COMMENT: ${{ github.event.comment.body }}
    - name: Checkout code
      uses: actions/checkout@v2
      with: 
        ref: ${{ steps.branchName.outputs.branch }}

As the required information isn't present in the event payload, you could use the GitHub API to fetch it. The github-script action allows you to write actions in your workflow file, which is pretty convenient:

name: Checkout PR on comment

on:
  issue_comment:
    # triggers on created, edited and deleted by default, you may wanna restrict it, e.g.:
    #types: [created]
jobs:
  pr-commented:
    name: PR commented
    if: github.event.issue.pull_request
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v3
        id: get-pr
        with:
          script: |
            const request = {
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: context.issue.number
            }
            core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`)
            try {
              const result = await github.pulls.get(request)
              return result.data
            } catch (err) {
              core.setFailed(`Request failed with error ${err}`)
            }
      - uses: actions/checkout@v2
        with:
          repository: ${{ fromJSON(steps.get-pr.outputs.result).head.repo.full_name }}
          ref: ${{ fromJSON(steps.get-pr.outputs.result).head.sha }} # or .head.ref for branch name
Was this page helpful?
0 / 5 - 0 ratings

Related issues

hannesa2 picture hannesa2  路  5Comments

rster2002 picture rster2002  路  7Comments

thewavelength picture thewavelength  路  7Comments

bk2204 picture bk2204  路  3Comments

jcharnley picture jcharnley  路  4Comments