Skip to content

Commit c002bc2

Browse files
authored
Automate updating CHANGELOG.md file (#817)
1 parent 799570d commit c002bc2

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed

tool/releaseTools.psm1

+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
using module PowerShellForGitHub
5+
using namespace System.Management.Automation
6+
7+
$script:PullRequests = @()
8+
$script:BugFixes = @()
9+
$script:NewFeatures = @()
10+
$script:Repo = Get-GitHubRepository -OwnerName PowerShell -RepositoryName PowerShellGet
11+
$script:Path = (Get-Item $PSScriptRoot).Parent.FullName
12+
$script:ChangelogFile = "$Path/CHANGELOG.md"
13+
14+
<#
15+
.SYNOPSIS
16+
Creates and checks out the `release` branch if not already existing
17+
#>
18+
function Update-Branch {
19+
[CmdletBinding(SupportsShouldProcess)]
20+
param()
21+
$Branch = git branch --show-current
22+
if ($Branch -ne "release") {
23+
if ($PSCmdlet.ShouldProcess("release", "git checkout -B")) {
24+
git checkout -B "release"
25+
}
26+
}
27+
}
28+
29+
<#
30+
.SYNOPSIS
31+
Formats the pull requests into bullet points
32+
#>
33+
function Get-Bullet {
34+
param(
35+
[Parameter(Mandatory, ValueFromPipeline)]
36+
[PSCustomObject]$PullRequest
37+
)
38+
("-", $PullRequest.title, ("(#" + $PullRequest.PullRequestNumber + ")") -join " ").Trim()
39+
}
40+
41+
<#
42+
.SYNOPSIS
43+
Gets and categorizes the CHANGELOG content with PRs merged since the last release.
44+
.DESCRIPTION
45+
Uses the local Git repositories but does not pull, so ensure HEAD is where you
46+
want it.
47+
#>
48+
function Get-Changelog {
49+
# This will take some time because it has to pull all PRs and then filter
50+
$script:PullRequests = $script:Repo | Get-GitHubPullRequest -State 'closed' |
51+
Where-Object { $_.labels.LabelName -match 'Release' } |
52+
Where-Object { -not $_.title.StartsWith("[WIP]") } |
53+
Where-Object { -not $_.title.StartsWith("WIP") }
54+
55+
$PullRequests | ForEach-Object {
56+
if ($_.labels.LabelName -match 'PR-Bug') {
57+
$script:BugFixes += Get-Bullet($_)
58+
}
59+
else {
60+
$script:NewFeatures += Get-Bullet($_)
61+
}
62+
}
63+
}
64+
65+
<#
66+
.SYNOPSIS
67+
Creates the CHANGELOG content
68+
#>
69+
function Set-Changelog {
70+
param(
71+
[Parameter(Mandatory)]
72+
[string]$Version
73+
)
74+
@(
75+
"## $Version"
76+
""
77+
"### New Features"
78+
$script:NewFeatures
79+
""
80+
"### Bug Fixes"
81+
$script:BugFixes
82+
""
83+
)
84+
}
85+
86+
<#
87+
.SYNOPSIS
88+
Updates the CHANGELOG file
89+
#>
90+
function Update-Changelog {
91+
param(
92+
[Parameter(Mandatory)]
93+
[string]$Version
94+
)
95+
Get-Changelog
96+
97+
$CurrentChangeLog = Get-Content -Path $script:ChangelogFile
98+
@(
99+
$CurrentChangeLog[0..1]
100+
Set-Changelog $Version
101+
$CurrentChangeLog[2..$CurrentChangeLog.Length]
102+
) | Set-Content -Encoding utf8NoBOM -Path $script:ChangelogFile
103+
}
104+
105+
<#
106+
.SYNOPSIS
107+
Updates the PowerShellGet.psd1 file
108+
# Note: Update ModuleVersion and Prerelease after this, manually.
109+
#>
110+
function Update-PSDFile {
111+
[CmdletBinding(SupportsShouldProcess)]
112+
param(
113+
[Parameter(Mandatory)]
114+
[string]$Version
115+
)
116+
$CurrentPSDFile = Get-Content -Path ".\src\PowerShellGet.psd1"
117+
$Header = $CurrentPSDFile.Where({$_.StartsWith("## ")}, "First")
118+
119+
@(
120+
$CurrentPSDFile.Where({ $_ -eq $Header }, "Until")
121+
Set-Changelog $Version
122+
$CurrentPSDFile.Where({ $_ -eq $Header }, "SkipUntil")
123+
) | Set-Content -Encoding utf8NoBOM -Path ".\src\PowerShellGet.psd1"
124+
125+
if ($PSCmdlet.ShouldProcess(".\src\PowerShellGet.psd1", "git add")) {
126+
git add "src\PowerShellGet.psd1"
127+
}
128+
}
129+
130+
<#
131+
.SYNOPSIS
132+
Creates a draft GitHub PR to update the CHANGELOG.md file
133+
#>
134+
function New-ReleasePR {
135+
[CmdletBinding(SupportsShouldProcess)]
136+
param(
137+
[Parameter(Mandatory)]
138+
[string]$Version,
139+
140+
[Parameter(Mandatory)]
141+
[string]$Username
142+
)
143+
144+
Update-Branch
145+
if ($PSCmdlet.ShouldProcess("$script:ChangelogFile", "git commit")) {
146+
git add $ChangelogFile
147+
git commit -m "Update CHANGELOG for ``$Version``"
148+
}
149+
150+
if ($PSCmdlet.ShouldProcess("release", "git push")) {
151+
Write-Host "Pushing release branch..."
152+
git push --force-with-lease origin release
153+
}
154+
155+
$Params = @{
156+
Head = "$($Username):release"
157+
Base = "master"
158+
Draft = $true
159+
Title = "Update CHANGELOG for ``$Version``"
160+
Body = "An automated PR to update the CHANGELOG.md file for a new release"
161+
}
162+
163+
$PR = $script:Repo | New-GitHubPullRequest @Params
164+
Write-Host "Draft PR URL: $($PR.html_url)"
165+
}
166+
167+
<#
168+
.SYNOPSIS
169+
Given the version and the username for the forked repository, updates the CHANGELOG.md file and creates a draft GitHub PR
170+
#>
171+
function New-Release {
172+
param(
173+
[Parameter(Mandatory)]
174+
[string]$Version,
175+
176+
[Parameter(Mandatory)]
177+
[string]$Username
178+
)
179+
Update-Changelog $Version
180+
Update-PSDFile $Version
181+
New-ReleasePR -Version $Version -Username $Username
182+
}
183+
184+
<#
185+
.SYNOPSIS
186+
Removes the `Release` label after updating the CHANGELOG.md file
187+
#>
188+
function Remove-Release-Label {
189+
$script:PullRequests = $script:Repo | Get-GitHubPullRequest -State 'closed' |
190+
Where-Object { $_.labels.LabelName -match 'Release' } |
191+
Where-Object { -not $_.title.StartsWith("[WIP]") } |
192+
Where-Object { -not $_.title.StartsWith("WIP") }
193+
194+
$script:PullRequests | ForEach-Object {
195+
$script:Repo | Remove-GitHubIssueLabel -Label Release -Issue $_.PullRequestNumber
196+
}
197+
}

tool/setupReleaseTools.ps1

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
param(
5+
[Parameter(Mandatory)]
6+
[string]$Token
7+
)
8+
9+
Write-Host "Install and import PowerShell modules"
10+
Set-PSResourceRepository -Name PSGallery -Trusted | Out-Null
11+
Install-Module -Name PowerShellForGitHub -Scope CurrentUser -Force
12+
Import-Module $PSScriptRoot/releaseTools.psm1
13+
14+
Write-Host "Setup authentication"
15+
Set-GitHubConfiguration -SuppressTelemetryReminder
16+
$password = ConvertTo-SecureString -String $Token -AsPlainText -Force
17+
Set-GitHubAuthentication -Credential (New-Object System.Management.Automation.PSCredential ("token", $password))

0 commit comments

Comments
 (0)