From db65b1d4c0e4551b6c8feca77b1ba9aa1a762539 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sat, 13 Jun 2020 11:47:09 +0100 Subject: [PATCH 1/5] Add GitHubRepositories Dependabot services --- GitHubRepositories.ps1 | 497 +++++++++++++++++++++++++++++ PowerShellForGitHub.psd1 | 5 + Tests/GitHubRepositories.tests.ps1 | 121 ++++++- USAGE.md | 40 +++ 4 files changed, 662 insertions(+), 1 deletion(-) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index c59e33e5..bee8a324 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -2054,3 +2054,500 @@ filter Add-GitHubRepositoryAdditionalProperties Write-Output $item } } + +Function Get-GitHubRepositoryVulnerabilityAlerts { + <# + .SYNOPSIS + Retrieves the status of vulnerability alerts for a repository on GitHub. + + .DESCRIPTION + Retrieves the status of vulnerability alerts for a repository on GitHub. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Get-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub + + Retrieves the status of vulnerability alerts for the PowerShellForGithub repository. + .EXAMPLE + Get-GitHubRepositoryVulnerabilityAlerts -Uri https://github.com/PowerShell/PowerShellForGitHub + + Retrieves the status of vulnerability alerts for the PowerShellForGithub repository. +#> + [CmdletBinding( + PositionalBinding = $false, + DefaultParameterSetName='Elements')] + param( + [Parameter(ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + Position = 1, + ParameterSetName='Uri')] + [string] $Uri, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + Write-InvocationLog -Invocation $MyInvocation + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + $params = @{ + UriFragment = "repos/$OwnerName/$RepositoryName/vulnerability-alerts" + Description = "Getting Vulnerability Alerts status for $RepositoryName" + AcceptHeader = $script:dorianAcceptHeader + Method = 'Get' + AccessToken = $AccessToken + TelemetryEventName = $MyInvocation.MyCommand.Name + TelemetryProperties = $telemetryProperties + NoStatus = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters ` + -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + try + { + Invoke-GHRestMethod @params | Out-Null + $result = $true + } + catch + { + # Temporary code to handle current differences in exception object between PS5 and PS7 + if ($PSVersionTable.PSedition -eq 'Core') + { + if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException] -and + ($_.ErrorDetails.Message | ConvertFrom-Json).message -eq 'Vulnerability alerts are disabled.') + { + $result = $false + } + else + { + throw $_ + } + } + else + { + if ($_.Exception.Message -like '*Vulnerability alerts are disabled.*') + { + $result = $false + } + else + { + throw $_ + } + } + } + + return $result +} + +Function Enable-GitHubRepositoryVulnerabilityAlerts { + <# + .SYNOPSIS + Enables vulnerability alerts for a repository on GitHub. + + .DESCRIPTION + Enables vulnerability alerts for a repository on GitHub. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Enable-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub + + Enables vulnerability alerts for the PowerShellForGithub repository. + .EXAMPLE + Enable-GitHubRepositoryVulnerabilityAlerts -Uri https://github.com/PowerShell/PowerShellForGitHub + + Enables vulnerability alerts for the PowerShellForGithub repository. +#> + [CmdletBinding( + PositionalBinding = $false, + SupportsShouldProcess, + DefaultParameterSetName='Elements')] + param( + [Parameter( + ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + Position = 1, + ParameterSetName='Uri')] + [string] $Uri, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + if ($PSCmdlet.ShouldProcess($RepositoryName, 'Enable Vulnerability Alerts')) + { + Write-InvocationLog -Invocation $MyInvocation + + $params = @{ + UriFragment = "repos/$OwnerName/$RepositoryName/vulnerability-alerts" + Description = "Enabling Vulnerability Alerts for $RepositoryName" + AcceptHeader = $script:dorianAcceptHeader + Method = 'Put' + AccessToken = $AccessToken + TelemetryEventName = $MyInvocation.MyCommand.Name + TelemetryProperties = $telemetryProperties + NoStatus = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters ` + -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + Invoke-GHRestMethod @params + } +} + +Function Disable-GitHubRepositoryVulnerabilityAlerts { + <# + .SYNOPSIS + Disables vulnerability alerts for a repository on GitHub. + + .DESCRIPTION + Disables vulnerability alerts for a repository on GitHub. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Disable-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub + + Disables vulnerability alerts for the PowerShellForGithub repository. + + .EXAMPLE + Disable-GitHubRepositoryVulnerabilityAlerts -Uri https://github.com/PowerShell/PowerShellForGitHub + + Disables vulnerability alerts for the PowerShellForGithub repository. +#> + [CmdletBinding( + PositionalBinding = $false, + SupportsShouldProcess, + DefaultParameterSetName='Elements')] + param( + [Parameter(ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + Position = 1, + ParameterSetName='Uri')] + [string] $Uri, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + if ($PSCmdlet.ShouldProcess($RepositoryName, 'Enable Vulnerability Alerts')) + { + Write-InvocationLog + + $params = @{ + UriFragment = "repos/$OwnerName/$RepositoryName/vulnerability-alerts" + Description = "Enabling Vulnerability Alerts for $RepositoryName" + AcceptHeader = $script:dorianAcceptHeader + Method = 'Delete' + AccessToken = $AccessToken + TelemetryEventName = $MyInvocation.MyCommand.Name + TelemetryProperties = $telemetryProperties + NoStatus = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters ` + -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + Invoke-GHRestMethod @params + } +} + +Function Enable-GitHubRepositorySecurityFixes { + <# + .SYNOPSIS + Enables automated security fixes for a repository on GitHub. + + .DESCRIPTION + Enables automated security fixes for a repository on GitHub. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Enable-GitHubRepositorySecurityFixes -OwnerName Microsoft -RepositoryName PowerShellForGitHub + + Enables automated security fixes for the PowerShellForGitHub repository. + .EXAMPLE + Enable-GitHubRepositorySecurityFixes -Uri https://github.com/PowerShell/PowerShellForGitHub + + Enables automated security fixes for the PowerShellForGitHub repository. +#> + [CmdletBinding( + PositionalBinding = $false, + SupportsShouldProcess, + DefaultParameterSetName='Elements')] + param( + [Parameter( + ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + Position = 1, + ParameterSetName='Uri')] + [string] $Uri, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + if ($PSCmdlet.ShouldProcess($RepositoryName, 'Enable Vulnerability Alerts')) + { + Write-InvocationLog -Invocation $MyInvocation + + $params = @{ + UriFragment = "repos/$OwnerName/$RepositoryName/automated-security-fixes" + Description = "Enabling Automated Security Fixes for $RepositoryName" + AcceptHeader = $script:londonAcceptHeader + Method = 'Put' + AccessToken = $AccessToken + TelemetryEventName = $MyInvocation.MyCommand.Name + TelemetryProperties = $telemetryProperties + NoStatus = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters ` + -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + Invoke-GHRestMethod @params + } +} + +Function Disable-GitHubRepositorySecurityFixes { + <# + .SYNOPSIS + Disables automated security fixes for a repository on GitHub. + + .DESCRIPTION + Disables automated security fixes for a repository on GitHub. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .PARAMETER NoStatus + If this switch is specified, long-running commands will run on the main thread + with no commandline status update. When not specified, those commands run in + the background, enabling the command prompt to provide status information. + If not supplied here, the DefaultNoStatus configuration property value will be used. + + .EXAMPLE + Disable-GitHubRepositorySecurityFixes -OwnerName Microsoft -RepositoryName PowerShellForGitHub + + Disables automated security fixes for the PowerShellForGithub repository. + .EXAMPLE + Disable-GitHubRepositorySecurityFixes -Uri https://github.com/PowerShell/PowerShellForGitHub + + Disables automated security fixes for the PowerShellForGithub repository. +#> + [CmdletBinding( + PositionalBinding = $false, + SupportsShouldProcess, + DefaultParameterSetName='Elements')] + param( + [Parameter(ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + Position = 1, + ParameterSetName='Uri')] + [string] $Uri, + + [string] $AccessToken, + + [switch] $NoStatus + ) + + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + if ($PSCmdlet.ShouldProcess($RepositoryName, 'Enable Vulnerability Alerts')) + { + Write-InvocationLog + + $params = @{ + UriFragment = "repos/$OwnerName/$RepositoryName/automated-security-fixes" + Description = "Enabling Automated Security Fixes for $RepositoryName" + AcceptHeader = $script:londonAcceptHeader + Method = 'Delete' + AccessToken = $AccessToken + TelemetryEventName = $MyInvocation.MyCommand.Name + TelemetryProperties = $telemetryProperties + NoStatus = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters ` + -Name NoStatus -ConfigValueName DefaultNoStatus) + } + + Invoke-GHRestMethod @params + } +} diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index c7f51c82..9e1d044f 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -54,6 +54,10 @@ 'Backup-GitHubConfiguration', 'Clear-GitHubAuthentication', 'ConvertFrom-GitHubMarkdown', + 'Disable-GitHubRepositorySecurityFixes', + 'Disable-GitHubRepositoryVulnerabilityAlerts', + 'Enable-GitHubRepositorySecurityFixes', + 'Enable-GitHubRepositoryVulnerabilityAlerts', 'Get-GitHubAssignee', 'Get-GitHubCloneTraffic', 'Get-GitHubCodeOfConduct', @@ -86,6 +90,7 @@ 'Get-GitHubRepositoryTag', 'Get-GitHubRepositoryTopic', 'Get-GitHubRepositoryUniqueContributor', + 'Get-GitHubRepositoryVulnerabilityAlerts', 'Get-GitHubTeam', 'Get-GitHubTeamMember', 'Get-GitHubUser', diff --git a/Tests/GitHubRepositories.tests.ps1 b/Tests/GitHubRepositories.tests.ps1 index 3dc1bf81..9ebaefeb 100644 --- a/Tests/GitHubRepositories.tests.ps1 +++ b/Tests/GitHubRepositories.tests.ps1 @@ -812,6 +812,125 @@ try $collaborators[0].PSObject.TypeNames[0] = 'GitHub.User' } } + Describe 'GitHubRepositories\Get-GitHubRepositoryVulnerabilityAlerts' { + BeforeAll -ScriptBlock { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) + } + + Context 'When the Git Hub Repository Vulnerability Alert Status is Enabled' { + BeforeAll -ScriptBlock { + Enable-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url + $result = Get-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url + } + + It 'Should return an object of the correct type' { + $result | Should -BeOfType System.Boolean + } + + It 'Should return the correct value' { + $result | Should -Be $true + } + } + + Context 'When the Git Hub Repository Vulnerability Alert Status is Disabled' { + BeforeAll -ScriptBlock { + Disable-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url + $status = Get-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url + } + + It 'Should return an object of the correct type' { + $status | Should -BeOfType System.Boolean + } + + It 'Should return the correct value' { + $status | Should -BeFalse + } + } + + Context 'When Invoke-GHRestMethod returns an unexpected error' { + It 'Should throw' { + $getGitHubRepositoryVulnerabilityAlertsParms = @{ + OwnerName = 'octocat' + RepositoryName = 'IncorrectRepostioryName' + } + { Get-GitHubRepositoryVulnerabilityAlerts @getGitHubRepositoryVulnerabilityAlertsParms } | + Should -Throw + } + } + + AfterAll -ScriptBlock { + Remove-GitHubRepository -Uri $repo.svn_url -Force + } + } + + Describe 'GitHubRepositories\Enable-GitHubRepositoryVulnerabilityAlerts' { + BeforeAll { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) + } + + Context 'When Enabling GitHub Repository Vulnerability Alerts' { + It 'Should not throw' { + { Enable-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url } | + Should -Not -Throw + } + } + + AfterAll -ScriptBlock { + Remove-GitHubRepository -Uri $repo.svn_url -Force + } + } + + Describe 'GitHubRepositories\Disable-GitHubRepositoryVulnerabilityAlerts' { + BeforeAll { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) + Enable-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url + } + + Context 'When Disabling GitHub Repository Vulnerability Alerts' { + It 'Should not throw' { + { Disable-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url } | + Should -Not -Throw + } + } + + AfterAll -ScriptBlock { + Remove-GitHubRepository -Uri $repo.svn_url -Force + } + } + + Describe 'GitHubRepositories\Enable-GitHubRepositorySecurityFixes' { + BeforeAll { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) + } + + Context 'When Enabling GitHub Repository Security Fixes' { + It 'Should not throw' { + { Enable-GitHubRepositorySecurityFixes -Uri $repo.svn_url } | + Should -Not -Throw + } + } + + AfterAll -ScriptBlock { + Remove-GitHubRepository -Uri $repo.svn_url -Force + } + } + + Describe 'GitHubRepositories\Disable-GitHubRepositorySecurityFixes' { + BeforeAll { + $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) + Enable-GitHubRepositorySecurityFixes -Uri $repo.svn_url + } + + Context 'When Disabling GitHub Repository Vulnerability Alerts' { + It 'Should not throw' { + { Disable-GitHubRepositorySecurityFixes -Uri $repo.svn_url } | + Should -Not -Throw + } + } + + AfterAll -ScriptBlock { + Remove-GitHubRepository -Uri $repo.svn_url -Force + } } } finally @@ -822,4 +941,4 @@ finally Restore-GitHubConfiguration -Path $script:originalConfigFile $script:originalConfigFile = $null } -} \ No newline at end of file +} diff --git a/USAGE.md b/USAGE.md index 59cc3954..787e5282 100644 --- a/USAGE.md +++ b/USAGE.md @@ -29,6 +29,12 @@ * [Updating the current authenticated user](#updating-the-current-authenticated-user) * [Getting any user](#getting-any-user) * [Getting all users](#getting-all-users) + * [Repositories](#repositories) + * [Get repository vulnerability alert status](#get-repository-vulnerability-alert-status) + * [Enable repository vulnerability alerts](#enable-repository-vulnerability-alerts) + * [Disable repository vulnerability alerts](#disable-repository-vulnerability-alerts) + * [Enable repository automatic security fixes](#enable-repository-automatic-security-fixes) + * [Disable repository automatic security fixes](#disable-repository-automatic-security-fixes) * [Forks](#forks) * [Get all the forks for a repository](#get-all-the-forks-for-a-repository) * [Create a new fork](#create-a-new-fork) @@ -414,6 +420,40 @@ Get-GitHubUser ---------- +### Repositories + +#### Get repository vulnerability alert status + +```powershell +Get-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub +``` + +#### Enable repository vulnerability alerts + +```powershell +Enable-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub +``` + +#### Disable repository vulnerability alerts + +```powershell +Disable-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub +``` + +#### Enable repository automatic security fixes + +```powershell +Enable-GitHubRepositorySecurityFixes -OwnerName Microsoft -RepositoryName PowerShellForGitHub +``` + +#### Disable repository automatic security fixes + +```powershell +Enable-GitHubRepositorySecurityFixes -OwnerName Microsoft -RepositoryName PowerShellForGitHub +``` + +---------- + ### Forks #### Get all the forks for a repository From 899cf13b7fb971d7eebe58a9bc4a2e087e7f7bd9 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sat, 13 Jun 2020 11:57:43 +0100 Subject: [PATCH 2/5] Add CBH Inputs and Outputs --- GitHubRepositories.ps1 | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index bee8a324..1d76a5d6 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -2088,6 +2088,12 @@ Function Get-GitHubRepositoryVulnerabilityAlerts { the background, enabling the command prompt to provide status information. If not supplied here, the DefaultNoStatus configuration property value will be used. + .INPUTS + None + + .OUTPUTS + PSCustomObject + .EXAMPLE Get-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub @@ -2210,6 +2216,12 @@ Function Enable-GitHubRepositoryVulnerabilityAlerts { the background, enabling the command prompt to provide status information. If not supplied here, the DefaultNoStatus configuration property value will be used. + .INPUTS + None + + .OUTPUTS + None + .EXAMPLE Enable-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub @@ -2304,6 +2316,12 @@ Function Disable-GitHubRepositoryVulnerabilityAlerts { the background, enabling the command prompt to provide status information. If not supplied here, the DefaultNoStatus configuration property value will be used. + .INPUTS + None + + .OUTPUTS + None + .EXAMPLE Disable-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub @@ -2398,6 +2416,12 @@ Function Enable-GitHubRepositorySecurityFixes { the background, enabling the command prompt to provide status information. If not supplied here, the DefaultNoStatus configuration property value will be used. + .INPUTS + None + + .OUTPUTS + None + .EXAMPLE Enable-GitHubRepositorySecurityFixes -OwnerName Microsoft -RepositoryName PowerShellForGitHub @@ -2492,6 +2516,12 @@ Function Disable-GitHubRepositorySecurityFixes { the background, enabling the command prompt to provide status information. If not supplied here, the DefaultNoStatus configuration property value will be used. + .INPUTS + None + + .OUTPUTS + None + .EXAMPLE Disable-GitHubRepositorySecurityFixes -OwnerName Microsoft -RepositoryName PowerShellForGitHub From 2cb08d015aa0b8077cb85936561ce280f7071153 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 14 Jun 2020 16:39:09 +0100 Subject: [PATCH 3/5] Rename functions to singular --- GitHubRepositories.ps1 | 30 ++++++++++++------------- PowerShellForGitHub.psd1 | 10 ++++----- Tests/GitHubRepositories.tests.ps1 | 36 +++++++++++++++--------------- USAGE.md | 12 +++++----- 4 files changed, 44 insertions(+), 44 deletions(-) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index 1d76a5d6..9ee3ac43 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -2055,7 +2055,7 @@ filter Add-GitHubRepositoryAdditionalProperties } } -Function Get-GitHubRepositoryVulnerabilityAlerts { +Function Get-GitHubRepositoryVulnerabilityAlert { <# .SYNOPSIS Retrieves the status of vulnerability alerts for a repository on GitHub. @@ -2095,11 +2095,11 @@ Function Get-GitHubRepositoryVulnerabilityAlerts { PSCustomObject .EXAMPLE - Get-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub + Get-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub Retrieves the status of vulnerability alerts for the PowerShellForGithub repository. .EXAMPLE - Get-GitHubRepositoryVulnerabilityAlerts -Uri https://github.com/PowerShell/PowerShellForGitHub + Get-GitHubRepositoryVulnerabilityAlert -Uri https://github.com/PowerShell/PowerShellForGitHub Retrieves the status of vulnerability alerts for the PowerShellForGithub repository. #> @@ -2183,7 +2183,7 @@ Function Get-GitHubRepositoryVulnerabilityAlerts { return $result } -Function Enable-GitHubRepositoryVulnerabilityAlerts { +Function Enable-GitHubRepositoryVulnerabilityAlert { <# .SYNOPSIS Enables vulnerability alerts for a repository on GitHub. @@ -2223,11 +2223,11 @@ Function Enable-GitHubRepositoryVulnerabilityAlerts { None .EXAMPLE - Enable-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub + Enable-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub Enables vulnerability alerts for the PowerShellForGithub repository. .EXAMPLE - Enable-GitHubRepositoryVulnerabilityAlerts -Uri https://github.com/PowerShell/PowerShellForGitHub + Enable-GitHubRepositoryVulnerabilityAlert -Uri https://github.com/PowerShell/PowerShellForGitHub Enables vulnerability alerts for the PowerShellForGithub repository. #> @@ -2283,7 +2283,7 @@ Function Enable-GitHubRepositoryVulnerabilityAlerts { } } -Function Disable-GitHubRepositoryVulnerabilityAlerts { +Function Disable-GitHubRepositoryVulnerabilityAlert { <# .SYNOPSIS Disables vulnerability alerts for a repository on GitHub. @@ -2323,12 +2323,12 @@ Function Disable-GitHubRepositoryVulnerabilityAlerts { None .EXAMPLE - Disable-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub + Disable-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub Disables vulnerability alerts for the PowerShellForGithub repository. .EXAMPLE - Disable-GitHubRepositoryVulnerabilityAlerts -Uri https://github.com/PowerShell/PowerShellForGitHub + Disable-GitHubRepositoryVulnerabilityAlert -Uri https://github.com/PowerShell/PowerShellForGitHub Disables vulnerability alerts for the PowerShellForGithub repository. #> @@ -2383,7 +2383,7 @@ Function Disable-GitHubRepositoryVulnerabilityAlerts { } } -Function Enable-GitHubRepositorySecurityFixes { +Function Enable-GitHubRepositorySecurityFix { <# .SYNOPSIS Enables automated security fixes for a repository on GitHub. @@ -2423,11 +2423,11 @@ Function Enable-GitHubRepositorySecurityFixes { None .EXAMPLE - Enable-GitHubRepositorySecurityFixes -OwnerName Microsoft -RepositoryName PowerShellForGitHub + Enable-GitHubRepositorySecurityFix -OwnerName Microsoft -RepositoryName PowerShellForGitHub Enables automated security fixes for the PowerShellForGitHub repository. .EXAMPLE - Enable-GitHubRepositorySecurityFixes -Uri https://github.com/PowerShell/PowerShellForGitHub + Enable-GitHubRepositorySecurityFix -Uri https://github.com/PowerShell/PowerShellForGitHub Enables automated security fixes for the PowerShellForGitHub repository. #> @@ -2483,7 +2483,7 @@ Function Enable-GitHubRepositorySecurityFixes { } } -Function Disable-GitHubRepositorySecurityFixes { +Function Disable-GitHubRepositorySecurityFix { <# .SYNOPSIS Disables automated security fixes for a repository on GitHub. @@ -2523,11 +2523,11 @@ Function Disable-GitHubRepositorySecurityFixes { None .EXAMPLE - Disable-GitHubRepositorySecurityFixes -OwnerName Microsoft -RepositoryName PowerShellForGitHub + Disable-GitHubRepositorySecurityFix -OwnerName Microsoft -RepositoryName PowerShellForGitHub Disables automated security fixes for the PowerShellForGithub repository. .EXAMPLE - Disable-GitHubRepositorySecurityFixes -Uri https://github.com/PowerShell/PowerShellForGitHub + Disable-GitHubRepositorySecurityFix -Uri https://github.com/PowerShell/PowerShellForGitHub Disables automated security fixes for the PowerShellForGithub repository. #> diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 9e1d044f..7fc396e8 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -54,10 +54,10 @@ 'Backup-GitHubConfiguration', 'Clear-GitHubAuthentication', 'ConvertFrom-GitHubMarkdown', - 'Disable-GitHubRepositorySecurityFixes', - 'Disable-GitHubRepositoryVulnerabilityAlerts', - 'Enable-GitHubRepositorySecurityFixes', - 'Enable-GitHubRepositoryVulnerabilityAlerts', + 'Disable-GitHubRepositorySecurityFix', + 'Disable-GitHubRepositoryVulnerabilityAlert', + 'Enable-GitHubRepositorySecurityFix', + 'Enable-GitHubRepositoryVulnerabilityAlert', 'Get-GitHubAssignee', 'Get-GitHubCloneTraffic', 'Get-GitHubCodeOfConduct', @@ -90,7 +90,7 @@ 'Get-GitHubRepositoryTag', 'Get-GitHubRepositoryTopic', 'Get-GitHubRepositoryUniqueContributor', - 'Get-GitHubRepositoryVulnerabilityAlerts', + 'Get-GitHubRepositoryVulnerabilityAlert', 'Get-GitHubTeam', 'Get-GitHubTeamMember', 'Get-GitHubUser', diff --git a/Tests/GitHubRepositories.tests.ps1 b/Tests/GitHubRepositories.tests.ps1 index 9ebaefeb..464f7672 100644 --- a/Tests/GitHubRepositories.tests.ps1 +++ b/Tests/GitHubRepositories.tests.ps1 @@ -812,15 +812,15 @@ try $collaborators[0].PSObject.TypeNames[0] = 'GitHub.User' } } - Describe 'GitHubRepositories\Get-GitHubRepositoryVulnerabilityAlerts' { + Describe 'GitHubRepositories\Get-GitHubRepositoryVulnerabilityAlert' { BeforeAll -ScriptBlock { $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) } Context 'When the Git Hub Repository Vulnerability Alert Status is Enabled' { BeforeAll -ScriptBlock { - Enable-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url - $result = Get-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url + Enable-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url + $result = Get-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url } It 'Should return an object of the correct type' { @@ -834,8 +834,8 @@ try Context 'When the Git Hub Repository Vulnerability Alert Status is Disabled' { BeforeAll -ScriptBlock { - Disable-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url - $status = Get-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url + Disable-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url + $status = Get-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url } It 'Should return an object of the correct type' { @@ -849,11 +849,11 @@ try Context 'When Invoke-GHRestMethod returns an unexpected error' { It 'Should throw' { - $getGitHubRepositoryVulnerabilityAlertsParms = @{ + $getGitHubRepositoryVulnerabilityAlertParms = @{ OwnerName = 'octocat' RepositoryName = 'IncorrectRepostioryName' } - { Get-GitHubRepositoryVulnerabilityAlerts @getGitHubRepositoryVulnerabilityAlertsParms } | + { Get-GitHubRepositoryVulnerabilityAlert @getGitHubRepositoryVulnerabilityAlertParms } | Should -Throw } } @@ -863,14 +863,14 @@ try } } - Describe 'GitHubRepositories\Enable-GitHubRepositoryVulnerabilityAlerts' { + Describe 'GitHubRepositories\Enable-GitHubRepositoryVulnerabilityAlert' { BeforeAll { $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) } Context 'When Enabling GitHub Repository Vulnerability Alerts' { It 'Should not throw' { - { Enable-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url } | + { Enable-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url } | Should -Not -Throw } } @@ -880,15 +880,15 @@ try } } - Describe 'GitHubRepositories\Disable-GitHubRepositoryVulnerabilityAlerts' { + Describe 'GitHubRepositories\Disable-GitHubRepositoryVulnerabilityAlert' { BeforeAll { $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) - Enable-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url + Enable-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url } Context 'When Disabling GitHub Repository Vulnerability Alerts' { It 'Should not throw' { - { Disable-GitHubRepositoryVulnerabilityAlerts -Uri $repo.svn_url } | + { Disable-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url } | Should -Not -Throw } } @@ -898,14 +898,14 @@ try } } - Describe 'GitHubRepositories\Enable-GitHubRepositorySecurityFixes' { + Describe 'GitHubRepositories\Enable-GitHubRepositorySecurityFix' { BeforeAll { $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) } Context 'When Enabling GitHub Repository Security Fixes' { It 'Should not throw' { - { Enable-GitHubRepositorySecurityFixes -Uri $repo.svn_url } | + { Enable-GitHubRepositorySecurityFix -Uri $repo.svn_url } | Should -Not -Throw } } @@ -915,15 +915,15 @@ try } } - Describe 'GitHubRepositories\Disable-GitHubRepositorySecurityFixes' { + Describe 'GitHubRepositories\Disable-GitHubRepositorySecurityFix' { BeforeAll { $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) - Enable-GitHubRepositorySecurityFixes -Uri $repo.svn_url + Enable-GitHubRepositorySecurityFix -Uri $repo.svn_url } - Context 'When Disabling GitHub Repository Vulnerability Alerts' { + Context 'When Disabling GitHub Repository Security Fixes' { It 'Should not throw' { - { Disable-GitHubRepositorySecurityFixes -Uri $repo.svn_url } | + { Disable-GitHubRepositorySecurityFix -Uri $repo.svn_url } | Should -Not -Throw } } diff --git a/USAGE.md b/USAGE.md index 787e5282..200f82e9 100644 --- a/USAGE.md +++ b/USAGE.md @@ -425,31 +425,31 @@ Get-GitHubUser #### Get repository vulnerability alert status ```powershell -Get-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub +Get-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub ``` #### Enable repository vulnerability alerts ```powershell -Enable-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub +Enable-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub ``` -#### Disable repository vulnerability alerts +#### Disable repository vulnerability alert ```powershell -Disable-GitHubRepositoryVulnerabilityAlerts -OwnerName Microsoft -RepositoryName PowerShellForGitHub +Disable-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub ``` #### Enable repository automatic security fixes ```powershell -Enable-GitHubRepositorySecurityFixes -OwnerName Microsoft -RepositoryName PowerShellForGitHub +Enable-GitHubRepositorySecurityFix -OwnerName Microsoft -RepositoryName PowerShellForGitHub ``` #### Disable repository automatic security fixes ```powershell -Enable-GitHubRepositorySecurityFixes -OwnerName Microsoft -RepositoryName PowerShellForGitHub +Enable-GitHubRepositorySecurityFix -OwnerName Microsoft -RepositoryName PowerShellForGitHub ``` ---------- From 37a7b06baa79dec76227b2efc0d8ab2921ba9951 Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sat, 20 Jun 2020 11:55:46 +0100 Subject: [PATCH 4/5] Fix review comments --- GitHubRepositories.ps1 | 78 ++++++++++++++++++++---------- PowerShellForGitHub.psd1 | 2 +- Tests/GitHubRepositories.tests.ps1 | 10 ++-- USAGE.md | 10 ++-- 4 files changed, 65 insertions(+), 35 deletions(-) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index 9ee3ac43..54557b24 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -2055,7 +2055,8 @@ filter Add-GitHubRepositoryAdditionalProperties } } -Function Get-GitHubRepositoryVulnerabilityAlert { +Filter Test-GitHubRepositoryVulnerabilityAlert +{ <# .SYNOPSIS Retrieves the status of vulnerability alerts for a repository on GitHub. @@ -2089,17 +2090,20 @@ Function Get-GitHubRepositoryVulnerabilityAlert { If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS - None + GitHub.Repository .OUTPUTS - PSCustomObject + System.Boolean + + .NOTES + The authenticated user must have admin access to the repository. .EXAMPLE - Get-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub + Test-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub Retrieves the status of vulnerability alerts for the PowerShellForGithub repository. .EXAMPLE - Get-GitHubRepositoryVulnerabilityAlert -Uri https://github.com/PowerShell/PowerShellForGitHub + Test-GitHubRepositoryVulnerabilityAlert -Uri https://github.com/PowerShell/PowerShellForGitHub Retrieves the status of vulnerability alerts for the PowerShellForGithub repository. #> @@ -2116,7 +2120,9 @@ Function Get-GitHubRepositoryVulnerabilityAlert { [Parameter( Mandatory, Position = 1, + ValueFromPipelineByPropertyName, ParameterSetName='Uri')] + [Alias('RepositoryUrl')] [string] $Uri, [string] $AccessToken, @@ -2124,7 +2130,7 @@ Function Get-GitHubRepositoryVulnerabilityAlert { [switch] $NoStatus ) - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters $OwnerName = $elements.ownerName @@ -2137,7 +2143,7 @@ Function Get-GitHubRepositoryVulnerabilityAlert { $params = @{ UriFragment = "repos/$OwnerName/$RepositoryName/vulnerability-alerts" - Description = "Getting Vulnerability Alerts status for $RepositoryName" + Description = "Getting Vulnerability Alerts status for $RepositoryName" AcceptHeader = $script:dorianAcceptHeader Method = 'Get' AccessToken = $AccessToken @@ -2183,7 +2189,8 @@ Function Get-GitHubRepositoryVulnerabilityAlert { return $result } -Function Enable-GitHubRepositoryVulnerabilityAlert { +filter Enable-GitHubRepositoryVulnerabilityAlert +{ <# .SYNOPSIS Enables vulnerability alerts for a repository on GitHub. @@ -2217,15 +2224,19 @@ Function Enable-GitHubRepositoryVulnerabilityAlert { If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS - None + GitHub.Repository .OUTPUTS None + .NOTES + The authenticated user must have admin access to the repository. + .EXAMPLE Enable-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub Enables vulnerability alerts for the PowerShellForGithub repository. + .EXAMPLE Enable-GitHubRepositoryVulnerabilityAlert -Uri https://github.com/PowerShell/PowerShellForGitHub @@ -2246,7 +2257,9 @@ Function Enable-GitHubRepositoryVulnerabilityAlert { [Parameter( Mandatory, Position = 1, + ValueFromPipelineByPropertyName, ParameterSetName='Uri')] + [Alias('RepositoryUrl')] [string] $Uri, [string] $AccessToken, @@ -2265,7 +2278,7 @@ Function Enable-GitHubRepositoryVulnerabilityAlert { if ($PSCmdlet.ShouldProcess($RepositoryName, 'Enable Vulnerability Alerts')) { - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog $params = @{ UriFragment = "repos/$OwnerName/$RepositoryName/vulnerability-alerts" @@ -2279,11 +2292,12 @@ Function Enable-GitHubRepositoryVulnerabilityAlert { -Name NoStatus -ConfigValueName DefaultNoStatus) } - Invoke-GHRestMethod @params + Invoke-GHRestMethod @params |Out-Null } } -Function Disable-GitHubRepositoryVulnerabilityAlert { +filter Disable-GitHubRepositoryVulnerabilityAlert +{ <# .SYNOPSIS Disables vulnerability alerts for a repository on GitHub. @@ -2317,11 +2331,14 @@ Function Disable-GitHubRepositoryVulnerabilityAlert { If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS - None + GitHub.Repository .OUTPUTS None + .NOTES + The authenticated user must have admin access to the repository. + .EXAMPLE Disable-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub @@ -2346,6 +2363,7 @@ Function Disable-GitHubRepositoryVulnerabilityAlert { [Parameter( Mandatory, Position = 1, + ValueFromPipelineByPropertyName, ParameterSetName='Uri')] [string] $Uri, @@ -2363,13 +2381,13 @@ Function Disable-GitHubRepositoryVulnerabilityAlert { 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } - if ($PSCmdlet.ShouldProcess($RepositoryName, 'Enable Vulnerability Alerts')) + if ($PSCmdlet.ShouldProcess($RepositoryName, 'Disable Vulnerability Alerts')) { Write-InvocationLog $params = @{ UriFragment = "repos/$OwnerName/$RepositoryName/vulnerability-alerts" - Description = "Enabling Vulnerability Alerts for $RepositoryName" + Description = "Disabling Vulnerability Alerts for $RepositoryName" AcceptHeader = $script:dorianAcceptHeader Method = 'Delete' AccessToken = $AccessToken @@ -2379,11 +2397,12 @@ Function Disable-GitHubRepositoryVulnerabilityAlert { -Name NoStatus -ConfigValueName DefaultNoStatus) } - Invoke-GHRestMethod @params + Invoke-GHRestMethod @params | Out-Null } } -Function Enable-GitHubRepositorySecurityFix { +filter Enable-GitHubRepositorySecurityFix +{ <# .SYNOPSIS Enables automated security fixes for a repository on GitHub. @@ -2417,11 +2436,14 @@ Function Enable-GitHubRepositorySecurityFix { If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS - None + GitHub.Repository .OUTPUTS None + .NOTES + The authenticated user must have admin access to the repository. + .EXAMPLE Enable-GitHubRepositorySecurityFix -OwnerName Microsoft -RepositoryName PowerShellForGitHub @@ -2446,6 +2468,7 @@ Function Enable-GitHubRepositorySecurityFix { [Parameter( Mandatory, Position = 1, + ValueFromPipelineByPropertyName, ParameterSetName='Uri')] [string] $Uri, @@ -2463,9 +2486,9 @@ Function Enable-GitHubRepositorySecurityFix { 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } - if ($PSCmdlet.ShouldProcess($RepositoryName, 'Enable Vulnerability Alerts')) + if ($PSCmdlet.ShouldProcess($RepositoryName, 'Enable Automated Security Fixes')) { - Write-InvocationLog -Invocation $MyInvocation + Write-InvocationLog $params = @{ UriFragment = "repos/$OwnerName/$RepositoryName/automated-security-fixes" @@ -2483,7 +2506,8 @@ Function Enable-GitHubRepositorySecurityFix { } } -Function Disable-GitHubRepositorySecurityFix { +filter Disable-GitHubRepositorySecurityFix +{ <# .SYNOPSIS Disables automated security fixes for a repository on GitHub. @@ -2517,11 +2541,14 @@ Function Disable-GitHubRepositorySecurityFix { If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS - None + GitHub.Repository .OUTPUTS None + .NOTES + The authenticated user must have admin access to the repository. + .EXAMPLE Disable-GitHubRepositorySecurityFix -OwnerName Microsoft -RepositoryName PowerShellForGitHub @@ -2545,6 +2572,7 @@ Function Disable-GitHubRepositorySecurityFix { [Parameter( Mandatory, Position = 1, + ValueFromPipelineByPropertyName, ParameterSetName='Uri')] [string] $Uri, @@ -2562,13 +2590,13 @@ Function Disable-GitHubRepositorySecurityFix { 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } - if ($PSCmdlet.ShouldProcess($RepositoryName, 'Enable Vulnerability Alerts')) + if ($PSCmdlet.ShouldProcess($RepositoryName, 'Disable Automated Security Fixes')) { Write-InvocationLog $params = @{ UriFragment = "repos/$OwnerName/$RepositoryName/automated-security-fixes" - Description = "Enabling Automated Security Fixes for $RepositoryName" + Description = "Disabling Automated Security Fixes for $RepositoryName" AcceptHeader = $script:londonAcceptHeader Method = 'Delete' AccessToken = $AccessToken @@ -2578,6 +2606,6 @@ Function Disable-GitHubRepositorySecurityFix { -Name NoStatus -ConfigValueName DefaultNoStatus) } - Invoke-GHRestMethod @params + Invoke-GHRestMethod @params | Out-Null } } diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 7fc396e8..36467c7e 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -90,7 +90,6 @@ 'Get-GitHubRepositoryTag', 'Get-GitHubRepositoryTopic', 'Get-GitHubRepositoryUniqueContributor', - 'Get-GitHubRepositoryVulnerabilityAlert', 'Get-GitHubTeam', 'Get-GitHubTeamMember', 'Get-GitHubUser', @@ -142,6 +141,7 @@ 'Test-GitHubAssignee', 'Test-GitHubAuthenticationConfigured', 'Test-GitHubOrganizationMember', + 'Test-GitHubRepositoryVulnerabilityAlert', 'Unlock-GitHubIssue', 'Update-GitHubCurrentUser', 'Update-GitHubIssue', diff --git a/Tests/GitHubRepositories.tests.ps1 b/Tests/GitHubRepositories.tests.ps1 index 464f7672..04aa8e01 100644 --- a/Tests/GitHubRepositories.tests.ps1 +++ b/Tests/GitHubRepositories.tests.ps1 @@ -812,7 +812,9 @@ try $collaborators[0].PSObject.TypeNames[0] = 'GitHub.User' } } - Describe 'GitHubRepositories\Get-GitHubRepositoryVulnerabilityAlert' { + } + + Describe 'GitHubRepositories\Test-GitHubRepositoryVulnerabilityAlert' { BeforeAll -ScriptBlock { $repo = New-GitHubRepository -RepositoryName ([Guid]::NewGuid().Guid) } @@ -820,7 +822,7 @@ try Context 'When the Git Hub Repository Vulnerability Alert Status is Enabled' { BeforeAll -ScriptBlock { Enable-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url - $result = Get-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url + $result = Test-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url } It 'Should return an object of the correct type' { @@ -835,7 +837,7 @@ try Context 'When the Git Hub Repository Vulnerability Alert Status is Disabled' { BeforeAll -ScriptBlock { Disable-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url - $status = Get-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url + $status = Test-GitHubRepositoryVulnerabilityAlert -Uri $repo.svn_url } It 'Should return an object of the correct type' { @@ -853,7 +855,7 @@ try OwnerName = 'octocat' RepositoryName = 'IncorrectRepostioryName' } - { Get-GitHubRepositoryVulnerabilityAlert @getGitHubRepositoryVulnerabilityAlertParms } | + { Test-GitHubRepositoryVulnerabilityAlert @getGitHubRepositoryVulnerabilityAlertParms } | Should -Throw } } diff --git a/USAGE.md b/USAGE.md index 200f82e9..fd96d10d 100644 --- a/USAGE.md +++ b/USAGE.md @@ -425,31 +425,31 @@ Get-GitHubUser #### Get repository vulnerability alert status ```powershell -Get-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub +Test-GitHubRepositoryVulnerabilityAlert -OwnerName microsoft -RepositoryName PowerShellForGitHub ``` #### Enable repository vulnerability alerts ```powershell -Enable-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub +Enable-GitHubRepositoryVulnerabilityAlert -OwnerName microsoft -RepositoryName PowerShellForGitHub ``` #### Disable repository vulnerability alert ```powershell -Disable-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub +Disable-GitHubRepositoryVulnerabilityAlert -OwnerName microsoft -RepositoryName PowerShellForGitHub ``` #### Enable repository automatic security fixes ```powershell -Enable-GitHubRepositorySecurityFix -OwnerName Microsoft -RepositoryName PowerShellForGitHub +Enable-GitHubRepositorySecurityFix -OwnerName microsoft -RepositoryName PowerShellForGitHub ``` #### Disable repository automatic security fixes ```powershell -Enable-GitHubRepositorySecurityFix -OwnerName Microsoft -RepositoryName PowerShellForGitHub +Disable-GitHubRepositorySecurityFix -OwnerName microsoft -RepositoryName PowerShellForGitHub ``` ---------- From fc8f92710162b0ad531b75226549aaffaa490fed Mon Sep 17 00:00:00 2001 From: Simon Heather Date: Sun, 21 Jun 2020 10:29:48 +0100 Subject: [PATCH 5/5] Fix review comments --- GitHubRepositories.ps1 | 245 +++++++++++++++++++++++++---------------- 1 file changed, 153 insertions(+), 92 deletions(-) diff --git a/GitHubRepositories.ps1 b/GitHubRepositories.ps1 index 54557b24..59ef8909 100644 --- a/GitHubRepositories.ps1 +++ b/GitHubRepositories.ps1 @@ -1965,97 +1965,7 @@ filter Move-GitHubRepositoryOwnership return (Invoke-GHRestMethod @params | Add-GitHubRepositoryAdditionalProperties) } -filter Add-GitHubRepositoryAdditionalProperties -{ -<# - .SYNOPSIS - Adds type name and additional properties to ease pipelining to GitHub Repository objects. - - .PARAMETER InputObject - The GitHub object to add additional properties to. - - .PARAMETER TypeName - The type that should be assigned to the object. - - .PARAMETER OwnerName - Owner of the repository. This information might be obtainable from InputObject, so this - is optional based on what InputObject contains. - - .PARAMETER RepositoryName - Name of the repository. This information might be obtainable from InputObject, so this - is optional based on what InputObject contains. - - .INPUTS - [PSCustomObject] - - .OUTPUTS - GitHub.Repository -#> - [CmdletBinding()] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification="Internal helper that is definitely adding more than one property.")] - param( - [Parameter( - Mandatory, - ValueFromPipeline)] - [AllowNull()] - [AllowEmptyCollection()] - [PSCustomObject[]] $InputObject, - - [ValidateNotNullOrEmpty()] - [string] $TypeName = $script:GitHubRepositoryTypeName, - - [string] $OwnerName, - - [string] $RepositoryName - ) - - foreach ($item in $InputObject) - { - $item.PSObject.TypeNames.Insert(0, $TypeName) - - if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport)) - { - $repositoryUrl = [String]::Empty - if ([String]::IsNullOrEmpty($item.html_url)) - { - if ($PSBoundParameters.ContainsKey('OwnerName') -and - $PSBoundParameters.ContainsKey('RepositoryName')) - { - $repositoryUrl = (Join-GitHubUri -OwnerName $OwnerName -RepositoryName $RepositoryName) - } - } - else - { - $elements = Split-GitHubUri -Uri $item.html_url - $repositoryUrl = Join-GitHubUri @elements - } - - if (-not [String]::IsNullOrEmpty($repositoryUrl)) - { - Add-Member -InputObject $item -Name 'RepositoryUrl' -Value $repositoryUrl -MemberType NoteProperty -Force - } - - if ($item.id -gt 0) - { - Add-Member -InputObject $item -Name 'RepositoryId' -Value $item.id -MemberType NoteProperty -Force - } - - if ($null -ne $item.owner) - { - $null = Add-GitHubUserAdditionalProperties -InputObject $item.owner - } - - if ($null -ne $item.organization) - { - $null = Add-GitHubOrganizationAdditionalProperties -InputObject $item.organization - } - } - - Write-Output $item - } -} - -Filter Test-GitHubRepositoryVulnerabilityAlert +filter Test-GitHubRepositoryVulnerabilityAlert { <# .SYNOPSIS @@ -2090,6 +2000,18 @@ Filter Test-GitHubRepositoryVulnerabilityAlert If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Release GitHub.Repository .OUTPUTS @@ -2102,6 +2024,7 @@ Filter Test-GitHubRepositoryVulnerabilityAlert Test-GitHubRepositoryVulnerabilityAlert -OwnerName Microsoft -RepositoryName PowerShellForGitHub Retrieves the status of vulnerability alerts for the PowerShellForGithub repository. + .EXAMPLE Test-GitHubRepositoryVulnerabilityAlert -Uri https://github.com/PowerShell/PowerShellForGitHub @@ -2224,6 +2147,18 @@ filter Enable-GitHubRepositoryVulnerabilityAlert If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Release GitHub.Repository .OUTPUTS @@ -2292,7 +2227,7 @@ filter Enable-GitHubRepositoryVulnerabilityAlert -Name NoStatus -ConfigValueName DefaultNoStatus) } - Invoke-GHRestMethod @params |Out-Null + Invoke-GHRestMethod @params | Out-Null } } @@ -2331,6 +2266,18 @@ filter Disable-GitHubRepositoryVulnerabilityAlert If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Release GitHub.Repository .OUTPUTS @@ -2436,6 +2383,18 @@ filter Enable-GitHubRepositorySecurityFix If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Release GitHub.Repository .OUTPUTS @@ -2541,6 +2500,18 @@ filter Disable-GitHubRepositorySecurityFix If not supplied here, the DefaultNoStatus configuration property value will be used. .INPUTS + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Release GitHub.Repository .OUTPUTS @@ -2609,3 +2580,93 @@ filter Disable-GitHubRepositorySecurityFix Invoke-GHRestMethod @params | Out-Null } } + +filter Add-GitHubRepositoryAdditionalProperties +{ +<# + .SYNOPSIS + Adds type name and additional properties to ease pipelining to GitHub Repository objects. + + .PARAMETER InputObject + The GitHub object to add additional properties to. + + .PARAMETER TypeName + The type that should be assigned to the object. + + .PARAMETER OwnerName + Owner of the repository. This information might be obtainable from InputObject, so this + is optional based on what InputObject contains. + + .PARAMETER RepositoryName + Name of the repository. This information might be obtainable from InputObject, so this + is optional based on what InputObject contains. + + .INPUTS + [PSCustomObject] + + .OUTPUTS + GitHub.Repository +#> + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification="Internal helper that is definitely adding more than one property.")] + param( + [Parameter( + Mandatory, + ValueFromPipeline)] + [AllowNull()] + [AllowEmptyCollection()] + [PSCustomObject[]] $InputObject, + + [ValidateNotNullOrEmpty()] + [string] $TypeName = $script:GitHubRepositoryTypeName, + + [string] $OwnerName, + + [string] $RepositoryName + ) + + foreach ($item in $InputObject) + { + $item.PSObject.TypeNames.Insert(0, $TypeName) + + if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport)) + { + $repositoryUrl = [String]::Empty + if ([String]::IsNullOrEmpty($item.html_url)) + { + if ($PSBoundParameters.ContainsKey('OwnerName') -and + $PSBoundParameters.ContainsKey('RepositoryName')) + { + $repositoryUrl = (Join-GitHubUri -OwnerName $OwnerName -RepositoryName $RepositoryName) + } + } + else + { + $elements = Split-GitHubUri -Uri $item.html_url + $repositoryUrl = Join-GitHubUri @elements + } + + if (-not [String]::IsNullOrEmpty($repositoryUrl)) + { + Add-Member -InputObject $item -Name 'RepositoryUrl' -Value $repositoryUrl -MemberType NoteProperty -Force + } + + if ($item.id -gt 0) + { + Add-Member -InputObject $item -Name 'RepositoryId' -Value $item.id -MemberType NoteProperty -Force + } + + if ($null -ne $item.owner) + { + $null = Add-GitHubUserAdditionalProperties -InputObject $item.owner + } + + if ($null -ne $item.organization) + { + $null = Add-GitHubOrganizationAdditionalProperties -InputObject $item.organization + } + } + + Write-Output $item + } +}