From 077d6c97beb5f813cbfbcd3477aac4036d327146 Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Mon, 21 Mar 2022 12:38:56 -0700 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools for PR 2847 (#3455) * Add extra group conditions, template consolidation * Module fixes. Enable provisioner identity permissions via opensource api. Co-authored-by: Ben Broderick Phillips --- .../scripts/Helpers/Metadata-Helpers.ps1 | 35 +++++++++++++++---- eng/common/scripts/Import-AzModules.ps1 | 2 +- eng/common/scripts/Update-DocsMsMetadata.ps1 | 32 ++++++++--------- 3 files changed, 46 insertions(+), 23 deletions(-) diff --git a/eng/common/scripts/Helpers/Metadata-Helpers.ps1 b/eng/common/scripts/Helpers/Metadata-Helpers.ps1 index 8891837a9..a8daf0d83 100644 --- a/eng/common/scripts/Helpers/Metadata-Helpers.ps1 +++ b/eng/common/scripts/Helpers/Metadata-Helpers.ps1 @@ -1,11 +1,11 @@ -function Generate-AadToken ($TenantId, $ClientId, $ClientSecret) +function Generate-AadToken ($TenantId, $ClientId, $ClientSecret) { $LoginAPIBaseURI = "https://login.microsoftonline.com/$TenantId/oauth2/token" $headers = @{ "content-type" = "application/x-www-form-urlencoded" } - + $body = @{ "grant_type" = "client_credentials" "client_id" = $ClientId @@ -17,8 +17,9 @@ function Generate-AadToken ($TenantId, $ClientId, $ClientSecret) return $resp.access_token } -function GetMsAliasFromGithub ($TenantId, $ClientId, $ClientSecret, $GithubUser) +function GetMsAliasFromGithub ([string]$TenantId, [string]$ClientId, [string]$ClientSecret, [string]$GithubUser) { + # API documentation (out of date): https://github.com/microsoft/opensource-management-portal/blob/main/docs/api.md $OpensourceAPIBaseURI = "https://repos.opensource.microsoft.com/api/people/links/github/$GithubUser" $Headers = @{ @@ -31,8 +32,7 @@ function GetMsAliasFromGithub ($TenantId, $ClientId, $ClientSecret, $GithubUser) $Headers["Authorization"] = "Bearer $opsAuthToken" Write-Host "Fetching aad identity for github user: $GithubUser" $resp = Invoke-RestMethod $OpensourceAPIBaseURI -Method 'GET' -Headers $Headers -MaximumRetryCount 3 - } - catch { + } catch { Write-Warning $_ return $null } @@ -47,7 +47,30 @@ function GetMsAliasFromGithub ($TenantId, $ClientId, $ClientSecret, $GithubUser) return $null } -function GetPrimaryCodeOwner ($TargetDirectory) +function GetAllGithubUsers ([string]$TenantId, [string]$ClientId, [string]$ClientSecret) +{ + # API documentation (out of date): https://github.com/microsoft/opensource-management-portal/blob/main/docs/api.md + $OpensourceAPIBaseURI = "https://repos.opensource.microsoft.com/api/people/links" + + $Headers = @{ + "Content-Type" = "application/json" + "api-version" = "2019-10-01" + } + + try { + $opsAuthToken = Generate-AadToken -TenantId $TenantId -ClientId $ClientId -ClientSecret $ClientSecret + $Headers["Authorization"] = "Bearer $opsAuthToken" + Write-Host "Fetching all github alias links" + $resp = Invoke-RestMethod $OpensourceAPIBaseURI -Method 'GET' -Headers $Headers -MaximumRetryCount 3 + } catch { + Write-Warning $_ + return $null + } + + return $resp +} + +function GetPrimaryCodeOwner ([string]$TargetDirectory) { $codeOwnerArray = &"$PSScriptRoot/../get-codeowners.ps1" -TargetDirectory $TargetDirectory if ($codeOwnerArray) { diff --git a/eng/common/scripts/Import-AzModules.ps1 b/eng/common/scripts/Import-AzModules.ps1 index 6e97ba970..4eec04b85 100644 --- a/eng/common/scripts/Import-AzModules.ps1 +++ b/eng/common/scripts/Import-AzModules.ps1 @@ -5,4 +5,4 @@ param ( . (Join-Path $PSScriptRoot Helpers PSModule-Helpers.ps1) -Install-ModuleIfNotInstalled "Az" $AzModuleVersion | Import-Module \ No newline at end of file +Install-ModuleIfNotInstalled "Az" $AzModuleVersion | Import-Module diff --git a/eng/common/scripts/Update-DocsMsMetadata.ps1 b/eng/common/scripts/Update-DocsMsMetadata.ps1 index 93893098c..540d4da4d 100644 --- a/eng/common/scripts/Update-DocsMsMetadata.ps1 +++ b/eng/common/scripts/Update-DocsMsMetadata.ps1 @@ -4,10 +4,10 @@ Updates package README.md for publishing to docs.microsoft.com .DESCRIPTION Given a PackageInfo .json file, format the package README.md file with metadata -and other information needed to release reference docs: +and other information needed to release reference docs: * Adjust README.md content to include metadata -* Insert the package verison number in the README.md title +* Insert the package verison number in the README.md title * Copy file to the appropriate location in the documentation repository * Copy PackageInfo .json file to the metadata location in the reference docs repository. This enables the Docs CI build to onboard packages which have not @@ -18,7 +18,7 @@ List of locations of the artifact information .json file. This is usually stored in build artifacts under packages/PackageInfo/.json. Can also be a single item. -.PARAMETER DocRepoLocation +.PARAMETER DocRepoLocation Location of the root of the docs.microsoft.com reference doc location. Further path information is provided by $GetDocsMsMetadataForPackageFn @@ -47,7 +47,7 @@ param( [array]$PackageInfoJsonLocations, [Parameter(Mandatory = $true)] - [string]$DocRepoLocation, + [string]$DocRepoLocation, [Parameter(Mandatory = $true)] [string]$Language, @@ -104,12 +104,12 @@ function GetAdjustedReadmeContent($ReadmeContent, $PackageInfo, $PackageMetadata $replacementPattern = "`${1}$tag" $ReadmeContent = $ReadmeContent -replace $releaseReplaceRegex, $replacementPattern } - + # Get the first code owners of the package. Write-Host "Retrieve the code owner from $($PackageInfo.DirectoryPath)." - $author = GetPrimaryCodeOwner -TargetDirectory $PackageInfo.DirectoryPath + $author = GetPrimaryCodeOwner -TargetDirectory $PackageInfo.DirectoryPath if (!$author) { - $author = "ramya-rao-a" + $author = "ramya-rao-a" $msauthor = "ramyar" } else { @@ -148,10 +148,10 @@ function GetPackageInfoJson ($packageInfoJsonLocation) { $packageInfoJson = Get-Content $packageInfoJsonLocation -Raw $packageInfo = ConvertFrom-Json $packageInfoJson if ($packageInfo.DevVersion) { - # If the package is of a dev version there may be language-specific needs to - # specify the appropriate version. For example, in the case of JS, the dev + # If the package is of a dev version there may be language-specific needs to + # specify the appropriate version. For example, in the case of JS, the dev # version is always 'dev' when interacting with NPM. - if ($GetDocsMsDevLanguageSpecificPackageInfoFn -and (Test-Path "Function:$GetDocsMsDevLanguageSpecificPackageInfoFn")) { + if ($GetDocsMsDevLanguageSpecificPackageInfoFn -and (Test-Path "Function:$GetDocsMsDevLanguageSpecificPackageInfoFn")) { $packageInfo = &$GetDocsMsDevLanguageSpecificPackageInfoFn $packageInfo } else { # Default: use the dev version from package info as the version for @@ -162,16 +162,16 @@ function GetPackageInfoJson ($packageInfoJsonLocation) { return $packageInfo } -function UpdateDocsMsMetadataForPackage($packageInfoJsonLocation, $packageInfo) { +function UpdateDocsMsMetadataForPackage($packageInfoJsonLocation, $packageInfo) { $originalVersion = [AzureEngSemanticVersion]::ParseVersionString($packageInfo.Version) $packageMetadataArray = (Get-CSVMetadata).Where({ $_.Package -eq $packageInfo.Name -and $_.Hide -ne 'true' -and $_.New -eq 'true' }) if ($packageInfo.Group) { $packageMetadataArray = ($packageMetadataArray).Where({$_.GroupId -eq $packageInfo.Group}) } - if ($packageMetadataArray.Count -eq 0) { + if ($packageMetadataArray.Count -eq 0) { LogWarning "Could not retrieve metadata for $($packageInfo.Name) from metadata CSV. Using best effort defaults." $packageMetadata = $null - } elseif ($packageMetadataArray.Count -gt 1) { + } elseif ($packageMetadataArray.Count -gt 1) { LogWarning "Multiple metadata entries for $($packageInfo.Name) in metadata CSV. Using first entry." $packageMetadata = $packageMetadataArray[0] } else { @@ -199,10 +199,10 @@ function UpdateDocsMsMetadataForPackage($packageInfoJsonLocation, $packageInfo) Write-Warning "$($packageInfo.Name) does not have Readme file. Skipping update readme." return } - + $readmeContent = Get-Content $packageInfo.ReadMePath -Raw - $outputReadmeContent = "" - if ($readmeContent) { + $outputReadmeContent = "" + if ($readmeContent) { $outputReadmeContent = GetAdjustedReadmeContent $readmeContent $packageInfo $packageMetadata }