Update changelog.xml #4
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: process-pr-merge | |
# This workflow processes commits pushed to the main branch to: | |
# 1. Determine if the commit is associated with a PR, and if not exit gracefully. | |
# 2. Extract relevant details (change type, release note, and bump type) from the PR body | |
# and use them to update the changelog if applicable (change type is not "OTHER"). | |
# 3. Trigger a release action if warranted by the bump type (MAJOR, MINOR), or skip if WAIT is selected. | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
determine-pr: | |
runs-on: ubuntu-latest | |
env: | |
GH_TOKEN: ${{ secrets.ACTIONS_PAT }} | |
outputs: | |
pr_number: ${{ steps.check-pr.outputs.pr_number }} | |
pr_body: ${{ steps.check-pr.outputs.pr_body }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.ACTIONS_PAT }} | |
fetch-depth: 0 | |
- name: Check if this is a PR commit | |
id: check-pr | |
run: | | |
commit_sha=$(git log -1 --format="%H") | |
echo "Commit SHA: $commit_sha" | |
pr_number=$(gh api -X GET "repos/${{ github.repository }}/commits/$commit_sha/pulls" --jq '.[0].number') | |
if [ -z "$pr_number" ]; then | |
echo "No PR associated with commit $commit_sha." | |
echo "pr_number=" >> $GITHUB_OUTPUT | |
exit 0 | |
fi | |
pr_body=$(gh api -X GET "repos/${{ github.repository }}/pulls/$pr_number" --jq '.body' | sed 's/\r//g') | |
echo "PR Number: $pr_number" | |
echo "PR Body: $pr_body" | |
echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT | |
echo "pr_body=${pr_body}" >> $GITHUB_OUTPUT | |
update-changelog: | |
runs-on: ubuntu-latest | |
needs: determine-pr | |
outputs: | |
bump_type: ${{ steps.extract-bump.outputs.bump_type }} | |
if: needs.determine-pr.outputs.pr_number != '' | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.ACTIONS_PAT }} | |
fetch-depth: 0 | |
- name: Extract change type and release note | |
id: extract-details | |
run: | | |
pr_body="${{ needs.determine-pr.outputs.pr_body }}" | |
echo "Extracting details from PR body..." | |
release_note=$(echo "$pr_body" | awk 'BEGIN { found=0 } /```release-note/ { found=1; next } /```/ { found=0 } found { print }' | sed '/^$/d') | |
echo "Release Note: $release_note" | |
change_type=$(echo "$pr_body" | grep -oP '(?<=- \[x\] `)[A-Z]+(?=`)' | head -n 1) | |
echo "Change Type: $change_type" | |
if [ -z "$release_note" ]; then | |
echo "No release note found." | |
exit 1 | |
fi | |
case "$change_type" in | |
"ADDITION") | |
section="### Added" | |
;; | |
"CHANGE") | |
section="### Changed" | |
;; | |
"FIX") | |
section="### Fixed" | |
;; | |
"OTHER") | |
echo "Change type is 'OTHER'. Skipping changelog update." | |
exit 0 | |
;; | |
*) | |
echo "Invalid change type: $change_type." | |
exit 1 | |
;; | |
esac | |
echo "release_note=${release_note}" >> $GITHUB_OUTPUT | |
echo "section=${section}" >> $GITHUB_OUTPUT | |
- name: Update changelog | |
run: | | |
echo "Updating changelog under section: ${{ steps.extract-details.outputs.section }}" | |
sed -i "0,/${{ steps.extract-details.outputs.section }}/s@${{ steps.extract-details.outputs.section }}@${{ steps.extract-details.outputs.section }}\n- ${{ steps.extract-details.outputs.release_note }}@" CHANGELOG.md | |
cat CHANGELOG.md | |
- name: Commit and push changelog update | |
uses: EndBug/add-and-commit@v7 | |
with: | |
add: 'CHANGELOG.md' | |
message: "Update CHANGELOG.md with release note from PR #${{ needs.determine-pr.outputs.pr_number }}" | |
author_email: git-action-bot@example.com | |
author_name: Git Action Bot | |
token: ${{ secrets.GITHUB_TOKEN }} | |
push: true | |
- name: Extract bump type | |
id: extract-bump | |
run: | | |
pr_body="${{ needs.determine-pr.outputs.pr_body }}" | |
echo "Extracting bump type from PR body..." | |
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 "Detected bump type: MAJOR" | |
echo "bump_type=major" >> $GITHUB_OUTPUT | |
elif [ "$minor_checked" == "true" ]; then | |
echo "Detected bump type: MINOR" | |
echo "bump_type=minor" >> $GITHUB_OUTPUT | |
elif [ "$wait_checked" == "true" ]; then | |
echo "WAIT was checked. No release required." | |
echo "bump_type=" >> $GITHUB_OUTPUT | |
else | |
echo "No valid bump type specified. Skipping release." | |
echo "bump_type=" >> $GITHUB_OUTPUT | |
fi | |
dispatch-release: | |
runs-on: ubuntu-latest | |
needs: update-changelog | |
if: needs.update-changelog.outputs.bump_type != '' | |
steps: | |
- name: Dispatch GitHub Release Action | |
run: | | |
echo "Dispatching release action with bump type: ${{ needs.update-changelog.outputs.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": "${{ needs.update-changelog.outputs.bump_type }}"}}' |