From 5c79d8fe25e5a3708b719d1b5a9ee6a96d4afc98 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Wed, 19 Jan 2022 14:02:55 -0800 Subject: [PATCH 1/3] Get ms alias from github identity --- eng/common/scripts/Get-MsAlias-FromGithub.ps1 | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 eng/common/scripts/Get-MsAlias-FromGithub.ps1 diff --git a/eng/common/scripts/Get-MsAlias-FromGithub.ps1 b/eng/common/scripts/Get-MsAlias-FromGithub.ps1 new file mode 100644 index 00000000000..5e10d7ea629 --- /dev/null +++ b/eng/common/scripts/Get-MsAlias-FromGithub.ps1 @@ -0,0 +1,63 @@ +<# +.DESCRIPTION +Get the corresponding ms alias from github identity +.PARAMETER AadToken +The aad access token. +.PARAMETER GithubName +Github identity. E.g sima-zhu +.PARAMETER ContentType +Content type of http requests. +.PARAMETER AdditionalHeaders +Additional parameters for http request headers in key-value pair format, e.g. @{ key1 = val1; key2 = val2; key3 = val3} +#> +[CmdletBinding(SupportsShouldProcess = $true)] +param( + [Parameter(Mandatory = $true)] + [string]$AadToken, + + [Parameter(Mandatory = $true)] + [string]$GithubName, + + [Parameter(Mandatory = $false)] + [string]$ApiVersion = "2019-10-01", + + [Parameter(Mandatory = $false)] + [string]$ContentType = "application/json", + + [Parameter(Mandatory = $false)] + [hashtable]$AdditionalHeaders +) +. "${PSScriptRoot}\logging.ps1" + +$OpensourceAPIBaseURI = "https://repos.opensource.microsoft.com/api/people/links/github/$GithubName" + +$Headers = @{ + "Authorization" = "Bearer $AadToken" + "Content-Type" = $ContentType + "api-version" = $ApiVersion +} + +function Load-RequestHeaders() { + if ($AdditionalHeaders) { + return $Headers + $AdditionalHeaders + } + return $Headers +} + + +try { + $headers = Load-RequestHeaders + $resp = Invoke-RestMethod $OpensourceAPIBaseURI -Method 'GET' -Headers $Headers +} +catch { + LogError $PSItem.ToString() + exit 1 +} + +$resp | Write-Verbose + +if ($resp.aad) { + return $resp.aad.alias +} + +LogError "Failed to retrieve the ms alias from given github identity: $GithubName." From 34d7113e0b3d6c5cd7c826a7506faf8a0e294ee4 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Wed, 19 Jan 2022 14:10:19 -0800 Subject: [PATCH 2/3] Printing more message --- eng/common/scripts/Get-MsAlias-FromGithub.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eng/common/scripts/Get-MsAlias-FromGithub.ps1 b/eng/common/scripts/Get-MsAlias-FromGithub.ps1 index 5e10d7ea629..82950133200 100644 --- a/eng/common/scripts/Get-MsAlias-FromGithub.ps1 +++ b/eng/common/scripts/Get-MsAlias-FromGithub.ps1 @@ -47,10 +47,11 @@ function Load-RequestHeaders() { try { $headers = Load-RequestHeaders + Write-Host "Fetching ms alias for github identity: $GithubName". $resp = Invoke-RestMethod $OpensourceAPIBaseURI -Method 'GET' -Headers $Headers } catch { - LogError $PSItem.ToString() + LogError $_ exit 1 } From f3eba05edf5609756401877719822562468d524f Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Wed, 19 Jan 2022 14:21:07 -0800 Subject: [PATCH 3/3] Printing more messages --- eng/common/scripts/Get-MsAlias-FromGithub.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/Get-MsAlias-FromGithub.ps1 b/eng/common/scripts/Get-MsAlias-FromGithub.ps1 index 82950133200..0490fb97cac 100644 --- a/eng/common/scripts/Get-MsAlias-FromGithub.ps1 +++ b/eng/common/scripts/Get-MsAlias-FromGithub.ps1 @@ -47,7 +47,7 @@ function Load-RequestHeaders() { try { $headers = Load-RequestHeaders - Write-Host "Fetching ms alias for github identity: $GithubName". + Write-Host "Fetching ms alias for github identity: $GithubName." $resp = Invoke-RestMethod $OpensourceAPIBaseURI -Method 'GET' -Headers $Headers } catch {