Skip to content

docs: update changelog for v2.1.0 #24

docs: update changelog for v2.1.0

docs: update changelog for v2.1.0 #24

name: Update Changelog
on:
pull_request:
types: [closed]
branches:
- main
jobs:
update-changelog:
if: |
github.event.pull_request.merged == true &&
!startsWith(github.event.pull_request.title, 'docs: update changelog for v')
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.base_ref }}
token: ${{ secrets.PAT_TOKEN }}
- name: Get PR title and body
id: pr-info
run: |
echo "title=${{ github.event.pull_request.title }}" >> $GITHUB_OUTPUT
echo "body<<EOF" >> $GITHUB_OUTPUT
echo "${{ github.event.pull_request.body }}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Check if only changelog was modified
id: changelog-check
run: |
CHANGED_FILES=$(git diff --name-only HEAD^ HEAD)
if [ "$CHANGED_FILES" = "CHANGELOG.md" ]; then
echo "only_changelog=true" >> $GITHUB_OUTPUT
else
echo "only_changelog=false" >> $GITHUB_OUTPUT
fi
- name: Update Changelog
if: steps.changelog-check.outputs.only_changelog != 'true'
run: |
# Check if CHANGELOG.md exists and create if needed
if [ ! -f CHANGELOG.md ]; then
echo "Creating initial CHANGELOG.md"
cat > CHANGELOG.md << EOL
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
EOL
CURRENT_VERSION="0.0.0"
else
echo "Reading current version from CHANGELOG.md"
CURRENT_VERSION=$(grep -m 1 "## \[v" CHANGELOG.md | sed -E 's/## \[v([0-9]+\.[0-9]+\.[0-9]+)\].*/\1/')
if [ -z "$CURRENT_VERSION" ]; then
echo "Warning: No version found in CHANGELOG.md, using 0.0.0"
CURRENT_VERSION="0.0.0"
else
echo "Current version: $CURRENT_VERSION"
fi
fi
# Get today's date
DATE=$(date +%Y-%m-%d)
echo "Generating changelog entry for date: $DATE"
# Get PR details with better merge handling
PR_TITLE="${{ steps.pr-info.outputs.title }}"
PR_BODY="${{ steps.pr-info.outputs.body }}"
# Special handling for develop->main merges
if [[ "$PR_TITLE" == "Merge branch 'develop' into main" ]]; then
echo "Detected develop->main merge, analyzing commit messages"
# Get all commits since last merge to main
COMMITS=$(git log --format="%s" HEAD^..HEAD)
# Look for feature or breaking changes in commits
if echo "$COMMITS" | grep -q "!\\|BREAKING CHANGE"; then
PR_TITLE="BREAKING CHANGE: Updates from develop branch"
elif echo "$COMMITS" | grep -q "feat:\\|feature:"; then
PR_TITLE="feat: Updates from develop branch"
else
PR_TITLE="fix: Updates from develop branch"
fi
fi
# Increment version based on PR title
IFS='.' read -r major minor patch <<< "$CURRENT_VERSION"
echo "Processing PR title: $PR_TITLE"
# Version increment logic with error handling
if [[ ! "$major" =~ ^[0-9]+$ ]] || [[ ! "$minor" =~ ^[0-9]+$ ]] || [[ ! "$patch" =~ ^[0-9]+$ ]]; then
echo "Error: Invalid version format. Using 0.0.0"
major=0; minor=0; patch=0
fi
if [[ "$PR_TITLE" == *"!"* || "$PR_TITLE" == *"BREAKING CHANGE"* ]]; then
echo "Detected breaking change, incrementing major version"
NEW_VERSION="$((major + 1)).0.0"
elif [[ "$PR_TITLE" == *"feat:"* || "$PR_TITLE" == *"feature:"* ]]; then
echo "Detected new feature, incrementing minor version"
NEW_VERSION="${major}.$((minor + 1)).0"
else
echo "Detected patch change, incrementing patch version"
NEW_VERSION="${major}.${minor}.$((patch + 1))"
fi
echo "New version will be: $NEW_VERSION"
# Create new changelog entry with error handling
NEW_ENTRY="## [v${NEW_VERSION}] - ${DATE}\n\n"
# Determine change type from PR title with improved categorization
case "$PR_TITLE" in
*"!"*|*"BREAKING CHANGE"*) CHANGE_TYPE="Breaking Change" ;;
*"feat:"*|*"feature:"*) CHANGE_TYPE="Added" ;;
*"fix:"*) CHANGE_TYPE="Fixed" ;;
*"docs:"*) CHANGE_TYPE="Documentation" ;;
*"refactor:"*) CHANGE_TYPE="Changed" ;;
*"security:"*) CHANGE_TYPE="Security" ;;
*"perf:"*) CHANGE_TYPE="Performance" ;;
*"test:"*) CHANGE_TYPE="Testing" ;;
*) CHANGE_TYPE="Changed" ;;
esac
echo "Change type determined as: $CHANGE_TYPE"
NEW_ENTRY+="### ${CHANGE_TYPE}\n"
# Clean PR title for changelog entry
CLEAN_TITLE=$(echo "${PR_TITLE#*: }" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
NEW_ENTRY+="- ${CLEAN_TITLE}\n"
# Process PR body for changelog
PR_BODY=$(cat << 'EOF'
${{ steps.pr-info.outputs.body }}
EOF
)
if [ -n "$PR_BODY" ]; then
echo "Processing PR body for additional details"
echo "$PR_BODY" | while IFS= read -r line; do
# Skip PR template checkboxes, empty lines, and markdown headers
if [[ ! "$line" =~ ^[[:space:]]*-[[:space:]]*\[[[:space:]xX[:space:]]\] ]] && \
[[ ! "$line" =~ ^[[:space:]]*#+ ]] && \
[ -n "$(echo "$line" | tr -d '[:space:]')" ]; then
NEW_ENTRY+=" - $(echo "$line" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')\n"
fi
done
fi
# Add version link with repository URL from environment
REPO_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}"
NEW_ENTRY+="\n[v${NEW_VERSION}]: ${REPO_URL}/releases/tag/v${NEW_VERSION}\n"
# Create temporary file with new content with error handling
if ! echo -e "# Changelog\n\nAll notable changes to this project will be documented in this file.\n\nThe format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),\nand this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n\n${NEW_ENTRY}\n$(tail -n +7 CHANGELOG.md 2>/dev/null || true)" > CHANGELOG.md.new; then
echo "Error: Failed to create new changelog file"
exit 1
fi
if ! mv CHANGELOG.md.new CHANGELOG.md; then
echo "Error: Failed to update changelog file"
exit 1
fi
# Configure Git with error handling
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
# Create branch and commit changes with error handling
TARGET_BRANCH="${{ github.base_ref }}"
BRANCH_NAME="bot/update-changelog-v${NEW_VERSION}-${TARGET_BRANCH}"
if ! git checkout -b "$BRANCH_NAME"; then
echo "Error: Failed to create new branch"
exit 1
fi
if ! git add CHANGELOG.md; then
echo "Error: Failed to stage changes"
exit 1
fi
if ! git commit -m "docs: update changelog for v${NEW_VERSION}"; then
echo "Error: Failed to commit changes"
exit 1
fi
if ! git push origin "$BRANCH_NAME"; then
echo "Error: Failed to push changes"
exit 1
fi
# Create PR with error handling and more descriptive message
PR_BODY="## Automated Changelog Update
This PR updates the changelog for version ${NEW_VERSION}
### Changes
- Type: ${CHANGE_TYPE}
- Original PR Title: ${PR_TITLE}
- Target Branch: ${TARGET_BRANCH}
This is an automated update by the changelog workflow."
if ! gh pr create \
--title "docs: update changelog for v${NEW_VERSION}" \
--body "$PR_BODY" \
--base "$TARGET_BRANCH" \
--head "$BRANCH_NAME"; then
echo "Warning: Failed to create PR, but changes were pushed to branch"
fi
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}