Skip to content

Commit 938eaab

Browse files
committed
Reorganize codebase and add Codespaces management πŸš€πŸ›°οΈπŸ“‘
1 parent e3dede5 commit 938eaab

Some content is hidden

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

49 files changed

+434
-40
lines changed

β€ŽCompleters/Codespaces.ps1

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
$codespaceNameCompleter = {
2+
[CmdletBinding()]
3+
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
4+
[hashtable] $TokenParam = @{}
5+
Add-DefaultParameterValues -Command $commandName -Parameters $TokenParam
6+
7+
try {
8+
$CodespaceList = Get-GitHubCodespace @TokenParam
9+
foreach ($Codespace in $CodespaceList) {
10+
if ($Codespace.Name -match $wordToComplete) {
11+
Write-Output -InputObject $Codespace.Name
12+
}
13+
}
14+
}
15+
catch {
16+
#Set-Content -Path psgithub.error.log -Value $PSItem
17+
}
18+
}
19+
20+
Get-Command -Module PSGitHub -Name *Codespace* | `
21+
Where-Object -FilterScript { $PSItem.Parameters -and $PSItem.Parameters.ContainsKey('CodespaceName') } | `
22+
ForEach-Object { Register-ArgumentCompleter -CommandName $PSItem.Name -ParameterName CodespaceName -ScriptBlock $codespaceNameCompleter }

β€ŽCompleters/Repository.ps1

