@@ -283,6 +283,122 @@ function copy_file_from_prev_state([string] $filePath, [string] $restorePath) {
283283 Copy-Item - Recurse - Verbose " $previousStateFile " " $restorePath "
284284}
285285
286+ function replicate () {
287+
288+ param (
289+ [string ]$resourceFrom ,
290+ [string ]$resourceTo ,
291+ [switch ]$files_only ,
292+ [switch ]$metadata_only ,
293+ [switch ]$webhook_data_only
294+ )
295+ write-output " here we go"
296+ if ($PSBoundParameters.Count -lt 2 ) {
297+ throw " Usage: shipctl replicate FROM_resource TO_resource [-files_only|-metadata_only|-webhook_data_only]"
298+ }
299+ $toCurrentJob = $false
300+ $typeFrom = $ (get_resource_type " $resourceFrom " )
301+ $typeTo = $ (get_resource_type " $resourceTo " )
302+
303+ if ($resourceFrom -eq $env: JOB_NAME ) {
304+ throw " Error: current job cannot be the FROM argument"
305+ }
306+ if ($typeTo -eq " ciRepo" ) {
307+ throw " Error: cannot replicate to ciRepo"
308+ }
309+ if (($typeFrom -match " ^gitRepo|ciRepo|syncRepo$" ) -and ($typeTo -match " ^gitRepo|syncRepo$" )) {
310+ $metadata_only = $true
311+ } elseif (" $resourceTo " -eq " $env: JOB_NAME " ) {
312+ $toCurrentJob = $true
313+ } elseif (" $typeFrom " -ne " $typeTo " ) {
314+ throw " Error: resources must be the same type"
315+ }
316+
317+ # copy files
318+ if (! $metadata_only ) {
319+ $pathFrom = " $env: JOB_PATH /IN/$resourceFrom /$typeFrom "
320+ $pathTo = " "
321+ if (! $toCurrentJob ) {
322+ $pathTo = " $env: JOB_PATH /OUT/$resourceTo /$typeTo "
323+ } else {
324+ $pathTo = " $env: JOB_STATE "
325+ }
326+ $resultTo = test-path " $pathTo "
327+ $resultFrom = test-path " $pathFrom "
328+ if ((Test-Path " $pathFrom " ) -and (Test-Path " $pathTo " )) {
329+ $directory = Get-ChildItem $pathFrom | Measure-Object
330+ if ($directory.count -gt 0 ) {
331+ Copy-Item - Path $pathFrom /* - Destination $pathTo - recurse - Force
332+ }
333+ Get-ChildItem $pathTo
334+ }
335+ }
336+ # copy metadata
337+ if (! $files_only ) {
338+ $metadataFromContents = Get-Content - Raw - Path " $env: JOB_PATH /IN/$resourceFrom /version.json" | ConvertFrom-Json
339+ $mdFilePathTo = " "
340+ if ($toCurrentJob ) {
341+ $mdFilePathTo = " $env: JOB_STATE /outputVersion.json"
342+ } else {
343+ $mdFilePathTo = " $env: JOB_PATH /OUT/$resourceTo /version.json"
344+ }
345+ if (! $webhook_data_only ) {
346+ $fromVersion = $metadataFromContents.version.propertyBag # read json at mdFilePathFrom.version.propertyBag
347+
348+ if ($toCurrentJob ) {
349+ $outputVersion = @ {
350+ " propertyBag" = $fromVersion
351+ }
352+ (ConvertTo-Json $outputVersion - depth 100 ) | Out-File - filepath " $mdFilePathTo " - Encoding ASCII
353+
354+ } else {
355+ $toVersionJson = Get-Content - Raw - Path $mdFilePathTo | ConvertFrom-Json
356+ $toVersionJson.version.propertyBag = $fromVersion
357+ (ConvertTo-Json $toVersionJson - depth 100 ) | Out-File - filepath " $mdFilePathTo " - Encoding ASCII
358+ }
359+ } else {
360+ $fromShaData = $metadataFromContents.version.propertyBag.shaData # read .version.propertyBag.shaData
361+ $fromWebhookRequestHeaders = $metadataFromContents.version.propertyBag.webhookRequestHeaders # read .version.propertyBag.webhookRequestHeaders
362+ $fromWebhookRequestBody = $metadataFromContents.version.propertyBag.webhookRequestBody # read .version.propertyBag.webhookRequestBody
363+ if ($toCurrentJob ) {
364+ $outputVersion = @ {
365+ " propertyBag" = @ {}
366+ }
367+ # TODO: can't set these properties if they don't already exist in the object. investigate.
368+ if ($fromShaData ) {
369+ $outputVersion.propertyBag.shaData = $fromShaData
370+ }
371+ if ($fromWebhookRequestHeaders ) {
372+ $outputVersion.propertyBag.webhookRequestHeaders = $fromWebhookRequestHeaders
373+ }
374+ if ($fromWebhookRequestBody ) {
375+ $outputVersion.propertyBag.webhookRequestBody = $fromWebhookRequestBody
376+ }
377+ (ConvertTo-Json $outputVersion - depth 100 ) | Out-File - filepath " $mdFilePathTo " - Encoding ASCII
378+ } else {
379+ $toVersionJson = Get-Content - Raw - Path $mdFilePathTo | ConvertFrom-Json
380+ # TODO: add-member doesn't seem to be working as i thought it would.
381+ if ($fromShaData ) {
382+ # if (!$toVersionJson.version.propertyBag.shaData) {
383+ Add-Member - InputObject $toVersionJson.propertyBag - NotePropertyName shaData - NotePropertyValue $fromShaData
384+ # }
385+ # $toVersionJson.version.propertyBag.shaData = $fromShaData
386+ }
387+ if ($fromWebhookRequestHeaders ) {
388+ Add-Member - InputObject $toVersionJson.propertyBag - NotePropertyName webhookRequestHeaders - NotePropertyValue $fromWebhookRequestHeaders
389+ # $toVersionJson.version.propertyBag.webhookRequestHeaders = $fromWebhookRequestHeaders
390+ }
391+ if ($fromWebhookRequestBody ) {
392+ Add-Member - InputObject $toVersionJson.propertyBag - NotePropertyName webhookRequestBody - NotePropertyValue $fromWebhookRequestBody
393+ # $toVersionJson.version.propertyBag.webhookRequestBody = $fromWebhookRequestBody
394+ }
395+ $toVersionJson.propertyBag = $toVersionJson.propertyBag
396+ (ConvertTo-Json $toVersionJson - depth 100 ) | Out-File - filepath " $mdFilePathTo " - Encoding ASCII
397+ }
398+ }
399+ }
400+ }
401+
286402function refresh_file_to_state ([string ] $newStateFile ) {
287403 if (-not $newStateFile ) {
288404 throw " Usage: shipctl refresh_file_to_state FILE"
0 commit comments