Skip to content
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

PowerShellForGitHub: Improve and Standardise WhatIf/Confirm Processing #254

Merged
merged 15 commits into from
Aug 3, 2020
10 changes: 2 additions & 8 deletions GitHubAnalytics.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ function Group-GitHubIssue
$issues += Get-GitHubIssue -Uri 'https://github.com/powershell/xactivedirectory'
$issues | Group-GitHubIssue -Weeks 12 -DateType Closed
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='Weekly')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
[CmdletBinding(DefaultParameterSetName = 'Weekly')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="DateType due to PowerShell/PSScriptAnalyzer#1472")]
param
(
Expand Down Expand Up @@ -165,10 +162,7 @@ function Group-GitHubPullRequest
$pullRequests += Get-GitHubPullRequest -Uri 'https://github.com/powershell/xactivedirectory'
$pullRequests | Group-GitHubPullRequest -Weeks 12 -DateType Closed
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='Weekly')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
[CmdletBinding(DefaultParameterSetName='Weekly')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="DateType due to PowerShell/PSScriptAnalyzer#1472")]
param
(
Expand Down
46 changes: 23 additions & 23 deletions GitHubAssignees.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,8 @@ filter Get-GitHubAssignee

Lists the available assignees for issues from the microsoft\PowerShellForGitHub project.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='Elements')]
[CmdletBinding(DefaultParameterSetName = 'Elements')]
[OutputType({$script:GitHubUserTypeName})]
[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 @@ -188,11 +185,8 @@ filter Test-GitHubAssignee
Checks if a user has permission to be assigned to an issue
from the microsoft\PowerShellForGitHub project.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='Elements')]
[CmdletBinding(DefaultParameterSetName = 'Elements')]
[OutputType([bool])]
[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 @@ -348,7 +342,6 @@ function Add-GitHubAssignee
DefaultParameterSetName='Elements')]
[OutputType({$script:GitHubIssueTypeName})]
[Alias('New-GitHubAssignee')] # Non-standard usage of the New verb, but done to avoid a breaking change post 0.14.0
[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 @@ -426,6 +419,11 @@ function Add-GitHubAssignee
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

if (-not $PSCmdlet.ShouldProcess($Issue, "Add Assignee(s) $($userNames -join ',')"))
{
return
}

return (Invoke-GHRestMethod @params | Add-GitHubIssueAdditionalProperties)
}
}
Expand Down Expand Up @@ -606,21 +604,23 @@ function Remove-GitHubAssignee
$ConfirmPreference = 'None'
}

if ($PSCmdlet.ShouldProcess($userNames -join ', ', "Remove assignee(s)"))
if (-not $PSCmdlet.ShouldProcess($Issue, "Remove assignee(s) $($userNames -join ', ')"))
{
$params = @{
'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$Issue/assignees"
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Delete'
'Description' = "Removing assignees from issue $Issue for $RepositoryName"
'AccessToken' = $AccessToken
'AcceptHeader' = $script:symmetraAcceptHeader
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

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

$params = @{
'UriFragment' = "repos/$OwnerName/$RepositoryName/issues/$Issue/assignees"
'Body' = (ConvertTo-Json -InputObject $hashBody)
'Method' = 'Delete'
'Description' = "Removing assignees from issue $Issue for $RepositoryName"
'AccessToken' = $AccessToken
'AcceptHeader' = $script:symmetraAcceptHeader
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
}

return (Invoke-GHRestMethod @params | Add-GitHubIssueAdditionalProperties)
}
}
46 changes: 22 additions & 24 deletions GitHubBranches.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,8 @@ filter Get-GitHubRepositoryBranch
again. This tries to show some of the different types of objects you can pipe into this
function.
#>
[CmdletBinding(
SupportsShouldProcess,
DefaultParameterSetName='Elements')]
[CmdletBinding(DefaultParameterSetName = 'Elements')]
[OutputType({$script:GitHubBranchTypeName})]
[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.")]
[Alias('Get-GitHubBranch')]
param(
Expand Down Expand Up @@ -236,9 +233,6 @@ filter New-GitHubRepositoryBranch
PositionalBinding = $false
)]
[OutputType({$script:GitHubBranchTypeName})]
[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.')]
Expand Down Expand Up @@ -337,6 +331,11 @@ filter New-GitHubRepositoryBranch
sha = $originBranch.commit.sha
}

if (-not $PSCmdlet.ShouldProcess($BranchName, 'Create Repository Branch'))
{
return
}

$params = @{
'UriFragment' = $uriFragment
'Body' = (ConvertTo-Json -InputObject $hashBody)
Expand Down Expand Up @@ -430,9 +429,6 @@ filter Remove-GitHubRepositoryBranch
DefaultParameterSetName = 'Elements',
PositionalBinding = $false,
ConfirmImpact = 'High')]
[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.")]
Expand Down Expand Up @@ -467,6 +463,8 @@ filter Remove-GitHubRepositoryBranch
[switch] $NoStatus
)

Write-InvocationLog

$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
Expand All @@ -483,23 +481,23 @@ filter Remove-GitHubRepositoryBranch
$ConfirmPreference = 'None'
}

if ($PSCmdlet.ShouldProcess($BranchName, "Remove Repository Branch"))
if (-not $PSCmdlet.ShouldProcess($BranchName, "Remove Repository Branch"))
{
Write-InvocationLog

$params = @{
'UriFragment' = $uriFragment
'Method' = 'Delete'
'Description' = "Deleting branch $BranchName from $RepositoryName"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue `
-Name NoStatus -ConfigValueName DefaultNoStatus)
}
return
}

Invoke-GHRestMethod @params | Out-Null
$params = @{
'UriFragment' = $uriFragment
'Method' = 'Delete'
'Description' = "Deleting branch $BranchName from $RepositoryName"
'AccessToken' = $AccessToken
'TelemetryEventName' = $MyInvocation.MyCommand.Name
'TelemetryProperties' = $telemetryProperties
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue `
-Name NoStatus -ConfigValueName DefaultNoStatus)
}

Invoke-GHRestMethod @params | Out-Null
}

filter Add-GitHubBranchAdditionalProperties
Expand Down
Loading