Skip to content

Commit 45fe271

Browse files
rjmholtRobert Holt
authored andcommitted
Add GitHub release script
1 parent bdd250d commit 45fe271

File tree

2 files changed

+172
-1
lines changed

2 files changed

+172
-1
lines changed

tools/GitHubTools.psm1

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,107 @@ function New-GitHubPR
229229
Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers
230230
}
231231

232-
Export-ModuleMember -Function Copy-GitRepository,Submit-GitChanges,New-GitHubPR
232+
function Publish-GitHubRelease
233+
{
234+
param(
235+
[Parameter(Mandatory)]
236+
[string]
237+
$Organization,
238+
239+
[Parameter(Mandatory)]
240+
[string]
241+
$Repository,
242+
243+
[Parameter(Mandatory)]
244+
[string]
245+
$Tag,
246+
247+
[Parameter(Mandatory)]
248+
[string]
249+
$ReleaseName,
250+
251+
[Parameter(Mandatory)]
252+
[string]
253+
$Description,
254+
255+
[Parameter(Mandatory)]
256+
[string]
257+
$GitHubToken,
258+
259+
[Parameter()]
260+
[Alias('Branch', 'Commit')]
261+
[string]
262+
$Commitish,
263+
264+
[Parameter()]
265+
[string[]]
266+
$AssetPath,
267+
268+
[switch]
269+
$Draft,
270+
271+
[switch]
272+
$Prerelease
273+
)
274+
275+
$restParams = @{
276+
tag_name = $Tag
277+
name = $ReleaseName
278+
body = $Description
279+
draft = [bool]$Draft
280+
prerelease = [bool]$Prerelease
281+
}
282+
283+
if ($Commitish)
284+
{
285+
$restParams.target_commitish = $Commitish
286+
}
287+
288+
$restBody = ConvertTo-Json -InputObject $restParams
289+
$uri = "https://api.github.com/repos/$Organization/$Repository/releases"
290+
$headers = @{
291+
Accept = 'application/vnd.github.v3+json'
292+
Authorization = "token $GitHubToken"
293+
}
294+
295+
$response = Invoke-RestMethod -Method Post -Uri $uri -Body $restBody -Headers $headers
296+
297+
$releaseId = $response.id
298+
$assetBaseUri = "https://uploads.github.com/repos/$Organization/$Repository/releases/$releaseId/assets"
299+
foreach ($asset in $AssetPath)
300+
{
301+
$extension = [System.IO.Path]::GetExtension($asset)
302+
$fileName = [uri]::EscapeDataString([System.IO.Path]::GetFileName($asset))
303+
$contentType = 'text/plain'
304+
switch ($extension)
305+
{
306+
{ $_ -in '.zip','.vsix' }
307+
{
308+
$contentType = 'application/zip'
309+
break
310+
}
311+
312+
'.json'
313+
{
314+
$contentType = 'application/json'
315+
break
316+
}
317+
318+
default
319+
{
320+
$contentType = 'text/plain'
321+
}
322+
}
323+
324+
$assetUri = "${assetBaseUri}?name=$fileName"
325+
$headers = @{
326+
Authorization = "token $GitHubToken"
327+
}
328+
# This can be very slow, but it does work
329+
$null = Invoke-RestMethod -Method Post -Uri $assetUri -InFile $asset -ContentType $contentType -Headers $headers
330+
}
331+
332+
return $response
333+
}
334+
335+
Export-ModuleMember -Function Copy-GitRepository,Submit-GitChanges,New-GitHubPR
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
param(
5+
[Parameter(Mandatory)]
6+
[semver]
7+
$Version,
8+
9+
[Parameter(Mandatory)]
10+
[string]
11+
$GitHubToken,
12+
13+
[Parameter()]
14+
[string]
15+
$TargetFork = 'PowerShell',
16+
17+
[Parameter()]
18+
[string]
19+
$ChangelogPath = "$PSScriptRoot/../../CHANGELOG.md",
20+
21+
[Parameter()]
22+
[string[]]
23+
$AssetPath
24+
)
25+
26+
Import-Module "$PSScriptRoot/../GitHubTools.psm1"
27+
28+
function GetDescriptionFromChangelog
29+
{
30+
param(
31+
[Parameter(Mandatory)]
32+
[string]
33+
$ChangelogPath
34+
)
35+
36+
$lines = Get-Content -Path $ChangelogPath
37+
$sb = [System.Text.StringBuilder]::new($lines[2])
38+
for ($i = 3; -not $lines[$i].StartsWith('## '); $i++)
39+
{
40+
$null = $sb.Append("`n").Append($lines[$i])
41+
}
42+
43+
return $sb.ToString()
44+
}
45+
46+
$tag = "v$Version"
47+
48+
if (-not $PSBoundParameters.ContainsKey('AssetPath'))
49+
{
50+
$AssetPath = @(
51+
"$PSScriptRoot/../../PowerShell-$Version.vsix"
52+
"$PSScriptRoot/../../scripts/Install-VSCode.ps1"
53+
)
54+
}
55+
56+
$releaseParams = @{
57+
Organization = $TargetFork
58+
Repository = 'vscode-PowerShell'
59+
Tag = $tag
60+
ReleaseName = $tag
61+
Branch = "release/$Version"
62+
AssetPath = $AssetPath
63+
Prerelease = [bool]($Version.PreReleaseLabel)
64+
Description = GetDescriptionFromChangelog -ChangelogPath $ChangelogPath
65+
GitHubToken = $GitHubToken
66+
}
67+
CreateNewRelease @releaseParams
68+

0 commit comments

Comments
 (0)