Skip to content

Commit 2828444

Browse files
azure-sdkraych1
andauthored
Sync eng/common directory with azure-sdk-tools for PR 7569 (#33966)
* Correct the name of JS package folder * Uncomment the package verification * Logging more info for troubleshooting * Get sdkType and directory from the package info --------- Co-authored-by: Ray Chen <[email protected]>
1 parent 530f715 commit 2828444

File tree

1 file changed

+76
-66
lines changed

1 file changed

+76
-66
lines changed

eng/common/scripts/Verify-RestApiSpecLocation.ps1

Lines changed: 76 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -43,110 +43,112 @@ param (
4343
. (Join-Path $PSScriptRoot Helpers/PSModule-Helpers.ps1)
4444

4545
# Check if github token is set
46-
if(-not $GitHubPat) {
46+
if (-not $GitHubPat) {
4747
LogError "GitHubPat is not set. Please set the environment variable GH_TOKEN or pass the GitHubPat parameter."
4848
exit 1
4949
}
5050

51+
Write-Host "The spec used to release SDK should be from the main branch of Azure/azure-rest-api-specs repository."
52+
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName, ArtifactLocation:$ArtifactLocation, PackageInfoDirectory:$PackageInfoDirectory."
5153
Install-ModuleIfNotInstalled "powershell-yaml" "0.4.1" | Import-Module
5254

5355
# This function is used to verify the 'require' and 'input-file' settings in autorest.md point to the main branch of Azure/azure-rest-api-specs repository
5456
# input-file may be:
5557
# https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/specification/purview/data-plane/Azure.Analytics.Purview.MetadataPolicies/preview/2021-07-01-preview/purviewMetadataPolicy.json
5658
# or
5759
# https://github.com/Azure/azure-rest-api-specs/blob/0ebd4949e8e1cd9537ca5a07384c7661162cc7a6/specification/purview/data-plane/Azure.Analytics.Purview.Account/preview/2019-11-01-preview/account.json
58-
function Verify-Url([string]$fileUrl) {
59-
if($fileUrl -match "^https://(raw\.githubusercontent\.com|github\.com)/(?<repo>[^/]*/azure-rest-api-specs)(/(blob|tree))?/(?<commit>[^\/]+(\/[^\/]+)*|[0-9a-f]{40})/(?<path>specification/.*)") {
60+
function Verify-Url([string]$fileUrl, [string]$configFilePath) {
61+
if ($fileUrl -match "^https://(raw\.githubusercontent\.com|github\.com)/(?<repo>[^/]*/azure-rest-api-specs)(/(blob|tree))?/(?<commit>[^\/]+(\/[^\/]+)*|[0-9a-f]{40})/(?<path>specification/.*)") {
6062
$repo = $matches['repo']
6163
$commit = $matches['commit']
62-
if($repo -ne "Azure/azure-rest-api-specs") {
63-
LogError "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Invalid repo in the file url: $fileUrl. Repo should be 'Azure/azure-rest-api-specs'."
64+
if ($repo -ne "Azure/azure-rest-api-specs") {
65+
LogError "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Invalid repo in the file url: $fileUrl. Repo should be 'Azure/azure-rest-api-specs' in the config file:$configFilePath."
6466
exit 1
6567
}
6668
# check the commit hash belongs to main branch
67-
Verify-CommitFromMainBranch $commit
69+
Verify-CommitFromMainBranch $commit $configFilePath
6870
}
69-
else{
70-
LogError "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Invalid file url: $fileUrl"
71+
else {
72+
LogError "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Invalid file url: $fileUrl in the config file:$configFilePath. The spec location should point to the main branch of Azure/azure-rest-api-specs repository."
7173
exit 1
7274
}
7375
}
7476

7577
# This function is used to verify the 'repo' and 'commit' settings in tsp-location.yaml point to the main branch of Azure/azure-rest-api-specs repository
76-
function Verify-TspLocation([System.Object]$tspLocationObj) {
78+
function Verify-TspLocation([System.Object]$tspLocationObj, [string]$tspLocationYamlPath) {
7779
$repo = $tspLocationObj["repo"]
7880
$commit = $tspLocationObj["commit"]
79-
if($repo -ne "Azure/azure-rest-api-specs") {
80-
LogError "Invalid repo setting in the tsp-location.yaml: $repo. Repo should be 'Azure/azure-rest-api-specs'. ServiceDir:$ServiceDirectory, PackageName:$PackageName"
81+
if ($repo -ne "Azure/azure-rest-api-specs") {
82+
LogError "Invalid repo setting in the tsp-location.yaml: $repo. Repo should be 'Azure/azure-rest-api-specs'. ServiceDir:$ServiceDirectory, PackageName:$PackageName, tsp-location.yaml path:$tspLocationYamlPath."
8183
exit 1
8284
}
8385

8486
# check the commit hash belongs to main branch
85-
Verify-CommitFromMainBranch $commit
87+
Verify-CommitFromMainBranch $commit $tspLocationYamlPath
8688
}
8789

8890
# This function is used to verify the specific 'commit' belongs to the main branch of Azure/azure-rest-api-specs repository
89-
function Verify-CommitFromMainBranch([string]$commit) {
90-
if($commit -notmatch "^[0-9a-f]{40}$" -and $commit -ne "main") {
91-
LogError "Invalid commit hash or branch name: $commit. Branch name should be 'main' or the commit should be a 40-character SHA-1 hash. ServiceDir:$ServiceDirectory, PackageName:$PackageName"
91+
function Verify-CommitFromMainBranch([string]$commit, [string]$configFilePath) {
92+
if ($commit -notmatch "^[0-9a-f]{40}$" -and $commit -ne "main") {
93+
LogError "Invalid commit hash or branch name: $commit. Branch name should be 'main' or the commit should be a 40-character SHA-1 hash. ServiceDir:$ServiceDirectory, PackageName:$PackageName, please check the config file:$configFilePath."
9294
exit 1
9395
}
94-
if($commit -eq "main") {
95-
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Branch is $commit branch of Azure/azure-rest-api-specs repository."
96+
if ($commit -eq "main") {
97+
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName, Config file:$configFilePath. Branch is $commit branch of Azure/azure-rest-api-specs repository."
9698
return
9799
}
98100
try {
99101
$searchResult = Search-GitHubCommit -AuthToken $GitHubPat -CommitHash $commit -RepoOwner "Azure" -RepoName "azure-rest-api-specs"
100102
if ($searchResult.total_count -lt 1) {
101-
LogError "Commit $commit doesn't exist in 'main' branch of Azure/azure-rest-api-specs repository. ServiceDir:$ServiceDirectory, PackageName:$PackageName"
103+
LogError "Commit $commit doesn't exist in 'main' branch of Azure/azure-rest-api-specs repository. ServiceDir:$ServiceDirectory, PackageName:$PackageName, please check the config file:$configFilePath. The spec used to release SDK should be from the main branch of Azure/azure-rest-api-specs repository."
102104
exit 1
103105
}
104-
else{
106+
else {
105107
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Commit $commit exists in 'main' branch of Azure/azure-rest-api-specs repository."
106108
}
107109
}
108110
catch {
109-
LogError "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Failed to search commit $commit with exception:`n$_ "
111+
LogError "ServiceDir:$ServiceDirectory, PackageName:$PackageName, Config file:$configFilePath. The spec used to release SDK should be from the main branch of Azure/azure-rest-api-specs repository. Failed to search commit $commit with exception:`n$_ "
110112
exit 1
111113
}
112114
}
113115

114-
function Verify-YamlContent([string]$markdownContent) {
116+
function Verify-YamlContent([string]$markdownContent, [string]$configFilePath) {
115117
$splitString = '``` yaml|```yaml|```'
116118
$yamlContent = $markdownContent -split $splitString
117-
foreach($yamlSection in $yamlContent) {
119+
foreach ($yamlSection in $yamlContent) {
118120
if ($yamlSection) {
119121
try {
120122
# remove the lines like: $(tag) == 'package-preview-2023-09'
121123
$yamlSection = $yamlSection -replace '^\s*\$\(.+\)\s*==.+', ''
122124
$yamlobj = ConvertFrom-Yaml -Yaml $yamlSection
123-
if($yamlobj) {
125+
if ($yamlobj) {
124126
$batchValue = $yamlobj["batch"]
125127
$requireValue = $yamlobj["require"]
126128
$inputFileValue = $yamlobj["input-file"]
127129
if ($requireValue) {
128-
LogDebug "ServiceDir:$ServiceDirectory, PackageName:$PackageName. 'require' is set as:$requireValue"
129-
Verify-Url $requireValue
130+
LogDebug "ServiceDir:$ServiceDirectory, PackageName:$PackageName, Config file:$configFilePath. 'require' is set as:$requireValue"
131+
Verify-Url $requireValue $configFilePath
130132
}
131133
elseif ($inputFileValue) {
132-
LogDebug "ServiceDir:$ServiceDirectory, PackageName:$PackageName. 'input-file' is set as:$inputFileValue"
133-
foreach($inputFile in $inputFileValue) {
134-
Verify-Url $inputFile
134+
LogDebug "ServiceDir:$ServiceDirectory, PackageName:$PackageName, Config file:$configFilePath. 'input-file' is set as:$inputFileValue"
135+
foreach ($inputFile in $inputFileValue) {
136+
Verify-Url $inputFile $configFilePath
135137
}
136138
}
137139
elseif ($batchValue) {
138140
# there are some services which use batch mode for sdk generation, e.g. Azure.AI.Language.QuestionAnswering
139-
foreach($batch in $batchValue) {
141+
foreach ($batch in $batchValue) {
140142
$requireValue = $batch["require"]
141143
$inputFileValue = $batch["input-file"]
142144
if ($requireValue) {
143-
LogDebug "ServiceDir:$ServiceDirectory, PackageName:$PackageName. 'require' is set as:$requireValue"
144-
Verify-Url $requireValue
145+
LogDebug "ServiceDir:$ServiceDirectory, PackageName:$PackageName, Config file:$configFilePath. 'require' is set as:$requireValue"
146+
Verify-Url $requireValue $configFilePath
145147
}
146148
elseif ($inputFileValue) {
147-
LogDebug "ServiceDir:$ServiceDirectory, PackageName:$PackageName. 'input-file' is set as:$inputFileValue"
148-
foreach($inputFile in $inputFileValue) {
149-
Verify-Url $inputFile
149+
LogDebug "ServiceDir:$ServiceDirectory, PackageName:$PackageName, Config file:$configFilePath. 'input-file' is set as:$inputFileValue"
150+
foreach ($inputFile in $inputFileValue) {
151+
Verify-Url $inputFile $configFilePath
150152
}
151153
}
152154
}
@@ -163,19 +165,16 @@ function Verify-YamlContent([string]$markdownContent) {
163165
function Verify-PackageVersion() {
164166
try {
165167
$packages = @{}
166-
if ($FindArtifactForApiReviewFn -and (Test-Path "Function:$FindArtifactForApiReviewFn"))
167-
{
168+
if ($FindArtifactForApiReviewFn -and (Test-Path "Function:$FindArtifactForApiReviewFn")) {
168169
$packages = &$FindArtifactForApiReviewFn $ArtifactLocation $PackageName
169170
}
170-
else
171-
{
171+
else {
172172
LogError "The function for 'FindArtifactForApiReviewFn' was not found.`
173173
Make sure it is present in eng/scripts/Language-Settings.ps1 and referenced in eng/common/scripts/common.ps1.`
174174
See https://github.com/Azure/azure-sdk-tools/blob/main/doc/common/common_engsys.md#code-structure"
175175
exit 1
176176
}
177-
if (-not $PackageInfoDirectory)
178-
{
177+
if (-not $PackageInfoDirectory) {
179178
$PackageInfoDirectory = Join-Path -Path $ArtifactLocation "PackageInfo"
180179
if (-not (Test-Path -Path $PackageInfoDirectory)) {
181180
# Call Save-Package-Properties.ps1 script to generate package info json files
@@ -185,23 +184,19 @@ function Verify-PackageVersion() {
185184
}
186185

187186
$continueValidation = $false
188-
if ($packages)
189-
{
190-
foreach($pkgPath in $packages.Values)
191-
{
187+
if ($packages) {
188+
foreach ($pkgPath in $packages.Values) {
192189
$pkgPropPath = Join-Path -Path $PackageInfoDirectory "$PackageName.json"
193-
if (-Not (Test-Path $pkgPropPath))
194-
{
195-
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Package property file path $($pkgPropPath) is invalid."
196-
continue
190+
if (-Not (Test-Path $pkgPropPath)) {
191+
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Package property file path $($pkgPropPath) is invalid."
192+
continue
197193
}
198194
# Get package info from json file
199195
$pkgInfo = Get-Content $pkgPropPath | ConvertFrom-Json
200196
$version = [AzureEngSemanticVersion]::ParseVersionString($pkgInfo.Version)
201-
if ($null -eq $version)
202-
{
203-
LogError "ServiceDir:$ServiceDirectory, Version info is not available for package $PackageName, because version '$(pkgInfo.Version)' is invalid. Please check if the version follows Azure SDK package versioning guidelines."
204-
exit 1
197+
if ($null -eq $version) {
198+
LogError "ServiceDir:$ServiceDirectory, Version info is not available for package $PackageName, because version '$(pkgInfo.Version)' is invalid. Please check if the version follows Azure SDK package versioning guidelines."
199+
exit 1
205200
}
206201

207202
Write-Host "Version: $($version)"
@@ -216,23 +211,36 @@ function Verify-PackageVersion() {
216211
$continueValidation = $true
217212
}
218213
}
219-
if($continueValidation -eq $false) {
214+
if ($continueValidation -eq $false) {
220215
Write-Host "ServiceDir:$ServiceDirectory, no package info is found for package $PackageName, the validation of spec location is ignored."
221216
exit 0
222217
}
218+
# Return the package info
219+
return $pkgInfo
223220
}
224221
catch {
225222
LogError "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Failed to retrieve package and validate package version with exception:`n$_ "
226223
exit 1
227224
}
228225
}
229226

230-
try{
227+
try {
231228
# Verify package version is not a prerelease version, only continue the validation if the package is GA version
232-
Verify-PackageVersion
229+
$packageInfo = Verify-PackageVersion
230+
if (-not $packageInfo) {
231+
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName. The package info is not available."
232+
exit 0
233+
}
234+
# Get the package directory path
235+
$PackageDirectory = $null
236+
if ($packageInfo.DirectoryPath) {
237+
$PackageDirectory = Join-Path $RepoRoot $packageInfo.DirectoryPath
238+
}
233239

234-
$ServiceDir = Join-Path $RepoRoot 'sdk' $ServiceDirectory
235-
$PackageDirectory = Join-Path $ServiceDir $PackageName
240+
if ((-not $PackageDirectory) -or (-not (Test-Path $PackageDirectory))) {
241+
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName, PackageDirectory:$PackageDirectory. The package directory path is invalid."
242+
exit 0
243+
}
236244
Push-Location $PackageDirectory
237245

238246
# Load tsp-location.yaml if existed
@@ -243,39 +251,41 @@ try{
243251
if (Test-Path -Path $tspLocationYamlPath) {
244252
# typespec scenario
245253
$tspLocationYaml = Get-Content -Path $tspLocationYamlPath -Raw | ConvertFrom-Yaml
246-
Verify-TspLocation $tspLocationYaml
254+
Verify-TspLocation $tspLocationYaml $tspLocationYamlPath
247255
}
248256
elseif ($Language -eq "dotnet") {
249257
# only dotnet language sdk uses 'autorest.md' to configure the sdk generation
250258
if (Test-Path -Path $autorestMdPath) {
251259
try {
252260
$autorestMdContent = Get-Content -Path $autorestMdPath -Raw
253-
Verify-YamlContent $autorestMdContent
261+
Verify-YamlContent $autorestMdContent $autorestMdPath
254262
}
255263
catch {
256-
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Failed to parse autorest.md file with exception:`n$_ "
264+
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Failed to parse autorest.md file:$autorestMdPath with exception:`n$_ "
257265
}
258266
}
259267
}
260268
elseif ($Language -eq "java" -or $Language -eq "javascript" -or $Language -eq "python" -or $Language -eq "go") {
261-
# for these languages we ignore the validation because they always use the latest spec from main branch to release SDK
262-
# mgmt plane packages: azure-core-management|azure-resourcemanager|azure-resourcemanager-advisor (java), azure-mgmt-devcenter (python), arm-advisor (js), armaad (go)
263-
if($PackageName -match "^(arm|azure-mgmt|azure-resourcemanager|azure-core-management)[-a-z]*$") {
264-
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Ignore the validation for $Language management plane package."
269+
# for these languages we ignore the validation for mgmt plane SDK because they always use the latest spec from main branch to release SDK
270+
if ($packageInfo.SdkType -eq "mgmt") {
271+
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Ignore the validation for $Language management plane SDK release."
265272
exit 0
266273
}
267274
# for these languages they use 'swagger/readme.md' to configure the sdk generation for data plane scenarios
268275
if (Test-Path -Path $swaggerReadmePath) {
269276
try {
270277
$swaggerReadmeContent = Get-Content -Path $swaggerReadmePath -Raw
271-
Verify-YamlContent $swaggerReadmeContent
278+
Verify-YamlContent $swaggerReadmeContent $swaggerReadmePath
272279
}
273280
catch {
274-
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Failed to parse swagger/readme.md file with exception:`n$_ "
281+
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName. Failed to parse swagger/readme.md file:$swaggerReadmePath with exception:`n$_ "
275282
}
276283
}
277284
}
278285
}
286+
catch {
287+
Write-Host "ServiceDir:$ServiceDirectory, PackageName:$PackageName, PackageDirectory:$PackageDirectory. Failed to validate spec location with exception:`n$_ "
288+
}
279289
finally {
280290
Pop-Location
281291
}

0 commit comments

Comments
 (0)