Skip to content

Commit

Permalink
Get ms alias from github identity.
Browse files Browse the repository at this point in the history
  • Loading branch information
sima-zhu authored and azure-sdk committed Feb 2, 2022
1 parent 973a728 commit d44400e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 59 additions & 0 deletions eng/common/scripts/Helpers/Metadata-Helpers.ps1
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 31 additions & 15 deletions eng/common/scripts/Update-DocsMsMetadata.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)))"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit d44400e

Please sign in to comment.