Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools for PR 9102 (#31362)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
azure-sdk and scbedd authored Oct 11, 2024
1 parent 72187a1 commit acdcea7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@
],
"words": [
"AMQP",
"azuresdkdocs",
"BRCPF",
"Brcpf",
"CONTOSO",
"docfx",
"DTDL",
"ECONNRESET",
"ESDNI",
Expand All @@ -66,6 +68,7 @@
"Kubernetes",
"Localizable",
"Lucene",
"mgmt",
"MPNS",
"MSRC",
"Mibps",
Expand All @@ -78,6 +81,7 @@
"Petabit",
"Picometer",
"Plregon",
"psobject",
"Rasterize",
"Resourceid",
"Rollup",
Expand Down
48 changes: 47 additions & 1 deletion eng/common/scripts/Helpers/Package-Helpers.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function GetPackageKey($pkg) {

return $pkgKey
}

# Different language needs a different way to index the package. Build a map in convienice to lookup the package.
# E.g. <groupId>:<packageName> is the package key in java.
function GetPackageLookup($packageList) {
Expand Down Expand Up @@ -44,4 +44,50 @@ function GetDocsTocDisplayName($pkg) {
$displayName += " (deprecated)"
}
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
}
}
60 changes: 58 additions & 2 deletions eng/common/scripts/Package-Properties.ps1
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -66,6 +67,8 @@ class PackageProps
{
$this.ChangeLogPath = $null
}

$this.InitializeCIArtifacts()
}

hidden [void]Initialize(
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion eng/scripts/Language-Settings.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ function Get-javascript-PackageInfoFromRepo ($pkgPath, $serviceDirectory) {
}
$pkgProp.IsNewSdk = ($pkgProp.SdkType -eq "client") -or ($pkgProp.SdkType -eq "mgmt")
$pkgProp.ArtifactName = $jsStylePkgName
# the constructor for the package properties object attempts to initialize CI artifacts on instantiation
# of the class. however, due to the fact that we set the ArtifactName _after_ the constructor is called,
# we need to call it again here to ensure the CI artifacts are properly initialized
$pkgProp.InitializeCIArtifacts()
return $pkgProp
}
return $null
Expand Down Expand Up @@ -249,7 +253,7 @@ function SetPackageVersion ($PackageName, $Version, $ReleaseDate, $ReplaceLatest
}

# PackageName: Pass full package name e.g. @azure/abort-controller
# You can obtain full pacakge name using the 'Get-PkgProperties' function in 'eng\common\scripts\Package-Properties.Ps1'
# You can obtain full package name using the 'Get-PkgProperties' function in 'eng\common\scripts\Package-Properties.Ps1'
function GetExistingPackageVersions ($PackageName, $GroupId = $null) {
try {
$existingVersion = Invoke-RestMethod -Method GET -Uri "http://registry.npmjs.com/${PackageName}"
Expand Down

0 comments on commit acdcea7

Please sign in to comment.