From f6dba71348ab2845d4ee1b2dab534c3ba5547e52 Mon Sep 17 00:00:00 2001 From: Mike Harder Date: Tue, 9 Jan 2024 20:51:41 -0800 Subject: [PATCH] [TypeSpec Requirement] Handle specs with variable path depths (#27324) - Fixes #27319 --- eng/scripts/TypeSpec-Requirement.ps1 | 34 ++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/eng/scripts/TypeSpec-Requirement.ps1 b/eng/scripts/TypeSpec-Requirement.ps1 index 00abdce9300d..dce2b9d19d45 100644 --- a/eng/scripts/TypeSpec-Requirement.ps1 +++ b/eng/scripts/TypeSpec-Requirement.ps1 @@ -11,7 +11,7 @@ $pathsWithErrors = @() $filesToCheck = @(Get-ChangedSwaggerFiles).Where({ ($_ -notmatch "/(examples|scenarios|restler|common|common-types)/") -and - ($_ -match "specification/(?[^/]+/(data-plane|resource-manager).*?/(preview|stable)/([^/]+))/[^/]+\.json$") + ($_ -match "specification/[^/]+/(data-plane|resource-manager).*?/(preview|stable)/[^/]+/[^/]+\.json$") }) if (!$filesToCheck) { @@ -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" @@ -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/(?[^/]+/(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) { @@ -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 } @@ -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 }