run integration.ps1

[CmdletBinding()]
param(
    [Parameter(Mandatory = $true)][string]$ApiKey,
    [Parameter(Mandatory = $true)][string]$BaseUrl,
    [string]$TimelineId,
    [string]$TimelineName,
    [string]$ProjectId,
    [string]$DataItemAssetId,
    [string]$DataItemName,
    [string]$Role,
    [int]$UserCount = 1
)

$BaseUrl = $BaseUrl.TrimEnd('/')
$reportsDir = Join-Path (Get-Location).Path "reports"
if (-not (Test-Path $reportsDir)) {
    New-Item -ItemType Directory -Path $reportsDir -Force | Out-Null
}

function Test-PlaceholderValue {
    param([string]$Value)
    return [string]::IsNullOrWhiteSpace($Value) -or $Value -match '^\\$\\('
}

function Write-PrettyJson {
    param([Parameter(Mandatory = $true)]$InputObject)
    Write-Host ($InputObject | ConvertTo-Json -Depth 10)
}

if (Test-PlaceholderValue -Value $TimelineId) { $TimelineId = $null }
if (Test-PlaceholderValue -Value $TimelineName) { $TimelineName = $null }
if (Test-PlaceholderValue -Value $ProjectId) { $ProjectId = $null }
if (Test-PlaceholderValue -Value $DataItemAssetId) { $DataItemAssetId = $null }
if (Test-PlaceholderValue -Value $DataItemName) { $DataItemName = $null }
if (Test-PlaceholderValue -Value $Role) { $Role = $null }

if (-not $TimelineId -and -not $TimelineName) {
    throw "Provide either -TimelineId or -TimelineName."
}

if ($TimelineName -and -not $TimelineId -and -not $ProjectId) {
    throw "ProjectId is required when TimelineName is used without TimelineId."
}

$headers = @{ 
    Authorization = "X-API-KEY $ApiKey"
    "Content-Type" = "application/json"
}

$body = @{ userCount = $UserCount }

if ($TimelineId) { $body.timelineAssetId = $TimelineId }
if ($TimelineName -and -not $TimelineId) { $body.timelineName = $TimelineName }
if ($ProjectId) { $body.projectId = $ProjectId }
if ($DataItemAssetId) { $body.dataItemAssetId = $DataItemAssetId }
if ($DataItemName -and -not $DataItemAssetId) { $body.dataItemName = $DataItemName }
if ($Role) { $body.role = $Role }

$requestBody = $body | ConvertTo-Json -Depth 10

Write-Host "Submitting integration run request..."
Write-PrettyJson -InputObject $body

$response = Invoke-RestMethod `
    -Uri "$BaseUrl/api/integration/runIntegration" `
    -Method Post `
    -Headers $headers `
    -Body $requestBody

Write-Host "Integration run started."
Write-PrettyJson -InputObject $response

$runId = $response.runId
$pollResultUrl = $response.pollResultUrl
$trackItems = @($response.trackItems)

if (-not $runId) {
    throw "runId was not returned by the API."
}

if (-not $pollResultUrl) {
    throw "pollResultUrl was not returned by the API."
}

$trackItemIds = @($trackItems | ForEach-Object { $_.trackItemId } | Where-Object { $_ }) -join ","
$trackItemNames = @($trackItems | ForEach-Object { $_.sequenceName } | Where-Object { $_ }) -join "|"

$plannedRunTime = 0
if ($response.plannedRunTime) {
    [void][int]::TryParse("$($response.plannedRunTime)", [ref]$plannedRunTime)
}

$expectedRunTime = if ($plannedRunTime -gt 0) { $plannedRunTime + 300 } else { 3600 }

@{ 
    RunId = $runId
    PollResultUrl = $pollResultUrl
    TrackItemIds = $trackItemIds
    TrackItemNames = $trackItemNames
    ExpectedRunTime = $expectedRunTime
} | ConvertTo-Json -Depth 10 | Set-Content -Path "run-output.json" -Encoding utf8

Write-Host "##vso[task.setvariable variable=RunId]$runId"
Write-Host "##vso[task.setvariable variable=PollResultUrl]$pollResultUrl"
Write-Host "##vso[task.setvariable variable=TrackItemIds]$trackItemIds"
Write-Host "##vso[task.setvariable variable=TrackItemNames]$trackItemNames"
Write-Host "##vso[task.setvariable variable=ExpectedRunTime]$expectedRunTime"