question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any way to checkout PR from `issue_comment` event?

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:35
  • Comments:19

github_iconTop GitHub Comments

18reactions
Simran-Bcommented, Oct 12, 2020

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
13reactions
mareksuscakcommented, Sep 22, 2021

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). Is there a better way?

@aaronsteers do you even need to parse it? I though the following should do:

hub pr checkout ${{ github.event.issue.number }}

This has worked for us!

deploy:
  runs-on: ubuntu-latest
  steps:
    # Checkout source code
    - name: Checkout
      uses: actions/checkout@v2
    - name: Checkout Pull Request
      run: hub pr checkout ${{ github.event.issue.number }}
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Read more comments on GitHub >

github_iconTop Results From Across the Web

GitHub action for issue_comment doesn't shown in checks for ...
Basically you need to checkout to the PR origin. For that, first make a API request to the pr url and fetch all...
Read more >
Run GitHub Actions on Issue Comments - PäksTech
Checkout the correct commit. Since the issue comment event runs against the main branch we need to figure out a way to checkout...
Read more >
How To Checkout Git Pull Request In Three Easy Steps
First, get the pull request number from the Pull request details. It you are using Github, you can easily get the pull request...
Read more >
octokit/rest.js
We recommend to use the search above to find the endpoint method you are looking ... Authentication is optional for some REST API...
Read more >
544815 – Support Github pull request integration ... - Bugs
There is already code for some PR-related commands (checkout PR, merge PR, ... namespace is read-only, so you can't push new commits onto...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found