Skip to content

Commit

Permalink
Update the WhatIf/ShouldProcess handling per microsoft#254
Browse files Browse the repository at this point in the history
  • Loading branch information
HowardWolosky committed Jul 18, 2020
1 parent 228231b commit 9bcaeb3
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions GitHubReleases.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,8 @@ filter Get-GitHubRelease
Information about published releases are available to everyone. Only users with push
access will receive listings for draft releases.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='Elements')]
[CmdletBinding(DefaultParameterSetName='Elements')]
[OutputType({$script:GitHubReleaseTypeName})]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[Parameter(ParameterSetName='Elements')]
[Parameter(ParameterSetName="Elements-ReleaseId")]
Expand Down Expand Up @@ -313,7 +310,6 @@ filter New-GitHubRelease
SupportsShouldProcess,
DefaultParameterSetName='Elements')]
[OutputType({$script:GitHubReleaseTypeName})]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[Parameter(ParameterSetName='Elements')]
[string] $OwnerName,
Expand Down Expand Up @@ -384,6 +380,11 @@ filter New-GitHubRelease
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

if (-not $PSCmdlet.ShouldProcess($TagName, "Create release for $RepositoryName at tag"))
{
return
}

return (Invoke-GHRestMethod @params | Add-GitHubReleaseAdditionalProperties)
}

Expand Down Expand Up @@ -473,7 +474,6 @@ filter Set-GitHubRelease
SupportsShouldProcess,
DefaultParameterSetName='Elements')]
[OutputType({$script:GitHubReleaseTypeName})]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
param(
[Parameter(ParameterSetName='Elements')]
[string] $OwnerName,
Expand Down Expand Up @@ -548,6 +548,11 @@ filter Set-GitHubRelease
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

if (-not $PSCmdlet.ShouldProcess($Release, "Update GitHub Release"))
{
return
}

return (Invoke-GHRestMethod @params | Add-GitHubReleaseAdditionalProperties)
}

Expand Down Expand Up @@ -667,10 +672,12 @@ filter Remove-GitHubRelease
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

if ($PSCmdlet.ShouldProcess($Release, "Deleting release"))
if (-not $PSCmdlet.ShouldProcess($Release, "Remove GitHub Release"))
{
return Invoke-GHRestMethod @params
return
}

return Invoke-GHRestMethod @params
}

filter Get-GitHubReleaseAsset
Expand Down Expand Up @@ -749,11 +756,8 @@ filter Get-GitHubReleaseAsset
Downloads the asset 1234567890 to 'c:\users\PowerShellForGitHub\downloads\asset.zip' and
overwrites the file that may already be there.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='Elements-List')]
[CmdletBinding(DefaultParameterSetName='Elements-List')]
[OutputType({$script:GitHubReleaseAssetTypeName})]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[Parameter(ParameterSetName='Elements-List')]
Expand Down Expand Up @@ -972,7 +976,6 @@ filter New-GitHubReleaseAsset
SupportsShouldProcess,
DefaultParameterSetName='Elements')]
[OutputType({$script:GitHubReleaseAssetTypeName})]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[Parameter(ParameterSetName='Elements')]
Expand Down Expand Up @@ -1066,6 +1069,11 @@ filter New-GitHubReleaseAsset
$labelEncoded = [Uri]::EscapeDataString($Label)
if (-not [String]::IsNullOrWhiteSpace($Label)) { $queryParams += "label=$labelEncoded" }

if (-not $PSCmdlet.ShouldProcess($Path, "Create new GitHub Release Asset"))
{
return
}

$params = @{
'UriFragment' = $UploadUrl + '?' + ($queryParams -join '&')
'Method' = 'Post'
Expand Down Expand Up @@ -1155,7 +1163,6 @@ filter Set-GitHubReleaseAsset
SupportsShouldProcess,
DefaultParameterSetName='Elements')]
[OutputType({$script:GitHubReleaseAssetTypeName})]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="One or more parameters (like NoStatus) are only referenced by helper methods which get access to it from the stack via Get-Variable -Scope 1.")]
param(
[Parameter(ParameterSetName='Elements')]
Expand Down Expand Up @@ -1203,6 +1210,11 @@ filter Set-GitHubReleaseAsset
if (-not [String]::IsNullOrWhiteSpace($Name)) { $hashBody['name'] = $Name }
if (-not [String]::IsNullOrWhiteSpace($Label)) { $hashBody['label'] = $Label }

if (-not $PSCmdlet.ShouldProcess($Asset, "Update GitHub Release Asset"))
{
return
}

$params = @{
'UriFragment' = "/repos/$OwnerName/$RepositoryName/releases/assets/$Asset"
'Body' = (ConvertTo-Json -InputObject $hashBody)
Expand Down Expand Up @@ -1330,10 +1342,12 @@ filter Remove-GitHubReleaseAsset
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

if ($PSCmdlet.ShouldProcess($Asset, "Deleting asset"))
if (-not $PSCmdlet.ShouldProcess($Asset, "Delete GitHub Release Asset"))
{
return Invoke-GHRestMethod @params
return
}

return Invoke-GHRestMethod @params
}

filter Add-GitHubReleaseAdditionalProperties
Expand Down

0 comments on commit 9bcaeb3

Please sign in to comment.