Skip to content

Commit 4d75347

Browse files
committed
Final updates and expansive UT improvements for pipelining
1 parent 67f10c7 commit 4d75347

File tree

2 files changed

+943
-162
lines changed

2 files changed

+943
-162
lines changed

GitHubReleases.ps1

Lines changed: 98 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ filter New-GitHubRelease
247247
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
248248
them individually.
249249
250-
.PARAMETER TagName
250+
.PARAMETER Tag
251251
The name of the tag. The tag will be created around the committish if it doesn't exist
252252
in the remote, and will need to be synced back to the local repository afterwards.
253253
@@ -298,7 +298,7 @@ filter New-GitHubRelease
298298
GitHub.Release
299299
300300
.EXAMPLE
301-
New-GitHubRelease -OwnerName microsoft -RepositoryName PowerShellForGitHub -TagName 0.12.0
301+
New-GitHubRelease -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag 0.12.0
302302
303303
.NOTES
304304
Requires push access to the repository.
@@ -325,7 +325,7 @@ filter New-GitHubRelease
325325
[string] $Uri,
326326

327327
[Parameter(Mandatory)]
328-
[string] $TagName,
328+
[string] $Tag,
329329

330330
[Alias('Sha')]
331331
[Alias('BranchName')]
@@ -363,7 +363,7 @@ filter New-GitHubRelease
363363
}
364364

365365
$hashBody = @{
366-
'tag_name' = $TagName
366+
'tag_name' = $Tag
367367
}
368368

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

386-
if (-not $PSCmdlet.ShouldProcess($TagName, "Create release for $RepositoryName at tag"))
386+
if (-not $PSCmdlet.ShouldProcess($Tag, "Create release for $RepositoryName at tag"))
387387
{
388388
return
389389
}
@@ -418,7 +418,7 @@ filter Set-GitHubRelease
418418
.PARAMETER Release
419419
The ID of the release to edit.
420420
421-
.PARAMETER TagName
421+
.PARAMETER Tag
422422
The name of the tag.
423423
424424
.PARAMETER Committish
@@ -468,7 +468,7 @@ filter Set-GitHubRelease
468468
GitHub.Release
469469
470470
.EXAMPLE
471-
Set-GitHubRelease -OwnerName microsoft -RepositoryName PowerShellForGitHub -TagName 0.12.0 -Body 'Adds core support for Projects'
471+
Set-GitHubRelease -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag 0.12.0 -Body 'Adds core support for Projects'
472472
473473
.NOTES
474474
Requires push access to the repository.
@@ -497,7 +497,7 @@ filter Set-GitHubRelease
497497
[Alias('ReleaseId')]
498498
[int64] $Release,
499499

500-
[string] $TagName,
500+
[string] $Tag,
501501

502502
[Alias('Sha')]
503503
[Alias('BranchName')]
@@ -527,7 +527,7 @@ filter Set-GitHubRelease
527527
$telemetryProperties = @{
528528
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
529529
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
530-
'ProvidedTagName' = ($PSBoundParameters.ContainsKey('TagName'))
530+
'ProvidedTag' = ($PSBoundParameters.ContainsKey('Tag'))
531531
'ProvidedCommittish' = ($PSBoundParameters.ContainsKey('Committish'))
532532
'ProvidedName' = ($PSBoundParameters.ContainsKey('Name'))
533533
'ProvidedBody' = ($PSBoundParameters.ContainsKey('Body'))
@@ -536,7 +536,7 @@ filter Set-GitHubRelease
536536
}
537537

