Sync eng/common directory with azure-sdk-tools for PR 9102 (#1703)

* Add CompatibleConvertFrom-Yaml to common Package-Helpers
* Add CI artifact parsing to Save-Package-Properties, now each individual packageinfo.json file contains the relevant ci artifact lines for the package. This short circuits needing to re-parse this information in common checks.

---------

Co-authored-by: Scott Beddall <scbedd@microsoft.com>
This commit is contained in:
Azure SDK Bot 2024-10-11 13:00:09 -07:00 коммит произвёл GitHub
Родитель c09e84873e
Коммит eadde34e89
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 105 добавлений и 3 удалений

Просмотреть файл

@ -45,3 +45,49 @@ function GetDocsTocDisplayName($pkg) {
}
return $displayName
}
<#
.SYNOPSIS
This function is a safe wrapper around `yq` and `ConvertFrom-Yaml` to convert YAML content to a PowerShell HashTable object
.DESCRIPTION
This function wraps `yq` and `ConvertFrom-Yaml` to convert YAML content to a PowerShell HashTable object. The reason this function exists is
because while on a local user's machine, installing a module from the powershell gallery is an easy task, in pipelines we often have failures
to install modules from the gallery. This function will attempt to use the `yq` command if it is available on the machine, and only will install
the yaml module if `yq` is not available. This means that for the majority of runs on CI machines, the yaml module will not be installed.
.PARAMETER Content
The content to convert from YAML to a PowerShell HashTable object. Accepted as named argument or from the pipeline.
.EXAMPLE
CompatibleConvertFrom-Yaml -Content (Get-Content -Raw path/to/file.yml)
.EXAMPLE
Get-Content -Raw path/to/file.yml | CompatibleConvertFrom-Yaml
#>
function CompatibleConvertFrom-Yaml {
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string]$Content
)
if (!($Content)) {
throw "Content to parse is a required input."
}
# Initialize any variables or checks that need to be done once
$yqPresent = Get-Command 'yq' -ErrorAction SilentlyContinue
if (-not $yqPresent) {
. (Join-Path $PSScriptRoot PSModule-Helpers.ps1)
Install-ModuleIfNotInstalled -WhatIf:$false "powershell-yaml" "0.4.1" | Import-Module
}
# Process the content (for example, you could convert from YAML here)
if ($yqPresent) {
return ($content | yq -o=json | ConvertFrom-Json -AsHashTable)
}
else {
return ConvertFrom-Yaml $content
}
}

Просмотреть файл

@ -1,6 +1,6 @@
# Helper functions for retireving useful information from azure-sdk-for-* repo
# Helper functions for retrieving useful information from azure-sdk-for-* repo
. "${PSScriptRoot}\logging.ps1"
. "${PSScriptRoot}\Helpers\Package-Helpers.ps1"
class PackageProps
{
[string]$Name
@ -19,6 +19,7 @@ class PackageProps
[boolean]$IncludedForValidation
# does this package include other packages that we should trigger validation for?
[string[]]$AdditionalValidationPackages
[HashTable]$ArtifactDetails
PackageProps([string]$name, [string]$version, [string]$directoryPath, [string]$serviceDirectory)
{
@ -66,6 +67,8 @@ class PackageProps
{
$this.ChangeLogPath = $null
}
$this.InitializeCIArtifacts()
}
hidden [void]Initialize(
@ -79,6 +82,59 @@ class PackageProps
$this.Initialize($name, $version, $directoryPath, $serviceDirectory)
$this.Group = $group
}
hidden [object]GetValueSafely($hashtable, [string[]]$keys) {
$current = $hashtable
foreach ($key in $keys) {
if ($current.ContainsKey($key) -or $current[$key]) {
$current = $current[$key]
}
else {
return $null
}
}
return $current
}
hidden [HashTable]ParseYmlForArtifact([string]$ymlPath) {
if (Test-Path -Path $ymlPath) {
try {
$content = Get-Content -Raw -Path $ymlPath | CompatibleConvertFrom-Yaml
if ($content) {
$artifacts = $this.GetValueSafely($content, @("extends", "parameters", "Artifacts"))
$artifactForCurrentPackage = $artifacts | Where-Object { $_["name"] -eq $this.ArtifactName -or $_["name"] -eq $this.Name }
if ($artifactForCurrentPackage) {
return [HashTable]$artifactForCurrentPackage
}
}
}
catch {
Write-Host "Exception while parsing yml file $($ymlPath): $_"
}
}
return $null
}
[void]InitializeCIArtifacts(){
$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot ".." ".." "..")
$ciFolderPath = Join-Path -Path $RepoRoot -ChildPath (Join-Path "sdk" $this.ServiceDirectory)
$ciFiles = Get-ChildItem -Path $ciFolderPath -Filter "ci*.yml" -File
if (-not $this.ArtifactDetails) {
foreach($ciFile in $ciFiles) {
$ciArtifactResult = $this.ParseYmlForArtifact($ciFile.FullName)
if ($ciArtifactResult) {
$this.ArtifactDetails = [Hashtable]$ciArtifactResult
break
}
}
}
}
}
# Takes package name and service Name