gh-find-current-pr icon indicating copy to clipboard operation
gh-find-current-pr copied to clipboard

Doesn't seem to work on force push.

Open jeacott1 opened this issue 4 years ago • 5 comments

if create a PR for a branch, and then I force push to that branch associated with the PR, the push event rather than the pull_request event occurs as expected, and the PR remains open for the branch as expected, but this action doesn't seem to return any associated PR.

jeacott1 avatar Oct 13 '20 00:10 jeacott1

Bumping old issue: seems not working on synchronize event, but for me, it's just the push to the branch associated with PR.

ibexa-yuna avatar Jun 02 '21 14:06 ibexa-yuna

@ibexa-yuna the issue here is that the github.sha value (which is the default unless specified) is the sha of the merge commit (default behaviour of actions/checkout@v2) and not the head commit of the PR branch.

See output below from a workflow of mine that illustrates:

> Checking out the ref
  /usr/bin/git checkout --progress --force refs/remotes/pull/343/merge
  Note: switching to 'refs/remotes/pull/343/merge'.
  
  You are in 'detached HEAD' state. You can look around, make experimental
  changes and commit them, and you can discard any commits you make in this
  state without impacting any branches by switching back to a branch.
  
  If you want to create a new branch to retain commits you create, you may
  do so (now or later) by using -c with the switch command. Example:
  
    git switch -c <new-branch-name>
  
  Or undo this operation with:
  
    git switch -
  
  Turn off this advice by setting config variable advice.detachedHead to false
  
  HEAD is now at 35bb45e Merge 27009864972d275ca87043ba9b9b20b0fcd861ad into 4f38e977a4a6405fc8cc2adf55e4bd22a748c2dd
/usr/bin/git log -1 --format='%H'
'35bb45eb8a88f013f5d7b7f7d7749120e28a6d5e'
---
Run echo "SHA: 35bb45eb8a88f013f5d7b7f7d7749120e28a6d5e"
SHA: 35bb45eb8a88f013f5d7b7f7d7749120e28a6d5e

from a workflow Job like:

jobs:
  debug:
    name: Debug
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: debug-sha
        run: |
          echo "SHA: ${{ github.sha }}"

The sha that will bring back the corresponding PR it belongs too in the above example is 27009864972d275ca87043ba9b9b20b0fcd861ad but the github.sha value is actually the merge commit 35bb45eb8a88f013f5d7b7f7d7749120e28a6d5e. That merge commit sha brings back no results from octokit.rest.repos.listPullRequestsAssociatedWithCommit.

Instead, use the head sha off the PR event to get the related PR number:

      - name: Get Pull Request
        id: pr
        uses: jwalton/[email protected]
        with:
          sha: ${{ github.event.pull_request.head.sha }}

donovanmuller avatar Jun 02 '21 18:06 donovanmuller

Instead, use the head sha off the PR event to get the related PR number:

That does the trick. Thank you for this advice.

ibexa-yuna avatar Jun 02 '21 19:06 ibexa-yuna

Instead, use the head sha off the PR event to get the related PR number:

      - name: Get Pull Request
        id: pr
        uses: jwalton/[email protected]
        with:
          sha: ${{ github.event.pull_request.head.sha }}

Thanks @donovanmuller, is this still the correct approach?

When I have a workflow triggered by a push event, upon inspecting ${{ github.event }}, there is no pull_request object from which to grab the corresponding sha.

I'd like to update some comments in a related PR after the branch has been successfully merged and deployed.

dungle-scrubs avatar Dec 06 '21 07:12 dungle-scrubs

@dungle-scrubs I currently use this action successfully like so:

- name: Get Pull Request
  if: github.event_name == 'pull_request'
  uses: jwalton/[email protected]
  with:
    sha: ${{ github.event.pull_request.head.sha }}

I can only assume that you push is to a branch not related to an open Pull Request?

Quite difficult to say without context but I would recommend cloning this repo and running it locally (adjusting configs accordingly) and seeing what API call to octokit.rest.repos.listPullRequestsAssociatedWithCommit returns and debug from there.

donovanmuller avatar Dec 08 '21 08:12 donovanmuller