How to get latest commit from a PR merge?

I have a GitHub Action setup, which sends a Discord Webhook notification once a commit was performed towards a specific branch, and the GitHub Pages where built using it.

I currently use ${{ github.event.commit[0].message }} to display the commit message as a description in the send webhook.
The issue I face here is, that it always uses the very first made Commit and not the last one made when the PR was merged.

For example, when I made these commits in a PR:

Change value to true
Update description of docs
Minor tweaks

…will the Action use the very first commit, while I want to use the last one.

If possible would I now like to switch to displaying the title that was used in the Pull request.
All commits performed are always through commits, because I have branch-protection set up. Is there a way that I could get the title of a PR from a merge, or at the very least get the latest Commit message from the push?

My github action can be found here:

@andre601,

To get the title of the PR, you can use the expression “github.event.pull_request.title” to get it from the github context, when the workflow is running on the events for pull request.
For example:

echo "The title of PR is: ${{ github.event.pull_request.title }}"

To get the latest commit on the PR, you can do as the steps below:

  • Use the “List commits on a pull request” API to list all the commits on current PR, and save the response as JSON.
  • Use jq filter to get the latest commit from the commits list (the list is a JSON array). Generally, the latest commit is the last element of the array, so you need to get the index (length - 1) of this element.
    The following is a simple demo as reference:
on: pull_request

jobs:
  job1:
    name: Test
    if: ${{ github.event_name == 'pull_request' }}
    runs-on: ubuntu-latest
    steps:
      - name: List commits on the pull request
        run: |
          response=$(curl --request GET \
          --url 'https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/commits' \
          --header 'Authorization: token ${{ secrets.GITHUB_TOKEN }}' \
          --header 'Accept: application/vnd.github.v3+json' \
          --header 'Content-Type: application/json')
          
          echo $response > response.json

      - name: Get latest commit
        run: |
          echo "Get length of the array:"
          length=$(jq 'length' response.json)

          echo "Get index of the latest commit:"
          index=$(($length - 1))

          echo "Get context of the latest commit:"
          latest_commit=$(jq --argjson index $index '.[$index]' response.json)
          echo "$latest_commit"

          echo "Get commit message of the latest commit:"
          latest_commit_message=$(echo "$latest_commit" | jq -r '.commit.message')
          echo "$latest_commit_message"

          echo "Get commit SHA of the latest commit:"
          latest_commit_sha=$(echo "$latest_commit" | jq -r '.sha')
          echo "$latest_commit_sha"