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

Get pull request number from action #58

Closed
estensen opened this issue Oct 23, 2019 · 28 comments
Closed

Get pull request number from action #58

estensen opened this issue Oct 23, 2019 · 28 comments

Comments

@estensen
Copy link

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.

@peter-evans
Copy link
Contributor

You can extract the PR number from the environment variable GITHUB_REF. For on: pull_request workflows GITHUB_REF takes the format refs/pull/:prNumber/merge.

ref: https://help.github.com/en/github/automating-your-workflow-with-github-actions/events-that-trigger-workflows#pull-request-event-pull_request

@estensen
Copy link
Author

Thanks! I also found this:
pull_number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")

@n1ru4l
Copy link

n1ru4l commented Jan 21, 2020

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;
});

@littlebtc
Copy link

Today I found that the PR number will be missing if you re-run the job for on: pull_request workflow. The only reliable way to get it is through $GITHUB_EVENT_PATH.

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

@jpfeuffer
Copy link

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". :(

@salvamomo
Copy link

In case this is still useful to anybody, a simple shell solution:

PR_NUMBER=$(echo $GITHUB_REF | awk 'BEGIN { FS = "/" } ; { print $3 }')

@leeyspaul
Copy link

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.

@leeyspaul
Copy link

leeyspaul commented May 6, 2020

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

johnboyes added a commit to agilepathway/label-checker that referenced this issue May 7, 2020
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)
johnboyes added a commit to agilepathway/label-checker that referenced this issue May 7, 2020
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)
johnboyes added a commit to agilepathway/label-checker that referenced this issue May 7, 2020
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)
johnboyes added a commit to agilepathway/label-checker that referenced this issue May 7, 2020
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)
johnboyes added a commit to agilepathway/label-checker that referenced this issue May 7, 2020
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)
johnboyes added a commit to agilepathway/label-checker that referenced this issue May 7, 2020
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)
johnboyes added a commit to agilepathway/label-checker that referenced this issue May 7, 2020
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)
johnboyes added a commit to agilepathway/label-checker that referenced this issue May 7, 2020
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)
johnboyes added a commit to agilepathway/label-checker that referenced this issue May 7, 2020
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)
johnboyes added a commit to agilepathway/label-checker that referenced this issue May 7, 2020
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)
johnboyes added a commit to agilepathway/label-checker that referenced this issue May 7, 2020
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)
@hunkim
Copy link

hunkim commented Jun 23, 2020

Is this problem fixed?

@astamato
Copy link

astamato commented Jul 3, 2020

As I understood from the above, because there are several workarounds it's closed with no official solution.

@Bowenislandsong
Copy link

PR=$(IFS='/' read -r -a REF <<< "$GITHUB_REF" && echo ${REF[2]})

@kirosc
Copy link

kirosc commented Jul 23, 2020

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.

Because it's not the first pull request. Instead, it's a push event.
GitHub should add the PR number to the environment variable.

@leeyspaul
Copy link

leeyspaul commented Jul 23, 2020

@kirosc I wrote a small post on retrieving the pull number based on an issue event.

If you use ${{github.event.number}} you will get the PR number on pull request event.
If you use ${{github.event.issue.number}} you will get the PR number on any issue event e.g. updates.

Not sure if that helps

@dgroup
Copy link

dgroup commented Apr 21, 2022

One more way from stackoverflow: https://stackoverflow.com/a/69936361/6916890

 - name: Get Pull Request Number
   id: pr
   run: echo "::set-output name=pull_request_number::$(gh pr view --json number -q .number || echo "")"
   env:
     GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

and usage ${{ steps.pr.outputs.pull_request_number }}.

@ar-mohammed
Copy link

i have run the action/checkout but still i am not getting PR no

@polarathene
Copy link

polarathene commented Jul 20, 2022

@ar-mohammed it is available via the github.event context object, provided your workflow has been triggered by a pull_request event. Just use ${{ github.event.pull_request.number }} to retrieve it then.


If the workflow is triggered by a different event, then you can use the gh CLI tool to make a query as was shown above. When you don't know what PR number/url/branch to reference, the default is to return PR data for the current checked out branch.

That is what the above example is doing, followed by requesting the JSON field number and jq (-q) expression to return that number, otherwise an empty string.

@ygots
Copy link

ygots commented Feb 7, 2023

If there are still searchers passing by, https://github.com/marketplace/actions/find-current-pull-request solved this problem for me.

It looks like it doesn't support comment-triggering events - I found this jwalton/gh-find-current-pr#23

@RDhar
Copy link

RDhar commented Feb 11, 2023

Hi @estensen,

To expand on @polarathene's answer about the API endpoint, here's a working snippet to get the issue number in both push and pull_request events within a GitHub Actions workflow by leveraging actions/github-script:

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.

@mgaitan
Copy link

mgaitan commented Oct 23, 2023

I've created an action that works for both pull_request and merge_group events.
Check mgaitan/gha-get-pr-number

Alexander-Wilms added a commit to Alexander-Wilms/vcmi that referenced this issue Oct 29, 2023
@sumanth-lingappa
Copy link

The below worked for me:
peter-evans/create-or-update-comment#104 (comment)

@chriscz
Copy link

chriscz commented Aug 2, 2024

PR_NUMBER=$(echo $GITHUB_REF | awk 'BEGIN { FS = "/" } ; { print $3 }')

This is problematic because the refs you see in $GITHUB_REF tend to have 3 or more path components:

  • For a push to the main branch: refs/heads/main
  • For a push to the develop branch: refs/heads/develop
  • For a tag: refs/tags/<tag-name>
  • For a pull request: refs/pull/<pr-number>/merge

I want this pattern to only match the last case. So I added a pattern to the last rule:

PR_NUMBER=$(echo $GITHUB_REF | awk 'BEGIN { FS = "/" } ; /^refs\/pull/ { print $3 }')

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