From 0e0a9319b8c8c61f14e5341b967b94c23c63891b Mon Sep 17 00:00:00 2001 From: rulasg Date: Sun, 23 Feb 2025 12:38:45 +0100 Subject: [PATCH 1/4] Add Get-UserToken function to retrieve GitHub token and update setRepoProperties.ps1 to use it --- public/setRepoProperties.ps1 | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/public/setRepoProperties.ps1 b/public/setRepoProperties.ps1 index 4262622..4c9bc2c 100644 --- a/public/setRepoProperties.ps1 +++ b/public/setRepoProperties.ps1 @@ -5,7 +5,7 @@ # This command works # curl -L -H "Authorization: Bearer $env:GH_TOKEN" -X PATCH https://api.github.com/repos/solidifydemo/bit21/properties/values -d '{"properties":[{"property_name":"kk","value":"kkvalue23"}]}' $cmd = @' -curl -L -s -H "Authorization: Bearer $env:GH_TOKEN" -X PATCH https://api.github.com/repos/{owner}/{repo}/properties/values -d '{"properties":[{"property_name":"{name}","value":"{value}"}]}' +curl -L -s -H "Authorization: Bearer {token}" -X PATCH https://api.github.com/repos/{owner}/{repo}/properties/values -d '{"properties":[{"property_name":"{name}","value":"{value}"}]}' '@ Set-MyInvokeCommandAlias -Alias SetRepoProperty -Command $cmd @@ -34,7 +34,9 @@ function Set-RepoProperty{ "Setting property $Name to $Value for $Owner/$Repo" | Write-Verbose - $param = @{ owner = $Owner ; repo = $Repo ; name = $Name ; value = $Value } + $token = Get-UserToken + + $param = @{ owner = $Owner ; repo = $Repo ; name = $Name ; value = $Value ; token= $token} if($PSCmdlet.ShouldProcess("$Owner/$Repo","Set property $Name to $Value")){ $result = Invoke-MyCommandJson -Command SetRepoProperty -Parameters $param @@ -47,3 +49,12 @@ function Set-RepoProperty{ return $null } Export-ModuleMember -Function Set-RepoProperty +function Get-UserToken{ + $token = $env:GH_TOKEN + if($null -eq $token){ + "GH_TOKEN environment variable is not set" | Write-Error + return $null + } + return $token +} + From 0f9fb3b26f6aa867a91c48e0a5b8b6df64cc39dd Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 24 Feb 2025 17:11:23 +0100 Subject: [PATCH 2/4] Update setRepoProperties.ps1 to use Get-UserToken for token retrieval in Set-RepoProperty function --- Test/public/setRepoPropertiesSuccess.test.ps1 | 5 ++++- public/setRepoProperties.ps1 | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Test/public/setRepoPropertiesSuccess.test.ps1 b/Test/public/setRepoPropertiesSuccess.test.ps1 index 13d5838..18e06fd 100644 --- a/Test/public/setRepoPropertiesSuccess.test.ps1 +++ b/Test/public/setRepoPropertiesSuccess.test.ps1 @@ -21,18 +21,21 @@ curl -L -s -H "Authorization: Bearer $env:GH_TOKEN" -X PATCH https://api.github. function Test_SetRepoProperties_NotFound{ $owner = 'solidifydemo' ; $repo = 'bit21' ; $property = 'kk' ; $value = 'someValuekk' + $token = 'fakeToken' $cmd = @' -curl -L -s -H "Authorization: Bearer $env:GH_TOKEN" -X PATCH https://api.github.com/repos/{owner}/{repo}/properties/values -d '{"properties":[{"property_name":"{name}","value":"{value}"}]}' +curl -L -s -H "Authorization: Bearer {token}" -X PATCH https://api.github.com/repos/{owner}/{repo}/properties/values -d '{"properties":[{"property_name":"{name}","value":"{value}"}]}' '@ $cmd = $cmd -replace '{owner}',$owner $cmd = $cmd -replace '{repo}',$repo $cmd = $cmd -replace '{name}',$property $cmd = $cmd -replace '{value}',$value + $cmd = $cmd -replace '{token}',$token # If success return null $mockfile = $PSScriptRoot | Join-Path -ChildPath 'testData' -AdditionalChildPath 'setRepoPropertiesNotFound.json' Set-InvokeCommandMock -Alias $cmd -Command "Get-Content -Path $(($mockfile | Get-Item).FullName)" + Set-InvokeCommandMock -Alias getToken -Command "echo $token" $result = Set-RepoProperty -owner $owner -repo $repo -name $property -value $value @ErrorParameters diff --git a/public/setRepoProperties.ps1 b/public/setRepoProperties.ps1 index 4c9bc2c..9902ed4 100644 --- a/public/setRepoProperties.ps1 +++ b/public/setRepoProperties.ps1 @@ -9,6 +9,7 @@ curl -L -s -H "Authorization: Bearer {token}" -X PATCH https://api.github.com/re '@ Set-MyInvokeCommandAlias -Alias SetRepoProperty -Command $cmd +Set-MyInvokeCommandAlias -Alias getToken -Command "Get-UserToken" <# .SYNOPSIS @@ -34,7 +35,7 @@ function Set-RepoProperty{ "Setting property $Name to $Value for $Owner/$Repo" | Write-Verbose - $token = Get-UserToken + $token = Invoke-MyCommand -Command getToken $param = @{ owner = $Owner ; repo = $Repo ; name = $Name ; value = $Value ; token= $token} @@ -56,5 +57,5 @@ function Get-UserToken{ return $null } return $token -} +} Export-ModuleMember -Function Get-UserToken From 1a9bdaa7ae4906419c6f5267a281361af1e07e99 Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 24 Feb 2025 17:15:20 +0100 Subject: [PATCH 3/4] Add synopsis documentation for Get-UserToken function in setRepoProperties.ps1 --- public/setRepoProperties.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/public/setRepoProperties.ps1 b/public/setRepoProperties.ps1 index 9902ed4..856f251 100644 --- a/public/setRepoProperties.ps1 +++ b/public/setRepoProperties.ps1 @@ -50,6 +50,10 @@ function Set-RepoProperty{ return $null } Export-ModuleMember -Function Set-RepoProperty +<# +.SYNOPSIS + Gets the user token from the environment +#> function Get-UserToken{ $token = $env:GH_TOKEN if($null -eq $token){ From ee1838baae2b5058ce7458a8d9f34bd1da842d69 Mon Sep 17 00:00:00 2001 From: rulasg Date: Mon, 24 Feb 2025 17:25:22 +0100 Subject: [PATCH 4/4] Update test to use token variable in Set-RepoProperty command --- Test/public/setRepoPropertiesSuccess.test.ps1 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Test/public/setRepoPropertiesSuccess.test.ps1 b/Test/public/setRepoPropertiesSuccess.test.ps1 index 18e06fd..dcff2e2 100644 --- a/Test/public/setRepoPropertiesSuccess.test.ps1 +++ b/Test/public/setRepoPropertiesSuccess.test.ps1 @@ -1,17 +1,20 @@ function Test_SetRepoProperties_Success{ $owner = 'solidifydemo' ; $repo = 'bit21' ; $property = 'kk' ; $value = 'someValuekk' + $token = 'fakeToken' $cmd = @' -curl -L -s -H "Authorization: Bearer $env:GH_TOKEN" -X PATCH https://api.github.com/repos/{owner}/{repo}/properties/values -d '{"properties":[{"property_name":"{name}","value":"{value}"}]}' +curl -L -s -H "Authorization: Bearer {token}" -X PATCH https://api.github.com/repos/{owner}/{repo}/properties/values -d '{"properties":[{"property_name":"{name}","value":"{value}"}]}' '@ $cmd = $cmd -replace '{owner}',$owner $cmd = $cmd -replace '{repo}',$repo $cmd = $cmd -replace '{name}',$property $cmd = $cmd -replace '{value}',$value + $cmd = $cmd -replace '{token}',$token # If success return null Set-InvokeCommandMock -Alias $cmd -Command "echo null" + Set-InvokeCommandMock -Alias getToken -Command "echo $token" $result = Set-RepoProperty -owner $owner -repo $repo -name $property -value $value