-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Get pull request number from action #58
Comments
You can extract the PR number from the environment variable |
Thanks! I also found this: |
Javascript version "use strict";
const parsePullRequestId = githubRef => {
const result = /refs\/pull\/(\d+)\/merge/g.exec(githubRef);
if (!result) throw new Error("Reference not found.");
const [, pullRequestId] = result;
return pullRequestId;
};
const main = async () => {
const pullRequestId = parsePullRequestId(process.env.GITHUB_REF);
};
main().catch(err => {
console.error(err);
process.exitCode = 1;
}); |
Today I found that the PR number will be missing if you re-run the job for const fs = require('fs')
const ev = JSON.parse(
fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')
)
const prNum = ev.pull_request.number I think it is kind of a bug of GitHub Actions. 😭 |
I also think this is an important feature and it is a shame that this issue is closed "since you can hack your way around it". :( |
In case this is still useful to anybody, a simple shell solution:
|
Did anyone attempt to get the $GITHUB_REF but have a return with no PR number in the output? For example, my output is something like refs/head/master but has no pull number. Would love to see if anyone had a solution for this. |
Replying to myself, I didn't run actions/checkout before ${{github.event.issue.number}}! |
Prior to this commit rerunning the GitHub action would always lead to an error. This was because the GITHUB_REF environment variable exposed by the Github Action does not contain the pull request number when rerunning a job (see [1]). Fixed by now retrieving the pull request number from the Github event JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH environment variable. [1] actions/checkout#58 (comment)
Prior to this commit rerunning the GitHub action would always lead to an error. This was because the GITHUB_REF environment variable exposed by the Github Action does not contain the pull request number when rerunning a job (see [1]). Fixed by now retrieving the pull request number from the Github event JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH environment variable. [1] actions/checkout#58 (comment)
Prior to this commit rerunning the GitHub action would always lead to an error. This was because the GITHUB_REF environment variable exposed by the Github Action does not contain the pull request number when rerunning a job (see [1]). Fixed by now retrieving the pull request number from the Github event JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH environment variable. [1] actions/checkout#58 (comment)
Prior to this commit rerunning the GitHub action would always lead to an error. This was because the GITHUB_REF environment variable exposed by the Github Action does not contain the pull request number when rerunning a job (see [1]). Fixed by now retrieving the pull request number from the Github event JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH environment variable. [1] actions/checkout#58 (comment)
Prior to this commit rerunning the GitHub action would always lead to an error. This was because the GITHUB_REF environment variable exposed by the Github Action does not contain the pull request number when rerunning a job (see [1]). Fixed by now retrieving the pull request number from the Github event JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH environment variable. [1] actions/checkout#58 (comment)
Prior to this commit rerunning the GitHub action would always lead to an error. This was because the GITHUB_REF environment variable exposed by the Github Action does not contain the pull request number when rerunning a job (see [1]). Fixed by now retrieving the pull request number from the Github event JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH environment variable. [1] actions/checkout#58 (comment)
Prior to this commit rerunning the GitHub action would always lead to an error. This was because the GITHUB_REF environment variable exposed by the Github Action does not contain the pull request number when rerunning a job (see [1]). Fixed by now retrieving the pull request number from the Github event JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH environment variable. [1] actions/checkout#58 (comment)
Prior to this commit rerunning the GitHub action would always lead to an error. This was because the GITHUB_REF environment variable exposed by the Github Action does not contain the pull request number when rerunning a job (see [1]). Fixed by now retrieving the pull request number from the Github event JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH environment variable. [1] actions/checkout#58 (comment)
Prior to this commit rerunning the GitHub action would always lead to an error. This was because the GITHUB_REF environment variable exposed by the Github Action does not contain the pull request number when rerunning a job (see [1]). Fixed by now retrieving the pull request number from the Github event JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH environment variable. [1] actions/checkout#58 (comment)
Prior to this commit rerunning the GitHub action would always lead to an error. This was because the GITHUB_REF environment variable exposed by the Github Action does not contain the pull request number when rerunning a job (see [1]). Fixed by now retrieving the pull request number from the Github event JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH environment variable. [1] actions/checkout#58 (comment)
Prior to this commit rerunning the GitHub action would always lead to an error. This was because the GITHUB_REF environment variable exposed by the Github Action does not contain the pull request number when rerunning a job (see [1]). Fixed by now retrieving the pull request number from the Github event JSON (instead of via the GITHUB_REF), via the GITHUB_EVENT_PATH environment variable. [1] actions/checkout#58 (comment)
Is this problem fixed? |
As I understood from the above, because there are several workarounds it's closed with no official solution. |
PR=$(IFS='/' read -r -a REF <<< "$GITHUB_REF" && echo ${REF[2]}) |
Because it's not the first pull request. Instead, it's a |
@kirosc I wrote a small post on retrieving the pull number based on an If you use Not sure if that helps |
One more way from stackoverflow: https://stackoverflow.com/a/69936361/6916890
and usage |
i have run the action/checkout but still i am not getting PR no |
@ar-mohammed it is available via the If the workflow is triggered by a different event, then you can use the That is what the above example is doing, followed by requesting the JSON field |
It looks like it doesn't support comment-triggering events - I found this jwalton/gh-find-current-pr#23 |
Hi @estensen, To expand on @polarathene'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. |
I've created an action that works for both |
The below worked for me: |
This is problematic because the
I want this pattern to only match the last case. So I added a pattern to the last rule:
|
I'm trying to create an action that is triggered on a PR and tags a specified reviewer. For that I'm creating an action which uses the GitHub API to create a review request. To create this request I need the pull request number. The action is triggered by a PR, but I can't find the pull request number in the action logs.
The text was updated successfully, but these errors were encountered: