...
1function Generate-AadToken ($TenantId, $ClientId, $ClientSecret)
2{
3 $LoginAPIBaseURI = "https://login.microsoftonline.com/$TenantId/oauth2/token"
4
5 $headers = @{
6 "content-type" = "application/x-www-form-urlencoded"
7 }
8
9 $body = @{
10 "grant_type" = "client_credentials"
11 "client_id" = $ClientId
12 "client_secret" = $ClientSecret
13 "resource" = "api://repos.opensource.microsoft.com/audience/7e04aa67"
14 }
15 Write-Host "Generating aad token..."
16 $resp = Invoke-RestMethod $LoginAPIBaseURI -Method 'POST' -Headers $headers -Body $body
17 return $resp.access_token
18}
19
20function GetMsAliasFromGithub ([string]$TenantId, [string]$ClientId, [string]$ClientSecret, [string]$GithubUser)
21{
22 # API documentation (out of date): https://github.com/microsoft/opensource-management-portal/blob/main/docs/api.md
23 $OpensourceAPIBaseURI = "https://repos.opensource.microsoft.com/api/people/links/github/$GithubUser"
24
25 $Headers = @{
26 "Content-Type" = "application/json"
27 "api-version" = "2019-10-01"
28 }
29
30 try {
31 $opsAuthToken = Generate-AadToken -TenantId $TenantId -ClientId $ClientId -ClientSecret $ClientSecret
32 $Headers["Authorization"] = "Bearer $opsAuthToken"
33 Write-Host "Fetching aad identity for github user: $GithubUser"
34 $resp = Invoke-RestMethod $OpensourceAPIBaseURI -Method 'GET' -Headers $Headers -MaximumRetryCount 3
35 } catch {
36 Write-Warning $_
37 return $null
38 }
39
40 $resp | Write-Verbose
41
42 if ($resp.aad) {
43 Write-Host "Fetched aad identity $($resp.aad.alias) for github user $GithubUser. "
44 return $resp.aad.alias
45 }
46 Write-Warning "Failed to retrieve the aad identity from given github user: $GithubName"
47 return $null
48}
49
50function GetAllGithubUsers ([string]$TenantId, [string]$ClientId, [string]$ClientSecret)
51{
52 # API documentation (out of date): https://github.com/microsoft/opensource-management-portal/blob/main/docs/api.md
53 $OpensourceAPIBaseURI = "https://repos.opensource.microsoft.com/api/people/links"
54
55 $Headers = @{
56 "Content-Type" = "application/json"
57 "api-version" = "2019-10-01"
58 }
59
60 try {
61 $opsAuthToken = Generate-AadToken -TenantId $TenantId -ClientId $ClientId -ClientSecret $ClientSecret
62 $Headers["Authorization"] = "Bearer $opsAuthToken"
63 Write-Host "Fetching all github alias links"
64 $resp = Invoke-RestMethod $OpensourceAPIBaseURI -Method 'GET' -Headers $Headers -MaximumRetryCount 3
65 } catch {
66 Write-Warning $_
67 return $null
68 }
69
70 return $resp
71}
72
73function GetPrimaryCodeOwner ([string]$TargetDirectory)
74{
75 $codeOwnerArray = &"$PSScriptRoot/../get-codeowners.ps1" -TargetDirectory $TargetDirectory
76 if ($codeOwnerArray) {
77 Write-Host "Code Owners are $codeOwnerArray."
78 return $codeOwnerArray[0]
79 }
80 Write-Warning "No code owner found in $TargetDirectory."
81 return $null
82}
View as plain text