How to get PR title on a push event in github actions?

Viewed 786

On push event, is it possible to get the PR title , so that i can use the same to create a release and tag.

Currently, i am not able to get PR title on push event.

Can anyone please help.

2 Answers

I actually was in need for a similar use case. We needed to check if the PR title contained a Jira ticket as part of streamline our workflow.

I got to the following github actions extension https://github.com/8BitJonny/gh-get-current-pr

Few things to notice: If in your use case you need to get the PR title and your push event is pull_request you can just use github.head_ref (see here - https://docs.github.com/en/actions/learn-github-actions/contexts#github-context) as you have the information in the context.

However, in my case, my event was just push so I didn't have this information.

This is where gh-get-current-pr is useful.

add the following to your github actions yaml

  steps:
- uses: actions/checkout@v1
- uses: 8BitJonny/gh-get-current-pr@1.4.0
  id: PR
  with:
    github-token: ${{ secrets.GITHUB_TOKEN }}
    # Verbose setting SHA when using Pull_Request event trigger to fix #16
    sha: ${{ github.event.pull_request.head.sha }}
    # Only return if PR is still open
    filterOutClosed: true
- run: echo "Your PR is ${prNumber} and its JSON is ${prJSON}"
  if: success() && steps.PR.outputs.number
  env:
    prNumber: ${{ steps.PR.outputs.number }}
    # JSON object with the full PR object
    prJSON: ${{ steps.PR.outputs.pr }}
    # Direct access to common PR properties
    prUrl: ${{ steps.PR.outputs.pr_url }}
    prTitle: ${{ steps.PR.outputs.pr_title }}
    prBody: ${{ steps.PR.outputs.pr_body }}
    prCreatedAt: ${{ steps.PR.outputs.pr_created_at }}
    prMergedAt: ${{ steps.PR.outputs.pr_merged_at }}
    prClosedAt: ${{ steps.PR.outputs.pr_closed_at }}
    prLabel: ${{ steps.PR.outputs.pr_labels }}

This basically an example on how you can configure environment variables from the PR step output.

You can use ${{ github.event.push.head_commit.message }}. See push payload in the Docs.

Related