Skip to content

Commit 1f837c1

Browse files
committed
Add GitHub release script
1 parent 82c196b commit 1f837c1

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

tools/GitHubTools.psm1

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,106 @@ function OpenGitHubPr
187187

188188
Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers
189189
}
190+
191+
function CreateNewRelease
192+
{
193+
param(
194+
[Parameter(Mandatory)]
195+
[string]
196+
$Organization,
197+
198+
[Parameter(Mandatory)]
199+
[string]
200+
$Repository,
201+
202+
[Parameter(Mandatory)]
203+
[string]
204+
$Tag,
205+
206+
[Parameter(Mandatory)]
207+
[string]
208+
$ReleaseName,
209+
210+
[Parameter(Mandatory)]
211+
[string]
212+
$Description,
213+
214+
[Parameter(Mandatory)]
215+
[string]
216+
$GitHubToken,
217+
218+
[Parameter()]
219+
[Alias('Branch', 'Commit')]
220+
[string]
221+
$Commitish,
222+
223+
[Parameter()]
224+
[string[]]
225+
$AssetPath,
226+
227+
[switch]
228+
$Draft,
229+
230+
[switch]
231+
$Prerelease
232+
)
233+
234+
$restParams = @{
235+
tag_name = $Tag
236+
name = $ReleaseName
237+
body = $Description
238+
draft = [bool]$Draft
239+
prerelease = [bool]$Prerelease
240+
}
241+
242+
if ($Commitish)
243+
{
244+
$restParams.target_commitish = $Commitish
245+
}
246+
247+
$restBody = ConvertTo-Json -InputObject $restParams
248+
$uri = "https://api.github.com/repos/$Organization/$Repository/releases"
249+
$headers = @{
250+
Accept = 'application/vnd.github.v3+json'
251+
Authorization = "token $GitHubToken"
252+
}
253+
254+
$response = Invoke-RestMethod -Method Post -Uri $uri -Body $restBody -Headers $headers
255+
256+
$releaseId = $response.id
257+
$assetBaseUri = "https://uploads.github.com/repos/$Organization/$Repository/releases/$releaseId/assets"
258+
foreach ($asset in $AssetPath)
259+
{
260+
$extension = [System.IO.Path]::GetExtension($asset)
261+
$fileName = [uri]::EscapeDataString([System.IO.Path]::GetFileName($asset))
262+
$contentType = 'text/plain'
263+
switch ($extension)
264+
{
265+
{ $_ -in '.zip','.vsix' }
266+
{
267+
$contentType = 'application/zip'
268+
break
269+
}
270+
271+
'.json'
272+
{
273+
$contentType = 'application/json'
274+
break
275+
}
276+
277+
default
278+
{
279+
$contentType = 'text/plain'
280+
}
281+
}
282+
283+
$assetUri = "${assetBaseUri}?name=$fileName"
284+
$headers = @{
285+
Authorization = "token $GitHubToken"
286+
}
287+
# This can be very slow, but it does work
288+
$null = Invoke-RestMethod -Method Post -Uri $assetUri -InFile $asset -ContentType $contentType -Headers $headers
289+
}
290+
291+
return $response
292+
}
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)