Skip to content

Commit

Permalink
Final updates and expansive UT improvements for pipelining
Browse files Browse the repository at this point in the history
  • Loading branch information
HowardWolosky committed Jul 9, 2020
1 parent 67f10c7 commit 4d75347
Show file tree
Hide file tree
Showing 2 changed files with 943 additions and 162 deletions.
180 changes: 98 additions & 82 deletions GitHubReleases.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ filter New-GitHubRelease
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
them individually.
.PARAMETER TagName
.PARAMETER Tag
The name of the tag. The tag will be created around the committish if it doesn't exist
in the remote, and will need to be synced back to the local repository afterwards.
Expand Down Expand Up @@ -298,7 +298,7 @@ filter New-GitHubRelease
GitHub.Release
.EXAMPLE
New-GitHubRelease -OwnerName microsoft -RepositoryName PowerShellForGitHub -TagName 0.12.0
New-GitHubRelease -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag 0.12.0
.NOTES
Requires push access to the repository.
Expand All @@ -325,7 +325,7 @@ filter New-GitHubRelease
[string] $Uri,

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

[Alias('Sha')]
[Alias('BranchName')]
Expand Down Expand Up @@ -363,7 +363,7 @@ filter New-GitHubRelease
}

$hashBody = @{
'tag_name' = $TagName
'tag_name' = $Tag
}

if ($PSBoundParameters.ContainsKey('Committish')) { $hashBody['target_commitish'] = $Committish }
Expand All @@ -376,14 +376,14 @@ filter New-GitHubRelease
'UriFragment' = "/repos/$OwnerName/$RepositoryName/releases"
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Post'
'Description' = "Creating release at $TagName"
'Description' = "Creating release at $Tag"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

if (-not $PSCmdlet.ShouldProcess($TagName, "Create release for $RepositoryName at tag"))
if (-not $PSCmdlet.ShouldProcess($Tag, "Create release for $RepositoryName at tag"))
{
return
}
Expand Down Expand Up @@ -418,7 +418,7 @@ filter Set-GitHubRelease
.PARAMETER Release
The ID of the release to edit.
.PARAMETER TagName
.PARAMETER Tag
The name of the tag.
.PARAMETER Committish
Expand Down Expand Up @@ -468,7 +468,7 @@ filter Set-GitHubRelease
GitHub.Release
.EXAMPLE
Set-GitHubRelease -OwnerName microsoft -RepositoryName PowerShellForGitHub -TagName 0.12.0 -Body 'Adds core support for Projects'
Set-GitHubRelease -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag 0.12.0 -Body 'Adds core support for Projects'
.NOTES
Requires push access to the repository.
Expand Down Expand Up @@ -497,7 +497,7 @@ filter Set-GitHubRelease
[Alias('ReleaseId')]
[int64] $Release,

[string] $TagName,
[string] $Tag,

[Alias('Sha')]
[Alias('BranchName')]
Expand Down Expand Up @@ -527,7 +527,7 @@ filter Set-GitHubRelease
$telemetryProperties = @{
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
'ProvidedTagName' = ($PSBoundParameters.ContainsKey('TagName'))
'ProvidedTag' = ($PSBoundParameters.ContainsKey('Tag'))
'ProvidedCommittish' = ($PSBoundParameters.ContainsKey('Committish'))
'ProvidedName' = ($PSBoundParameters.ContainsKey('Name'))
'ProvidedBody' = ($PSBoundParameters.ContainsKey('Body'))
Expand All @@ -536,7 +536,7 @@ filter Set-GitHubRelease
}

$hashBody = @{}
if ($PSBoundParameters.ContainsKey('TagName')) { $hashBody['tag_name'] = $TagName }
if ($PSBoundParameters.ContainsKey('Tag')) { $hashBody['tag_name'] = $Tag }
if ($PSBoundParameters.ContainsKey('Committish')) { $hashBody['target_commitish'] = $Committish }
if ($PSBoundParameters.ContainsKey('Name')) { $hashBody['name'] = $Name }
if ($PSBoundParameters.ContainsKey('Body')) { $hashBody['body'] = $Body }
Expand All @@ -547,7 +547,7 @@ filter Set-GitHubRelease
'UriFragment' = "/repos/$OwnerName/$RepositoryName/releases/$Release"
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Patch'
'Description' = "Creating release at $TagName"
'Description' = "Creating release at $Tag"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
Expand Down Expand Up @@ -589,6 +589,9 @@ filter Remove-GitHubRelease
.PARAMETER Release
The ID of the release to remove.
.PARAMETER Force
If this switch is specified, you will not be prompted for confirmation of command execution.
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
Expand Down Expand Up @@ -652,6 +655,8 @@ filter Remove-GitHubRelease
[Alias('ReleaseId')]
[int64] $Release,

