Skip to content

refactor: improve terminal toggle logic #22

refactor: improve terminal toggle logic

refactor: improve terminal toggle logic #22

name: Update Changelog
on:
push:
branches: [main]
permissions:
contents: write
pull-requests: write
jobs:
update-changelog:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
with:
fetch-depth: 2 # We need at least 2 commits to get the last one
persist-credentials: true
- name: Get commit message
id: get-commit-msg
shell: bash
# zizmor-disable-next-line template-injection
run: |
# Get only the first line of commit message (subject line)
COMMIT_MSG=$(git log -1 --pretty=%s)
{
echo "commit_msg=${COMMIT_MSG}"
echo "commit_type=$(echo "${COMMIT_MSG}" | grep -o '^feat\|^fix\|^docs\|^style\|^refactor\|^perf\|^test\|^build\|^ci\|^chore' || echo 'other')"
echo "commit_scope=$(echo "${COMMIT_MSG}" | grep -o '([^)]*)')"
echo "commit_desc=$(echo "${COMMIT_MSG}" | sed -E 's/^(feat|fix|docs|style|refactor|perf|test|build|ci|chore)(\([^)]*\))?:\s*//')"
} >> "$GITHUB_OUTPUT"
- name: Update Changelog
id: update-changelog
shell: bash
# zizmor-disable-next-line template-injection
env:
COMMIT_TYPE_ENV: ${{ steps.get-commit-msg.outputs.commit_type }}
COMMIT_SCOPE_ENV: ${{ steps.get-commit-msg.outputs.commit_scope }}
COMMIT_DESC_ENV: ${{ steps.get-commit-msg.outputs.commit_desc }}
run: |
# Assign environment variables directly to shell variables
COMMIT_TYPE="$COMMIT_TYPE_ENV"
COMMIT_SCOPE="$COMMIT_SCOPE_ENV"
COMMIT_DESC="$COMMIT_DESC_ENV"
# Clean up scope format for display
SCOPE=""
if [ -n "$COMMIT_SCOPE" ]; then
SCOPE=$(echo "$COMMIT_SCOPE" | sed -E 's/^\(([^)]*)\)$/\1/')
SCOPE=" ($SCOPE)"
fi
# Format changelog entry based on commit type
ENTRY="- $COMMIT_DESC$SCOPE"
# Determine which section to add the entry to
SECTION=""
case "$COMMIT_TYPE" in
feat)
SECTION="Added"
;;
fix)
SECTION="Fixed"
;;
docs)
SECTION="Documentation"
;;
style|refactor)
SECTION="Changed"
;;
perf)
SECTION="Performance"
;;
test|build|ci|chore)
# Skip these commit types for changelog
echo "Skipping changelog update for commit type: $COMMIT_TYPE"
exit 0
;;
*)
# For other commit types, put in Changed section
SECTION="Changed"
;;
esac
# Prepare a new branch
BRANCH_NAME="changelog/update-$COMMIT_TYPE-$(date +%Y%m%d%H%M%S)"
git checkout -b "$BRANCH_NAME"
# Check if the section exists, if not, create it
if ! grep -q "### $SECTION" CHANGELOG.md; then
# Add the section after "## [Unreleased]" line
sed -i "/## \[Unreleased\]/a \\\n### $SECTION\n" CHANGELOG.md
fi
# Add entry to the section
# Ensure ENTRY is quoted to handle potential special characters if it were used in a more complex sed command,
# but for 'a' (append), it's generally safer.
sed -i "/### $SECTION/a $ENTRY" CHANGELOG.md
# Check if any changes were made
if git diff --quiet CHANGELOG.md; then
echo "No changes made to CHANGELOG.md"
# No temp files to clean up here for commit details if exiting early
exit 0
fi
# Write outputs directly to GITHUB_OUTPUT using a block redirect
{
echo "changelog_updated=true"
echo "branch_name=$BRANCH_NAME"
echo "commit_type=$COMMIT_TYPE"
echo "commit_desc=$COMMIT_DESC"
echo "section=$SECTION"
} >> "$GITHUB_OUTPUT"
# No temp files like commit_type.txt, etc. were created in this block anymore
- name: Set up git identity for Claude
if: steps.update-changelog.outputs.changelog_updated == 'true'
shell: bash
# zizmor-disable-next-line template-injection
run: |
git config --local user.email "[email protected]"
git config --local user.name "Claude"
- name: Commit changes
if: steps.update-changelog.outputs.changelog_updated == 'true'
shell: bash
# zizmor-disable-next-line template-injection
env:
COMMIT_TYPE_ENV: ${{ steps.get-commit-msg.outputs.commit_type }}
COMMIT_SCOPE_ENV: ${{ steps.get-commit-msg.outputs.commit_scope }}
BRANCH_NAME_ENV: ${{ steps.update-changelog.outputs.branch_name }}
run: |
# Assign environment variables directly to shell variables
COMMIT_TYPE="$COMMIT_TYPE_ENV"
COMMIT_SCOPE="$COMMIT_SCOPE_ENV"
BRANCH_NAME="$BRANCH_NAME_ENV"
git add CHANGELOG.md
git commit -m "docs(changelog): update for ${COMMIT_TYPE}${COMMIT_SCOPE}"
git push --set-upstream origin "$BRANCH_NAME"
# No temp files to clean up here
- name: Create Pull Request
if: steps.update-changelog.outputs.changelog_updated == 'true'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const commitType = process.env.COMMIT_TYPE;
const commitDesc = process.env.COMMIT_DESC;
const section = process.env.SECTION;
const branchName = process.env.BRANCH_NAME;
try {
const pr = await github.rest.pulls.create({
owner: context.repo.owner,
repo: context.repo.repo,
title: `docs(changelog): Update ${section} section for ${commitType} changes`,
head: branchName,
base: 'main',
body: `This PR was automatically generated to update the CHANGELOG.md file.
## Changes
Added to the **${section}** section:
\`\`\`
- ${commitDesc}
\`\`\`
This change reflects the latest commit to the main branch.
---
🤖 Generated with [Claude Code](https://claude.ai/code)`
});
console.log(`Pull Request created: ${pr.data.html_url}`);
} catch (error) {
console.error('Error creating pull request:', error);
core.setFailed('Failed to create pull request');
}
env:
COMMIT_TYPE: ${{ steps.update-changelog.outputs.commit_type }}
COMMIT_DESC: ${{ steps.update-changelog.outputs.commit_desc }}
SECTION: ${{ steps.update-changelog.outputs.section }}
BRANCH_NAME: ${{ steps.update-changelog.outputs.branch_name }}