Skip to content

Merge pull request #1146 from PhenoApps/l10n_main #14

Merge pull request #1146 from PhenoApps/l10n_main

Merge pull request #1146 from PhenoApps/l10n_main #14

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
env:
GH_TOKEN: ${{ secrets.ACTIONS_PAT }}
jobs:
determine-pr:
runs-on: ubuntu-latest
outputs:
pr_number: ${{ steps.check-pr.outputs.pr_number }}
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
echo "PR Number: $pr_number"
echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT
update-changelog:
runs-on: ubuntu-latest
needs: determine-pr
outputs:
bump_type: ${{ steps.extract-details.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, bump type, and release note
id: extract-details
run: |
pr_number="${{ needs.determine-pr.outputs.pr_number }}"
echo "Fetching PR details for PR #$pr_number..."
# Fetch PR body
pr_body=$(gh api -X GET "repos/${{ github.repository }}/pulls/$pr_number" --jq '.body' | sed 's/\r//g')
echo "Extracting details from PR Body: $pr_body"
# Extract release note
release_note=$(echo "$pr_body" | awk 'BEGIN { found=0 } /```release-note/ { found=1; next } /```/ { found=0 } found { print }' | sed '/^\s*$/d')
echo "Release Note: $release_note"
if [ -z "$release_note" ]; then
echo "No release note found."
exit 1
fi
echo "release_note=${release_note}" >> $GITHUB_OUTPUT
# Determine change type
change_type=$(echo "$pr_body" | grep -oP '(?<=- \[x\] \*\*`)[A-Z]+(?=`\*\*)' | head -n 1)
echo "Change Type: $change_type"
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 "section=${section}" >> $GITHUB_OUTPUT
# Check for bump type
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"
echo "bump_type=major" >> $GITHUB_OUTPUT
elif [ "$minor_checked" == "true" ]; then
echo "bump type: MINOR"
echo "bump_type=minor" >> $GITHUB_OUTPUT
elif [ "$wait_checked" == "true" ]; then
echo "bump type 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
- 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
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 }}"}}'