GitHub Actions - no way to get PR number on push event?

Hey there,

I’d like to get the commit SHA, branch, and pull request number on a push event.

I’ve found GITHUB_SHA and GITHUB_REF in the environment variables.  I don’t see how to get the pull request number though.  It looks like the push event payload doesn’t include the pull request number? https://developer.github.com/v3/activity/events/types/#pushevent

Is there some other way to get the pull request number from the environment?

Thanks,

Tim

1 Like

Hi @timhaines 

I think you could get the pull request information using this recent endpoint https://developer.github.com/v3/repos/commits/#list-pull-requests-associated-with-commit using the SHA that you are getting in the payload.

Let me know if that works for you!

6 Likes

It works! I used squash-and-merge to merge a PR, and you are able to use the SHA-1 of the squash-merged commit on master to get its PR nuber, which I didn’t expect to work.


$ curl -s -H "Accept: application/vnd.github.groot-preview+json" \

    [https://api.github.com/repos/brewsci/homebrew-bio/commits/fb4477fa762eaef76b9e1508c7f6d58dd9176c94/pulls](https://api.github.com/repos/brewsci/homebrew-bio/commits/fb4477fa762eaef76b9e1508c7f6d58dd9176c94/pulls) \

    | jq -r '.[].number'

879

1 Like

Just FYI, this API is not supported for production use.
Quote from https://developer.github.com/v3/repos/commits/#list-pull-requests-associated-with-commit 
_ Warning:  The API may change without advance notice during the preview period. Preview features are not supported for production use. If you experience any issues, contact GitHub Support or GitHub Premium Support__._

You might want to consider use GraphQL v4 which could achieve the same thing and supported for production use. 

Thanks @gimenete !! However, I think it only works for the first commit of a pull request because it’s not possible to find pull requests for the next GITHUB_SHA. After managing it with Git commands to find the first commit I realized that GITHUB_REF had the format refs/pull/<PULL_REQUEST_NUMBER>/merge so I got the number from it. Please, I think it would be interesting to have an environment variable from GITHUB such as GITHUB_PULL_REQUEST like TRAVIS_PULL_REQUEST on Travis CI. Thank you so much!

2 Likes

This worked for me:

run: |
    echo "::set-env name=PR_NUMBER::$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")"
1 Like

Thank you so much @sirago !! The same solution with another access way:

${{ github.event.pull_request.number }}

Source: https://help.github.com/en/actions/reference/events-that-trigger-workflows