538538
$hashBody = @{}
539-
if ($PSBoundParameters.ContainsKey('TagName')) { $hashBody['tag_name'] = $TagName }
539+
if ($PSBoundParameters.ContainsKey('Tag')) { $hashBody['tag_name'] = $Tag }
540540
if ($PSBoundParameters.ContainsKey('Committish')) { $hashBody['target_commitish'] = $Committish }
541541
if ($PSBoundParameters.ContainsKey('Name')) { $hashBody['name'] = $Name }
542542
if ($PSBoundParameters.ContainsKey('Body')) { $hashBody['body'] = $Body }
@@ -547,7 +547,7 @@ filter Set-GitHubRelease
547547
'UriFragment' = "/repos/$OwnerName/$RepositoryName/releases/$Release"
548548
'Body' = (ConvertTo-Json -InputObject $hashBody)
549549
'Method' = 'Patch'
550-
'Description' = "Creating release at $TagName"
550+
'Description' = "Creating release at $Tag"
551551
'AccessToken' = $AccessToken
552552
'TelemetryEventName' = $MyInvocation.MyCommand.Name
553553
'TelemetryProperties' = $telemetryProperties
@@ -589,6 +589,9 @@ filter Remove-GitHubRelease
589589
.PARAMETER Release
590590
The ID of the release to remove.
591591
592+
.PARAMETER Force
593+
If this switch is specified, you will not be prompted for confirmation of command execution.
594+
592595
.PARAMETER AccessToken
593596
If provided, this will be used as the AccessToken for authentication with the
594597
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
@@ -652,6 +655,8 @@ filter Remove-GitHubRelease
652655
[Alias('ReleaseId')]
653656
[int64] $Release,
654657

658+
[switch] $Force,
659+
655660
[string] $AccessToken,
656661

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

686+
if ($Force -and (-not $Confirm))
687+
{
688+
$ConfirmPreference = 'None'
689+
}
690+
681691
if (-not $PSCmdlet.ShouldProcess($Release, "Remove GitHub Release"))
682692
{
683693
return
@@ -890,15 +900,15 @@ filter Get-GitHubReleaseAsset
890900
if ($PSCmdlet.ParameterSetName -in ('Elements-Download', 'Uri-Download'))
891901
{
892902
Write-Log -Message "Moving [$($result.FullName)] to [$Path]" -Level Verbose
893-
return (Move-Item -Path $result -Destination $Path -Force:$Force -PassThru)
903+
return (Move-Item -Path $result -Destination $Path -Force:$Force -ErrorAction Stop -PassThru)
894904
}
895905
else
896906
{
897907
return ($result | Add-GitHubReleaseAssetAdditionalProperties)
898908
}
899909
}
900910

901-
function New-GitHubReleaseAsset
911+
filter New-GitHubReleaseAsset
902912
{
903913
<#
904914
.SYNOPSIS
@@ -969,7 +979,16 @@ function New-GitHubReleaseAsset
969979
GitHub.ReleaseAsset
970980
971981
.EXAMPLE
972-
New-GitHubReleaseAsset -OwnerName microsoft -RepositoryName PowerShellForGitHub -TagName 0.12.0
982+
New-GitHubReleaseAsset -OwnerName microsoft -RepositoryName PowerShellForGitHub -Release 123456 -Path 'c:\foo.zip'
983+
984+
Uploads the file located at 'c:\foo.zip' to the 123456 release in microsoft/PowerShellForGitHub
985+
986+
.EXAMPLE
987+
$release = New-GitHubRelease -OwnerName microsoft -RepositoryName PowerShellForGitHub -Tag 'stable'
988+
$release | New-GitHubReleaseAsset -Path 'c:\bar.txt'
989+
990+
Creates a new release tagged as 'stable' and then uploads 'c:\bar.txt' as an asset for
991+
that release.
973992
974993
.NOTES
975994
GitHub renames asset filenames that have special characters, non-alphanumeric characters,
@@ -1002,12 +1021,7 @@ function New-GitHubReleaseAsset
10021021

10031022
[Parameter(
10041023
Mandatory,
1005-
ValueFromPipelineByPropertyName,
1006-
ParameterSetName='Elements')]
1007-
[Parameter(
1008-
Mandatory,
1009-
ValueFromPipelineByPropertyName,
1010-
ParameterSetName='Uri')]
1024+
ValueFromPipelineByPropertyName)]
10111025
[Alias('ReleaseId')]
10121026
[int64] $Release,
10131027

@@ -1021,7 +1035,7 @@ function New-GitHubReleaseAsset
10211035
Mandatory,
10221036
ValueFromPipeline)]
10231037
[ValidateScript(
1024-
{if (Test-Path -Path $_ -PathType Leaf) { $true}
1038+
{if (Test-Path -Path $_ -PathType Leaf) { $true }
10251039
else { throw "$_ does not exist or is inaccessible." }})]
10261040
[string] $Path,
10271041

@@ -1034,80 +1048,72 @@ function New-GitHubReleaseAsset
10341048
[switch] $NoStatus
10351049
)
10361050

