chore(gh-action): Add GH action to update changelog #1
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 | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
fetch-depth: 0 | |
- name: Setup Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '18' | |
- name: Install dependencies | |
run: npm install | |
- name: Get commits and update changelog | |
run: | | |
# Create public directory if it doesn't exist | |
mkdir -p public | |
# Function to determine change type from commit message | |
get_change_type() { | |
local msg="$1" | |
if [[ $msg == *"feat:"* || $msg == *"feature:"* ]]; then | |
echo "feature" | |
elif [[ $msg == *"improve:"* || $msg == *"enhancement:"* ]]; then | |
echo "improvement" | |
elif [[ $msg == *"fix:"* || $msg == *"bug:"* ]]; then | |
echo "fix" | |
elif [[ $msg == *"beta:"* ]]; then | |
echo "beta" | |
else | |
echo "improvement" | |
fi | |
} | |
# Get commits since last tag or initial commit if no tags | |
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || git rev-list --max-parents=0 HEAD) | |
# Process commits and create JSON entries | |
COMMITS=$(git log --pretty=format:'{%n "commitHash": "%H",%n "date": "%aI",%n "author": "%an",%n "title": "%s",%n "description": "%b",%n "type": "'"$(get_change_type \"%s\")"'"%n},' $LAST_TAG..HEAD) | |
# Remove trailing comma and wrap in array | |
COMMITS="[${COMMITS%,}]" | |
# Create final JSON with metadata | |
echo "{ | |
\"lastUpdated\": \"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\", | |
\"entries\": $COMMITS | |
}" > public/changelog.json | |
- name: Commit and push if changed | |
run: | | |
git config --global user.name 'GitHub Action' | |
git config --global user.email 'action@github.com' | |
git add public/changelog.json | |
git diff --quiet && git diff --staged --quiet || (git commit -m "chore: update changelog" && git push) |