-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-release-note.ps1
More file actions
301 lines (249 loc) · 8.57 KB
/
generate-release-note.ps1
File metadata and controls
301 lines (249 loc) · 8.57 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
param(
[Parameter(Position = 0)]
[string]$Hash1 = "",
[Parameter(Position = 1)]
[string]$Hash2 = "",
[string]$OutputFile = "",
[string]$Agent = $(if ($env:AGENT) { $env:AGENT } else { "agent" }),
[int]$MaxDiffLength = 20000,
[Alias("h")]
[switch]$Help
)
function Show-Usage {
@"
Usage: generate-release-note.ps1 [HASH1] [HASH2] [-OutputFile FILE] [-Agent AGENT] [-MaxDiffLength LENGTH]
Generate release notes from git changes:
- If two hashes given: generate notes for changes between HASH1 and HASH2
- If one hash given: generate notes for all changes from beginning to HASH1
- If no hash given: generate notes for all commits in the repository
Examples:
generate-release-note.ps1
generate-release-note.ps1 abc1234
generate-release-note.ps1 abc1234 def5678
generate-release-note.ps1 abc1234 def5678 -OutputFile release.md
generate-release-note.ps1 v1.0.0 v2.0.0 -Agent copilot
"@ | Write-Host
}
if ($Help -or $Hash1 -eq "--help") {
Show-Usage
exit 0
}
# --- Validate prerequisites ---
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
Write-Error "git is not installed or not in PATH."
exit 127
}
if (-not (Get-Command $Agent -ErrorAction SilentlyContinue)) {
Write-Error "'$Agent' command not found in PATH."
exit 127
}
# --- Ensure we're in a git repo ---
try {
git rev-parse --show-toplevel 2>&1 | Out-Null
} catch {
Write-Error "Not inside a git repository."
exit 1
}
# --- Resolve range mode ---
$Range = ""
$SourceLabel = ""
if ($Hash1 -and $Hash2) {
# Both hashes provided
try {
$Resolved1 = git rev-parse --verify $Hash1 2>&1
if ($LASTEXITCODE -ne 0) { throw "Invalid commit reference: $Hash1" }
$Resolved2 = git rev-parse --verify $Hash2 2>&1
if ($LASTEXITCODE -ne 0) { throw "Invalid commit reference: $Hash2" }
$Range = "$Hash1..$Hash2"
$SourceLabel = "changes from $($Resolved1.Substring(0, 8)) to $($Resolved2.Substring(0, 8))"
} catch {
Write-Error $_
exit 1
}
} elseif ($Hash1) {
# Only one hash provided - from beginning to this hash
try {
$Resolved1 = git rev-parse --verify $Hash1 2>&1
if ($LASTEXITCODE -ne 0) { throw "Invalid commit reference: $Hash1" }
$FirstCommit = git rev-list --max-parents=0 HEAD 2>&1 | Select-Object -First 1
if (-not $FirstCommit -or $LASTEXITCODE -ne 0) {
throw "No commits found in repository"
}
$Range = "$FirstCommit..$Hash1"
$SourceLabel = "changes from beginning to $($Resolved1.Substring(0, 8))"
} catch {
Write-Error $_
exit 1
}
} else {
# No hash provided - all commits
$Range = ""
$SourceLabel = "all commits in repository"
}
# --- Gather context ---
Write-Host "Collecting $SourceLabel ..." -ForegroundColor Cyan
if ($Range) {
$CommitLog = (git log --oneline $Range 2>&1) -join "`n"
if ($LASTEXITCODE -ne 0) { $CommitLog = "(no commits in range)" }
$Stat = (git diff $Range --stat 2>&1) -join "`n"
if ($LASTEXITCODE -ne 0) { $Stat = "(no changes)" }
$Diff = (git diff $Range 2>&1) -join "`n"
if ($LASTEXITCODE -ne 0) { $Diff = "(no diff available)" }
$FileListRaw = (git diff $Range --name-status 2>&1) -join "`n"
if ($LASTEXITCODE -eq 0) {
$FileList = ($FileListRaw -split "`n" | ForEach-Object {
if ($_ -match '^([AMDRC])\s+(.+)$') {
$status = $Matches[1]
$file = $Matches[2]
switch ($status) {
'A' { "added: $file" }
'M' { "modified: $file" }
'D' { "deleted: $file" }
'R' { "renamed: $file" }
'C' { "copied: $file" }
default { "${status}: $file" }
}
}
}) -join "`n"
} else {
$FileList = ""
}
} else {
$CommitLog = (git log --oneline 2>&1) -join "`n"
if ($LASTEXITCODE -ne 0) { $CommitLog = "(no commits yet)" }
$FirstCommit = git rev-list --max-parents=0 HEAD 2>&1 | Select-Object -First 1
if ($FirstCommit -and $LASTEXITCODE -eq 0) {
$Stat = (git diff --stat $FirstCommit HEAD 2>&1) -join "`n"
if ($LASTEXITCODE -ne 0) { $Stat = "(no changes)" }
$Diff = (git diff $FirstCommit HEAD 2>&1) -join "`n"
if ($LASTEXITCODE -ne 0) { $Diff = "(no diff available)" }
$FileListRaw = (git diff $FirstCommit HEAD --name-status 2>&1) -join "`n"
if ($LASTEXITCODE -eq 0) {
$FileList = ($FileListRaw -split "`n" | ForEach-Object {
if ($_ -match '^([AMDRC])\s+(.+)$') {
$status = $Matches[1]
$file = $Matches[2]
switch ($status) {
'A' { "added: $file" }
'M' { "modified: $file" }
'D' { "deleted: $file" }
'R' { "renamed: $file" }
'C' { "copied: $file" }
default { "${status}: $file" }
}
}
}) -join "`n"
} else {
$FileList = ""
}
} else {
$Stat = "(no commits)"
$Diff = ""
$FileList = ""
}
}
$Branch = git branch --show-current 2>&1
if ($LASTEXITCODE -ne 0) { $Branch = "(detached HEAD)" }
$RepoRoot = git rev-parse --show-toplevel 2>&1
$RepoName = Split-Path -Leaf $RepoRoot
# Truncate diff if needed
if ($Diff.Length -gt $MaxDiffLength) {
$Diff = $Diff.Substring(0, $MaxDiffLength) + "`n... [diff truncated at $MaxDiffLength chars] ..."
}
# --- Build prompt ---
$Prompt = @"
Generate release notes from the following git repository changes.
Format the output as markdown with the following structure:
# Release Title (e.g., "v2.0.0 - Major Performance Update")
## Summary
A brief overview paragraph of the key changes and improvements.
## New Features
- Feature 1
- Feature 2
## Bug Fixes
- Fix 1
- Fix 2
## Improvements
- Improvement 1
- Improvement 2
## Breaking Changes (if any)
- Breaking change 1
## Technical Details (optional)
Additional technical information if relevant.
Rules:
- Include a descriptive release title as H1 (suggest version number if tags are present, or a descriptive name)
- Be concise and user-friendly
- Group related changes together
- Highlight breaking changes prominently
- Use clear, descriptive bullet points
- Focus on user-facing changes, not implementation details
- Output ONLY the release notes in markdown format, nothing else — no explanation, no quotes, no markdown fences
# Context
- Repository: $RepoName
- Branch: $Branch
- Source: $SourceLabel
## Commit Log
$CommitLog
## Changed Files
$FileList
## Diff Summary
$Stat
## Full Diff
$Diff
"@
# --- Call agent ---
Write-Host ""
Write-Host "Generating release notes via $Agent ..." -ForegroundColor Cyan
Write-Host ""
try {
if ($Agent -like "*copilot*") {
$RawOutput = & $Agent -p --trust $Prompt 2>&1
if ($LASTEXITCODE -ne 0) { throw "Agent call failed." }
# Clean copilot output
$InMessage = $false
$Message = ""
foreach ($line in ($RawOutput -split "`n")) {
# Skip usage stats and tool execution lines
if ($line -match '^Total usage est:|^API time spent:|^Total session time:|^Total code changes:|^Breakdown by AI model:|^ claude-|^ gpt-|^●|^ \$|^ └') {
continue
}
# Skip empty lines before message starts
if (-not $InMessage -and $line -match '^\s*$') {
continue
}
# Once we hit content (markdown heading), start collecting
if ($line -match '^#') {
$InMessage = $true
}
if ($InMessage) {
if ($Message) { $Message += "`n" }
$Message += $line
}
}
} else {
$Message = (& $Agent -p --trust $Prompt 2>&1) -join "`n"
if ($LASTEXITCODE -ne 0) { throw "Agent call failed." }
}
} catch {
Write-Error $_
exit 1
}
# --- Output ---
Write-Host "--- Release Notes ---" -ForegroundColor Green
Write-Host ""
Write-Host $Message
if ($OutputFile) {
$Message | Out-File -FilePath $OutputFile -Encoding UTF8
Write-Host ""
Write-Host "Saved to $OutputFile" -ForegroundColor Green
} else {
# Try to copy to clipboard
try {
$Message | Set-Clipboard
Write-Host ""
Write-Host "Copied to clipboard." -ForegroundColor Green
} catch {
# Clipboard not available, that's okay
}
}
exit 0