Skip to content

Commit 2ce0f54

Browse files
[Feature] Repositories - Rule Suites - Manage rule suites for repositories (#80)
- Fixes #64
1 parent 36a505b commit 2ce0f54

File tree

4 files changed

+246
-2
lines changed

4 files changed

+246
-2
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
filter Get-GitHubRepositoryRuleSuite {
2+
<#
3+
.SYNOPSIS
4+
List repository rule suites or a rule suite by id.
5+
6+
.DESCRIPTION
7+
Lists suites of rule evaluations at the repository level.
8+
If an ID is specified, gets information about a suite of rule evaluations from within a repository.
9+
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)."
10+
11+
.EXAMPLE
12+
$params = @{
13+
Owner = 'octocat'
14+
Repo = 'hello-world'
15+
Ref = 'main'
16+
TimePeriod = 'day'
17+
ActorName = 'octocat'
18+
RuleSuiteResult = 'all'
19+
}
20+
Get-GitHubRepositoryRuleSuite @params
21+
22+
Gets a list of rule suites for the main branch of the hello-world repository owned by octocat.
23+
24+
.EXAMPLE
25+
Get-GitHubRepositoryRuleSuite -Owner 'octocat' -Repo 'hello-world' -RuleSuiteId 123456789
26+
27+
Gets information about a suite of rule evaluations with id 123456789 from within the octocat/hello-world repository.
28+
29+
.NOTES
30+
https://docs.github.com/rest/repos/rule-suites#list-repository-rule-suites
31+
https://docs.github.com/rest/repos/rule-suites#get-a-repository-rule-suite
32+
33+
#>
34+
[OutputType([pscustomobject])]
35+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Long links')]
36+
[CmdletBinding(DefaultParameterSetName = 'Default')]
37+
param (
38+
# The account owner of the repository. The name is not case sensitive.
39+
[Parameter()]
40+
[Alias('org')]
41+
[string] $Owner = (Get-GitHubConfig -Name Owner),
42+
43+
# The name of the repository without the .git extension. The name is not case sensitive.
44+
[Parameter()]
45+
[string] $Repo = (Get-GitHubConfig -Name Repo),
46+
47+
# The name of the ref. Cannot contain wildcard characters.
48+
# When specified, only rule evaluations triggered for this ref will be returned.
49+
[Parameter()]
50+
[string] $Ref,
51+
52+
# The time period to filter by.
53+
# For example,day will filter for rule suites that occurred in the past 24 hours,
54+
# and week will filter for insights that occurred in the past 7 days (168 hours).
55+
[Parameter()]
56+
[ValidateSet('hour', 'day', 'week', 'month')]
57+
[string] $TimePeriod = 'day',
58+
59+
# The handle for the GitHub user account to filter on. When specified, only rule evaluations triggered by this actor will be returned.
60+
[Parameter()]
61+
[string] $ActorName,
62+
63+
# The rule results to filter on. When specified, only suites with this result will be returned.
64+
[Parameter()]
65+
[ValidateSet('pass', 'fail', 'bypass', 'all')]
66+
[string] $RuleSuiteResult = 'all',
67+
68+
# The number of results per page (max 100).
69+
[Parameter()]
70+
[ValidateRange(1, 100)]
71+
[int] $PerPage = 30,
72+
73+
# 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.
74+
[Parameter(
75+
Mandatory,
76+
ParameterSetName = 'ById'
77+
)]
78+
[int] $RuleSuiteId
79+
)
80+
81+
switch ($PSCmdlet.ParameterSetName) {
82+
'Default' {
83+
$params = @{
84+
Owner = $Owner
85+
Repo = $Repo
86+
Ref = $Ref
87+
TimePeriod = $TimePeriod
88+
ActorName = $ActorName
89+
RuleSuiteResult = $RuleSuiteResult
90+
PerPage = $PerPage
91+
}
92+
Get-GitHubRepositoryRuleSuiteList @params
93+
}
94+
'ById' {
95+
$params = @{
96+
Owner = $Owner
97+
Repo = $Repo
98+
RuleSuiteId = $RuleSuiteId
99+
}
100+
Get-GitHubRepositoryRuleSuiteById @params
101+
}
102+
}
103+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
filter Get-GitHubRepositoryRuleSuiteById {
2+
<#
3+
.SYNOPSIS
4+
Get a repository rule suite
5+
6+
.DESCRIPTION
7+
Gets information about a suite of rule evaluations from within a repository.
8+
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)."
9+
10+
.EXAMPLE
11+
Get-GitHubRepositoryRuleSuiteById -Owner 'octocat' -Repo 'hello-world' -RuleSuiteId 123456789
12+
13+
Gets information about a suite of rule evaluations with id 123456789 from within the octocat/hello-world repository.
14+
15+
.NOTES
16+
https://docs.github.com/rest/repos/rule-suites#get-a-repository-rule-suite
17+
18+
#>
19+
[OutputType([pscustomobject])]
20+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Long links')]
21+
[CmdletBinding()]
22+
param (
23+
# The account owner of the repository. The name is not case sensitive.
24+
[Parameter()]
25+
[Alias('org')]
26+
[string] $Owner = (Get-GitHubConfig -Name Owner),
27+
28+
# The name of the repository without the .git extension. The name is not case sensitive.
29+
[Parameter()]
30+
[string] $Repo = (Get-GitHubConfig -Name Repo),
31+
32+
# 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.
33+
[Parameter(Mandatory)]
34+
[int] $RuleSuiteId
35+
36+
)
37+
38+
$inputObject = @{
39+
APIEndpoint = "/repos/$Owner/$Repo/rulesets/rule-suites/$RuleSuiteId"
40+
Method = 'GET'
41+
}
42+
43+
Invoke-GitHubAPI @inputObject | ForEach-Object {
44+
Write-Output $_.Response
45+
}
46+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
filter Get-GitHubRepositoryRuleSuiteList {
2+
<#
3+
.SYNOPSIS
4+
List repository rule suites
5+
6+
.DESCRIPTION
7+
Lists suites of rule evaluations at the repository level.
8+
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)."
9+
10+
.EXAMPLE
11+
$params = @{
12+
Owner = 'octocat'
13+
Repo = 'hello-world'
14+
Ref = 'main'
15+
TimePeriod = 'day'
16+
ActorName = 'octocat'
17+
RuleSuiteResult = 'all'
18+
}
19+
Get-GitHubRepositoryRuleSuiteList @params
20+
21+
Gets a list of rule suites for the main branch of the hello-world repository owned by octocat.
22+
23+
.NOTES
24+
https://docs.github.com/rest/repos/rule-suites#list-repository-rule-suites
25+
26+
#>
27+
[OutputType([pscustomobject])]
28+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Long links')]
29+
[CmdletBinding()]
30+
param (
31+
# The account owner of the repository. The name is not case sensitive.
32+
[Parameter()]
33+
[Alias('org')]
34+
[string] $Owner = (Get-GitHubConfig -Name Owner),
35+
36+
# The name of the repository without the .git extension. The name is not case sensitive.
37+
[Parameter()]
38+
[string] $Repo = (Get-GitHubConfig -Name Repo),
39+
40+
# The name of the ref. Cannot contain wildcard characters.
41+
# When specified, only rule evaluations triggered for this ref will be returned.
42+
[Parameter()]
43+
[string] $Ref,
44+
45+
# The time period to filter by.
46+
# For example,day will filter for rule suites that occurred in the past 24 hours,
47+
# and week will filter for insights that occurred in the past 7 days (168 hours).
48+
[Parameter()]
49+
[ValidateSet('hour', 'day', 'week', 'month')]
50+
[string] $TimePeriod = 'day',
51+
52+
# The handle for the GitHub user account to filter on. When specified, only rule evaluations triggered by this actor will be returned.
53+
[Parameter()]
54+
[string] $ActorName,
55+
56+
# The rule results to filter on. When specified, only suites with this result will be returned.
57+
[Parameter()]
58+
[ValidateSet('pass', 'fail', 'bypass', 'all')]
59+
[string] $RuleSuiteResult = 'all',
60+
61+
# The number of results per page (max 100).
62+
[Parameter()]
63+
[ValidateRange(1, 100)]
64+
[int] $PerPage = 30
65+
)
66+
67+
$PSCmdlet.MyInvocation.MyCommand.Parameters.GetEnumerator() | ForEach-Object {
68+
$paramName = $_.Key
69+
$paramDefaultValue = Get-Variable -Name $paramName -ValueOnly -ErrorAction SilentlyContinue
70+
$providedValue = $PSBoundParameters[$paramName]
71+
Write-Verbose "[$paramName]"
72+
Write-Verbose " - Default: [$paramDefaultValue]"
73+
Write-Verbose " - Provided: [$providedValue]"
74+
if (-not $PSBoundParameters.ContainsKey($paramName) -and ($null -ne $paramDefaultValue)) {
75+
Write-Verbose ' - Using default value'
76+
$PSBoundParameters[$paramName] = $paramDefaultValue
77+
} else {
78+
Write-Verbose ' - Using provided value'
79+
}
80+
}
81+
82+
$body = $PSBoundParameters | ConvertFrom-HashTable | ConvertTo-HashTable -NameCasingStyle snake_case
83+
Remove-HashtableEntry -Hashtable $body -RemoveNames 'Owner', 'Repo' -RemoveTypes 'SwitchParameter'
84+
85+
86+
$inputObject = @{
87+
APIEndpoint = "/repos/$Owner/$Repo/rulesets/rule-suites"
88+
Method = 'GET'
89+
Body = $body
90+
}
91+
92+
Invoke-GitHubAPI @inputObject | ForEach-Object {
93+
Write-Output $_.Response
94+
}
95+
}

tools/utilities/GitHubAPI.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ $response = Invoke-RestMethod -Uri $APIDocURI -Method Get
2121
# @{n = 'PUT'; e = { (($_.value.psobject.Properties.Name) -contains 'PUT') } }, `
2222
# @{n = 'PATCH'; e = { (($_.value.psobject.Properties.Name) -contains 'PATCH') } } | Format-Table
2323

24-
$path = '/repos/{owner}/{repo}/tags/protection/{tag_protection_id}'
25-
$method = 'delete'
24+
$path = '/repos/{owner}/{repo}/rulesets/rule-suites/{rule_suite_id}'
25+
$method = 'get'
2626
$response.paths.$path.$method
2727
$response.paths.$path.$method.tags | clip # -> Namespace/foldername
2828
$response.paths.$path.$method.operationId | clip # -> FunctionName

0 commit comments

Comments
 (0)