Skip to content

GitHub release script #1937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 99 additions & 1 deletion tools/GitHubTools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,102 @@ function New-GitHubPR
Invoke-RestMethod -Method Post -Uri $uri -Body $body -Headers $headers
}

Export-ModuleMember -Function Copy-GitRepository,Submit-GitChanges,New-GitHubPR
function Publish-GitHubRelease
{
param(
[Parameter(Mandatory)]
[string]
$Organization,

[Parameter(Mandatory)]
[string]
$Repository,

[Parameter(Mandatory)]
[string]
$Tag,

[Parameter(Mandatory)]
[string]
$ReleaseName,

[Parameter(Mandatory)]
[string]
$Description,

[Parameter(Mandatory)]
[string]
$GitHubToken,

[Parameter()]
[Alias('Branch', 'Commit')]
[string]
$Commitish,

[Parameter()]
[string[]]
$AssetPath,

[switch]
$Draft,

[switch]
$Prerelease
)

$restParams = @{
tag_name = $Tag
name = $ReleaseName
body = $Description
draft = [bool]$Draft
prerelease = [bool]$Prerelease
}

if ($Commitish)
{
$restParams.target_commitish = $Commitish
}

$restBody = ConvertTo-Json -InputObject $restParams
$uri = "https://api.github.com/repos/$Organization/$Repository/releases"
$headers = @{
Accept = 'application/vnd.github.v3+json'
Authorization = "token $GitHubToken"
}

$response = Invoke-RestMethod -Method Post -Uri $uri -Body $restBody -Headers $headers

$releaseId = $response.id
$assetBaseUri = "https://uploads.github.com/repos/$Organization/$Repository/releases/$releaseId/assets"
foreach ($asset in $AssetPath)
{
$extension = [System.IO.Path]::GetExtension($asset)
$fileName = [uri]::EscapeDataString([System.IO.Path]::GetFileName($asset))
$contentType = 'text/plain'
switch ($extension)
{
{ $_ -in '.zip','.vsix' }
{
$contentType = 'application/zip'
break
}

'.json'
{
$contentType = 'application/json'
break
}
}

$assetUri = "${assetBaseUri}?name=$fileName"
$headers = @{
Authorization = "token $GitHubToken"
}
# This can be very slow, but it does work
$null = Invoke-RestMethod -Method Post -Uri $assetUri -InFile $asset -ContentType $contentType -Headers $headers
}

return $response
}

Export-ModuleMember -Function Copy-GitRepository,Submit-GitChanges,New-GitHubPR,Publish-GitHubRelease
65 changes: 65 additions & 0 deletions tools/postReleaseScripts/publishGHRelease.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

#requires -Version 6.0

param(
[Parameter(Mandatory)]
[semver]
$Version,

[Parameter(Mandatory)]
[string]
$GitHubToken,

[Parameter(Mandatory)]
[string]
$Repository,

[Parameter()]
[string]
$TargetFork = 'PowerShell',

[Parameter()]
[string]
$ChangelogPath = "$PSScriptRoot/../../CHANGELOG.md",

[Parameter()]
[string[]]
$AssetPath
)

Import-Module "$PSScriptRoot/../GitHubTools.psm1" -Force

function GetDescriptionFromChangelog
{
param(
[Parameter(Mandatory)]
[string]
$ChangelogPath
)

$lines = Get-Content -Path $ChangelogPath
$sb = [System.Text.StringBuilder]::new($lines[2])
for ($i = 3; -not $lines[$i].StartsWith('## '); $i++)
{
$null = $sb.Append("`n").Append($lines[$i])
}

return $sb.ToString()
}

$tag = "v$Version"

$releaseParams = @{
Organization = $TargetFork
Repository = $Repository
Tag = $tag
ReleaseName = $tag
Branch = "release/$Version"
AssetPath = $AssetPath
Prerelease = [bool]($Version.PreReleaseLabel)
Description = GetDescriptionFromChangelog -ChangelogPath $ChangelogPath
GitHubToken = $GitHubToken
}
Publish-GitHubRelease @releaseParams