|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +#Requires -Version 7.2 |
| 3 | + |
| 4 | +<# |
| 5 | +.SYNOPSIS |
| 6 | +Trigger the workflow that records the tests expected outputs, wait for its execution to finish, |
| 7 | +then bring back those results locally. |
| 8 | +
|
| 9 | +.DESCRIPTION |
| 10 | +This script is meant to run locally outside of Actions. It relies on `gh` and `git`. |
| 11 | +#> |
| 12 | + |
| 13 | +# Get the repository's path (ps1 extension on the script is required for PSScriptRoot to be available 🤦) |
| 14 | +$repositoryPath = Resolve-Path (Join-Path $PSScriptRoot '..') |
| 15 | + |
| 16 | +# Get the test_project's path |
| 17 | +$testProjectsPath = Resolve-Path (Join-Path $PSScriptRoot '../test_projects') |
| 18 | + |
| 19 | +# |
| 20 | +# Utilities |
| 21 | +# |
| 22 | + |
| 23 | +# Run a command and validate it returned a 0 exit code |
| 24 | +function Invoke-Command { |
| 25 | + param ( |
| 26 | + [ScriptBlock] $Command |
| 27 | + ) |
| 28 | + |
| 29 | + & $Command |
| 30 | + if ($LASTEXITCODE -ne 0) { |
| 31 | + Write-Error "Command failed: $Command" |
| 32 | + throw |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +# Get the current git ref name |
| 37 | +function Get-GitRef { |
| 38 | + $commitId = Invoke-Command { & git -C $repositoryPath rev-parse --abbrev-ref HEAD } |
| 39 | + $commitId.Trim() |
| 40 | +} |
| 41 | + |
| 42 | +# Create a temp folder and return its path |
| 43 | +function New-TemporaryFolder { |
| 44 | + $path = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid()) |
| 45 | + New-Item -ItemType 'Directory' -Path $path | Out-Null |
| 46 | + $path |
| 47 | +} |
| 48 | + |
| 49 | +# |
| 50 | +# Main |
| 51 | +# |
| 52 | + |
| 53 | +# Init |
| 54 | +Set-StrictMode -version 'Latest' |
| 55 | +$ErrorActionPreference = 'Stop' |
| 56 | + |
| 57 | +# Get git ref name |
| 58 | +$ref = Get-GitRef |
| 59 | + |
| 60 | +# # Run the workflow |
| 61 | +# Write-Host 'Queue workflow' |
| 62 | +# $workflow = 'record.yml' |
| 63 | +# Invoke-Command { & gh workflow run $workflow --ref $ref | Out-Null } |
| 64 | + |
| 65 | +# # Wait for a few seconds for the workflow to get created |
| 66 | +# Write-Host 'Wait a few seconds...' |
| 67 | +# Start-Sleep -Seconds 5 |
| 68 | + |
| 69 | +# # Lookup the run id (it is not perfect because of the APIs...) |
| 70 | +# Write-Host 'Lookup run id' |
| 71 | +# $runId = Invoke-Command { & gh run list --workflow $workflow --branch $ref --limit 100 --json databaseId --jq '.[].databaseId' } |
| 72 | + |
| 73 | +# # Wait for the workflow to finish |
| 74 | +# Write-Host "Wait for workflow $runId to complete" |
| 75 | +# Invoke-Command { & gh run watch $runId --exit-status } |
| 76 | + |
| 77 | +# # Download the artifacts in a temp folder |
| 78 | +# Write-Host 'Download artifacts' |
| 79 | +# $tempFolder = New-TemporaryFolder |
| 80 | +# Invoke-Command { & gh run download $runId --dir $tempFolder } |
| 81 | + |
| 82 | + |
| 83 | +# TEMP |
| 84 | +$tempFolder = '/var/folders/0p/xh302z2x64b64_4l9xc5n7bm0000gn/T/959ec25f-986e-48b1-b6a8-155cd7ba62fe' |
| 85 | + |
| 86 | +Write-Host $tempFolder |
| 87 | +$runId = 2748946582 |
| 88 | + |
| 89 | +# Iterate over the test projects |
| 90 | +Get-ChildItem -Path $testProjectsPath -Directory | ForEach-Object { |
| 91 | + # Construct the artifact path and make sure a matching artifact is found |
| 92 | + $artifactPath = Join-Path $tempFolder $_.BaseName |
| 93 | + if (Test-Path $artifactPath -PathType 'Container') { |
| 94 | + # Copy artifact to the expected output folder |
| 95 | + $destinationPath = Join-Path $testProjectsPath $_.BaseName '_expected' |
| 96 | + Copy-Item -Path $artifactPath -Destination $destinationPath -Recurse | Out-Null |
| 97 | + } |
| 98 | + |
| 99 | + # Ignore test project |
| 100 | + else { |
| 101 | + Write-Warning "Unable to find artifact for test project $($_.BaseName)" |
| 102 | + } |
| 103 | +} |
0 commit comments