[switch] $Force,

[string] $AccessToken,

[switch] $NoStatus
Expand All @@ -678,6 +683,11 @@ filter Remove-GitHubRelease
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

if ($Force -and (-not $Confirm))
{
$ConfirmPreference = 'None'
}

if (-not $PSCmdlet.ShouldProcess($Release, "Remove GitHub Release"))
{
return
Expand Down Expand Up @@ -890,15 +900,15 @@ filter Get-GitHubReleaseAsset
if ($PSCmdlet.ParameterSetName -in ('Elements-Download', 'Uri-Download'))
{
Write-Log -Message "Moving [$($result.FullName)] to [$Path]" -Level Verbose
return (Move-Item -Path $result -Destination $Path -Force:$Force -PassThru)
return (Move-Item -Path $result -Destination $Path -Force:$Force -ErrorAction Stop -PassThru)
}
else
{
return ($result | Add-GitHubReleaseAssetAdditionalProperties)
}
}

function New-GitHubReleaseAsset
filter New-GitHubReleaseAsset
{
<#
.SYNOPSIS
Expand Down Expand Up @@ -969,7 +979,16 @@ function New-GitHubReleaseAsset
GitHub.ReleaseAsset
.EXAMPLE
New-GitHubReleaseAsset -OwnerName microsoft -RepositoryName PowerShellForGitHub -TagName 0.12.0
New-GitHubReleaseAsset -OwnerName microsoft -RepositoryName PowerShellForGitHub -Release 123456 -Path 'c:\foo.zip'
Uploads the file located at 'c:\foo.zip' to the 123456 release in microsoft/PowerShellForGitHub
.EXAMPLE
$release = New-GitHubRelease -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag 'stable'
$release | New-GitHubReleaseAsset -Path 'c:\bar.txt'
Creates a new release tagged as 'stable' and then uploads 'c:\bar.txt' as an asset for
that release.
.NOTES
GitHub renames asset filenames that have special characters, non-alphanumeric characters,
Expand Down Expand Up @@ -1002,12 +1021,7 @@ function New-GitHubReleaseAsset

[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='Elements')]
[Parameter(
Mandatory,
ValueFromPipelineByPropertyName,
ParameterSetName='Uri')]
ValueFromPipelineByPropertyName)]
[Alias('ReleaseId')]
[int64] $Release,

Expand All @@ -1021,7 +1035,7 @@ function New-GitHubReleaseAsset
Mandatory,
ValueFromPipeline)]
[ValidateScript(
{if (Test-Path -Path $_ -PathType Leaf) { $true}
{if (Test-Path -Path $_ -PathType Leaf) { $true }
else { throw "$_ does not exist or is inaccessible." }})]
[string] $Path,

Expand All @@ -1034,80 +1048,72 @@ function New-GitHubReleaseAsset
[switch] $NoStatus
)

