Skip to content

Commit 0a503f8

Browse files
🩹 [Patch]: Align function parameters and logging (#207)
## Description - Fixes 🩹 [Patch]: Cleanup debug/verbose logging #187 - Fixes Align default parameter handling #78 This pull request introduces several new features and improvements, including new PowerShell filters for GitHub workflow runs, updates to the coverage report, and enhancements to existing scripts. The most important changes are summarized below: ### Main change: - Update all functions to use a function for showing the execution path to the function in begin and end blocks of the functions. The exec path starts at the first non '<ScriptBlock>' and non-powershell script so it doesn't drown out the logs with useless info. Main goal of this change is to clearly see where we are in the function execution. These only show if `-Debug` is provided. - Aligned all functions to use a $body hashtable for inputs to the API call, instead of having some strange function with automated case style management. Plain and simple, use a @Body hashtable, an $inputObject variable for the calls to the API and the API call itself. Easy as that. Likely more alignment to come via classes to drive consistency at a later point. - All functions should now have a begin process and end block. Debug functions are omitted from this requirement. ### Function changes: - Splitting `Get-GitHubWorkflowRun` into two new private functions and referencing them in the public function. - `Get-GitHubWorkflowRunByRepo` - `Get-GitHubWorkflowRunByWorkflow` ### Workflow Enhancements: * Added `Initialize-PSModule` step to the `Set-CoverageReport.yml` workflow, to install MarkdownPS automatically before the script is run. ### Codebase Enhancements: * Updated `Get-GitHubAppByName`, `Get-GitHubAuthenticatedApp`, `Assert-GitHubContext`, `Remove-GitHubContext`, `Resolve-GitHubContext`, and `Set-GitHubContext` scripts to use `Write-Debug` instead of `Write-Verbose` for improved debugging. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [x] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [ ] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas --------- Co-authored-by: github-actions <[email protected]>
1 parent 77ac2d7 commit 0a503f8

File tree

230 files changed

+6704
-3415
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

230 files changed

+6704
-3415
lines changed

.github/workflows/Set-CoverageReport.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ jobs:
1616
- name: Checkout repository
1717
uses: actions/checkout@v4
1818

19+
- name: Initialize-PSModule
20+
uses: PSModule/Initialize-PSModule@v1
21+
1922
- name: Update coverage report
2023
uses: PSModule/GitHub-Script@v1
2124
with:

Coverage.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
</tr>
1010
<tr>
1111
<td>Covered functions</td>
12-
<td>155</td>
12+
<td>156</td>
1313
</tr>
1414
<tr>
1515
<td>Missing functions</td>
16-
<td>825</td>
16+
<td>824</td>
1717
</tr>
1818
<tr>
1919
<td>Coverage</td>
20-
<td>15.82%</td>
20+
<td>15.92%</td>
2121
</tr>
2222
</table>
2323

@@ -54,7 +54,7 @@
5454
| `/emojis` | | :white_check_mark: | | | |
5555
| `/enterprises/{enterprise}/dependabot/alerts` | | :x: | | | |
5656
| `/enterprises/{enterprise}/secret-scanning/alerts` | | :x: | | | |
57-
| `/events` | | :x: | | | |
57+
| `/events` | | :white_check_mark: | | | |
5858
| `/feeds` | | :x: | | | |
5959
| `/gists` | | :x: | | :x: | |
6060
| `/gists/public` | | :x: | | | |
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
filter Get-GitHubWorkflowRunByRepo {
2+
<#
3+
.SYNOPSIS
4+
List workflow runs for a repository
5+
6+
.DESCRIPTION
7+
Lists all workflow runs for a repository. You can use parameters to narrow the list of results. For more information about using parameters,
8+
see [Parameters](https://docs.github.com/rest/guides/getting-started-with-the-rest-api#parameters).
9+
Anyone with read access to the repository can use this endpoint.
10+
OAuth app tokens and personal access tokens (classic) need the `repo` scope to use this endpoint with a private repository.
11+
This endpoint will return up to 1,000 results for each search when using the following parameters: `actor`, `branch`, `check_suite_id`,
12+
`created`, `event`, `head_sha`, `status`.
13+
14+
.EXAMPLE
15+
Get-GitHubWorkflowRunByRepo -Owner 'owner' -Repo 'repo'
16+
17+
Lists all workflow runs for a repository.
18+
19+
.EXAMPLE
20+
Get-GitHubWorkflowRunByRepo -Owner 'owner' -Repo 'repo' -Actor 'octocat' -Branch 'main' -Event 'push' -Status 'success'
21+
22+
Lists all workflow runs for a repository with the specified actor, branch, event, and status.
23+
24+
.NOTES
25+
[List workflow runs for a repository](https://docs.github.com/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-repository)
26+
#>
27+
[CmdletBinding(DefaultParameterSetName = 'Repo')]
28+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Contains a long link.')]
29+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', 'Event',
30+
Justification = 'A parameter that is used in the api call.')]
31+
param(
32+
# The account owner of the repository. The name is not case sensitive.
33+
[Parameter(Mandatory)]
34+
[string] $Owner,
35+
36+
# The name of the repository. The name is not case sensitive.
37+
[Parameter(Mandatory)]
38+
[string] $Repo,
39+
40+
# Returns someone's workflow runs. Use the login for the user who created the push associated with the check suite or workflow run.
41+
[Parameter()]
42+
[string] $Actor,
43+
44+
# Returns workflow runs associated with a branch. Use the name of the branch of the `push`.
45+
[Parameter()]
46+
[string] $Branch,
47+
48+
# Returns workflow run triggered by the event you specify. For example, `push`, `pull_request` or `issue`. For more information, see
49+
# "[Events that trigger workflows])(https://docs.github.com/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows)."
50+
[Parameter()]
51+
[string] $Event,
52+
53+
# Returns workflow runs with the check run status or conclusion that you specify. For example, a conclusion can be success or a status can be
54+
# `in_progress`. Only GitHub Actions can set a status of `waiting`, `pending`, or `requested`.
55+
# Can be one of: `completed`, `action_required`, `cancelled`, `failure`, `neutral`, `skipped`, `stale`, `success`, `timed_out`, `in_progress`,
56+
# `queued`, `requested`, `waiting`, `pending`.
57+
[Parameter()]
58+
[ValidateSet('completed', 'action_required', 'cancelled', 'failure', 'neutral', 'skipped', 'stale', 'success', 'timed_out', 'in_progress',
59+
'queued', 'requested', 'waiting', 'pending')]
60+
[string] $Status,
61+
62+
# Returns workflow runs created within the given date-time range. For more information on the syntax, see
63+
# "[Understanding the search syntax](https://docs.github.com/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax#query-for-dates)."
64+
[Parameter()]
65+
[datetime] $Created,
66+
67+
# If `true` pull requests are omitted from the response (empty array).
68+
[Parameter()]
69+
[switch] $ExcludePullRequests,
70+
71+
# Returns workflow runs with the check_suite_id that you specify.
72+
[Parameter()]
73+
[int] $CheckSuiteID,
74+
75+
# Only returns workflow runs that are associated with the specified head_sha.
76+
[Parameter()]
77+
[string] $HeadSHA,
78+
79+
# The number of results per page (max 100).
80+
[Parameter()]
81+
[ValidateRange(0, 100)]
82+
[int] $PerPage,
83+
84+
# The context to run the command in. Used to get the details for the API call.
85+
# Can be either a string or a GitHubContext object.
86+
[Parameter()]
87+
[object] $Context = (Get-GitHubContext)
88+
)
89+
90+
begin {
91+
$stackPath = Get-PSCallStackPath
92+
Write-Debug "[$stackPath] - Start"
93+
$Context = Resolve-GitHubContext -Context $Context
94+
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
95+
if ([string]::IsNullOrEmpty($Owner)) {
96+
$Owner = $Context.Owner
97+
}
98+
Write-Debug "Owner: [$Owner]"
99+
100+
if ([string]::IsNullOrEmpty($Repo)) {
101+
$Repo = $Context.Repo
102+
}
103+
Write-Debug "Repo: [$Repo]"
104+
}
105+
106+
process {
107+
try {
108+
$body = @{
109+
actor = $Actor
110+
branch = $Branch
111+
event = $Event
112+
status = $Status
113+
created = $Created
114+
exclude_pull_requests = $ExcludePullRequests
115+
check_suite_id = $CheckSuiteID
116+
head_sha = $HeadSHA
117+
per_page = $PerPage
118+
}
119+
120+
$inputObject = @{
121+
Context = $Context
122+
APIEndpoint = "/repos/$Owner/$Repo/actions/runs"
123+
Method = 'GET'
124+
Body = $body
125+
}
126+
127+
Invoke-GitHubAPI @inputObject | ForEach-Object {
128+
Write-Output $_.Response.workflow_runs
129+
}
130+
} catch {
131+
throw $_
132+
}
133+
}
134+
135+
end {
136+
Write-Debug "[$stackPath] - End"
137+
}
138+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
filter Get-GitHubWorkflowRunByWorkflow {
2+
<#
3+
.SYNOPSIS
4+
List workflow runs for a workflow
5+
6+
.DESCRIPTION
7+
List all workflow runs for a workflow. You can replace `workflow_id` with the workflow filename. For example, you could use `main.yaml`.
8+
You can use parameters to narrow the list of results. For more information about using parameters, see
9+
[Parameters](https://docs.github.com/rest/guides/getting-started-with-the-rest-api#parameters).
10+
Anyone with read access to the repository can use this endpoint
11+
OAuth app tokens and personal access tokens (classic) need the `repo` scope to use this endpoint with a private repository.
12+
This endpoint will return up to 1,000 results for each search when using the following parameters: `actor`, `branch`, `check_suite_id`,
13+
`created`, `event`, `head_sha`, `status`.
14+
15+
.EXAMPLE
16+
Get-GitHubWorkflowRunByWorkflow -Owner 'octocat' -Repo 'Hello-World' -ID '42'
17+
18+
Gets all workflow runs for the workflow with the ID `42` in the repository `Hello-World` owned by `octocat`.
19+
20+
.EXAMPLE
21+
Get-GitHubWorkflowRunByWorkflow -Owner 'octocat' -Repo 'Hello-World' -ID '42' -Actor 'octocat' -Branch 'main' -Event 'push' -Status 'success'
22+
23+
Gets all workflow runs for the workflow with the ID `42` in the repository `Hello-World` owned by `octocat` that were triggered by the user
24+
`octocat` on the branch `main` and have the status `success`.
25+
26+
.NOTES
27+
[List workflow runs for a workflow](https://docs.github.com/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-workflow)
28+
#>
29+
[CmdletBinding(DefaultParameterSetName = 'Repo')]
30+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidLongLines', '', Justification = 'Contains a long link.')]
31+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidAssignmentToAutomaticVariable', 'Event',
32+
Justification = 'A parameter that is used in the api call.')]
33+
param(
34+
# The account owner of the repository. The name is not case sensitive.
35+
[Parameter(Mandatory)]
36+
[string] $Owner,
37+
38+
# The name of the repository. The name is not case sensitive.
39+
[Parameter(Mandatory)]
40+
[string] $Repo,
41+
42+
# The ID of the workflow. You can also pass the workflow filename as a string.
43+
[Parameter(Mandatory)]
44+
[Alias('workflow_id', 'WorkflowID')]
45+
[string] $ID,
46+
47+
# Returns someone's workflow runs. Use the login for the user who created the push associated with the check suite or workflow run.
48+
[Parameter()]
49+
[string] $Actor,
50+
51+
# Returns workflow runs associated with a branch. Use the name of the branch of the `push`.
52+
[Parameter()]
53+
[string] $Branch,
54+
55+
# Returns workflow run triggered by the event you specify. For example, `push`, `pull_request` or `issue`. For more information, see
56+
# "[Events that trigger workflows])(https://docs.github.com/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows)."
57+
[Parameter()]
58+
[string] $Event,
59+
60+
# Returns workflow runs with the check run status or conclusion that you specify. For example, a conclusion can be success or a status can be
61+
# `in_progress`. Only GitHub Actions can set a status of `waiting`, `pending`, or `requested`.
62+
# Can be one of: `completed`, `action_required`, `cancelled`, `failure`, `neutral`, `skipped`, `stale`, `success`, `timed_out`, `in_progress`,
63+
# `queued`, `requested`, `waiting`, `pending`.
64+
[Parameter()]
65+
[ValidateSet('completed', 'action_required', 'cancelled', 'failure', 'neutral', 'skipped', 'stale', 'success', 'timed_out', 'in_progress',
66+
'queued', 'requested', 'waiting', 'pending')]
67+
[string] $Status,
68+
69+
# Returns workflow runs created within the given date-time range. For more information on the syntax, see
70+
# "[Understanding the search syntax](https://docs.github.com/search-github/getting-started-with-searching-on-github/understanding-the-search-syntax#query-for-dates)."
71+
[Parameter()]
72+
[datetime] $Created,
73+
74+
# If `true` pull requests are omitted from the response (empty array).
75+
[Parameter()]
76+
[switch] $ExcludePullRequests,
77+
78+
# Returns workflow runs with the check_suite_id that you specify.
79+
[Parameter()]
80+
[int] $CheckSuiteID,
81+
82+
# Only returns workflow runs that are associated with the specified head_sha.
83+
[Parameter()]
84+
[string] $HeadSHA,
85+
86+
# The number of results per page (max 100).
87+
[Parameter()]
88+
[ValidateRange(0, 100)]
89+
[int] $PerPage,
90+
91+
# The context to run the command in. Used to get the details for the API call.
92+
# Can be either a string or a GitHubContext object.
93+
[Parameter()]
94+
[object] $Context = (Get-GitHubContext)
95+
)
96+
97+
begin {
98+
$stackPath = Get-PSCallStackPath
99+
Write-Debug "[$stackPath] - Start"
100+
$Context = Resolve-GitHubContext -Context $Context
101+
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
102+
if ([string]::IsNullOrEmpty($Owner)) {
103+
$Owner = $Context.Owner
104+
}
105+
Write-Debug "Owner: [$Owner]"
106+
107+
if ([string]::IsNullOrEmpty($Repo)) {
108+
$Repo = $Context.Repo
109+
}
110+
Write-Debug "Repo: [$Repo]"
111+
}
112+
113+
process {
114+
try {
115+
$body = @{
116+
actor = $Actor
117+
branch = $Branch
118+
event = $Event
119+
status = $Status
120+
created = $Created
121+
exclude_pull_requests = $ExcludePullRequests
122+
check_suite_id = $CheckSuiteID
123+
head_sha = $HeadSHA
124+
per_page = $PerPage
125+
}
126+
127+
$inputObject = @{
128+
Context = $Context
129+
APIEndpoint = "/repos/$Owner/$Repo/actions/workflows/$ID/runs"
130+
Method = 'GET'
131+
Body = $body
132+
}
133+
134+
Invoke-GitHubAPI @inputObject | ForEach-Object {
135+
Write-Output $_.Response.workflow_runs
136+
}
137+
} catch {
138+
throw $_
139+
}
140+
}
141+
142+
end {
143+
Write-Debug "[$stackPath] - End"
144+
}
145+
}

src/functions/private/Apps/Get-GitHubAppByName.ps1

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,28 @@
3131
)
3232

3333
begin {
34-
$commandName = $MyInvocation.MyCommand.Name
35-
Write-Verbose "[$commandName] - Start"
34+
$stackPath = Get-PSCallStackPath
35+
Write-Debug "[$stackPath] - Start"
3636
$Context = Resolve-GitHubContext -Context $Context
3737
Assert-GitHubContext -Context $Context -AuthType IAT, PAT, UAT
3838
}
3939

4040
process {
41-
$inputObject = @{
42-
Context = $Context
43-
APIEndpoint = "/apps/$AppSlug"
44-
Method = 'GET'
45-
}
46-
47-
Invoke-GitHubAPI @inputObject | ForEach-Object {
48-
Write-Output $_.Response
41+
try {
42+
$inputObject = @{
43+
Context = $Context
44+
APIEndpoint = "/apps/$AppSlug"
45+
Method = 'GET'
46+
}
47+
48+
Invoke-GitHubAPI @inputObject | ForEach-Object {
49+
Write-Output $_.Response
50+
}
51+
} catch {
52+
throw $_
4953
}
5054
}
5155
end {
52-
Write-Verbose "[$commandName] - End"
56+
Write-Debug "[$stackPath] - End"
5357
}
5458
}

0 commit comments

Comments
 (0)