From 2d2877ca08098503e4fc96dcced16768d2f7f883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20=28Dibildos=29=20Gonz=C3=A1lez?= Date: Mon, 20 Oct 2025 12:47:18 +0200 Subject: [PATCH 1/3] fix(api): variable management. Replace Get-ApiHostFromEnv and Get-TokenFromEnv with Get-EnvVariable --- include/callAPI.ps1 | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/include/callAPI.ps1 b/include/callAPI.ps1 index 0661efd..fff3b14 100644 --- a/include/callAPI.ps1 +++ b/include/callAPI.ps1 @@ -150,7 +150,7 @@ function Get-ApiHost { return $ApiHost } - $envValue = Get-ApiHostFromEnv + $envValue = Get-EnvVariable -Name $ENV_VAR_HOST_NAME if(![string]::IsNullOrWhiteSpace($envValue)){ "Host from env $envValue" | Write-MyVerbose return $envValue @@ -160,9 +160,6 @@ function Get-ApiHost { return $DEFAULT_GH_HOST } Export-ModuleMember -Function Get-ApiHost -function Get-ApiHostFromEnv{ - (Get-Item -path "Env:$ENV_VAR_HOST_NAME" -ErrorAction SilentlyContinue).Value -} #################################################################################################### @@ -183,7 +180,7 @@ function Get-ApiToken { return $Token } - $envValue = Get-TokenFromEnv + $envValue = Get-EnvVariable -Name $ENV_VAR_TOKEN_NAME if(![string]::IsNullOrWhiteSpace($envValue)){ "Token from env" | Write-MyVerbose return $envValue @@ -203,6 +200,20 @@ function Get-ApiToken { return $result } Export-ModuleMember -Function Get-ApiToken -function Get-TokenFromEnv{ - (Get-Item -path "Env:$ENV_VAR_TOKEN_NAME" -ErrorAction SilentlyContinue).Value -} \ No newline at end of file +#################################################################################################### + +function Get-EnvVariable{ + param( + [Parameter(Mandatory)][string]$Name + ) + + if(! (Test-Path -Path "env:$Name") ){ + return $null + } + + $ret = "env:$Name" + + return $ret +} + + From 5bb8d800204af94d5cffc9a3224b32e1a8674a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20=28Dibildos=29=20Gonz=C3=A1lez?= Date: Mon, 20 Oct 2025 12:47:23 +0200 Subject: [PATCH 2/3] fix(test): trim whitespace in assertions for file content comparison --- Test/public/addIncludeToWorkspace.test.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Test/public/addIncludeToWorkspace.test.ps1 b/Test/public/addIncludeToWorkspace.test.ps1 index c122a40..3b2d1a5 100644 --- a/Test/public/addIncludeToWorkspace.test.ps1 +++ b/Test/public/addIncludeToWorkspace.test.ps1 @@ -102,13 +102,13 @@ function Test_AddIncludeToWorkspace_IfExists{ Assert-Count -Expected 1 -Presented $filesDest1 $contentFile1 = Get-Content -Path $filesDest1.FullName | Out-String $sourceFile1 = Get-Content -Path $include1.Path | Out-String - Assert-AreEqual -Expected $sourceFile1 -Presented $contentFile1 + Assert-AreEqual -Expected $sourceFile1.Trim() -Presented $contentFile1.Trim() $filesDest2 = Get-ChildItem -Path $destFolder2 Assert-Count -Expected 1 -Presented $filesDest2 $contentFile2 = Get-Content -Path $filesDest2.FullName | Out-String $sourceFile2 = Get-Content -Path $include2.Path | Out-String - Assert-AreEqual -Expected $sourceFile2 -Presented $contentFile2 + Assert-AreEqual -Expected $sourceFile2.Trim() -Presented $contentFile2.Trim() } function Test_AddIncludeToWorkspace_PipeParameters{ From 3c80c0aa7f77627e3b2f09937721972a1e2f7009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20=28Dibildos=29=20Gonz=C3=A1lez?= Date: Mon, 10 Nov 2025 08:38:26 +0100 Subject: [PATCH 3/3] feat(callApi): improve tracking --- helper/invokeCommand.helper.ps1 | 5 +-- include/MyWrite.ps1 | 28 +++++++++------- include/callAPI.ps1 | 57 ++++++++++++++++++++------------- 3 files changed, 54 insertions(+), 36 deletions(-) diff --git a/helper/invokeCommand.helper.ps1 b/helper/invokeCommand.helper.ps1 index 34b95ed..e71eb58 100644 --- a/helper/invokeCommand.helper.ps1 +++ b/helper/invokeCommand.helper.ps1 @@ -28,7 +28,7 @@ function Invoke-MyCommand{ [Parameter(Position=1)][hashtable]$Parameters ) - Write-MyDebug "[invoke] $Command" $Parameters + Write-MyDebug "invoke" $Command $Parameters return InvokeHelper\Invoke-MyCommand -Command $Command -Parameters $Parameters } @@ -46,4 +46,5 @@ function Reset-MyInvokeCommandAlias{ } # Reset all aliases for this module on each refresh -Reset-MyInvokeCommandAlias \ No newline at end of file +Reset-MyInvokeCommandAlias + diff --git a/include/MyWrite.ps1 b/include/MyWrite.ps1 index 4d31625..fe5460e 100644 --- a/include/MyWrite.ps1 +++ b/include/MyWrite.ps1 @@ -112,7 +112,7 @@ function Test-MyVerbose { return $trace } -function Set-ModuleNameVerbose{ +function Enable-ModuleNameVerbose{ param( [Parameter(Position = 0)][string]$section ) @@ -126,16 +126,16 @@ function Set-ModuleNameVerbose{ $moduleDebugVarName = $MODULE_NAME + "_VERBOSE" [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $flag) } -Rename-Item -path Function:Set-ModuleNameVerbose -NewName "Set-$($MODULE_NAME)Verbose" +Rename-Item -path Function:Enable-ModuleNameVerbose -NewName "Set-$($MODULE_NAME)Verbose" Export-ModuleMember -Function "Set-$($MODULE_NAME)Verbose" -function Clear-ModuleNameVerbose{ +function Disable-ModuleNameVerbose{ param() $moduleDebugVarName = $MODULE_NAME + "_VERBOSE" [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $null) } -Rename-Item -path Function:Clear-ModuleNameVerbose -NewName "Clear-$($MODULE_NAME)Verbose" +Rename-Item -path Function:Disable-ModuleNameVerbose -NewName "Clear-$($MODULE_NAME)Verbose" Export-ModuleMember -Function "Clear-$($MODULE_NAME)Verbose" function Test-MyDebug { @@ -143,19 +143,23 @@ function Test-MyDebug { [Parameter(Position = 0)][string]$section ) + # Get the module debug environment variable $moduleDebugVarName = $MODULE_NAME + "_DEBUG" $flag = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName) - # Enable debug + # check if debugging is enabled if ([string]::IsNullOrWhiteSpace( $flag )) { return $false } + $flag = $flag.ToLower() + $section = $section.ToLower() + $trace = ($flag -like '*all*') -or ( $section -like "*$flag*") return $trace } -function Set-ModuleNameDebug{ +function Enable-ModuleNameDebug{ param( [Parameter(Position = 0)][string]$section ) @@ -169,17 +173,17 @@ function Set-ModuleNameDebug{ $moduleDebugVarName = $MODULE_NAME + "_DEBUG" [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $flag) } -Rename-Item -path Function:Set-ModuleNameDebug -NewName "Set-$($MODULE_NAME)Debug" -Export-ModuleMember -Function "Set-$($MODULE_NAME)Debug" +Rename-Item -path Function:Enable-ModuleNameDebug -NewName "Enable-$($MODULE_NAME)Debug" +Export-ModuleMember -Function "Enable-$($MODULE_NAME)Debug" -function Clear-ModuleNameDebug{ +function Disable-ModuleNameDebug { param() $moduleDebugVarName = $MODULE_NAME + "_DEBUG" [System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $null) } -Rename-Item -path Function:Clear-ModuleNameDebug -NewName "Clear-$($MODULE_NAME)Debug" -Export-ModuleMember -Function "Clear-$($MODULE_NAME)Debug" +Rename-Item -path Function:Disable-ModuleNameDebug -NewName "Disable-$($MODULE_NAME)Debug" +Export-ModuleMember -Function "Disable-$($MODULE_NAME)Debug" function Get-ObjetString { param( @@ -201,3 +205,5 @@ function Get-ObjetString { } + + diff --git a/include/callAPI.ps1 b/include/callAPI.ps1 index fff3b14..90b1c3c 100644 --- a/include/callAPI.ps1 +++ b/include/callAPI.ps1 @@ -13,7 +13,7 @@ function Invoke-GraphQL { [Parameter()] [string]$OutFile ) - ">>>" | Write-MyVerbose + ">>>" | writedebug $ApiHost = Get-ApiHost -ApiHost:$ApiHost $token = Get-ApiToken -Token:$Token -ApiHost:$ApiHost @@ -35,21 +35,21 @@ function Invoke-GraphQL { } | ConvertTo-Json -Depth 100 # Trace request - "[[QUERY]]" | Write-MyVerbose - $Query | Write-MyVerbose + "[[QUERY]]" | writedebug + $Query | writedebug - "[[VARIABLES]]" | Write-MyVerbose - $Variables | ConvertTo-Json -Depth 100 | Write-MyVerbose + "[[VARIABLES]]" | writedebug + $Variables | ConvertTo-Json -Depth 100 | writedebug # Send the request $start = Get-Date - ">>> Invoke-RestMethod - $apiUri" | Write-MyVerbose + ">>> Invoke-RestMethod - $apiUri" | writedebug $response = Invoke-RestMethod -Uri $apiUri -Method Post -Body $body -Headers $headers -OutFile $OutFile - "<<< Invoke-RestMethod - $apiUri [ $(((Get-Date) - $start).TotalSeconds) seconds]" | Write-MyVerbose + "<<< Invoke-RestMethod - $apiUri [ $(((Get-Date) - $start).TotalSeconds) seconds]" | writedebug # Trace response - "[[RESPONSE]]" | Write-MyVerbose - $response | ConvertTo-Json -Depth 100 | Write-MyVerbose + "[[RESPONSE]]" | writedebug + $response | ConvertTo-Json -Depth 100 | writedebug if($response.errors){ throw "GraphQL query return errors - Error: $($response.errors.message)" @@ -71,7 +71,7 @@ function Invoke-RestAPI { [Parameter()] [string]$PageSize = 30 ) - ">>>" | Write-MyVerbose + ">>>" | writedebug $ApiHost = Get-ApiHost -ApiHost "$ApiHost" $token = Get-ApiToken -ApiHost "$ApiHost" @@ -95,10 +95,10 @@ function Invoke-RestAPI { # Send the request $start = Get-Date - ">>> Invoke-RestMethod - $url" | Write-MyVerbose + ">>> Invoke-RestMethod - $url" | writedebug $response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers -ResponseHeadersVariable responseHeaders - $responseHeaders | Write-MyVerbose + $responseHeaders | writedebug # Process paging if($responseHeaders.link){$paging=$true} @@ -111,7 +111,7 @@ function Invoke-RestAPI { if($nextUrl){ $nextUrl = $matches[1] - ">>> Invoke-RestMethod - $nextUrl" | Write-MyVerbose + ">>> Invoke-RestMethod - $nextUrl" | writedebug $nextResponse = Invoke-RestMethod -Uri $nextUrl -Method Get -Headers $headers -ResponseHeadersVariable responseHeaders $response += $nextResponse $paging = $null -ne $responseHeaders.link @@ -120,11 +120,11 @@ function Invoke-RestAPI { } } - "<<< Invoke-RestMethod - $url [ $(((Get-Date) - $start).TotalSeconds) seconds]" | Write-MyVerbose + "<<< Invoke-RestMethod - $url [ $(((Get-Date) - $start).TotalSeconds) seconds]" | writedebug # Trace response - "[[RESPONSE]]" | Write-MyVerbose - $response | ConvertTo-Json -Depth 100 | Write-MyVerbose + "[[RESPONSE]]" | writedebug + $response | ConvertTo-Json -Depth 100 | writedebug return $response @@ -146,17 +146,17 @@ function Get-ApiHost { ) if(![string]::IsNullOrWhiteSpace($ApiHost)){ - "ApiHost provided" | Write-MyVerbose + "ApiHost provided" | writedebug return $ApiHost } $envValue = Get-EnvVariable -Name $ENV_VAR_HOST_NAME if(![string]::IsNullOrWhiteSpace($envValue)){ - "Host from env $envValue" | Write-MyVerbose + "Host from env $envValue" | writedebug return $envValue } - "Default host $DEFAULT_GH_HOST" | Write-MyVerbose + "Default host $DEFAULT_GH_HOST" | writedebug return $DEFAULT_GH_HOST } Export-ModuleMember -Function Get-ApiHost @@ -182,7 +182,7 @@ function Get-ApiToken { $envValue = Get-EnvVariable -Name $ENV_VAR_TOKEN_NAME if(![string]::IsNullOrWhiteSpace($envValue)){ - "Token from env" | Write-MyVerbose + "Token from env" | writedebug return $envValue } @@ -190,7 +190,7 @@ function Get-ApiToken { host = $ApiHost } - "Token from GetToken for host [$ApiHost]" | Write-MyVerbose + "Token from GetToken for host [$ApiHost]" | writedebug $result = Invoke-MyCommand -Command "GetToken" -Parameters $params if(-Not $result){ @@ -207,13 +207,24 @@ function Get-EnvVariable{ [Parameter(Mandatory)][string]$Name ) - if(! (Test-Path -Path "env:$Name") ){ + if(! (Test-Path -Path "Env:$Name") ){ return $null } - $ret = "env:$Name" + $ret = "Env:$Name" return $ret } +#################################################################################################### +function writedebug{ + [CmdletBinding()] + param( + [Parameter(ValueFromPipeline)][string]$Message + ) + + process{ + Write-MyDebug $Message -Section "api" + } +}