Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't seem to work on force push. #8

Open
jeacott1 opened this issue Oct 13, 2020 · 5 comments
Open

Doesn't seem to work on force push. #8

jeacott1 opened this issue Oct 13, 2020 · 5 comments

Comments

@jeacott1
Copy link

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.

@ibexa-yuna
Copy link

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

@donovanmuller
Copy link
Contributor

donovanmuller commented Jun 2, 2021

@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 }}

@ibexa-yuna
Copy link

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

That does the trick. Thank you for this advice.

@dungle-scrubs
Copy link

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.

@donovanmuller
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants