Update CHANGELOG.md with release note from PR #1103 #49
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: update-changelog | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
update-changelog: | |
runs-on: ubuntu-latest | |
env: | |
GH_TOKEN: ${{ secrets.ACTIONS_PAT }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.ACTIONS_PAT }} | |
fetch-depth: 0 | |
- name: Extract release note from PR | |
id: extract_release_note | |
run: | | |
# Get the latest commit SHA | |
commit_sha=$(git log -1 --format="%H") | |
echo "Commit SHA: $commit_sha" | |
# Find the pull request associated with the commit | |
pr_number=$(gh api -X GET "repos/${{ github.repository }}/commits/$commit_sha/pulls" --jq '.[0].number') | |
# Exit gracefully if no PR is found | |
if [ -z "$pr_number" ]; then | |
echo "No associated PR found for commit $commit_sha." | |
exit 0 | |
fi | |
# Get the PR body and extract release note | |
pr_body=$(gh api -X GET "repos/${{ github.repository }}/pulls/$pr_number" --jq '.body' | sed 's/\r//g') | |
release_note=$(echo "$pr_body" | awk 'BEGIN { found=0 } /```release-note/ { found=1; next } /```/ { found=0 } found { print }' | sed '/^$/d') | |
# Detect change type | |
change_type=$(echo "$pr_body" | grep -oP '(?<=- \[x\] `)[A-Z]+(?=`)' | head -n 1) | |
change_type=$(echo "$pr_body" | grep -oP '(?<=- \[x\] \*\*`)[A-Z]+(?=`\*\*)' | head -n 1) | |
case "$change_type" in | |
"ADDITION") | |
section="### Added" | |
;; | |
"CHANGE") | |
section="### Changed" | |
;; | |
"FIX") | |
section="### Fixed" | |
;; | |
"OTHER") | |
echo "Change type is 'OTHER'. Skipping release note as it is not required." | |
exit 0 | |
;; | |
*) | |
echo "Unknown change type: $change_type" | |
exit 1 | |
;; | |
esac | |
# Exit if no release note is found (only applicable for non-OTHER types) | |
if [ -z "$release_note" ]; then | |
echo "No release note found for change type: $change_type." | |
exit 1 | |
fi | |
# Store the extracted information in environment variables | |
pr_url="https://github.com/${{ github.repository }}/pull/$pr_number" | |
echo "release_note=${release_note} ($pr_url)" >> $GITHUB_ENV | |
echo "section=$section" >> $GITHUB_ENV | |
echo "pr_number=$pr_number" >> $GITHUB_ENV | |
- name: Extract version bump type from PR body | |
id: extract_version_bump | |
run: | | |
pr_body=$(gh api -X GET "repos/${{ github.repository }}/pulls/${{ env.pr_number }}" --jq '.body' | sed 's/\r//g') | |
major_checked=$(echo "$pr_body" | grep -q '\[x\] \*\*`MAJOR`' && echo "true" || echo "false") | |
minor_checked=$(echo "$pr_body" | grep -q '\[x\] \*\*`MINOR`' && echo "true" || echo "false") | |
wait_checked=$(echo "$pr_body" | grep -q '\[x\] \*\*`WAIT`' && echo "true" || echo "false") | |
if [ "$major_checked" == "true" ]; then | |
echo "BUMP_TYPE=major" >> $GITHUB_ENV | |
elif [ "$minor_checked" == "true" ]; then | |
echo "BUMP_TYPE=minor" >> $GITHUB_ENV | |
elif [ "$wait_checked" == "true" ]; then | |
echo "WAIT was checked. No release required." | |
fi | |
- name: Add release note to changelog | |
if: env.pr_number != '' | |
run: | | |
# Insert the release note under the first matching section in Unreleased | |
sed -i "0,/${{ env.section }}/s@${{ env.section }}@${{ env.section }}\n- ${{ env.release_note }}@" CHANGELOG.md | |
# Show the updated changelog | |
cat CHANGELOG.md | |
- name: Commit and push changelog update | |
if: env.pr_number != '' | |
uses: EndBug/add-and-commit@v7 | |
with: | |
add: 'CHANGELOG.md' | |
message: "Update CHANGELOG.md with release note from PR #${{ env.pr_number }}" | |
author_email: [email protected] | |
author_name: Git Action Bot | |
token: ${{ secrets.GITHUB_TOKEN }} | |
push: true | |
- name: Dispatch GitHub Release Action | |
if: env.BUMP_TYPE != '' # Ensures no action if WAIT is selected | |
run: | | |
echo "Triggering release action with bump type: ${{ env.BUMP_TYPE }}" | |
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github.everest-preview+json" \ | |
https://api.github.com/repos/${{ github.repository }}/dispatches \ | |
-d '{"event_type": "trigger-release", "client_payload": {"bump_type": "${{ env.BUMP_TYPE }}"}}' |