From 703ef4d4369b761ee6811b697cbb2ce2adab9199 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Tue, 7 Nov 2023 22:30:40 +0100 Subject: [PATCH] Add commands for RuleSuite --- .../Get-GitHubRepositoryRuleSuite.ps1 | 103 ++++++++++++++++++ .../Get-GitHubRepositoryRuleSuiteById.ps1 | 46 ++++++++ .../Get-GitHubRepositoryRuleSuiteList.ps1 | 95 ++++++++++++++++ tools/utilities/GitHubAPI.ps1 | 4 +- 4 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuite.ps1 create mode 100644 src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuiteById.ps1 create mode 100644 src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuiteList.ps1 diff --git a/src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuite.ps1 b/src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuite.ps1 new file mode 100644 index 000000000..ec9476474 --- /dev/null +++ b/src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuite.ps1 @@ -0,0 +1,103 @@ +filter Get-GitHubRepositoryRuleSuite { + <# + .SYNOPSIS + List repository rule suites or a rule suite by id. + + .DESCRIPTION + Lists suites of rule evaluations at the repository level. + If an ID is specified, gets information about a suite of rule evaluations from within a repository. + For more information, see"[Managing rulesets for a repository](https://docs.github.com/repositories/configuring-branches-and-merges-in-your-repository/managing-rulesets/managing-rulesets-for-a-repository#viewing-insights-for-rulesets)." + + .EXAMPLE + $params = @{ + Owner = 'octocat' + Repo = 'hello-world' + Ref = 'main' + TimePeriod = 'day' + ActorName = 'octocat' + RuleSuiteResult = 'all' + } + Get-GitHubRepositoryRuleSuite @params + + Gets a list of rule suites for the main branch of the hello-world repository owned by octocat. + + .EXAMPLE + Get-GitHubRepositoryRuleSuite -Owner 'octocat' -Repo 'hello-world' -RuleSuiteId 123456789 + + Gets information about a suite of rule evaluations with id 123456789 from within the octocat/hello-world repository. + + .NOTES + https://docs.github.com/rest/repos/rule-suites#list-repository-rule-suites + https://docs.github.com/rest/repos/rule-suites#get-a-repository-rule-suite + + #> + [OutputType([pscustomobject])] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Long links')] + [CmdletBinding(DefaultParameterSetName = 'Default')] + param ( + # The account owner of the repository. The name is not case sensitive. + [Parameter()] + [Alias('org')] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + # The name of the repository without the .git extension. The name is not case sensitive. + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + # The name of the ref. Cannot contain wildcard characters. + # When specified, only rule evaluations triggered for this ref will be returned. + [Parameter()] + [string] $Ref, + + # The time period to filter by. + # For example,day will filter for rule suites that occurred in the past 24 hours, + # and week will filter for insights that occurred in the past 7 days (168 hours). + [Parameter()] + [ValidateSet('hour', 'day', 'week', 'month')] + [string] $TimePeriod = 'day', + + # The handle for the GitHub user account to filter on. When specified, only rule evaluations triggered by this actor will be returned. + [Parameter()] + [string] $ActorName, + + # The rule results to filter on. When specified, only suites with this result will be returned. + [Parameter()] + [ValidateSet('pass', 'fail', 'bypass', 'all')] + [string] $RuleSuiteResult = 'all', + + # The number of results per page (max 100). + [Parameter()] + [ValidateRange(1, 100)] + [int] $PerPage = 30, + + # The unique identifier of the rule suite result. To get this ID, you can use GET /repos/ { owner }/ { repo }/rulesets/rule-suites for repositories and GET /orgs/ { org }/rulesets/rule-suites for organizations. + [Parameter( + Mandatory, + ParameterSetName = 'ById' + )] + [int] $RuleSuiteId + ) + + switch ($PSCmdlet.ParameterSetName) { + 'Default' { + $params = @{ + Owner = $Owner + Repo = $Repo + Ref = $Ref + TimePeriod = $TimePeriod + ActorName = $ActorName + RuleSuiteResult = $RuleSuiteResult + PerPage = $PerPage + } + Get-GitHubRepositoryRuleSuiteList @params + } + 'ById' { + $params = @{ + Owner = $Owner + Repo = $Repo + RuleSuiteId = $RuleSuiteId + } + Get-GitHubRepositoryRuleSuiteById @params + } + } +} diff --git a/src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuiteById.ps1 b/src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuiteById.ps1 new file mode 100644 index 000000000..f06ad2bbe --- /dev/null +++ b/src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuiteById.ps1 @@ -0,0 +1,46 @@ +filter Get-GitHubRepositoryRuleSuiteById { + <# + .SYNOPSIS + Get a repository rule suite + + .DESCRIPTION + Gets information about a suite of rule evaluations from within a repository. + For more information, see "[Managing rulesets for a repository](https://docs.github.com/repositories/configuring-branches-and-merges-in-your-repository/managing-rulesets/managing-rulesets-for-a-repository#viewing-insights-for-rulesets)." + + .EXAMPLE + Get-GitHubRepositoryRuleSuiteById -Owner 'octocat' -Repo 'hello-world' -RuleSuiteId 123456789 + + Gets information about a suite of rule evaluations with id 123456789 from within the octocat/hello-world repository. + + .NOTES + https://docs.github.com/rest/repos/rule-suites#get-a-repository-rule-suite + + #> + [OutputType([pscustomobject])] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Long links')] + [CmdletBinding()] + param ( + # The account owner of the repository. The name is not case sensitive. + [Parameter()] + [Alias('org')] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + # The name of the repository without the .git extension. The name is not case sensitive. + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + # The unique identifier of the rule suite result. To get this ID, you can use GET /repos/ { owner }/ { repo }/rulesets/rule-suites for repositories and GET /orgs/ { org }/rulesets/rule-suites for organizations. + [Parameter(Mandatory)] + [int] $RuleSuiteId + + ) + + $inputObject = @{ + APIEndpoint = "/repos/$Owner/$Repo/rulesets/rule-suites/$RuleSuiteId" + Method = 'GET' + } + + Invoke-GitHubAPI @inputObject | ForEach-Object { + Write-Output $_.Response + } +} diff --git a/src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuiteList.ps1 b/src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuiteList.ps1 new file mode 100644 index 000000000..632c5db80 --- /dev/null +++ b/src/GitHub/public/Repositories/RuleSuite/Get-GitHubRepositoryRuleSuiteList.ps1 @@ -0,0 +1,95 @@ +filter Get-GitHubRepositoryRuleSuiteList { + <# + .SYNOPSIS + List repository rule suites + + .DESCRIPTION + Lists suites of rule evaluations at the repository level. + For more information, see"[Managing rulesets for a repository](https://docs.github.com/repositories/configuring-branches-and-merges-in-your-repository/managing-rulesets/managing-rulesets-for-a-repository#viewing-insights-for-rulesets)." + + .EXAMPLE + $params = @{ + Owner = 'octocat' + Repo = 'hello-world' + Ref = 'main' + TimePeriod = 'day' + ActorName = 'octocat' + RuleSuiteResult = 'all' + } + Get-GitHubRepositoryRuleSuiteList @params + + Gets a list of rule suites for the main branch of the hello-world repository owned by octocat. + + .NOTES + https://docs.github.com/rest/repos/rule-suites#list-repository-rule-suites + + #> + [OutputType([pscustomobject])] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Long links')] + [CmdletBinding()] + param ( + # The account owner of the repository. The name is not case sensitive. + [Parameter()] + [Alias('org')] + [string] $Owner = (Get-GitHubConfig -Name Owner), + + # The name of the repository without the .git extension. The name is not case sensitive. + [Parameter()] + [string] $Repo = (Get-GitHubConfig -Name Repo), + + # The name of the ref. Cannot contain wildcard characters. + # When specified, only rule evaluations triggered for this ref will be returned. + [Parameter()] + [string] $Ref, + + # The time period to filter by. + # For example,day will filter for rule suites that occurred in the past 24 hours, + # and week will filter for insights that occurred in the past 7 days (168 hours). + [Parameter()] + [ValidateSet('hour', 'day', 'week', 'month')] + [string] $TimePeriod = 'day', + + # The handle for the GitHub user account to filter on. When specified, only rule evaluations triggered by this actor will be returned. + [Parameter()] + [string] $ActorName, + + # The rule results to filter on. When specified, only suites with this result will be returned. + [Parameter()] + [ValidateSet('pass', 'fail', 'bypass', 'all')] + [string] $RuleSuiteResult = 'all', + + # The number of results per page (max 100). + [Parameter()] + [ValidateRange(1, 100)] + [int] $PerPage = 30 + ) + + $PSCmdlet.MyInvocation.MyCommand.Parameters.GetEnumerator() | ForEach-Object { + $paramName = $_.Key + $paramDefaultValue = Get-Variable -Name $paramName -ValueOnly -ErrorAction SilentlyContinue + $providedValue = $PSBoundParameters[$paramName] + Write-Verbose "[$paramName]" + Write-Verbose " - Default: [$paramDefaultValue]" + Write-Verbose " - Provided: [$providedValue]" + if (-not $PSBoundParameters.ContainsKey($paramName) -and ($null -ne $paramDefaultValue)) { + Write-Verbose ' - Using default value' + $PSBoundParameters[$paramName] = $paramDefaultValue + } else { + Write-Verbose ' - Using provided value' + } + } + + $body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case + Remove-HashtableEntry -Hashtable $body -RemoveNames 'Owner', 'Repo' -RemoveTypes 'SwitchParameter' + + + $inputObject = @{ + APIEndpoint = "/repos/$Owner/$Repo/rulesets/rule-suites" + Method = 'GET' + Body = $body + } + + Invoke-GitHubAPI @inputObject | ForEach-Object { + Write-Output $_.Response + } +} diff --git a/tools/utilities/GitHubAPI.ps1 b/tools/utilities/GitHubAPI.ps1 index 23934060c..fcb166d65 100644 --- a/tools/utilities/GitHubAPI.ps1 +++ b/tools/utilities/GitHubAPI.ps1 @@ -21,8 +21,8 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get # @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, ` # @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table -$path = '/repos/{owner}/{repo}/tags/protection/{tag_protection_id}' -$method = 'delete' +$path = '/repos/{owner}/{repo}/rulesets/rule-suites/{rule_suite_id}' +$method = 'get' $response.paths.$path.$method $response.paths.$path.$method.tags | clip # -> Namespace/foldername $response.paths.$path.$method.operationId | clip # -> FunctionName