Skip to content

Commit a740196

Browse files
authored
[Ignore] GitHub release script (#1937)
1 parent 7116401 commit a740196

File tree

2 files changed

+178
-1
lines changed

2 files changed

+178
-1
lines changed

tools/GitHubTools.psm1

+99-1
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,102 @@ 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+
319+
$assetUri = "${assetBaseUri}?name=$fileName"
320+
$headers = @{
321+
Authorization = "token $GitHubToken"
322+
}
323+
# This can be very slow, but it does work
324+
$null = Invoke-RestMethod -Method Post -Uri $assetUri -InFile $asset -ContentType $contentType -Headers $headers
325+
}
326+
327+
return $response
328+
}
329+
330+
Export-ModuleMember -Function Copy-GitRepository,Submit-GitChanges,New-GitHubPR,Publish-GitHubRelease
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
#requires -Version 6.0
5+
6+
param(
7+
[Parameter(Mandatory)]
8+
[semver]
9+
$Version,
10+
11+
[Parameter(Mandatory)]
12+
[string]
13+
$GitHubToken,
14+
15+
[Parameter(Mandatory)]
16+
[string]
17+
$Repository,
18+
19+
[Parameter()]
20+
[string]
21+
$TargetFork = 'PowerShell',
22+
23+
[Parameter()]
24+
[string]
25+
$ChangelogPath = "$PSScriptRoot/../../CHANGELOG.md",
26+
27+
[Parameter()]
28+
[string[]]
29+
$AssetPath
30+
)
31+
32+
Import-Module "$PSScriptRoot/../GitHubTools.psm1" -Force
33+
34+
<#
35+
.SYNOPSIS
36+
Get the release description from the CHANGELOG
37+
38+
.DESCRIPTION
39+
Gets the latest CHANGELOG entry from the CHANGELOG for use as the GitHub release description
40+
41+
.PARAMETER ChangelogPath
42+
Path to the changelog file
43+
#>
44+
45+
function GetDescriptionFromChangelog
46+
{
47+
param(
48+
[Parameter(Mandatory)]
49+
[string]
50+
$ChangelogPath
51+
)
52+
53+
$lines = Get-Content -Path $ChangelogPath
54+
# First two lines are the title and newline
55+
# Third looks like '## vX.Y.Z-releasetag'
56+
$sb = [System.Text.StringBuilder]::new($lines[2])
57+
# Read through until the next '## vX.Y.Z-releasetag' H2
58+
for ($i = 3; -not $lines[$i].StartsWith('## '); $i++)
59+
{
60+
$null = $sb.Append("`n").Append($lines[$i])
61+
}
62+
63+
return $sb.ToString()
64+
}
65+
66+
$tag = "v$Version"
67+
68+
$releaseParams = @{
69+
Organization = $TargetFork
70+
Repository = $Repository
71+
Tag = $tag
72+
ReleaseName = $tag
73+
Branch = "release/$Version"
74+
AssetPath = $AssetPath
75+
Prerelease = [bool]($Version.PreReleaseLabel)
76+
Description = GetDescriptionFromChangelog -ChangelogPath $ChangelogPath
77+
GitHubToken = $GitHubToken
78+
}
79+
Publish-GitHubRelease @releaseParams

0 commit comments

Comments
 (0)