Skip to content

Commit b85139d

Browse files
committed
fix(auto-release): 修复 commit hash 和 url 提取存在的问题,完善 release 消息
1 parent f99a61a commit b85139d

File tree

1 file changed

+70
-35
lines changed

1 file changed

+70
-35
lines changed

.github/workflows/auto-release.yml

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -61,55 +61,90 @@ jobs:
6161
with:
6262
fetch-depth: 0
6363

64-
- name: Generate changelog
65-
id: changelog
64+
- name: Get current commit hash
65+
id: get_commit
6666
run: |
67-
NEW_VERSION="${{ needs.check-version.outputs.new_version }}"
67+
COMMIT_SHORT=$(echo "${{ github.sha }}" | cut -c1-7)
68+
echo "COMMIT_SHORT=$COMMIT_SHORT" >> $GITHUB_OUTPUT
69+
70+
- name: Generate release notes
71+
id: release_notes
72+
run: |
73+
VERSION="${{ needs.check-version.outputs.new_version }}"
6874
OLD_VERSION="${{ needs.check-version.outputs.old_version }}"
6975
70-
echo "## 🎉 Release $NEW_VERSION" > changelog.md
71-
echo "" >> changelog.md
76+
# 1. 尝试提取 CHANGELOG.md
77+
if [ -f "CHANGELOG.md" ]; then
78+
awk -v ver="$VERSION" '
79+
/^## \[?v?/ && index($0, ver) { flag=1 }
80+
/^## \[?v?/ && !index($0, ver) { flag=0 }
81+
flag
82+
' CHANGELOG.md > release_notes.md
83+
fi
84+
85+
# 确保 release_notes.md 存在
86+
touch release_notes.md
87+
88+
# 2. 生成提交列表
89+
echo "" >> release_notes.md
90+
echo "### 🔍 Commits" >> release_notes.md
7291
73-
# 如果有上一个版本标签,生成两个版本之间的提交日志
74-
if git rev-parse "$OLD_VERSION" >/dev/null 2>&1; then
75-
echo "### 📝 Changes since $OLD_VERSION" >> changelog.md
76-
echo "" >> changelog.md
92+
process_log() {
93+
local range="$1"
94+
git log --pretty=format:"%s|%h|%H" "$range" > commits.txt
7795
78-
# 获取提交信息并分类
79-
git log "$OLD_VERSION..HEAD" --pretty=format:"%s|||%h|||%an" --no-merges | while IFS='|||' read -r subject hash author; do
80-
# 根据提交信息的前缀进行分类
81-
if echo "$subject" | grep -qiE "^(feat|feature):"; then
82-
echo "- ✨ **Feature**: ${subject#*: } (${hash})" >> changelog.md
83-
elif echo "$subject" | grep -qiE "^fix:"; then
84-
echo "- 🐛 **Fix**: ${subject#*: } (${hash})" >> changelog.md
85-
elif echo "$subject" | grep -qiE "^docs:"; then
86-
echo "- 📚 **Docs**: ${subject#*: } (${hash})" >> changelog.md
87-
elif echo "$subject" | grep -qiE "^(refactor|style|perf|test|chore):"; then
88-
prefix=$(echo "$subject" | cut -d: -f1)
89-
echo "- 🔧 **${prefix^}**: ${subject#*: } (${hash})" >> changelog.md
90-
else
91-
echo "- 📌 $subject (${hash})" >> changelog.md
92-
fi
93-
done
96+
# Helper to extract categories
97+
print_category() {
98+
local marker="$1"
99+
local title="$2"
100+
local pattern="$3"
101+
local commits=$(grep -iE "$pattern" commits.txt)
102+
if [ ! -z "$commits" ]; then
103+
echo "#### $marker $title" >> release_notes.md
104+
echo "$commits" | while IFS='|' read -r msg hash full_hash; do
105+
echo "- $msg ([$hash](https://github.com/${{ github.repository }}/commit/$full_hash))" >> release_notes.md
106+
done
107+
echo "" >> release_notes.md
108+
fi
109+
}
110+
111+
print_category "✨" "Features" "^(feat|feature):"
112+
print_category "🐛" "Fixes" "^fix:"
113+
print_category "📚" "Documentation" "^docs:"
114+
print_category "💄" "Style & Refactor" "^(style|refactor|perf):"
115+
print_category "🔧" "Chore & Test" "^(chore|test|ci|build):"
116+
117+
# Other changes (inverse grep)
118+
local others=$(grep -ivE "^(feat|feature|fix|docs|style|refactor|perf|chore|test|ci|build):" commits.txt)
119+
if [ ! -z "$others" ]; then
120+
echo "#### 📌 Other Changes" >> release_notes.md
121+
echo "$others" | while IFS='|' read -r msg hash full_hash; do
122+
echo "- $msg ([$hash](https://github.com/${{ github.repository }}/commit/$full_hash))" >> release_notes.md
123+
done
124+
fi
125+
126+
rm commits.txt
127+
}
128+
129+
if [ "$OLD_VERSION" != "none" ] && git rev-parse "$OLD_VERSION" >/dev/null 2>&1; then
130+
process_log "$OLD_VERSION..HEAD"
131+
132+
echo "" >> release_notes.md
133+
echo "**Full Changelog**: https://github.com/${{ github.repository }}/compare/$OLD_VERSION...${{ github.ref_name }}" >> release_notes.md
94134
else
95-
echo "### 📝 Changes" >> changelog.md
96-
echo "" >> changelog.md
97-
echo "Initial release or no previous version tag found." >> changelog.md
98-
echo "" >> changelog.md
99-
echo "#### Recent commits:" >> changelog.md
100-
git log -10 --pretty=format:"- %s (%h)" --no-merges >> changelog.md
135+
echo "Initial release or previous version tag not found." >> release_notes.md
136+
echo "#### 🆕 Initial Commits" >> release_notes.md
137+
git log -10 --pretty=format:"- %s ([%h](https://github.com/${{ github.repository }}/commit/%H))" >> release_notes.md
101138
fi
102139
103-
echo "" >> changelog.md
104-
echo "---" >> changelog.md
105-
echo "**Full Changelog**: https://github.com/${{ github.repository }}/compare/$OLD_VERSION...$NEW_VERSION" >> changelog.md
140+
cat release_notes.md
106141
107142
- name: Create Release
108143
uses: softprops/action-gh-release@v1
109144
with:
110145
tag_name: ${{ needs.check-version.outputs.new_version }}
111146
name: Release ${{ needs.check-version.outputs.new_version }}
112-
body_path: changelog.md
147+
body_path: release_notes.md
113148
draft: false
114149
prerelease: false
115150
generate_release_notes: false

0 commit comments

Comments
 (0)