From 63f64be5fb3cd021b6064e8b0f20eb7746b53017 Mon Sep 17 00:00:00 2001 From: Azure SDK Bot <53356347+azure-sdk@users.noreply.github.com> Date: Wed, 2 Feb 2022 10:54:59 -0800 Subject: [PATCH] Sync eng/common directory with azure-sdk-tools for PR 2608 (#16959) * Get ms alias from github identity. * Update Update-DocsMsMetadata.ps1 Co-authored-by: sima-zhu Co-authored-by: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> --- .../steps/update-docsms-metadata.yml | 5 +- .../scripts/Helpers/Metadata-Helpers.ps1 | 59 +++++++++++++++++++ eng/common/scripts/Update-DocsMsMetadata.ps1 | 44 +++++++++----- 3 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 eng/common/scripts/Helpers/Metadata-Helpers.ps1 diff --git a/eng/common/pipelines/templates/steps/update-docsms-metadata.yml b/eng/common/pipelines/templates/steps/update-docsms-metadata.yml index ce57f620c90b..ddda7e6caf0d 100644 --- a/eng/common/pipelines/templates/steps/update-docsms-metadata.yml +++ b/eng/common/pipelines/templates/steps/update-docsms-metadata.yml @@ -94,7 +94,10 @@ steps: -Language '${{parameters.Language}}' ` -RepoId '${{ parameters.RepoId }}' ` -DocValidationImageId '${{ parameters.DocValidationImageId }}' ` - -PackageSourceOverride '${{ parameters.PackageSourceOverride }}' + -PackageSourceOverride '${{ parameters.PackageSourceOverride }}' ` + -TenantId '$(opensource-aad-tenant-id)' ` + -ClientId '$(opensource-aad-app-id)' ` + -ClientSecret '$(opensource-aad-secret)' displayName: Apply Documentation Updates - template: /eng/common/pipelines/templates/steps/git-push-changes.yml diff --git a/eng/common/scripts/Helpers/Metadata-Helpers.ps1 b/eng/common/scripts/Helpers/Metadata-Helpers.ps1 new file mode 100644 index 000000000000..49d6f68d9582 --- /dev/null +++ b/eng/common/scripts/Helpers/Metadata-Helpers.ps1 @@ -0,0 +1,59 @@ +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 + "client_secret" = $ClientSecret + "resource" = "api://repos.opensource.microsoft.com/audience/7e04aa67" + } + Write-Host "Generating aad token..." + $resp = Invoke-RestMethod $LoginAPIBaseURI -Method 'POST' -Headers $headers -Body $body + return $resp.access_token +} + +function GetMsAliasFromGithub ($TenantId, $ClientId, $ClientSecret, $GithubUser) +{ + $OpensourceAPIBaseURI = "https://repos.opensource.microsoft.com/api/people/links/github/$GithubUser" + + $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 aad identity for github user: $GithubUser" + $resp = Invoke-RestMethod $OpensourceAPIBaseURI -Method 'GET' -Headers $Headers + } + catch { + Write-Error $_ + return $null + } + + $resp | Write-Verbose + + if ($resp.aad) { + Write-Host "Fetched aad identity $($resp.aad.alias) for github user $GithubUser." + return $resp.aad.alias + } + Write-Error "Failed to retrieve the aad identity from given github user: $GithubName" + return $null +} + +function GetPrimaryCodeOwner ($TargetDirectory) +{ + $codeOwnerArray = &"$PSScriptRoot/../get-codeowners.ps1" -TargetDirectory $TargetDirectory + if ($codeOwnerArray) { + Write-Host "Code Owners are $codeOwnerArray." + return $codeOwnerArray[0] + } + Write-Error "No code owner found in $TargetDirectory." + return $null +} \ No newline at end of file diff --git a/eng/common/scripts/Update-DocsMsMetadata.ps1 b/eng/common/scripts/Update-DocsMsMetadata.ps1 index ae2e3c36ee0b..a51a1dfb932c 100644 --- a/eng/common/scripts/Update-DocsMsMetadata.ps1 +++ b/eng/common/scripts/Update-DocsMsMetadata.ps1 @@ -32,11 +32,14 @@ GitHub repository ID of the SDK. Typically of the form: 'Azure/azure-sdk-for-js' The docker image id in format of '$containerRegistry/$imageName:$tag' e.g. azuresdkimages.azurecr.io/jsrefautocr:latest -.PARAMETER PackageSourceOverride -Optional parameter to supply a different package source (useful for daily dev -docs generation from pacakges which are not published to the default feed). This -variable is meant to be used in the domain-specific business logic in -&$ValidateDocsMsPackagesFn +.PARAMETER TenantId +The aad tenant id/object id. + +.PARAMETER ClientId +The add client id/application id. + +.PARAMETER ClientSecret +The client secret of add app. #> param( @@ -56,10 +59,20 @@ param( [string]$DocValidationImageId, [Parameter(Mandatory = $false)] - [string]$PackageSourceOverride + [string]$PackageSourceOverride, + + [Parameter(Mandatory = $false)] + [string]$TenantId, + + [Parameter(Mandatory = $false)] + [string]$ClientId, + + [Parameter(Mandatory = $false)] + [string]$ClientSecret ) . (Join-Path $PSScriptRoot common.ps1) +. (Join-Path $PSScriptRoot Helpers Metadata-Helpers.ps1) $releaseReplaceRegex = "(https://github.com/$RepoId/(?:blob|tree)/)(?:master|main)" $TITLE_REGEX = "(\#\s+(?Azure .+? (?:client|plugin|shared) library for (?:JavaScript|Java|Python|\.NET|C)))" @@ -94,15 +107,18 @@ function GetAdjustedReadmeContent($ReadmeContent, $PackageInfo, $PackageMetadata } # Get the first code owners of the package. - $author = "ramya-rao-a" - $msauthor = "ramyar" Write-Host "Retrieve the code owner from $($PackageInfo.DirectoryPath)." - $codeOwnerArray = ."$PSScriptRoot/get-codeowners.ps1" ` - -TargetDirectory $PackageInfo.DirectoryPath - if ($codeOwnerArray) { - Write-Host "Code Owners are $($codeOwnerArray -join ",")" - $author = $codeOwnerArray[0] - $msauthor = $author # This is a placeholder for now. Will change to the right ms alias. + $author = GetPrimaryCodeOwner -TargetDirectory $PackageInfo.DirectoryPath + if (!$author) { + $author = "ramya-rao-a" + $msauthor = "ramyar" + } + else { + $msauthor = GetMsAliasFromGithub -TenantId $TenantId -ClientId $ClientId -ClientSecret $ClientSecret -GithubUser $author + } + # Default value + if (!$msauthor) { + $msauthor = $author } Write-Host "The author of package: $author" Write-Host "The ms author of package: $msauthor"