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 15, 2020
1 parent 6b13df1 commit 8a6f977
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 @@ -107,11 +107,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 @@ -312,7 +309,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 @@ -383,6 +379,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 @@ -472,7 +473,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 @@ -547,6 +547,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 @@ -666,10 +671,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 @@ -748,11 +755,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 @@ -971,7 +975,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 @@ -1065,6 +1068,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 @@ -1154,7 +1162,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 @@ -1202,6 +1209,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 @@ -1329,10 +1341,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 8a6f977

Please sign in to comment.