-
Notifications
You must be signed in to change notification settings - Fork 11
219 lines (195 loc) · 8.59 KB
/
changelog.yml
File metadata and controls
219 lines (195 loc) · 8.59 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
name: Update Changelog
on:
push:
branches:
- dev
workflow_dispatch:
jobs:
update-changelog:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Generate Changelog
run: |
python3 << 'EOF'
import subprocess
import sys
from datetime import datetime
from collections import defaultdict
def run_git_log(args):
try:
result = subprocess.run(['git'] + args, capture_output=True, text=True, check=True)
# FIX #1: Split by a null character instead of a newline to handle multi-line bodies.
return result.stdout.strip().split('\x00') if result.stdout.strip() else []
except subprocess.CalledProcessError as e:
print(f"Error running git: {e}")
sys.exit(1)
def get_commits_by_tag():
try:
latest_tag = subprocess.run(
['git', 'describe', '--tags', '--abbrev=0'],
capture_output=True, text=True, check=True
).stdout.strip()
tag_date = subprocess.run(
['git', 'log', '-1', '--format=%ad', '--date=short', latest_tag],
capture_output=True, text=True, check=True
).stdout.strip()
tagged = run_git_log([
'log', latest_tag,
# FIX #2: Add the %x00 null character as a safe delimiter for commits.
'--pretty=format:%s|||%b|||%an|||%ad|||%H%x00',
'--date=short',
'--invert-grep',
'--grep=docs: update changelog',
'--grep=changelog.yml',
'--grep=\\[skip ci\\]'
])
unreleased = run_git_log([
'log', f'{latest_tag}..HEAD',
# FIX #3: Add the delimiter here as well.
'--pretty=format:%s|||%b|||%an|||%ad|||%H%x00',
'--date=short',
'--invert-grep',
'--grep=docs: update changelog',
'--grep=changelog.yml',
'--grep=\\[skip ci\\]'
])
return {
'tagged': {latest_tag: {'commits': tagged, 'date': tag_date}},
'unreleased': unreleased
}
except subprocess.CalledProcessError:
all_commits = run_git_log([
'log',
# FIX #4: And add the delimiter here for the fallback case.
'--pretty=format:%s|||%b|||%an|||%ad|||%H%x00',
'--date=short',
'--invert-grep',
'--grep=docs: update changelog',
'--grep=changelog.yml',
'--grep=\\[skip ci\\]'
])
return {
'tagged': {},
'unreleased': all_commits
}
def categorize_commit(subject, body):
text = (subject + ' ' + body).lower()
if any(x in text for x in ['security', 'vulnerability', 'cve', 'exploit']):
return 'security'
if any(x in text for x in ['breaking change', 'breaking:', 'break:']):
return 'breaking'
if any(x in text for x in ['deprecat', 'obsolete', 'phase out']):
return 'deprecated'
if any(x in text for x in ['remove', 'delete', 'drop', 'eliminate']):
return 'removed'
# Correction: Check for 'added' keywords before 'fixed' keywords.
if any(x in text for x in ['add', 'new', 'create', 'implement', 'feat', 'feature']):
return 'added'
if any(x in text for x in ['fix', 'resolve', 'correct', 'patch', 'bug', 'issue']):
return 'fixed'
if any(x in text for x in ['chore', 'chr']):
return 'internal'
return 'changed'
def format_commit_entry(commit):
entry = f"- **{commit['subject']}** ({commit['date']} – {commit['author']})"
body = commit['body'].replace('\\n', '\n')
if body.strip():
lines = [line.strip() for line in body.splitlines() if line.strip()]
for line in lines:
entry += f"\n {line}"
return entry + "\n"
def parse_commits(lines):
commits = []
for line in lines:
if not line: continue
parts = line.split('|||')
if len(parts) >= 5:
subject, body, author, date, hash_id = map(str.strip, parts)
commits.append({
'subject': subject,
'body': body,
'author': author,
'date': date,
'hash': hash_id[:7]
})
return commits
def build_changelog(commits_by_version):
sections = [
('security', 'Security'),
('breaking', 'Breaking Changes'),
('deprecated', 'Deprecated'),
('added', 'Added'),
('changed', 'Changed'),
('fixed', 'Fixed'),
('removed', 'Removed')
]
lines = [
"# 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).",
""
]
unreleased = parse_commits(commits_by_version['unreleased'])
if unreleased:
lines.append("## [Unreleased]")
lines.append("")
categorized = defaultdict(list)
for commit in unreleased:
cat = categorize_commit(commit['subject'], commit['body'])
categorized[cat].append(commit)
for key, label in sections:
if categorized[key]:
lines.append(f"### {label}")
for commit in categorized[key]:
lines.append(format_commit_entry(commit))
lines.append("")
else:
lines.append("## [Unreleased]\n")
lines.append("_No unreleased changes._\n")
for tag, info in commits_by_version['tagged'].items():
commits = parse_commits(info['commits'])
lines.append(f"## [{tag}] - {info['date']}\n")
categorized = defaultdict(list)
for commit in commits:
cat = categorize_commit(commit['subject'], commit['body'])
categorized[cat].append(commit)
for key, label in sections:
if categorized[key]:
lines.append(f"### {label}")
for commit in categorized[key]:
lines.append(format_commit_entry(commit))
lines.append("")
return "\n".join(lines)
try:
commit_data = get_commits_by_tag()
changelog = build_changelog(commit_data)
with open("CHANGELOG.md", "w", encoding="utf-8") as f:
f.write(changelog)
print("Changelog generated.")
except Exception as e:
print(f"Error generating changelog: {e}")
sys.exit(1)
EOF
- name: Commit Updated Changelog
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add CHANGELOG.md
if git diff --staged --quiet; then
echo "No changes to commit"
else
git commit -m "docs: update changelog [skip ci]"
git push
fi