+33-13
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,45 @@ Get-Command *-GitHub* | Where-Object { $_.Parameters -and $_.Parameters.Contains
4444

4545
$repositoryNameCompleter = {
4646
[CmdletBinding()]
47-
param([string]$command, [string]$parameter, [string]$wordToComplete, [CommandAst]$commandAst, [Hashtable]$params)
48-
Add-DefaultParameterValues -Command $command -Parameters $params
47+
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
48+
49+
#Add-DefaultParameterValues -Command $commandName -Parameters $fakeBoundParameter
50+
$RepoArray = [System.Object[]]::new(40)
51+
4952
$findRepoParams = @{
50-
Query = $wordToComplete
53+
Query = $wordToComplete
54+
}
55+
if ($fakeBoundParameter.ContainsKey('Owner')) {
56+
$findRepoParams.Query += ' user:{0}' -f $fakeBoundParameter.Owner
5157
}
52-
if ($params.ContainsKey('Token')) {
53-
$findRepoParams.Token = $params.Token
58+
try {
59+
Find-GitHubRepository @findRepoParams | ForEach-Object -Begin { $i = 0 } -Process {
60+
$RepoArray[$i] = $PSItem
61+
$i++
62+
}
5463
}
55-
if ($params.ContainsKey('Owner')) {
56-
$findRepoParams.Query += " user:$($params.Owner)"
64+
catch {
65+
# Once array is filled up, stop execution
5766
}
58-
Find-GitHubRepository @findRepoParams |
59-
Where-Object { $_.Name -like "$wordToComplete*" } |
60-
Select-Object -First 5 |
61-
ForEach-Object {
62-
$tooltip = if ($_.Description) { $_.Description } else { $_.Name }
63-
[CompletionResult]::new($_.Name, $_.Name, [CompletionResultType]::ParameterValue, $tooltip)
67+
68+
try {
69+
foreach ($Repo in $RepoArray) {
70+
$tooltip = if ($Repo.Description) { $Repo.Description } else { $Repo.Name }
71+
if (!$fakeBoundParameter.ContainsKey('Owner')) {
72+
$CompletionText = '{0} -Owner {1}' -f $Repo.Name, $Repo.Owner
73+
[CompletionResult]::new($CompletionText, $Repo.Name, [CompletionResultType]::ParameterValue, $tooltip)
74+
}
75+
else {
76+
[CompletionResult]::new($Repo.Name, $Repo.Name, [CompletionResultType]::ParameterValue, $tooltip)
77+
}
6478
}
79+
}
80+
catch {
81+
# Set-Content -Path $HOME/psgithub.error.log -Value $PSItem
82+
# Add-Content -Path $HOME/psgithub.error.log -Value ($RepoArray.SyncRoot | ConvertTo-Json)
83+
}
6584
}
85+
6686
Get-Command *-GitHub* | Where-Object { $_.Parameters -and $_.Parameters.ContainsKey('RepositoryName') } | ForEach-Object {
6787
Register-ArgumentCompleter -CommandName $_.Name -ParameterName RepositoryName -ScriptBlock $repositoryNameCompleter
6888
}

β€ŽFormats/Codespace.Format.ps1xml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<Configuration>
2+
<ViewDefinitions>
3+
<View>
4+
<Name>Table</Name>
5+
<ViewSelectedBy>
6+
<TypeName>PSGitHub.Codespace</TypeName>
7+
</ViewSelectedBy>
8+
<TableControl>
9+
<AutoSize></AutoSize>
10+
<TableHeaders>
11+
<TableColumnHeader>
12+
<Label>ID</Label>
13+
</TableColumnHeader>
14+
<TableColumnHeader>
15+
<Label>Name</Label>
16+
</TableColumnHeader>
17+
<TableColumnHeader>
18+
<Label>Size</Label>
19+
</TableColumnHeader>
20+
<TableColumnHeader>
21+
<Label>State</Label>
22+
</TableColumnHeader>
23+
<TableColumnHeader>
24+
<Label>Location</Label>
25+
</TableColumnHeader>
26+
<TableColumnHeader>
27+
<Label>Repository</Label>
28+
</TableColumnHeader>
29+
</TableHeaders>
30+
<TableRowEntries>
31+
<TableRowEntry>
32+
<TableColumnItems>
33+
<TableColumnItem>
34+
<PropertyName>id</PropertyName>
35+
</TableColumnItem>
36+
<TableColumnItem>
37+
<PropertyName>name</PropertyName>
38+
</TableColumnItem>
39+
<TableColumnItem>
40+
<ScriptBlock>$_.machine.name</ScriptBlock>
41+
</TableColumnItem>
42+
<TableColumnItem>
43+
<PropertyName>state</PropertyName>
44+
</TableColumnItem>
45+
<TableColumnItem>
46+
<PropertyName>location</PropertyName>
47+
</TableColumnItem>
48+
<TableColumnItem>
49+
<ScriptBlock>$_.repository.full_name</ScriptBlock>
50+
</TableColumnItem>
51+
</TableColumnItems>
52+
</TableRowEntry>
53+
</TableRowEntries>
54+
</TableControl>
55+
</View>
56+
</ViewDefinitions>
57+
</Configuration>

β€ŽFormats/Runner.Format.ps1xml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<Configuration>
2+
<ViewDefinitions>
3+
<View>
4+
<Name>Table</Name>
5+
<ViewSelectedBy>
6+
<TypeName>PSGitHub.Runner</TypeName>
7+
</ViewSelectedBy>
8+
<TableControl>
9+
<AutoSize></AutoSize>
10+
<TableHeaders>
11+
<TableColumnHeader>
12+
<Label>ID</Label>
13+
</TableColumnHeader>
14+
<TableColumnHeader>
15+
<Label>Name</Label>
16+
</TableColumnHeader>
17+
<TableColumnHeader>
18+
<Label>OS</Label>
19+
</TableColumnHeader>
20+
<TableColumnHeader>
21+
<Label>Status</Label>
22+
</TableColumnHeader>
23+
<TableColumnHeader>
24+
<Label>Busy</Label>
25+
</TableColumnHeader>
26+
<TableColumnHeader>
27+
<Label>Labels</Label>
28+
</TableColumnHeader>
29+
</TableHeaders>
30+
<TableRowEntries>
31+
<TableRowEntry>
32+
<TableColumnItems>
33+
<TableColumnItem>
34+
<PropertyName>id</PropertyName>
35+
</TableColumnItem>
36+
<TableColumnItem>
37+
<PropertyName>name</PropertyName>
38+
</TableColumnItem>
39+
<TableColumnItem>
40+
<PropertyName>os</PropertyName>
41+
</TableColumnItem>
42+
<TableColumnItem>
43+
<PropertyName>status</PropertyName>
44+
</TableColumnItem>
45+
<TableColumnItem>
46+
<PropertyName>busy</PropertyName>
47+
</TableColumnItem>
48+
<TableColumnItem>
49+
<ScriptBlock>$_.labels.name -join ','</ScriptBlock>
50+
</TableColumnItem>
51+
</TableColumnItems>
52+
</TableRowEntry>
53+
</TableRowEntries>
54+
</TableControl>
55+
</View>
56+
</ViewDefinitions>
57+
</Configuration>

β€ŽFunctions/Public/Add-GitHubRunnerLabel.ps1 renamed to β€ŽFunctions/Public/Actions/Add-GitHubRunnerLabel.ps1

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ function Add-GitHubRunnerLabel {
1010
[string] $Owner,
1111

1212
[Parameter(Mandatory, Position = 1)]
13-
[Alias('RepositoryName')]
14-
[string] $Repo,
13+
[string] $RepositoryName,
1514

1615
[Parameter(Mandatory, Position = 2)]
1716
[string] $RunnerId,
@@ -29,7 +28,7 @@ function Add-GitHubRunnerLabel {
2928
$Body = @{
3029
labels = $Label
3130
} | ConvertTo-Json
32-
$Path = 'repos/{0}/{1}/actions/runners/{2}/labels' -f $Owner, $Repo, $RunnerId
31+
$Path = 'repos/{0}/{1}/actions/runners/{2}/labels' -f $Owner, $RepositoryName, $RunnerId
3332
Invoke-GitHubApi $Path -BaseUri $BaseUri -Token $Token -Method Post -Body $Body |
3433
ForEach-Object { $_ } |
3534
ForEach-Object {

β€ŽFunctions/Public/Get-GitHubRunner.ps1 renamed to β€ŽFunctions/Public/Actions/Get-GitHubRunner.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Get-GitHubRunner {
1111

1212
[Parameter(Mandatory, Position = 1)]
1313
[Alias('RepositoryName')]
14-
[string] $Repo,
14+
[string] $RepositoryName,
1515

1616
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
1717
# Defaults to "https://api.github.com"
@@ -20,7 +20,7 @@ function Get-GitHubRunner {
2020
)
2121

2222
process {
23-
$Path = 'repos/{0}/{1}/actions/runners' -f $Owner, $Repo
23+
$Path = 'repos/{0}/{1}/actions/runners' -f $Owner, $RepositoryName
2424
Invoke-GitHubApi $Path -BaseUri $BaseUri -Token $Token |
2525
ForEach-Object { $_.runners } |
2626
ForEach-Object {

β€ŽFunctions/Public/Get-GitHubRunnerApplication.ps1 renamed to β€ŽFunctions/Public/Actions/Get-GitHubRunnerApplication.ps1

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Get-GitHubRunnerApplication {
1111

1212
[Parameter(Mandatory, Position = 1)]
1313
[Alias('RepositoryName')]
14-
[string] $Repo,
14+
[string] $RepositoryName,
1515

1616
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
1717
# Defaults to "https://api.github.com"
@@ -20,7 +20,7 @@ function Get-GitHubRunnerApplication {
2020
)
2121

2222
process {
23-
$Path = 'repos/{0}/{1}/actions/runners/downloads' -f $Owner, $Repo
23+
$Path = 'repos/{0}/{1}/actions/runners/downloads' -f $Owner, $RepositoryName
2424
Invoke-GitHubApi $Path -BaseUri $BaseUri -Token $Token |
2525
ForEach-Object { $_ } |
2626
ForEach-Object {
@@ -31,5 +31,5 @@ function Get-GitHubRunnerApplication {
3131
}
3232

3333
Export-ModuleMember -Alias @(
34-
(New-Alias -Name gghrunapp -Value Get-GitHubRunner -PassThru)
34+
(New-Alias -Name gghrunapp -Value Get-GitHubRunnerApplication -PassThru)
3535
)

β€ŽFunctions/Public/Get-GitHubRunnerRegistrationToken.ps1 renamed to β€ŽFunctions/Public/Actions/Get-GitHubRunnerRegistrationToken.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function Get-GitHubRunnerRegistrationToken {
1111

1212
[Parameter(Mandatory, Position = 1)]
1313
[Alias('RepositoryName')]
14-
[string] $Repo,
14+
[string] $RepositoryName,
1515

1616
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
1717
# Defaults to "https://api.github.com"
@@ -20,7 +20,7 @@ function Get-GitHubRunnerRegistrationToken {
2020
)
2121

2222
process {
23-
$Path = 'repos/{0}/{1}/actions/runners/registration-token' -f $Owner, $Repo
23+
$Path = 'repos/{0}/{1}/actions/runners/registration-token' -f $Owner, $RepositoryName
2424
Invoke-GitHubApi $Path -BaseUri $BaseUri -Token $Token -Method Post |
2525
ForEach-Object { $_ } |
2626
ForEach-Object {

β€ŽFunctions/Public/Remove-GitHubRunner.ps1 renamed to β€ŽFunctions/Public/Actions/Remove-GitHubRunner.ps1

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ function Remove-GitHubRunner {
88
param(
99
[Parameter(Mandatory, Position = 0)]
1010
[string] $Owner,
11-
11+
1212
[Parameter(Mandatory, Position = 1)]
13-
[string] $Repo,
14-
13+
[string] $RepositoryName,
14+
1515
[Parameter(Mandatory, Position = 2)]
1616
[int] $RunnerId,
1717

@@ -22,7 +22,7 @@ function Remove-GitHubRunner {
2222
)
2323

2424
process {
25-
$Path = 'repos/{0}/{1}/actions/runners/{2}' -f $Owner, $Repo, $RunnerId
25+
$Path = 'repos/{0}/{1}/actions/runners/{2}' -f $Owner, $RepositoryName, $RunnerId
2626
$null = Invoke-GitHubApi $Path -BaseUri $BaseUri -Token $Token -Method Delete
2727
}
2828
}

β€ŽFunctions/Public/Remove-GitHubRunnerLabel.ps1 renamed to β€ŽFunctions/Public/Actions/Remove-GitHubRunnerLabel.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function Remove-GitHubRunnerLabel {
1010
[string] $Owner,
1111

1212
[Parameter(Mandatory, Position = 1)]
13-
[string] $Repo,
13+
[string] $RepositoryName,
1414

1515
[Parameter(Mandatory, Position = 2)]
1616
[string] $RunnerId,
@@ -25,7 +25,7 @@ function Remove-GitHubRunnerLabel {
2525
)
2626

2727
process {
28-
$Path = 'repos/{0}/{1}/actions/runners/{2}/labels/{3}' -f $Owner, $Repo, $RunnerId, $Label
28+
$Path = 'repos/{0}/{1}/actions/runners/{2}/labels/{3}' -f $Owner, $RepositoryName, $RunnerId, $Label
2929
Invoke-GitHubApi $Path -BaseUri $BaseUri -Token $Token -Method Delete -Body $Body |
3030
ForEach-Object { $_ } |
3131
ForEach-Object {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function Get-GitHubCodespace {
2+
<#
3+
.SYNOPSIS
4+
Lists the authenticated user's codespaces.
5+
#>
6+
[CmdletBinding()]
7+
[OutputType('PSGitHub.Codespace')]
8+
param(
9+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
10+
# Defaults to "https://api.github.com"
11+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
12+
[Security.SecureString] $Token
13+
)
14+
15+
process {
16+
$Path = 'user/codespaces'
17+
Invoke-GitHubApi $Path -BaseUri $BaseUri -Token $Token |
18+
ForEach-Object { $_.codespaces } |
19+
ForEach-Object {
20+
$_.PSTypeNames.Insert(0, 'PSGitHub.Codespace')
21+
$_
22+
}
23+
}
24+
}
25+
26+
Export-ModuleMember -Alias @(
27+
(New-Alias -Name gghcs -Value Get-GitHubCodespace -PassThru)
28+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function Get-GitHubCodespaceMachine {
2+
<#
3+
.SYNOPSIS
4+
List the machine types a codespace can transition to use.
5+
#>
6+
[CmdletBinding()]
7+
[OutputType('PSGitHub.CodespaceMachine')]
8+
param(
9+
[Parameter(Mandatory = $true, Position = 0)]
10+
[Alias('Name')]
11+
[string] $CodespaceName,
12+
13+
# Optional base URL of the GitHub API, for example "https://ghe.mycompany.com/api/v3/" (including the trailing slash).
14+
# Defaults to "https://api.github.com"
15+
[Uri] $BaseUri = [Uri]::new('https://api.github.com'),
16+
[Security.SecureString] $Token
17+
)
18+
19+
process {
20+
$Path = 'user/codespaces/{0}/machines' -f $CodespaceName
21+
Invoke-GitHubApi $Path -BaseUri $BaseUri -Token $Token |
22+
ForEach-Object { $_.machines } |
23+
ForEach-Object {
24+
$_.PSTypeNames.Insert(0, 'PSGitHub.CodespaceMachine')
25+
$_
26+
}
27+
}
28+
}
29+

0 commit comments

Comments
Β (0)