-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathreleaseTools.psm1
197 lines (175 loc) · 5.2 KB
/
releaseTools.psm1
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
using module PowerShellForGitHub
using namespace System.Management.Automation
$script:PullRequests = @()
$script:BugFixes = @()
$script:NewFeatures = @()
$script:Repo = Get-GitHubRepository -OwnerName PowerShell -RepositoryName PSResourceGet
$script:Path = (Get-Item $PSScriptRoot).Parent.FullName
$script:ChangelogFile = "$Path/CHANGELOG.md"
<#
.SYNOPSIS
Creates and checks out the `release` branch if not already existing
#>
function Update-Branch {
[CmdletBinding(SupportsShouldProcess)]
param()
$Branch = git branch --show-current
if ($Branch -ne "release") {
if ($PSCmdlet.ShouldProcess("release", "git checkout -B")) {
git checkout -B "release"
}
}
}
<#
.SYNOPSIS
Formats the pull requests into bullet points
#>
function Get-Bullet {
param(
[Parameter(Mandatory, ValueFromPipeline)]
[PSCustomObject]$PullRequest
)
("-", $PullRequest.title, ("(#" + $PullRequest.PullRequestNumber + ")") -join " ").Trim()
}
<#
.SYNOPSIS
Gets and categorizes the CHANGELOG content with PRs merged since the last release.
.DESCRIPTION
Uses the local Git repositories but does not pull, so ensure HEAD is where you
want it.
#>
function Get-Changelog {
# This will take some time because it has to pull all PRs and then filter
$script:PullRequests = $script:Repo | Get-GitHubPullRequest -State 'closed' |
Where-Object { $_.labels.LabelName -match 'Release' } |
Where-Object { -not $_.title.StartsWith("[WIP]") } |
Where-Object { -not $_.title.StartsWith("WIP") }
$PullRequests | ForEach-Object {
if ($_.labels.LabelName -match 'PR-Bug') {
$script:BugFixes += Get-Bullet($_)
}
else {
$script:NewFeatures += Get-Bullet($_)
}
}
}
<#
.SYNOPSIS
Creates the CHANGELOG content
#>
function Set-Changelog {
param(
[Parameter(Mandatory)]
[string]$Version
)
@(
"## $Version"
""
"### New Features"
$script:NewFeatures
""
"### Bug Fixes"
$script:BugFixes
""
)
}
<#
.SYNOPSIS
Updates the CHANGELOG file
#>
function Update-Changelog {
param(
[Parameter(Mandatory)]
[string]$Version
)
Get-Changelog
$CurrentChangeLog = Get-Content -Path $script:ChangelogFile
@(
$CurrentChangeLog[0..1]
Set-Changelog $Version
$CurrentChangeLog[2..$CurrentChangeLog.Length]
) | Set-Content -Encoding utf8NoBOM -Path $script:ChangelogFile
}
<#
.SYNOPSIS
Updates the PSResourceGet.psd1 file
# Note: Update ModuleVersion and Prerelease after this, manually.
#>
function Update-PSDFile {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)]
[string]$Version
)
$CurrentPSDFile = Get-Content -Path ".\src\PSResourceGet.psd1"
$Header = $CurrentPSDFile.Where({$_.StartsWith("## ")}, "First")
@(
$CurrentPSDFile.Where({ $_ -eq $Header }, "Until")
Set-Changelog $Version
$CurrentPSDFile.Where({ $_ -eq $Header }, "SkipUntil")
) | Set-Content -Encoding utf8NoBOM -Path ".\src\PSResourceGet.psd1"
if ($PSCmdlet.ShouldProcess(".\src\PSResourceGet.psd1", "git add")) {
git add "src\PSResourceGet.psd1"
}
}
<#
.SYNOPSIS
Creates a draft GitHub PR to update the CHANGELOG.md file
#>
function New-ReleasePR {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Mandatory)]
[string]$Version,
[Parameter(Mandatory)]
[string]$Username
)
Update-Branch
if ($PSCmdlet.ShouldProcess("$script:ChangelogFile", "git commit")) {
git add $ChangelogFile
git commit -m "Update CHANGELOG for ``$Version``"
}
if ($PSCmdlet.ShouldProcess("release", "git push")) {
Write-Host "Pushing release branch..."
git push --force-with-lease origin release
}
$Params = @{
Head = "$($Username):release"
Base = "master"
Draft = $true
Title = "Update CHANGELOG for ``$Version``"
Body = "An automated PR to update the CHANGELOG.md file for a new release"
}
$PR = $script:Repo | New-GitHubPullRequest @Params
Write-Host "Draft PR URL: $($PR.html_url)"
}
<#
.SYNOPSIS
Given the version and the username for the forked repository, updates the CHANGELOG.md file and creates a draft GitHub PR
#>
function New-Release {
param(
[Parameter(Mandatory)]
[string]$Version,
[Parameter(Mandatory)]
[string]$Username
)
Update-Changelog $Version
Update-PSDFile $Version
New-ReleasePR -Version $Version -Username $Username
}
<#
.SYNOPSIS
Removes the `Release` label after updating the CHANGELOG.md file
#>
function Remove-Release-Label {
$script:PullRequests = $script:Repo | Get-GitHubPullRequest -State 'closed' |
Where-Object { $_.labels.LabelName -match 'Release' } |
Where-Object { -not $_.title.StartsWith("[WIP]") } |
Where-Object { -not $_.title.StartsWith("WIP") }
$script:PullRequests | ForEach-Object {
$script:Repo | Remove-GitHubIssueLabel -Label Release -Issue $_.PullRequestNumber
}
}