Skip to content

Commit 23b0861

Browse files
authored
Merge pull request #217 from rulasg/Invoke-ProjectRecordUpdateWithIntegration
feat(integration): implement Invoke-ProjectRecordUpdateWithIntegration for item updates
2 parents 431fb15 + 86fd910 commit 23b0861

1 file changed

Lines changed: 47 additions & 17 deletions

File tree

public/integrations/update-ProjectItemsWithIntegration.ps1

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,51 @@ function Invoke-ProjectInjectionWithIntegration{
6060

6161
$Fields = Get-ProjectFields -Owner $owner -ProjectNumber $projectNumber
6262

63-
6463
foreach($item in $items){
6564

65+
$params = @{
66+
Owner = $owner
67+
ProjectNumber = $projectNumber
68+
Item = $item
69+
Fields = $Fields
70+
IntegrationField = $IntegrationField
71+
IntegrationCommand = $IntegrationCommand
72+
Slug = $Slug
73+
}
74+
Invoke-ProjectRecordUpdateWithIntegration @params
75+
76+
# Check for commit every $CommitMaxItems updates to avoid having too many staged items in memory. If $CommitMaxItems is -1, it will not commit until all items are processed.
77+
$stagedcount = (Get-ProjectItemStaged -Owner $Owner -ProjectNumber $ProjectNumber).Values.Count
78+
if( $stagedcount -ge $CommitMaxItems -and $CommitMaxItems -ne -1){
79+
"[Update-ProjectItemsWithIntegration] Committing staged items before continue updating. Staged count: $stagedcount" | Write-MyDebug -section "Integration"
80+
Sync-ProjectItemStagedAsync -Owner $Owner -ProjectNumber $ProjectNumber
81+
} else {
82+
"[Update-ProjectItemsWithIntegration] Count $updateCount / $CommitMaxItems" | Write-MyDebug -section "Integration"
83+
}
84+
}
85+
86+
# Sync the last staged items
87+
if($CommitMaxItems -ne -1){
88+
"[Update-ProjectItemsWithIntegration] Committing staged items at the end of the process" | Write-MyDebug -section "Integration"
89+
Sync-ProjectItemStagedAsync -Owner $Owner -ProjectNumber $ProjectNumber
90+
}
91+
} # Do not export this function to avoid conflicts with Update-ProjectItemsWithIntegration
92+
93+
function Invoke-ProjectRecordUpdateWithIntegration{
94+
[CmdletBinding()]
95+
param(
96+
[Parameter(Mandatory)][string]$Owner,
97+
[Parameter(Mandatory)][string]$ProjectNumber,
98+
[Parameter(Mandatory)][object]$Item,
99+
[Parameter(Mandatory)][object]$Fields,
100+
[Parameter(Mandatory)][string]$IntegrationField,
101+
[Parameter(Mandatory)][string]$IntegrationCommand,
102+
[Parameter()] [string]$Slug
103+
)
104+
66105
# Skip if the item does not have the integration field
67106
if(-not $item.$IntegrationField){
68-
"[Update-ProjectItemsWithIntegration] $($item.id) does not have the integration field $IntegrationField, skipping" | Write-MyDebug -section "Integration"
107+
"[Invoke-ProjectRecordUpdateWithIntegration] $($item.id) does not have the integration field $IntegrationField, skipping" | Write-MyDebug -section "Integration"
69108
continue
70109
}
71110

@@ -76,15 +115,15 @@ function Invoke-ProjectInjectionWithIntegration{
76115
$values = Invoke-MyCommand -Command $command
77116
}
78117
catch {
79-
"Something went wrong with the integration command for $($item.id)" | Write-Error
118+
"[Invoke-ProjectRecordUpdateWithIntegration] Something went wrong with the integration command for $($item.id)" | Write-Error
80119
}
81120
# Call the ingetration Command with the integration field value as parameter
82121

83-
Write-MyDebug "[Update-ProjectItemsWithIntegration] Values" -Section "Integration" -Object $values
122+
Write-MyDebug "[Invoke-ProjectRecordUpdateWithIntegration] Values" -Section "Integration" -Object $values
84123

85124
# Check if Values is empty or null
86125
if($null -eq $values -or $values.Count -eq 0){
87-
"No values returned from the integration command for $($item.id)" | Write-MyVerbose
126+
"[Invoke-ProjectRecordUpdateWithIntegration] No values returned from the integration command for $($item.id)" | Write-Mydebug -section "Integration"
88127
continue
89128
}
90129

@@ -98,17 +137,8 @@ function Invoke-ProjectInjectionWithIntegration{
98137
Fields = $Fields
99138
}
100139

101-
Write-MyDebug "[Update-ProjectItemsWithIntegration] >> Editing with values" -Section "Integration"
140+
Write-MyDebug "[Invoke-ProjectRecordUpdateWithIntegration] >> Edit-ProjectItemWithValues [$($fields.Count)]" -Section "Integration"
102141
Edit-ProjectItemWithValues @param
103-
Write-MyDebug "[Update-ProjectItemsWithIntegration] << Editing with values" -Section "Integration"
142+
Write-MyDebug "[Invoke-ProjectRecordUpdateWithIntegration] << Edit-ProjectItemWithValues [$($fields.Count)]" -Section "Integration"
104143

105-
# Commit
106-
$updateCount++
107-
if(($CommitMaxItems -ne -1) -and $updateCount -ge $CommitMaxItems){
108-
Sync-ProjectItemStagedAsync -Owner $Owner -ProjectNumber $ProjectNumber
109-
$updateCount = 0
110-
} else {
111-
"[Update-ProjectItemsWithIntegration] Count $updateCount / $CommitMaxItems" | Write-MyDebug -section "Integration"
112-
}
113-
}
114-
} # Do not export this function to avoid conflicts with Update-ProjectItemsWithIntegration
144+
}

0 commit comments

Comments
 (0)