-
Notifications
You must be signed in to change notification settings - Fork 194
192 lines (165 loc) · 6.89 KB
/
update-changelog.yml
File metadata and controls
192 lines (165 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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: |
COMMIT_MSG=$(git log -1 --pretty=%B)
{
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 "noreply@anthropic.com"
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 }}