Skip to content

Instantly share code, notes, and snippets.

@Shahzad6077
Created December 18, 2021 15:26
Show Gist options
  • Save Shahzad6077/764c7396090484e72ae8dbdf48588b75 to your computer and use it in GitHub Desktop.
Save Shahzad6077/764c7396090484e72ae8dbdf48588b75 to your computer and use it in GitHub Desktop.
How to get latest commit from a PR merge? [Answer](https://git.521000.bestmunity/t/how-to-get-latest-commit-from-a-pr-merge/121384/2)
name: echo the latest commit on PR
on: pull_request
jobs:
GetRecentCommit:
name: Get Latest Commit
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 "Get commit message of the latest commit:"
latest_commit_message=$(echo "$latest_commit" | jq -r '.commit.message')
echo "$latest_commit_message"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment