Skip to content

Commit d671e45

Browse files
authored
Merge pull request #151 from rulasg/show-projectitem
refactor and fix various functions for improved logging and pipeline support
2 parents 1a9134f + 88c1c2e commit d671e45

13 files changed

Lines changed: 168 additions & 80 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Test Transcript helper functions
2+
# These functions help manage the transcript file during tests
3+
# and ensure it is cleaned up after use.
4+
5+
16
$TEST_TRANSCRIPT_FILE = "test_transcript.log"
27

38
function Start-MyTranscript {

Test/public/project_item.test.ps1

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,21 @@ function Test_ShowProjectItem_SUCCESS{
261261

262262
$item = Get-ProjectItem -Owner $owner -ProjectNumber $projectNumber -ItemId $id
263263

264-
$result = $item | Show-ProjectItem -AdditionalFields "Status"
264+
# Act 0
265+
$result0 = $item | Show-ProjectItem
265266

266-
Assert-Count -Expected 1 -Presented $result
267+
Assert-Count -Expected 1 -Presented $result0
267268

268-
Assert-AreEqual -Expected $id -Presented $result[0].id
269-
Assert-AreEqual -Expected $title -Presented $result[0].Title
270-
Assert-AreEqual -Expected $status -Presented $result[0].Status
269+
Assert-AreEqual -Expected $id -Presented $result0[0].id
270+
Assert-AreEqual -Expected $title -Presented $result0[0].Title
271+
272+
$result1 = $item | Show-ProjectItem -Attributes "id","Title","Status"
273+
274+
Assert-Count -Expected 1 -Presented $result1
275+
276+
Assert-AreEqual -Expected $id -Presented $result1[0].id
277+
Assert-AreEqual -Expected $title -Presented $result1[0].Title
278+
Assert-AreEqual -Expected $status -Presented $result1[0].Status
271279
}
272280

273281
function Test_ShowProjectItem_SUCCESS_Multiple{
@@ -279,16 +287,16 @@ function Test_ShowProjectItem_SUCCESS_Multiple{
279287
$p = Get-Mock_Project_700; $Owner = $p.owner; $ProjectNumber = $p.number
280288

281289
# Arrange - get a few items using search-projectitem
282-
$items = Search-ProjectItem -Owner $owner -ProjectNumber $projectNumber -Filter $p.searchInTitle.titleFilter
290+
$items = Search-ProjectItem -Owner $owner -ProjectNumber $projectNumber -Filter $p.searchInTitle.titleFilter -PassThru
283291
$itemsCount = $items.Count
284292
Assert-Count -Expected $p.searchInTitle.totalCount -Presented $items
285293

286-
$result = $items | Show-ProjectItem -AdditionalFields "Status"
294+
$result = $items | Show-ProjectItem -Attributes "id","url","Status"
287295

288296
Assert-Count -Expected $itemsCount -Presented $result
289297

290298
# Get properties of the first item to verify
291-
$expectedProperties = @("id", "Title", "Status")
299+
$expectedProperties = @("id","url","Status")
292300

293301
# Verify all items have the same structure
294302
for ($i = 1; $i -lt $result.Count; $i++) {

helper/invokeCommand.helper.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,5 @@ function Invoke-MyCommand{
3535
[Parameter(Position=1)][hashtable]$Parameters
3636
)
3737

38-
Write-Debug "[invoke] $Command" $Parameters
39-
4038
return InvokeHelper\Invoke-MyCommand -Command $Command -Parameters $Parameters
4139
}

include/MyWrite.ps1

Lines changed: 77 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
# Include MyWrite.ps1
2+
# Provides Write-MyError, Write-MyWarning, Write-MyVerbose, Write-MyHost, Write-MyDebug
3+
# and Test-Verbose, Test-Debug functions for consistent logging and debugging output.
4+
# Use env variables ModuleHelper_VERBOSE and ModuleHelper_DEBUG to control verbosity and debug output.
5+
# Example: $env:ModuleHelper_DEBUG="all" or $env:ModuleHelper_DEBUG="Sync-Project"
6+
7+
$ModuleRootPath = Get-ModuleRootPath -ModuleRootPath $ModuleRootPath
8+
$MODULE_NAME = (Get-ChildItem -Path $ModuleRootPath -Filter *.psd1 | Select-Object -First 1).BaseName
19

210
$ERROR_COLOR = "Red"
311
$WARNING_COLOR = "Yellow"
12+
$VERBOSE_COLOR = "DarkYellow"
413
$OUTPUT_COLOR = "DarkCyan"
514
$DEBUG_COLOR = "DarkGray"
615

@@ -24,7 +33,10 @@ function Write-MyVerbose {
2433
param(
2534
[Parameter(ValueFromPipeline)][string]$Message
2635
)
27-
Write-Verbose -Message $message
36+
37+
if (Test-Verbose) {
38+
Write-ToConsole $message -Color $VERBOSE_COLOR
39+
}
2840
}
2941

3042
function Write-MyHost {
@@ -37,33 +49,25 @@ function Write-MyHost {
3749
Write-ToConsole $message -Color $OUTPUT_COLOR -NoNewLine:$NoNewLine
3850
}
3951

40-
function Write-Debug {
52+
function Write-MyDebug {
4153
param(
4254
[Parameter(Position = 0)][string]$section,
4355
[Parameter(Position = 1, ValueFromPipeline)][string]$Message,
4456
[Parameter(Position = 2)][object]$Object
4557
)
4658

47-
$flag = $env:ProjectHelper_DEBUG
59+
process{
4860

49-
# Enable debug
50-
if ([string]::IsNullOrWhiteSpace( $flag )) {
51-
return
52-
}
61+
if (Test-Debug -section $section) {
5362

54-
$trace = ($flag -like '*all*') -or ( $section -like "*$flag*")
55-
56-
# Write-Host $message -ForegroundColor $DEBUG_COLOR
57-
if ($trace) {
58-
59-
if ($Object) {
60-
$objJson = $Object | ConvertTo-Json -Depth 10 -ErrorAction SilentlyContinue
61-
$message = $message + " - " + $objJson
63+
if ($Object) {
64+
$objString = $Object | Get-ObjetString
65+
$message = $message + " - " + $objString
66+
}
67+
$timestamp = Get-Date -Format 'HH:mm:ss.fff'
68+
"[$timestamp][D][$section] $message" | Write-ToConsole -Color $DEBUG_COLOR
6269
}
63-
64-
$message = "[DEBUG][$section] " + $message
65-
Write-ToConsole $message -Color $DEBUG_COLOR
66-
}
70+
}
6771
}
6872

6973
function Write-ToConsole {
@@ -74,4 +78,58 @@ function Write-ToConsole {
7478

7579
)
7680
Microsoft.PowerShell.Utility\Write-Host $message -ForegroundColor $Color -NoNewLine:$NoNewLine
81+
}
82+
83+
84+
function Test-Verbose {
85+
param(
86+
[Parameter(Position = 0)][string]$section
87+
)
88+
89+
$moduleDebugVarName = $MODULE_NAME + "_VERBOSE"
90+
$flag = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName)
91+
92+
# Enable debug
93+
if ([string]::IsNullOrWhiteSpace( $flag )) {
94+
return $false
95+
}
96+
97+
$trace = ($flag -like '*all*') -or ( $section -like "*$flag*")
98+
return $trace
99+
}
100+
101+
function Test-Debug {
102+
param(
103+
[Parameter(Position = 0)][string]$section
104+
)
105+
106+
$moduleDebugVarName = $MODULE_NAME + "_DEBUG"
107+
$flag = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName)
108+
109+
# Enable debug
110+
if ([string]::IsNullOrWhiteSpace( $flag )) {
111+
return $false
112+
}
113+
114+
$trace = ($flag -like '*all*') -or ( $section -like "*$flag*")
115+
return $trace
116+
}
117+
118+
function Get-ObjetString {
119+
param(
120+
[Parameter(ValueFromPipeline, Position = 0)][object]$Object
121+
)
122+
123+
process{
124+
125+
if ($null -eq $Object) {
126+
return "null"
127+
}
128+
129+
if ($Object -is [string]) {
130+
return $Object
131+
}
132+
133+
return $Object | ConvertTo-Json -Depth 10 -ErrorAction SilentlyContinue
134+
}
77135
}

private/invokeRestMethord.ps1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ function Invoke-RestMethod{
2020
$params.OutFile = $OutFile
2121
}
2222

23+
">> $Method $Uri" | Write-MyDebug -section "invokeRestMethod"
2324
$result = Microsoft.PowerShell.Utility\Invoke-RestMethod @params
25+
"<< $Method $Uri" | Write-MyDebug -section "invokeRestMethod"
26+
2427

2528
return $result
2629
}

private/projectDatabase/project_database_Async.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,17 @@ function Sync-ProjectAsync {
5757
$value = $itemStaged.$fieldId.Value
5858
$field = $itemStaged.$fieldId.Field
5959

60+
# Staged has the display value. Convert to the update value
61+
$valueToSend = ConvertTo-FieldValue -Field $field -Value $value
62+
6063
$params = @{
6164
Database = $db
6265
ItemId = $itemId
6366
FieldId = $fieldId
6467
FieldName = $field.name
6568
FieldType = $field.type
6669
FieldDataType = $field.dataType
67-
Value = $value
70+
Value = $valueToSend
6871
}
6972

7073

private/projectDatabase/project_database_Sync.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ function Sync-Project{
5050
# Staged has the display value.
5151
# Convert to the update value
5252
$value = $itemStaged.$fieldId.Value
53-
$valueToSend = ConvertTo-FieldValue -Field $itemStaged.$fieldId.Field -Value $value
5453
$field = $itemStaged.$fieldId.Field
54+
$valueToSend = ConvertTo-FieldValue -Field $field -Value $value
5555

5656
$params = @{
5757
Database = $db
@@ -69,7 +69,7 @@ function Sync-Project{
6969

7070
if ( ! (Test-UpdateProjectItemCall $call) ) {
7171
"FAILED !!" | Write-MyHost
72-
Write-Debug -section "Sync-Project" -Message "Update-ProjectItem call failed" -Object $call
72+
Write-MyDebug -section "Sync-Project" -Message "Update-ProjectItem call failed" -Object $call
7373
continue
7474
}
7575

private/projectDatabase/project_database_call.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ function Test-UpdateProjectItemCall {
367367
$ret = $null -ne $call.Result.data.$($call.ResultDataType)
368368

369369
if (! $ret) {
370-
"Update Project Item call Failed: $($call.Result.errors.message -join ', ')" | Write-Debug
370+
"Update Project Item call Failed: $($call.Result.errors.message -join ', ')" | Write-MyDebug
371371
}
372372

373373
return $ret

public/driver_gh.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ function Invoke-GitHubUpdateItemValues{
159159
[string]$Type
160160
)
161161

162+
"ProjectId: $ProjectId, ItemId: $ItemId, FieldId: $FieldId, Value: $Value, Type: $Type" | Write-MyDebug -section "driver_gh"
163+
162164
# Use the environmentraviable
163165
$token = Get-GithubToken
164166
if(-not $token){

public/environmentCache.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ function Reset-ProjectHelperEnvironment{
3333
function Set-ProjectHelperEnvironment{
3434
[CmdletBinding()]
3535
param(
36-
[Parameter()][string]$Owner,
37-
[Parameter()][string]$ProjectNumber,
38-
[Parameter()][string[]]$DisplayFields
36+
[Parameter(ValueFromPipelineByPropertyName)][string]$Owner,
37+
[Parameter(ValueFromPipelineByPropertyName)][string]$ProjectNumber,
38+
[Parameter(ValueFromPipelineByPropertyName)][string[]]$DisplayFields
3939
)
4040

4141
if(! [string]::IsNullOrWhiteSpace($Owner)) {

0 commit comments

Comments
 (0)