forked from justeattakeaway/httpclient-interception
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-dotnet-sdk.ps1
More file actions
executable file
·401 lines (328 loc) · 11.8 KB
/
update-dotnet-sdk.ps1
File metadata and controls
executable file
·401 lines (328 loc) · 11.8 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#! /usr/bin/pwsh
<#
.SYNOPSIS
Attempts to update the version of the .NET SDK used by the repository's global.json file.
.DESCRIPTION
Attempts to update the version of the .NET SDK specified in a global.json file to the latest release of
the .NET SDK for a release channel of .NET, as specified by the https://github.com/dotnet/core repository.
.PARAMETER Channel
Default: 3.1
The .NET release channel to download the SDK for (2.1, 3.1, 5.0, etc.).
.PARAMETER BranchName
The optional Git branch name to use.
.PARAMETER CommitMessage
The optional Git commit message to use.
.PARAMETER GitHubToken
The optional GitHub token to use to create a pull request for any update.
.PARAMETER GlobalJsonFile
Default: ./global.json
The optional path to the global.json file to update.
.PARAMETER UserEmail
The optional email address to use for the Git commit.
.PARAMETER UserName
The optional user name to use for the Git commit.
.PARAMETER DryRun
If set, will not actually make changes to the file system, Git or GitHub.
.PARAMETER Verbose
Displays additional diagnostics information.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)][string] $Channel = "3.1",
[Parameter(Mandatory = $false)][string] $BranchName = "",
[Parameter(Mandatory = $false)][string] $CommitMessage = "",
[Parameter(Mandatory = $false)][string] $GitHubToken = "",
[Parameter(Mandatory = $false)][string] $GlobalJsonFile = "./global.json",
[Parameter(Mandatory = $false)][string] $UserEmail = "",
[Parameter(Mandatory = $false)][string] $UserName = "",
[Parameter(Mandatory = $false)][switch] $DryRun
)
function Get-Global-Json-Version([string] $FileName) {
if (-Not (Test-Path $FileName)) {
throw "Unable to find '$FileName'"
}
try {
$JsonContent = Get-Json-From-File $FileName | Select-Object -Expand "sdk" -ErrorAction SilentlyContinue
}
catch {
throw "JSON file unreadable: '$FileName'"
}
$Version = $null
if ($JsonContent) {
Say-Verbose "JSON: $JsonContent"
try {
$JsonContent.PSObject.Properties | ForEach-Object {
$PropertyName = $_.Name
if ($PropertyName -eq "version") {
$Version = $_.Value
}
}
}
catch {
throw "Unable to parse the SDK node in '$FileName'"
}
}
else {
throw "Unable to find the SDK node in '$FileName'"
}
if ($null -eq $Version) {
throw "Unable to find the SDK:version node in '$FileName'"
}
return $Version
}
function Get-Latest-SDK-Version([string] $FileName) {
if (-Not (Test-Path $FileName)) {
throw "Unable to find '$FileName'"
}
try {
$JsonContent = Get-Json-From-File $FileName | Select-Object -ErrorAction SilentlyContinue
}
catch {
throw "JSON file unreadable: '$FileName'"
}
$Version = $null
if ($JsonContent) {
Say-Verbose "JSON: $JsonContent"
try {
$JsonContent.PSObject.Properties | ForEach-Object {
$PropertyName = $_.Name
if ($PropertyName -eq "latest-sdk") {
$Version = $_.Value
}
}
}
catch {
throw "Unable to parse the root node in '$FileName'"
}
}
else {
throw "Unable to find the root node in '$FileName'"
}
if ($null -eq $Version) {
throw "Unable to find the latest-sdk node in '$FileName'"
}
return $Version
}
function Get-Latest-Runtime-Version([string] $FileName, [string] $SdkVersion, [bool] $AllowNull = $false) {
if (-Not (Test-Path $FileName)) {
throw "Unable to find '$FileName'"
}
try {
$JsonContent = Get-Json-From-File $FileName | Select-Object -Expand "releases" -ErrorAction SilentlyContinue
}
catch {
throw "JSON file unreadable: '$FileName'"
}
$Version = $null
if ($JsonContent) {
Say-Verbose "JSON: $JsonContent"
try {
foreach ($_ in $JsonContent) {
if ($_.sdk.version -eq $SdkVersion) {
$Version = $_.sdk."runtime-version"
break;
}
}
}
catch {
throw "Unable to parse the releases sdk node in '$FileName'"
}
if ($null -eq $Version) {
try {
foreach ($_ in $JsonContent) {
if ($_.sdks.version -eq $SdkVersion) {
$Version = $_.sdks."runtime-version" | Select -First 1
break;
}
}
}
catch {
throw "Unable to parse the releases sdks node in '$FileName'"
}
}
}
else {
throw "Unable to find the releases node in '$FileName'"
}
if (($null -eq $Version) -And ($AllowNull -eq $false)) {
throw "Unable to find the releases node in '$FileName' for SDK version $SdkVersion"
}
return $Version
}
function Get-Json-From-File([string]$FileName) {
if (-Not (Test-Path $FileName)) {
throw "Unable to find '$FileName'"
}
try {
return Get-Content($FileName) -Raw | ConvertFrom-Json
}
catch {
throw "JSON file unreadable: '$FileName'"
}
}
function Say([string] $message) {
Write-Host "update-dotnet-sdk: $message"
}
function Say-Verbose([string] $message) {
Write-Verbose "update-dotnet-sdk: $message"
}
$CurrentSDKVersion = Get-Global-Json-Version $GlobalJsonFile
Say "Current .NET SDK version is $CurrentSDKVersion"
$ReleaseNotesUrl = "https://raw.githubusercontent.com/dotnet/core/master/release-notes/$Channel/releases.json"
$ReleaseNotesPath = [System.IO.Path]::Combine([System.IO.Path]::GetTempPath(), [System.IO.Path]::GetRandomFileName())
Say-Verbose "Downloading .NET $Channel release notes JSON from $ReleaseNotesUrl to $ReleaseNotesPath..."
$OldProgressPreference = $ProgressPreference
$ProgressPreference = "SilentlyContinue"
try {
Invoke-WebRequest $ReleaseNotesUrl -OutFile $ReleaseNotesPath -UseBasicParsing
}
catch {
Say "Cannot download $ReleaseNotesUrl"
throw
}
finally {
$ProgressPreference = $OldProgressPreference
}
$LatestSDKVersion = Get-Latest-SDK-Version $ReleaseNotesPath
$LatestRuntimeVersion = Get-Latest-Runtime-Version $ReleaseNotesPath $LatestSDKVersion
$CurrentRuntimeVersion = Get-Latest-Runtime-Version $ReleaseNotesPath $CurrentSDKVersion -AllowNull ($LatestSDKVersion -lt $CurrentSDKVersion)
if ($null -eq $CurrentRuntimeVersion) {
Say "Unable to determine runtime version for .NET SDK version $CurrentSDKVersion."
return
}
Say-Verbose "Current .NET runtime version is $CurrentRuntimeVersion"
Say "Latest .NET SDK version for channel '$Channel' is $LatestSDKVersion (runtime version $LatestRuntimeVersion)"
if ($CurrentSDKVersion -ge $LatestSDKVersion) {
Say "The .NET SDK version specified by '$GlobalJsonFile' is up-to-date"
return
}
Say-Verbose "Updating SDK version in '$GlobalJsonFile' to $LatestSDKVersion..."
$GlobalJson = Get-Json-From-File $GlobalJsonFile
$GlobalJson.sdk.version = $LatestSDKVersion
if ($DryRun) {
Say "Skipped update of SDK version in '$GlobalJsonFile' to $LatestSDKVersion"
}
else {
$GlobalJson | ConvertTo-Json | Set-Content -Path $GlobalJsonFile
Say "Updated SDK version in '$GlobalJsonFile' to $LatestSDKVersion"
}
if (-Not $BranchName) {
$BranchName = "update-dotnet-sdk-$LatestSDKVersion".ToLowerInvariant()
}
if (-Not $CommitMessage) {
$CommitMessage = "Update .NET SDK`n`nUpdate .NET SDK to version $LatestSDKVersion."
}
Say-Verbose "Commit message: $CommitMessage"
# Set the remote for GitHub Actions to detect if the branch/PR has already been created
if ($env:CI) {
git remote set-url origin https://github.com/$env:GITHUB_REPOSITORY.git | Out-Null
try {
git fetch origin | Out-Null
}
catch {
# HACK - It worked, ignore exit code 1
}
}
$Base = (git rev-parse --abbrev-ref HEAD | Out-String).Trim()
$BranchExists = (git rev-parse --verify --quiet remotes/origin/$BranchName | Out-String).Trim()
if ($BranchExists) {
Say "The $BranchName branch already exists"
return
}
if ($UserName) {
if ($DryRun) {
Say "Skipped update of git user name to '$UserName'"
}
else {
git config user.name $UserName
Say "Updated git user name to '$UserName'"
}
}
if ($UserEmail) {
if ($DryRun) {
Say "Skipped update of git user email to '$UserEmail'"
}
else {
git config user.email $UserEmail
Say "Updated git user email to '$UserEmail'"
}
}
if ($DryRun) {
Say "Skipped git checkout for branch $BranchName"
}
else {
try {
git checkout -b $BranchName | Out-Null
}
catch {
# HACK - It worked, ignore exit code 1
}
Say-Verbose "Created git branch $BranchName"
}
if ($DryRun) {
Say "Skipped git commit for SDK update on branch $BranchName"
}
else {
git add $GlobalJsonFile | Out-Null
Say-Verbose "Staged git commit for '$GlobalJsonFile'"
git commit -m $CommitMessage | Out-Null
$GitSha = (git log --format="%H" -n 1 | Out-String).Substring(0, 7)
Say "Commited .NET SDK update to git ($GitSha)"
}
if ($env:CI -And $GitHubToken) {
if ($DryRun) {
Say "Skipped git push to origin of branch $BranchName"
}
else {
try {
git push -u origin $BranchName | Out-Null
}
catch {
# HACK - It worked, ignore exit code 1
}
Say "Pushed changes to repository $env:GITHUB_REPOSITORY"
}
$PullRequestUri = "https://api.github.com/repos/$env:GITHUB_REPOSITORY/pulls"
$Headers = @{
"Accept" = "application/vnd.github.v3+json";
"Authorization" = "token $GitHubToken";
"User-Agent" = "update-dotnet-sdk.ps1";
}
$PullRequestBody = "Updates the .NET SDK to version [``$LatestSDKVersion``](https://github.com/dotnet/core/blob/master/release-notes/$Channel/$LatestRuntimeVersion/$LatestSDKVersion-download.md), "
if ($LatestRuntimeVersion -eq $CurrentRuntimeVersion) {
$PullRequestBody += "which includes version [``$LatestRuntimeVersion``](https://github.com/dotnet/core/blob/master/release-notes/$Channel/$LatestRuntimeVersion/$LatestRuntimeVersion.md) of the .NET runtime."
}
else {
$PullRequestBody += "which also updates the .NET runtime from version [``$CurrentRuntimeVersion``](https://github.com/dotnet/core/blob/master/release-notes/$Channel/$CurrentRuntimeVersion/$CurrentRuntimeVersion.md) to version [``$LatestRuntimeVersion``](https://github.com/dotnet/core/blob/master/release-notes/$Channel/$LatestRuntimeVersion/$LatestRuntimeVersion.md)."
}
$PullRequestBody += "`n`nThis pull request was auto-generated by [GitHub Actions](https://github.com/$env:GITHUB_REPOSITORY/actions/runs/$env:GITHUB_RUN_ID)."
$Body = @{
"title" = "Update .NET SDK to $LatestSDKVersion";
"head" = $BranchName;
"base" = $Base;
"body" = $PullRequestBody;
"maintainer_can_modify" = $true;
"draft" = $false;
} | ConvertTo-Json
Say-Verbose "Pull Request: $Body"
if ($DryRun) {
Say "Skipped creating GitHub pull request for branch $BranchName to $Base"
}
else {
try {
$PullRequest = Invoke-RestMethod `
-Uri $PullRequestUri `
-Method POST `
-ContentType "application/json" `
-Headers $Headers `
-Body $Body
}
catch {
Say "Failed to open Pull Request"
$_.Exception | Get-Error
throw
}
Say "Created pull request #$($PullRequest.number): $($PullRequest.title)"
Say "View the pull request at $($PullRequest.html_url)"
}
}