Skip to content

Instantly share code, notes, and snippets.

@gasparnagy
Last active February 6, 2025 16:00
Show Gist options
  • Select an option

  • Save gasparnagy/b6bec2530497639ea4e3245f0affcdeb to your computer and use it in GitHub Desktop.

Select an option

Save gasparnagy/b6bec2530497639ea4e3245f0affcdeb to your computer and use it in GitHub Desktop.
Create scenario stubs for SpecSync based on WIQL query
[CmdletBinding()]
param (
$outputFile,
$pat = $env:SYSTEM_ACCESSTOKEN,
$projectUrl = $env:SYSTEM_COLLECTIONURI + $env:SYSTEM_TEAMPROJECT,
$scopePlanId,
$scopeSuiteId,
$queryFilter
)
if ($pat -eq $null -or $pat -eq '') {
Write-Error 'PAT is not specified! Use -pat option!' -ErrorAction Stop
}
if ($projectUrl -eq $null -or $projectUrl -eq '') {
Write-Error 'Project URL is not specified! Use -projectUrl option!' -ErrorAction Stop
}
if ($outputFile -eq $null -or $outputFile -eq '') {
$date = Get-Date -Format "yyyy-MM-dd-HH-mm-ss"
$outputFile = "NewScenarios-$date.feature"
}
if ($scopePlanId -eq $null -or $scopePlanId -eq '') {
Write-Error 'Scope Test Plan ID is not specified! Use -scopePlanId option!' -ErrorAction Stop
}
if ($scopeSuiteId -eq $null -or $scopeSuiteId -eq '') {
Write-Error 'Scope Test Suite ID is not specified! Use -scopeSuiteId option!' -ErrorAction Stop
}
if ($queryFilter -eq $null -or $queryFilter -eq '') {
Write-Error 'Query filter is not specified! Use -queryFilter option!' -ErrorAction Stop
}
Write-Host "Importing Test Cases from project $projectUrl to $outputFile"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))
$header = @{Authorization = "Basic $token"}
$requestBody = @{
"query" = "SELECT [System.Id] From WorkItems Where [System.WorkItemType] = 'Test Case' AND [State] <> 'Closed' AND [State] <> 'Removed' AND ($queryFilter)"
} | ConvertTo-Json
$queryResult = Invoke-RestMethod -Uri ("$projectUrl/_apis/wit/wiql?api-version=5.1") -Headers $header -Method Post -ContentType "application/json" -Body ($requestBody)
Write-Debug "Query Result = $($queryResult | ConvertTo-Json -Depth 100)"
$queryWorkItems = @($queryResult.workItems | ForEach-Object { $_.id })
Write-Debug "Query Work Items: $queryWorkItems"
$scopeResult = Invoke-RestMethod -Uri ("$projectUrl/_apis/testplan/Plans/$scopePlanId/Suites/$scopeSuiteId/TestCase?expand=false&excludeFlags=3&witFields=Id&api-version=7.1") -Headers $header -Method Get
Write-Debug "Scope Result = $($scopeResult | ConvertTo-Json -Depth 100)"
$scopeWorkItems = @($scopeResult.value | ForEach-Object { $_.workItem.id })
Write-Debug "Scope Work Items: $scopeWorkItems"
$newWorkItems = $queryWorkItems | ?{-not ($scopeWorkItems -contains $_)}
Write-Debug "New Work Items: $newWorkItems"
if ($newWorkItems.Count -eq 0) {
Write-Host "No new scenarios to import."
return
}
$date = Get-Date
Write-Output "Feature: New scenarios imported at $date" > $outputFile
Write-Output "" >> $outputFile
$newWorkItems | ForEach-Object {
Write-Output "@tc:$_" >> $outputFile
Write-Output "Scenario: TC $_" >> $outputFile
Write-Output "" >> $outputFile
}
Write-Host "Success. $($newWorkItems.Count) new scenario placeholders saved to $outputFile. Invoke"
Write-Host " dotnet specsync pull --filter '`$sourceFile ~ $outputFile'"
Write-Host "to download Test Case details."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment