Skip to content

Commit

Permalink
PowerShellForGitHub: Improve and Standardise WhatIf/Confirm Processing (
Browse files Browse the repository at this point in the history
#254)

Improves and standardizes the WhatIf/Confirm ('ShouldProcess') processing across all the functions.

* Moved the `$PSCmdlet.ShouldProcess` check out of `Invoke-GHRestMethod` and added it to the calling functions with the correct operation and target.
* Added `WhatIf:$false` and `Confirm:$false` to the `Out-File` Cmdlet call in the `Write-Log` function in the Helpers module. This will prevent the `Write-Log` function displaying `WhatIf` and `Confirm` prompts.
* Removed `ShouldProcess` from non-state changing functions.
* Modified the current `ShouldProcess` conditions to use an early return.
* Modified the `Set-GitHubIssueLabel` function to remove `ConfirmImpact='High'` and set `ConfirmPreference` to 'Low' if no labels have been specified instead.
* Modified the `Update-GitHubRepository` function to remove `ConfirmImpact='High'` and set `ConfirmPreference` to 'Low' if the repo is being renamed instead.
  • Loading branch information
X-Guardian committed Aug 3, 2020
1 parent 981b85c commit 2f16de1
Show file tree
Hide file tree
Showing 28 changed files with 676 additions and 600 deletions.
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)
}
}
110 changes: 55 additions & 55 deletions GitHubBranches.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,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 @@ -237,9 +234,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 @@ -291,8 +285,6 @@ filter New-GitHubRepositoryBranch
OwnerName = $OwnerName
RepositoryName = $RepositoryName
BranchName = $BranchName
Whatif = $false
Confirm = $false
}
if ($PSBoundParameters.ContainsKey('AccessToken'))
{
Expand Down Expand Up @@ -338,6 +330,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 @@ -431,9 +428,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 @@ -468,6 +462,8 @@ filter Remove-GitHubRepositoryBranch
[switch] $NoStatus
)

Write-InvocationLog

$elements = Resolve-RepositoryElements
$OwnerName = $elements.ownerName
$RepositoryName = $elements.repositoryName
Expand All @@ -484,23 +480,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 Get-GitHubRepositoryBranchProtectionRule
Expand Down Expand Up @@ -965,27 +961,29 @@ filter New-GitHubRepositoryBranchProtectionRule
$hashBody['allow_deletions'] = $AllowDeletions.ToBool()
}

if ($PSCmdlet.ShouldProcess(
if (-not $PSCmdlet.ShouldProcess(
"'$BranchName' branch of repository '$RepositoryName'",
'Create GitHub Repository Branch Protection Rule'))
{
$jsonConversionDepth = 3

$params = @{
UriFragment = "repos/$OwnerName/$RepositoryName/branches/$BranchName/protection"
Body = (ConvertTo-Json -InputObject $hashBody -Depth $jsonConversionDepth)
Description = "Setting $BranchName branch protection status for $RepositoryName"
Method = 'Put'
AcceptHeader = $script:lukeCageAcceptHeader
AccessToken = $AccessToken
TelemetryEventName = $MyInvocation.MyCommand.Name
TelemetryProperties = $telemetryProperties
NoStatus = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus `
-ConfigValueName DefaultNoStatus)
}
return
}

$jsonConversionDepth = 3

return (Invoke-GHRestMethod @params | Add-GitHubBranchProtectionRuleAdditionalProperties)
$params = @{
UriFragment = "repos/$OwnerName/$RepositoryName/branches/$BranchName/protection"
Body = (ConvertTo-Json -InputObject $hashBody -Depth $jsonConversionDepth)
Description = "Setting $BranchName branch protection status for $RepositoryName"
Method = 'Put'
AcceptHeader = $script:lukeCageAcceptHeader
AccessToken = $AccessToken
TelemetryEventName = $MyInvocation.MyCommand.Name
TelemetryProperties = $telemetryProperties
NoStatus = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus `
-ConfigValueName DefaultNoStatus)
}

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

filter Remove-GitHubRepositoryBranchProtectionRule
Expand Down Expand Up @@ -1100,23 +1098,25 @@ filter Remove-GitHubRepositoryBranchProtectionRule
$ConfirmPreference = 'None'
}

if ($PSCmdlet.ShouldProcess("'$BranchName' branch of repository '$RepositoryName'",
if (-not $PSCmdlet.ShouldProcess("'$BranchName' branch of repository '$RepositoryName'",
'Remove GitHub Repository Branch Protection Rule'))
{
$params = @{
UriFragment = "repos/$OwnerName/$RepositoryName/branches/$BranchName/protection"
Description = "Removing $BranchName branch protection rule for $RepositoryName"
Method = 'Delete'
AcceptHeader = $script:lukeCageAcceptHeader
AccessToken = $AccessToken
TelemetryEventName = $MyInvocation.MyCommand.Name
TelemetryProperties = $telemetryProperties
NoStatus = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus `
-ConfigValueName DefaultNoStatus)
}
return
}

return Invoke-GHRestMethod @params | Out-Null
$params = @{
UriFragment = "repos/$OwnerName/$RepositoryName/branches/$BranchName/protection"
Description = "Removing $BranchName branch protection rule for $RepositoryName"
Method = 'Delete'
AcceptHeader = $script:lukeCageAcceptHeader
AccessToken = $AccessToken
TelemetryEventName = $MyInvocation.MyCommand.Name
TelemetryProperties = $telemetryProperties
NoStatus = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus `
-ConfigValueName DefaultNoStatus)
}

return Invoke-GHRestMethod @params | Out-Null
}

filter Add-GitHubBranchAdditionalProperties
Expand Down

0 comments on commit 2f16de1

Please sign in to comment.