From 4e8be0222b0fc4a86b568c5034e625e6557640ef Mon Sep 17 00:00:00 2001 From: sima-zhu <sizhu@microsoft.com> Date: Wed, 2 Feb 2022 09:01:29 -0800 Subject: [PATCH 1/2] Get ms alias from github identity. --- .../steps/update-docsms-metadata.yml | 5 +- .../scripts/Helpers/Metadata-Helpers.ps1 | 59 +++++++++++++++++++ eng/common/scripts/Update-DocsMsMetadata.ps1 | 46 ++++++++++----- 3 files changed, 94 insertions(+), 16 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 ce57f620c..ddda7e6ca 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 000000000..49d6f68d9 --- /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 ae2e3c36e..4c73eb8b0 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 +) +Set-StrictMode -Version 3 . (Join-Path $PSScriptRoot common.ps1) +. (Join-Path $PSScriptRoot Helpers Metadata-Helpers.ps1) $releaseReplaceRegex = "(https://github.com/$RepoId/(?:blob|tree)/)(?:master|main)" $TITLE_REGEX = "(\#\s+(?<filetitle>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" From b05173ea4d305f542d5520da1a9424bb1b648e3c Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Wed, 2 Feb 2022 09:48:10 -0800 Subject: [PATCH 2/2] Update Update-DocsMsMetadata.ps1 --- eng/common/scripts/Update-DocsMsMetadata.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/Update-DocsMsMetadata.ps1 b/eng/common/scripts/Update-DocsMsMetadata.ps1 index 4c73eb8b0..a51a1dfb9 100644 --- a/eng/common/scripts/Update-DocsMsMetadata.ps1 +++ b/eng/common/scripts/Update-DocsMsMetadata.ps1 @@ -70,7 +70,7 @@ param( [Parameter(Mandatory = $false)] [string]$ClientSecret ) -Set-StrictMode -Version 3 + . (Join-Path $PSScriptRoot common.ps1) . (Join-Path $PSScriptRoot Helpers Metadata-Helpers.ps1)