1037-
begin
1038-
{
1039-
Write-InvocationLog
1051+
Write-InvocationLog
10401052

1041-
$telemetryProperties = @{
1042-
'ProvidedUploadUrl' = ($PSBoundParameters.ContainsKey('UploadUrl'))
1043-
'ProvidedLabel' = ($PSBoundParameters.ContainsKey('Label'))
1044-
'ProvidedContentType' = ($PSBoundParameters.ContainsKey('ContentType'))
1045-
}
1053+
$telemetryProperties = @{
1054+
'ProvidedUploadUrl' = ($PSBoundParameters.ContainsKey('UploadUrl'))
1055+
'ProvidedLabel' = ($PSBoundParameters.ContainsKey('Label'))
1056+
'ProvidedContentType' = ($PSBoundParameters.ContainsKey('ContentType'))
1057+
}
10461058

1047-
# If UploadUrl wasn't provided, we'll need to query for it first.
1048-
if ($PSCmdlet.ParameterSetName -in ('Elements', 'Uri'))
1049-
{
1050-
$elements = Resolve-RepositoryElements
1051-
$OwnerName = $elements.ownerName
1052-
$RepositoryName = $elements.repositoryName
1053-
1054-
$telemetryProperties['OwnerName'] = (Get-PiiSafeString -PlainText $OwnerName)
1055-
$telemetryProperties['RepositoryName'] = (Get-PiiSafeString -PlainText $RepositoryName)
1056-
1057-
$params = @{
1058-
'OwnerName' = $OwnerName
1059-
'RepositoryName' = $RepositoryName
1060-
'Release' = $Release
1061-
'AccessToken' = $AccessToken
1062-
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
1063-
}
1059+
# If UploadUrl wasn't provided, we'll need to query for it first.
1060+
if ([String]::IsNullOrEmpty($UploadUrl))
1061+
{
1062+
$elements = Resolve-RepositoryElements
1063+
$OwnerName = $elements.ownerName
1064+
$RepositoryName = $elements.repositoryName
10641065

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

1069-
# Remove the '{name,label}' from the Url if it's there
1070-
if ($UploadUrl -match '(.*){')
1071-
{
1072-
$UploadUrl = $Matches[1]
1069+
$params = @{
1070+
'OwnerName' = $OwnerName
1071+
'RepositoryName' = $RepositoryName
1072+
'Release' = $Release
1073+
'AccessToken' = $AccessToken
1074+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
10731075
}
1076+
1077+
$releaseInfo = Get-GitHubRelease @params
1078+
$UploadUrl = $releaseInfo.upload_url
10741079
}
10751080

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

1084-
if ($PSBoundParameters.ContainsKey('Label'))
1085-
{
1086-
$labelEncoded = [Uri]::EscapeDataString($Label)
1087-
$queryParams += "label=$labelEncoded"
1088-
}
1087+
$Path = Resolve-UnverifiedPath -Path $Path
1088+
$file = Get-Item -Path $Path
1089+
$fileName = $file.Name
1090+
$fileNameEncoded = [Uri]::EscapeDataString($fileName)
1091+
$queryParams = @("name=$fileNameEncoded")
10891092

1090-
if (-not $PSCmdlet.ShouldProcess($Path, "Create new GitHub Release Asset"))
1091-
{
1092-
return
1093-
}
1093+
if ($PSBoundParameters.ContainsKey('Label'))
1094+
{
1095+
$labelEncoded = [Uri]::EscapeDataString($Label)
1096+
$queryParams += "label=$labelEncoded"
1097+
}
10941098

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

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

1110-
end {}
1116+
return (Invoke-GHRestMethod @params | Add-GitHubReleaseAssetAdditionalProperties)
11111117
}
11121118

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

1349+
[switch] $Force,
1350+
13401351
[string] $AccessToken,
13411352

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

1377+
if ($Force -and (-not $Confirm))
1378+
{
1379+
$ConfirmPreference = 'None'
1380+
}
1381+
13661382
if (-not $PSCmdlet.ShouldProcess($Asset, "Delete GitHub Release Asset"))
13671383
{
13681384
return

0 commit comments

Comments
 (0)