Skip to content

Commit

Permalink
Update logic for geting autorest config readme and resource provicer …
Browse files Browse the repository at this point in the history
…id (#31756)
  • Loading branch information
chidozieononiwu authored Dec 5, 2024
1 parent 2d15f1e commit 090bf2c
Showing 1 changed file with 70 additions and 40 deletions.
110 changes: 70 additions & 40 deletions eng/scripts/Create-APIView.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,53 @@
. $PSScriptRoot/ChangedFiles-Functions.ps1
. $PSScriptRoot/../common/scripts/logging.ps1

$defaultTagRegex = "^tag:\s*(?<tag>.+)"
$tagRegex = '^```\s*yaml\s*\$\(tag\)\s*==\s*''(?<tag>.+)'''

<#
.DESCRIPTION
Get the readme.md file associated with a swagger file.
Gets configuration info (tags and configFilePath) associated with a swagger file.
.PARAMETER SwaggerFile
Path to a swagger files inside the 'specification' directory.
.OUTPUTS
the readme.md file associated with the swagger file or null if not found.
the configuration info (tags and configFilePath) or null if not found.
#>
function Get-SwaggerReadMeFile {
param (
[Parameter(Mandatory = $true)]
[string]$SwaggerFile
)
$currentPath = Resolve-Path $SwaggerFile
function Get-AutoRestConfigInfo {
param (
[Parameter(Mandatory = $true)]
[string]$SwaggerFile
)

$currentPath = Resolve-Path $SwaggerFile

while ($currentPath -ne [System.IO.Path]::GetPathRoot($currentPath)) {
$currentPath = [System.IO.Path]::GetDirectoryName($currentPath)
$currentFilePath = [System.IO.Path]::GetFileName($currentPath)
while ($currentPath -ne [System.IO.Path]::GetPathRoot($currentPath)) {
$currentPath = [System.IO.Path]::GetDirectoryName($currentPath)
$currentFilePath = [System.IO.Path]::GetFileName($currentPath)

if ($currentFilePath -eq "specification") {
break
}
if ($currentFilePath -eq "specification") {
break
}

$readmeFile = Get-ChildItem -Path $currentPath -Filter "readme.md" -File -ErrorAction SilentlyContinue
if ($readmeFile -and $readmeFile.Name -eq "readme.md") {
return $readmeFile.FullName
$readmeFile = Get-ChildItem -Path $currentPath -Filter "readme.md" -File -ErrorAction SilentlyContinue
if ($readmeFile -and $readmeFile.Name -eq "readme.md") {
$tagInfo = Get-TagInformationFromReadMeFile -ReadMeFilePath $readmeFile.FullName

if ($tagInfo.DefaultTag) {
$tagInfo | Add-Member -MemberType NoteProperty -Name "ConfigPath" -Value $readmeFile
return $tagInfo
}
}
}

return $null
return $null
}

<#
.DESCRIPTION
Use the directory structure convention to get the resource provider name.
Append the service directory name if there are multiple services in the same resource provider
.PARAMETER ReadMeFilePath
ReadMe File Path for a resource provider.
Expand All @@ -49,13 +58,32 @@ function Get-SwaggerReadMeFile {
#>
function Get-ResourceProviderFromReadMePath {
param (
[Parameter(Mandatory = $true)]
[string]$ReadMeFilePath
[Parameter(Mandatory = $true)]
[string]$ReadMeFilePath
)

$directoryPath = [System.IO.Path]::GetDirectoryName($ReadMeFilePath)
$resourceProviderDirectory = Get-ChildItem -Path $directoryPath -Directory | Select-Object -First 1
return $resourceProviderDirectory.Name
$pathName = [System.IO.Path]::GetFileName($directoryPath)

if ($pathName -eq "resource-manager" -or $pathName -eq "data-plane") {
$resourceProviderDirectory = Get-ChildItem -Path $directoryPath -Directory | Select-Object -First 1
return $resourceProviderDirectory.Name
}
else {
$currentPath = Resolve-Path $directoryPath
$serviceName = $pathName

while ($currentPath -ne [System.IO.Path]::GetPathRoot($currentPath)) {
$pathName = [System.IO.Path]::GetFileName($currentPath)

if ($pathName -eq "resource-manager" -or $pathName -eq "data-plane") {
$resourceProviderDirectory = Get-ChildItem -Path $currentPath -Directory | Select-Object -First 1
return $resourceProviderDirectory.Name + "-" + $serviceName
}
$currentPath = Resolve-Path ([System.IO.Path]::GetDirectoryName($currentPath))
}
}
return $null
}

<#
Expand Down Expand Up @@ -185,18 +213,18 @@ function New-SwaggerAPIViewTokens {
}
LogGroupEnd

# Get Related Swagger ReadMe Files
$swaggerReadMeFiles = [System.Collections.Generic.HashSet[string]]::new()
# Get Related AutoRest Configuration Information
$autoRestConfigInfo = [System.Collections.Generic.Dictionary[string, object]]::new()
$changedSwaggerFiles | ForEach-Object {
$readmeFile = Get-SwaggerReadMeFile -swaggerFile $_
if ($readmeFile) {
$swaggerReadMeFiles.Add($readmeFile) | Out-Null
$configInfo = Get-AutoRestConfigInfo -swaggerFile $_
if ($null -ne $configInfo -and -not $autoRestConfigInfo.ContainsKey($configInfo.ConfigPath)) {
$autoRestConfigInfo[$configInfo.ConfigPath] = $configInfo
}
}

LogGroupStart " Swagger APIView Tokens will be generated for the following configuration files..."
$swaggerReadMeFiles | ForEach-Object {
LogInfo " - $_"
$autoRestConfigInfo.GetEnumerator() | ForEach-Object {
LogInfo " - $($_.Key)"
}
LogGroupEnd

Expand All @@ -205,25 +233,26 @@ function New-SwaggerAPIViewTokens {
$swaggerAPIViewArtifactsDirectory = [System.IO.Path]::Combine($ArtiFactsStagingDirectory, $APIViewArtifactsDirectoryName)

# Generate Swagger APIView Tokens
foreach ($readMeFile in $swaggerReadMeFiles) {
foreach ($entry in $autoRestConfigInfo.GetEnumerator()) {
$configInfo = $entry.Value
$readMeFile = $entry.Key
git checkout $SourceCommitId
if (Test-Path -Path $readmeFile) {
$resourceProvider = Get-ResourceProviderFromReadMePath -ReadMeFilePath $readMeFile
$tokenDirectory = [System.IO.Path]::Combine($swaggerAPIViewArtifactsDirectory, $resourceProvider)
New-Item -ItemType Directory -Path $tokenDirectory | Out-Null
New-Item -ItemType Directory -Path $tokenDirectory -Force | Out-Null

# Generate New APIView Token using default tag on source branch
$sourceTagInfo = Get-TagInformationFromReadMeFile -ReadMeFilePath $readMeFile
Invoke-SwaggerAPIViewParser -Type "New" -ReadMeFilePath $readMeFile -ResourceProvider $resourceProvider -TokenDirectory $tokenDirectory `
-TempDirectory $TempDirectory -Tag $sourceTagInfo.DefaultTag | Out-Null
-TempDirectory $TempDirectory -Tag $configInfo.DefaultTag | Out-Null

# Generate BaseLine APIView Token using source commit tag on target branch or defaukt tag if source commit tag does not exist
git checkout $TargetCommitId
if (Test-Path -Path $readMeFile) {
$targetTagInfo = Get-TagInformationFromReadMeFile -ReadMeFilePath $readMeFile
$baseLineTag = $targetTagInfo.DefaultTag
if ($targetTagInfo.Tags.Contains($sourceTagInfo.DefaultTag)) {
$baseLineTag = $sourceTagInfo.DefaultTag
if ($targetTagInfo.Tags.Contains($configInfo.DefaultTag)) {
$baseLineTag = $configInfo.DefaultTag
}
Invoke-SwaggerAPIViewParser -Type "Baseline" -ReadMeFilePath $readMeFile -ResourceProvider $resourceProvider -TokenDirectory $tokenDirectory `
-TempDirectory $TempDirectory -Tag $baseLineTag | Out-Null
Expand Down Expand Up @@ -367,18 +396,19 @@ function Get-TagInformationFromReadMeFile {
$defaultTag = $null

foreach ($line in $markDownContent) {
if ($line -match "### Basic Information") {
$line = $line.Trim()
if ($line -match "###\s+Basic\s+Information") {
$checkForDefaultTag = $true
}

if ($checkForDefaulttag -and ($null -eq $defaultTag)) {
if ($line -match "^tag:\s*(?<tag>.+)") {
if ($line -match $defaultTagRegex) {
$defaultTag = $matches["tag"]
$checkForDefaultTag = $false
}
}

if ($line -match '^```\s*yaml\s*\$\(tag\)\s*==\s*''(?<tag>.+)''') {
if ($line -match $tagRegex) {
$tag = $matches["tag"]
$tags.Add($tag) | Out-Null
}
Expand All @@ -388,4 +418,4 @@ function Get-TagInformationFromReadMeFile {
Tags = $tags
DefaultTag = $defaultTag
}
}
}

0 comments on commit 090bf2c

Please sign in to comment.