Skip to content

Commit

Permalink
[TypeSpec Requirement] Handle specs with variable path depths (#27324)
Browse files Browse the repository at this point in the history
- Fixes #27319
  • Loading branch information
mikeharder authored Jan 10, 2024
1 parent ebce1c6 commit f6dba71
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions eng/scripts/TypeSpec-Requirement.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $pathsWithErrors = @()

$filesToCheck = @(Get-ChangedSwaggerFiles).Where({
($_ -notmatch "/(examples|scenarios|restler|common|common-types)/") -and
($_ -match "specification/(?<relSpecPath>[^/]+/(data-plane|resource-manager).*?/(preview|stable)/([^/]+))/[^/]+\.json$")
($_ -match "specification/[^/]+/(data-plane|resource-manager).*?/(preview|stable)/[^/]+/[^/]+\.json$")
})

if (!$filesToCheck) {
Expand All @@ -21,8 +21,12 @@ else {
# Cache responses to GitHub web requests, for efficiency and to prevent rate limiting
$responseCache = @{}

# Example: specification/foo/resource-manager/Microsoft.Foo/stable/2023-01-01/Foo.json
# - Forward slashes on both Linux and Windows
# - May be nested 4 or 5 levels deep, perhaps even deeper
# - Examples
# - specification/foo/data-plane/Foo/stable/2023-01-01/Foo.json
# - specification/foo/data-plane/Foo/bar/stable/2023-01-01/Foo.json
# - specification/foo/resource-manager/Microsoft.Foo/stable/2023-01-01/Foo.json
foreach ($file in $filesToCheck) {
LogInfo "Checking $file"

Expand All @@ -37,12 +41,22 @@ else {
LogInfo " OpenAPI was not generated from TypeSpec (missing '/info/x-typespec-generated')"
}

# Example: specification/foo/resource-manager/Microsoft.Foo
$pathToServiceName = ($file -split '/')[0..3] -join '/'
# Extract path between "specification/" and "/(preview|stable)"
if ($file -match "specification/(?<servicePath>[^/]+/(data-plane|resource-manager).*?)/(preview|stable)/[^/]+/[^/]+\.json$") {
$servicePath = $Matches["servicePath"]
}
else {
LogError " Path to OpenAPI did not match expected regex. Unable to extract service path."
LogJobFailure
exit 1
}

$urlToStableFolder = "https://github.com/Azure/azure-rest-api-specs/tree/main/$servicePath/stable"

$urlToStableFolder = "https://github.com/Azure/azure-rest-api-specs/tree/main/$pathToServiceName/stable"
# Avoid conflict with pipeline secret
$logUrlToStableFolder = $urlToStableFolder -replace '^https://',''

LogInfo " Checking $urlToStableFolder"
LogInfo " Checking $logUrlToStableFolder"

$responseStatus = $responseCache[$urlToStableFolder];
if ($null -ne $responseStatus) {
Expand All @@ -56,7 +70,7 @@ else {
$responseCache[$urlToStableFolder] = $responseStatus
}
catch {
LogError " Exception making web request to ${urlToStableFolder}: $_"
LogError " Exception making web request to ${logUrlToStableFolder}: $_"
LogJobFailure
exit 1
}
Expand All @@ -65,14 +79,14 @@ else {
LogInfo " Status: $responseStatus"

if ($responseStatus -eq 200) {
LogInfo " Branch 'main' contains path '$pathToServiceName/stable', so spec already exists and is not required to use TypeSpec"
LogInfo " Branch 'main' contains path '$servicePath/stable', so spec already exists and is not required to use TypeSpec"
}
elseif ($response.StatusCode -eq 404) {
LogInfo " Branch 'main' does not contain path '$pathToServiceName/stable', so spec is new and must use TypeSpec"
LogInfo " Branch 'main' does not contain path '$servicePath/stable', so spec is new and must use TypeSpec"
$pathsWithErrors += $file
}
else {
LogError "Unexpected response from ${urlToStableFolder}: ${response.StatusCode}"
LogError "Unexpected response from ${logUrlToStableFolder}: ${response.StatusCode}"
LogJobFailure
exit 1
}
Expand Down

0 comments on commit f6dba71

Please sign in to comment.