begin
{
Write-InvocationLog
Write-InvocationLog

$telemetryProperties = @{
'ProvidedUploadUrl' = ($PSBoundParameters.ContainsKey('UploadUrl'))
'ProvidedLabel' = ($PSBoundParameters.ContainsKey('Label'))
'ProvidedContentType' = ($PSBoundParameters.ContainsKey('ContentType'))
}
$telemetryProperties = @{
'ProvidedUploadUrl' = ($PSBoundParameters.ContainsKey('UploadUrl'))
'ProvidedLabel' = ($PSBoundParameters.ContainsKey('Label'))
'ProvidedContentType' = ($PSBoundParameters.ContainsKey('ContentType'))
}

# If UploadUrl wasn't provided, we'll need to query for it first.
if ($PSCmdlet.ParameterSetName -in ('Elements', 'Uri'))
{
$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName

$telemetryProperties['OwnerName'] = (Get-PiiSafeString -PlainText $OwnerName)
$telemetryProperties['RepositoryName'] = (Get-PiiSafeString -PlainText $RepositoryName)

$params = @{
'OwnerName' = $OwnerName
'RepositoryName' = $RepositoryName
'Release' = $Release
'AccessToken' = $AccessToken
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
# If UploadUrl wasn't provided, we'll need to query for it first.
if ([String]::IsNullOrEmpty($UploadUrl))
{
$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName

$releaseInfo = Get-GitHubRelease @params
$UploadUrl = $releaseInfo.upload_url
}
$telemetryProperties['OwnerName'] = (Get-PiiSafeString -PlainText $OwnerName)
$telemetryProperties['RepositoryName'] = (Get-PiiSafeString -PlainText $RepositoryName)

# Remove the '{name,label}' from the Url if it's there
if ($UploadUrl -match '(.*){')
{
$UploadUrl = $Matches[1]
$params = @{
'OwnerName' = $OwnerName
'RepositoryName' = $RepositoryName
'Release' = $Release
'AccessToken' = $AccessToken
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

$releaseInfo = Get-GitHubRelease @params
$UploadUrl = $releaseInfo.upload_url
}

process
# Remove the '{name,label}' from the Url if it's there
if ($UploadUrl -match '(.*){')
{
$Path = Resolve-UnverifiedPath -Path $Path
$file = Get-Item -Path $Path
$fileName = $file.Name
$fileNameEncoded = [Uri]::EscapeDataString($fileName)
$queryParams = @("name=$fileNameEncoded")
$UploadUrl = $Matches[1]
}

if ($PSBoundParameters.ContainsKey('Label'))
{
$labelEncoded = [Uri]::EscapeDataString($Label)
$queryParams += "label=$labelEncoded"
}
$Path = Resolve-UnverifiedPath -Path $Path
$file = Get-Item -Path $Path
$fileName = $file.Name
$fileNameEncoded = [Uri]::EscapeDataString($fileName)
$queryParams = @("name=$fileNameEncoded")

if (-not $PSCmdlet.ShouldProcess($Path, "Create new GitHub Release Asset"))
{
return
}
if ($PSBoundParameters.ContainsKey('Label'))
{
$labelEncoded = [Uri]::EscapeDataString($Label)
$queryParams += "label=$labelEncoded"
}

$params = @{
'UriFragment' = $UploadUrl + '?' + ($queryParams -join '&')
'Method' = 'Post'
'Description' = "Uploading $fileName as a release asset"
'InFile' = $Path
'ContentType' = $ContentType
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}
if (-not $PSCmdlet.ShouldProcess($Path, "Create new GitHub Release Asset"))
{
return
}

return (Invoke-GHRestMethod @params | Add-GitHubReleaseAssetAdditionalProperties)
$params = @{
'UriFragment' = $UploadUrl + '?' + ($queryParams -join '&')
'Method' = 'Post'
'Description' = "Uploading release asset: $fileName"
'InFile' = $Path
'ContentType' = $ContentType
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

end {}
return (Invoke-GHRestMethod @params | Add-GitHubReleaseAssetAdditionalProperties)
}

filter Set-GitHubReleaseAsset
Expand Down Expand Up @@ -1277,6 +1283,9 @@ filter Remove-GitHubReleaseAsset
.PARAMETER Asset
The ID of the asset to remove.
.PARAMETER Force
If this switch is specified, you will not be prompted for confirmation of command execution.
.PARAMETER AccessToken
If provided, this will be used as the AccessToken for authentication with the
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
Expand Down Expand Up @@ -1337,6 +1346,8 @@ filter Remove-GitHubReleaseAsset
[Alias('AssetId')]
[int64] $Asset,

[switch] $Force,

[string] $AccessToken,

[switch] $NoStatus
Expand All @@ -1363,6 +1374,11 @@ filter Remove-GitHubReleaseAsset
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

if ($Force -and (-not $Confirm))
{
$ConfirmPreference = 'None'
}

if (-not $PSCmdlet.ShouldProcess($Asset, "Delete GitHub Release Asset"))
{
return
Expand Down
Loading

0 comments on commit 4d75347

Please sign in to comment.