-
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 |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 6 replies
-
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! |
Beta Was this translation helpful? Give feedback.
-
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
|
Beta Was this translation helpful? Give feedback.
-
Just FYI, this API is not supported for production use. You might want to consider use GraphQL v4 which could achieve the same thing and supported for production use. |
Beta Was this translation helpful? Give feedback.
-
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! |
Beta Was this translation helpful? Give feedback.
-
This worked for me:
|
Beta Was this translation helpful? Give feedback.
-
Thank you so much @sirago !! The same solution with another access way:
Source: https://help.github.com/en/actions/reference/events-that-trigger-workflows |
Beta Was this translation helpful? Give feedback.
-
Still not work for me… tried both way |
Beta Was this translation helpful? Give feedback.
-
Hi @razzinger , please, try this trigger for pull requests to be able to get the pull request number:
I hope it works for you! 🙏 |
Beta Was this translation helpful? Give feedback.
-
Hi @timhaines, To expand on @gimenete's answer about the API endpoint, here's a working snippet to get the issue number in both steps:
- uses: actions/github-script@v6
id: get_issue_number
with:
script: |
if (context.issue.number) {
// Return issue number if present
return context.issue.number;
} else {
// Otherwise return issue number from commit
return (
await github.rest.repos.listPullRequestsAssociatedWithCommit({
commit_sha: context.sha,
owner: context.repo.owner,
repo: context.repo.repo,
})
).data[0].number;
}
result-encoding: string
- name: Issue number
run: echo '${{steps.get_issue_number.outputs.result}}' The script queries the list labels for an issue REST API endpoint via octokit/rest.js client. |
Beta Was this translation helpful? Give feedback.
-
If anyone is wondering how to get a complete, yet working, yml file, here it is: name: 'Your awesome new GA'
on:
push:
branches:
- main
jobs:
pr-number:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
steps:
- uses: actions/github-script@v7
id: get_pr_data
with:
script: |
return (
await github.rest.repos.listPullRequestsAssociatedWithCommit({
commit_sha: context.sha,
owner: context.repo.owner,
repo: context.repo.repo,
})
).data[0];
- name: Pull Request data
run: |
echo '${{ fromJson(steps.get_pr_data.outputs.result).number }}'
echo '${{ fromJson(steps.get_pr_data.outputs.result).title }}' Please notice that both permissions ( |
Beta Was this translation helpful? Give feedback.
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!