-
Notifications
You must be signed in to change notification settings - Fork 7
/
generate_changelog.ps1
80 lines (71 loc) · 2.39 KB
/
generate_changelog.ps1
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
param (
[string]$cdgitfolder,
[Parameter(Mandatory = $true)][string]$branch,
[string]$workitembaseuri = '{0}',
[string]$since,
[string]$logp
)
#$since="2021-10-01"
Write-Output "Creating Changelogs from git history using branch=$branch"
function ConcatArrayToString($array, $sep = '') {
$line = '';
for ($a = 0; $a -lt $array.Count; $a++) {
$line += $array[$a] + $sep
}
return $line
}
function DisplayArray($arr) {
#echo $arr.Count
foreach ($items in $arr) {
Write-Output $items
}
}
#setting up git command
$gitcommand = 'git rev-list --first-parent'
if (-not [string]::IsNullOrEmpty($cdgitfolder) -and -not [string]::IsNullOrWhiteSpace($cdgitfolder)) {
Set-Location $cdgitfolder
}
if (-not [string]::IsNullOrEmpty($since) -and -not [string]::IsNullOrWhiteSpace($since)) {
$gitcommand = $gitcommand + ' --date=local --since=' + $since
}
if (-not [string]::IsNullOrEmpty($branch) -and -not [string]::IsNullOrWhiteSpace($branch)) {
$gitcommand = $gitcommand + ' ' + $branch
}
Write-Output $gitcommand
#receive SHA-1 commits
[array] $shas = Invoke-Expression $gitcommand
#for ($i=0; $i -lt 2; $i++)
for ($i = 0; $i -lt $shas.length; $i++) {
$sha = $shas[$i]
$commitmessage = git show -s --format='%B' $sha
#remove new line
$messageOneLine = ConcatArrayToString($commitmessage)
#echo $messageOneLine
#extract workitems from commit message
$workitemfound = $messageOneLine -match '(#[0-9]{6})'
$workitems = New-Object System.Collections.Generic.List[string]
$workitempayload = ''
if ($workitemfound) {
foreach ($key in $Matches.Keys) {
$workitemnr = $Matches[$key]
$workitemnr = $workitemnr.replace('#', '')
$workitemuri = [string]::Format($workitembaseuri, $workitemnr)
#dont add workitems twice (if its contained in the commit message multiple times)
if ($workitems.Contains($workitemuri) -eq $false) {
$workitems.Add($workitemuri)
}
}
$workitempayload = ConcatArrayToString($workitems, ' ')
}
$since_author = git show -s --format='%ai;%an;' $sha
$committitle = git show -s --pretty='format:%s' $sha
$var = -join ($sha, ';', $since_author, $committitle, ';', $workitempayload);
#print and write to file
if (-not [string]::IsNullOrEmpty($logp) -and -not [string]::IsNullOrWhiteSpace($logp)) {
$p = $logp + "changelog.txt"
}
else {
$p = "changelog.txt"
}
Write-Output $var | Tee-Object $p -append
}