Skip to content

Commit 9e7a331

Browse files
🪲[Fix]: Piping GitHubSecret and GitHubVariable objects to Remove-GitHubSecret and Remove-GitHubVariable (#499)
This pull request improves the handling and testing of secret and variable removal in the GitHub module. The main focus is on making the removal functions (`Remove-GitHubSecret`, `Remove-GitHubVariable`) more robust and scope-aware, and on expanding the test coverage to ensure correct behavior across different usage patterns and scopes. - Fixes #388 Key changes include: ### Functional improvements to removal logic * Updated `Remove-GitHubSecret.ps1` and `Remove-GitHubVariable.ps1` to dispatch removal actions based on the `Scope` property of each item, ensuring the correct removal function is called for environment, repository, or organization scopes, and providing clear error handling for unsupported scopes. ### Documentation and class property clarifications * Improved property descriptions in `GitHubSecret.ps1` and `GitHubVariable.ps1` to clarify that properties refer to where the secret or variable is stored, enhancing code readability and maintainability. ### Expanded and improved test coverage * Refactored and expanded tests in `Secrets.Tests.ps1` and `Variables.Tests.ps1` to: - Add separate tests for pipeline-based removal using both direct pipeline and variable assignment approaches. - Ensure secrets and variables are created and removed correctly in all supported scopes. - Add logging and verification steps to improve test clarity and debugging. These changes make the module's behavior more predictable and easier to test, especially when handling secrets and variables in different scopes and using pipeline operations. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: MariusStorhaug <17722253+MariusStorhaug@users.noreply.github.com> Co-authored-by: Marius Storhaug <marstor@hotmail.com>
1 parent 89c27cd commit 9e7a331

File tree

6 files changed

+237
-48
lines changed

6 files changed

+237
-48
lines changed

‎src/classes/public/Secrets/GitHubSecret.ps1

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
class GitHubSecret {
2-
# The key ID of the public key.
2+
# The name of the secret.
33
[string] $Name
44

5-
# The scope of the variable, organization, repository, or environment.
5+
# The scope of the secret, organization, repository, or environment.
66
[string] $Scope
77

8-
# The name of the organization or user the Public Key is associated with.
8+
# The name of the organization or user the secret is stored in.
99
[string] $Owner
1010

11-
# The name of the repository the Public Key is associated with.
11+
# The name of the repository the secret is stored in.
1212
[string] $Repository
1313

14-
# The name of the environment the Public Key is associated with.
14+
# The name of the environment the secret is stored in.
1515
[string] $Environment
1616

17-
# The date and time the variable was created.
17+
# The date and time the secret was created.
1818
[datetime] $CreatedAt
1919

20-
# The date and time the variable was last updated.
20+
# The date and time the secret was last updated.
2121
[datetime] $UpdatedAt
2222

23-
# The visibility of the variable.
23+
# The visibility of the secret.
2424
[string] $Visibility
2525

26-
# The ids of the repositories that the variable is visible to.
26+
# The ids of the repositories that the secret is visible to.
2727
[GitHubRepository[]] $SelectedRepositories
2828

2929
GitHubSecret() {}

‎src/classes/public/Variables/GitHubVariable.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
# The scope of the variable, organization, repository, or environment.
99
[string] $Scope
1010

11-
# The name of the organization or user the variable is associated with.
11+
# The name of the organization or user the variable is stored in.
1212
[string] $Owner
1313

14-
# The name of the repository the variable is associated with.
14+
# The name of the repository the variable is stored in.
1515
[string] $Repository
1616

17-
# The name of the environment the variable is associated with.
17+
# The name of the environment the variable is stored in.
1818
[string] $Environment
1919

2020
# The date and time the variable was created.

‎src/functions/public/Secrets/Remove-GitHubSecret.ps1

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,38 @@
7878
switch ($PSCmdlet.ParameterSetName) {
7979
'ArrayInput' {
8080
foreach ($item in $InputObject) {
81-
$params = @{
82-
Owner = $item.Owner
83-
Repository = $item.Repository
84-
Environment = $item.Environment
85-
Name = $item.Name
86-
Context = $item.Context
81+
switch ($item.Scope) {
82+
'environment' {
83+
$params = @{
84+
Owner = $item.Owner
85+
Repository = $item.Repository
86+
Environment = $item.Environment
87+
Name = $item.Name
88+
Context = $Context
89+
}
90+
Remove-GitHubSecretFromEnvironment @params
91+
}
92+
'repository' {
93+
$params = @{
94+
Owner = $item.Owner
95+
Repository = $item.Repository
96+
Name = $item.Name
97+
Context = $Context
98+
}
99+
Remove-GitHubSecretFromRepository @params
100+
}
101+
'organization' {
102+
$params = @{
103+
Owner = $item.Owner
104+
Name = $item.Name
105+
Context = $Context
106+
}
107+
Remove-GitHubSecretFromOwner @params
108+
}
109+
default {
110+
throw "Secret '$($item.Name)' has unsupported Scope value '$scope'."
111+
}
87112
}
88-
$params | Remove-HashtableEntry -NullOrEmptyValues
89-
Remove-GitHubSecret @params
90113
}
91114
break
92115
}

‎src/functions/public/Variables/Remove-GitHubVariable.ps1

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,38 @@
8484
switch ($PSCmdlet.ParameterSetName) {
8585
'ArrayInput' {
8686
foreach ($item in $InputObject) {
87-
$params = @{
88-
Owner = $item.Owner
89-
Repository = $item.Repository
90-
Environment = $item.Environment
91-
Name = $item.Name
92-
Context = $item.Context
87+
switch ($item.Scope) {
88+
'environment' {
89+
$params = @{
90+
Owner = $item.Owner
91+
Repository = $item.Repository
92+
Environment = $item.Environment
93+
Name = $item.Name
94+
Context = $Context
95+
}
96+
Remove-GitHubVariableFromEnvironment @params
97+
}
98+
'repository' {
99+
$params = @{
100+
Owner = $item.Owner
101+
Repository = $item.Repository
102+
Name = $item.Name
103+
Context = $Context
104+
}
105+
Remove-GitHubVariableFromRepository @params
106+
}
107+
'organization' {
108+
$params = @{
109+
Owner = $item.Owner
110+
Name = $item.Name
111+
Context = $Context
112+
}
113+
Remove-GitHubVariableFromOwner @params
114+
}
115+
default {
116+
throw "Variable '$($item.Name)' has unsupported Scope value '$($item.Scope)'."
117+
}
93118
}
94-
$params | Remove-HashtableEntry -NullOrEmptyValues
95-
Remove-GitHubVariable @params
96119
}
97120
break
98121
}

‎tests/Secrets.Tests.ps1

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,14 +215,18 @@ Describe 'Secrets' {
215215
}
216216
}
217217

218-
It 'Remove-GitHubSecret' {
218+
It 'Remove-GitHubSecret via pipeline - using pipeline' {
219219
$testSecretName = "$secretName`TestSecret*"
220+
LogGroup 'Create secret(s) for pipeline removal' {
221+
$create = Set-GitHubSecret @scope -Name "$secretName`TestSecretPipeline" -Value 'PipelineTestValue'
222+
Write-Host "$($create | Format-List | Out-String)"
223+
}
220224
LogGroup 'Before remove' {
221225
$before = Get-GitHubSecret @scope -Name $testSecretName
222226
Write-Host "$($before | Format-List | Out-String)"
223227
}
224228
LogGroup 'Remove' {
225-
$before | Remove-GitHubSecret
229+
Get-GitHubSecret @scope -Name $testSecretName | Remove-GitHubSecret
226230
}
227231
LogGroup 'After remove' {
228232
$after = Get-GitHubSecret @scope -Name $testSecretName
@@ -231,6 +235,26 @@ Describe 'Secrets' {
231235
$after.Count | Should -Be 0
232236
}
233237

238+
It 'Remove-GitHubSecret via pipeline - using variable' {
239+
$pipelineTestSecretName = "$secretName`PipelineTest"
240+
LogGroup 'Create test secret for pipeline removal' {
241+
$createResult = Set-GitHubSecret @scope -Name $pipelineTestSecretName -Value 'PipelineTestValue'
242+
Write-Host "$($createResult | Format-List | Out-String)"
243+
}
244+
LogGroup 'Get secret for pipeline removal' {
245+
$secretToRemove = Get-GitHubSecret @scope -Name $pipelineTestSecretName
246+
Write-Host "$($secretToRemove | Format-List | Out-String)"
247+
}
248+
LogGroup 'Remove via pipeline' {
249+
{ $secretToRemove | Remove-GitHubSecret } | Should -Not -Throw
250+
}
251+
LogGroup 'Verify removal' {
252+
$after = Get-GitHubSecret @scope -Name $pipelineTestSecretName
253+
Write-Host "$($after | Format-List | Out-String)"
254+
$after | Should -BeNullOrEmpty
255+
}
256+
}
257+
234258
Context 'SelectedRepository' {
235259
It 'Get-GitHubSecretSelectedRepository - gets a list of selected repositories' {
236260
LogGroup "SelectedRepositories - [$orgSecretName]" {
@@ -402,18 +426,42 @@ Describe 'Secrets' {
402426
}
403427
}
404428

405-
It 'Remove-GitHubSecret' {
429+
It 'Remove-GitHubSecret via pipeline - using pipeline' {
430+
LogGroup 'Create secret(s) for pipeline removal' {
431+
$create = Set-GitHubSecret @scope -Name "$secretName`PipelineRemoval" -Value 'PipelineTestValue'
432+
Write-Host "$($create | Format-List | Out-String)"
433+
}
406434
$before = Get-GitHubSecret @scope -Name "*$os*"
407435
LogGroup 'Secrets - Before' {
408436
Write-Host "$($before | Format-Table | Out-String)"
409437
}
410-
$before | Remove-GitHubSecret
438+
Get-GitHubSecret @scope -Name "*$os*" | Remove-GitHubSecret
411439
$after = Get-GitHubSecret @scope -Name "*$os*"
412440
LogGroup 'Secrets -After' {
413441
Write-Host "$($after | Format-Table | Out-String)"
414442
}
415443
$after.Count | Should -Be 0
416444
}
445+
446+
It 'Remove-GitHubSecret via pipeline - using variable' {
447+
$pipelineTestSecretName = "$secretName`PipelineTest"
448+
LogGroup 'Create test secret for pipeline removal' {
449+
$createResult = Set-GitHubSecret @scope -Name $pipelineTestSecretName -Value 'PipelineTestValue'
450+
Write-Host "$($createResult | Format-List | Out-String)"
451+
}
452+
LogGroup 'Get secret for pipeline removal' {
453+
$secretToRemove = Get-GitHubSecret @scope -Name $pipelineTestSecretName
454+
Write-Host "$($secretToRemove | Format-List | Out-String)"
455+
}
456+
LogGroup 'Remove via pipeline' {
457+
{ $secretToRemove | Remove-GitHubSecret } | Should -Not -Throw
458+
}
459+
LogGroup 'Verify removal' {
460+
$after = Get-GitHubSecret @scope -Name $pipelineTestSecretName
461+
Write-Host "$($after | Format-List | Out-String)"
462+
$after | Should -BeNullOrEmpty
463+
}
464+
}
417465
}
418466

419467
Context 'Environment' -Skip:($OwnerType -in ('repository', 'enterprise')) {
@@ -512,18 +560,43 @@ Describe 'Secrets' {
512560
}
513561
}
514562

515-
It 'Remove-GitHubSecret' {
563+
It 'Remove-GitHubSecret via pipeline - using pipeline' {
564+
LogGroup 'Create secret(s) for pipeline removal' {
565+
$create = Set-GitHubSecret @scope -Name "$secretName`PipelineRemoval" -Value 'PipelineTestValue'
566+
Write-Host "$($create | Format-List | Out-String)"
567+
}
516568
LogGroup 'Secrets - Before' {
517569
$before = Get-GitHubSecret @scope -Name "*$os*"
518570
Write-Host "$($before | Format-Table | Out-String)"
519571
}
520-
$before | Remove-GitHubSecret
572+
Get-GitHubSecret @scope -Name "*$os*" | Remove-GitHubSecret
521573
LogGroup 'Secrets - After' {
522574
$after = Get-GitHubSecret @scope -Name "*$os*"
523575
Write-Host "$($after | Format-Table | Out-String)"
524576
}
525577
$after.Count | Should -Be 0
526578
}
579+
580+
It 'Remove-GitHubSecret via pipeline - using variable' {
581+
$pipelineTestSecretName = "$secretName`PipelineTest"
582+
LogGroup 'Create test secret for pipeline removal' {
583+
$createResult = Set-GitHubSecret @scope -Name $pipelineTestSecretName -Value 'PipelineTestValue'
584+
Write-Host "$($createResult | Format-List | Out-String)"
585+
}
586+
LogGroup 'Get secret and test whitespace handling' {
587+
$secretToRemove = Get-GitHubSecret @scope -Name $pipelineTestSecretName
588+
Write-Host "$($secretToRemove | Format-List | Out-String)"
589+
Write-Host "Testing that environment secrets with valid Environment property work correctly"
590+
}
591+
LogGroup 'Remove via pipeline' {
592+
{ $secretToRemove | Remove-GitHubSecret } | Should -Not -Throw
593+
}
594+
LogGroup 'Verify removal' {
595+
$after = Get-GitHubSecret @scope -Name $pipelineTestSecretName
596+
Write-Host "$($after | Format-List | Out-String)"
597+
$after | Should -BeNullOrEmpty
598+
}
599+
}
527600
}
528601
}
529602
}

0 commit comments

Comments
 (0)