Skip to content

Commit

Permalink
Sync eng/common directory with azure-sdk-tools repository for Tools P…
Browse files Browse the repository at this point in the history
…R 981
  • Loading branch information
azure-sdk committed Sep 24, 2020
1 parent 51d3f66 commit e79f39e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 14 deletions.
9 changes: 6 additions & 3 deletions eng/common/pipelines/templates/steps/get-pr-owners.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
parameters:
parameters:
TargetVariable: ''
TargetUserVariable: ''
TargetTeamVariable: ''
ServiceDirectory: ''

steps:
Expand All @@ -18,7 +20,7 @@ steps:
--kusto-database-var KUSTO_DB `
--kusto-table-var KUSTO_TABLE `
--identity "$(Build.QueuedBy)" `
--targetvar "${{ parameters.TargetVariable }}"
--targetvar "${{ coalesce(parameters.TargetVariable, parameters.TargetUserVariable) }}"
displayName: 'Resolving Queuing User'
continueOnError: true
workingDirectory: $(Build.SourcesDirectory)/tools_repo/tools/notification-configuration/identity-resolver
Expand All @@ -42,4 +44,5 @@ steps:
arguments: >
-TargetDirectory "/sdk/${{ parameters.ServiceDirectory }}/"
-RootDirectory "$(Build.SourcesDirectory)"
-VsoVariable "${{ parameters.TargetVariable }}"
-VsoOwningUsers = "${{ coalesce(parameters.TargetVariable, parameters.TargetUserVariable) }}"
-VsoOwningTeams = "${{ parameters.TargetTeamVariable }}"
103 changes: 92 additions & 11 deletions eng/common/scripts/get-codeowners.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
param (
$TargetDirectory, # should be in relative form from root of repo. EG: sdk/servicebus
$RootDirectory, # ideally $(Build.SourcesDirectory)
$VsoVariable = "" # target devops output variable
$AuthToken,
$VsoOwningUsers = "", # target devops output variable
$VsoOwningTeams = "",
$VsoOwningLabels = ""
)

. (Join-Path ${PSScriptRoot} logging.ps1)

$target = $TargetDirectory.ToLower().Trim("/")
$codeOwnersLocation = Join-Path $RootDirectory -ChildPath ".github/CODEOWNERS"
$ownedFolders = @{}
Expand All @@ -14,30 +20,105 @@ if (!(Test-Path $codeOwnersLocation)) {

$codeOwnersContent = Get-Content $codeOwnersLocation

function VerifyAlias($APIUrl)
{
if ($AuthToken) {
$headers = @{
Authorization = "bearer $AuthToken"
}
}
try
{
$response = Invoke-RestMethod -Headers $headers $APIUrl
}
catch
{
Write-Host "Invoke-RestMethod ${APIUrl} failed with exception:`n$_"
Write-Host "This might be because a team alias was used for user API request or vice versa."
return $false
}
return $true
}

foreach ($contentLine in $codeOwnersContent) {
if (-not $contentLine.StartsWith("#") -and $contentLine){
$splitLine = $contentLine -split "\s+"

# CODEOWNERS file can also have labels present after the owner aliases
# gh aliases start with @ in codeowners. don't pass on to API calls
$ownedFolders[$splitLine[0].ToLower().Trim("/")] = ($splitLine[1..$($splitLine.Length)] `
| ? { $_.StartsWith("@") } `
| % { return $_.substring(1) }) -join ","

$aliases = ($splitLine[1..$($splitLine.Length)] | ? { $_.StartsWith("@") } | % { return $_.substring(1) }) -join ","
$labels = @()

if ($null -ne $previousLine -and $previousLine.Contains("PRLabel:"))
{
$previousLine = $previousLine.substring($previousLine.IndexOf(':') + 1)
$splitPrevLine = $previousLine -split "%"
$labels = ($splitPrevLine[1..$($splitPrevLine.Length)] | % { return $_.Trim() })
}

$ownedFolders[$splitLine[0].ToLower().Trim("/")] = @{ Aliases = $aliases; Labels = $labels }
}
$previousLine = $contentLine
}

$results = $ownedFolders[$target]

if ($results) {
Write-Host "Found a folder $results to match $target"

if ($VsoVariable) {
$alreadyPresent = [System.Environment]::GetEnvironmentVariable($VsoVariable)
Write-Host "Found a folder to match $target"
$aliases = $results.Aliases -split ","
$users = @()
$teams = @()

foreach ($str in $aliases)
{
if ($str.IndexOf('/') -ne -1) # Check if it's a team alias e.g. Azure/azure-sdk-eng
{
$org = $str.substring(0, $str.IndexOf('/'))
$team_slug = $str.substring($str.IndexOf('/') + 1)
$teamApiUrl = "https://api.github.com/orgs/$org/teams/$team_slug"
if (VerifyAlias -APIUrl $teamApiUrl)
{
$teams += $team_slug
continue
}
}
else
{
$usersApiUrl = "https://api.github.com/users/$str"
if (VerifyAlias -APIUrl $usersApiUrl)
{
$users += $str
continue
}
LogWarning "Alias ${str} is neither a recognized github user nor a team"
}
}

$labels = $results.Labels

if ($VsoOwningUsers) {
$presentOwningUsers = [System.Environment]::GetEnvironmentVariable($VsoOwningUsers)
if ($presentOwningUsers) {
$users += $presentOwningUsers
}
Write-Host "##vso[task.setvariable variable=$VsoOwningUsers;]($users -join ',')"
}

if ($VsoOwningTeams) {
$presentOwningTeams = [System.Environment]::GetEnvironmentVariable($VsoOwningTeams)
if ($presentOwningTeams) {
$teams += $presentOwningUsers
}
Write-Host "##vso[task.setvariable variable=$VsoOwningTeams;]($teams -join ',')"
}

if ($alreadyPresent) {
$results += ",$alreadyPresent"
if ($VsoOwningLabels) {
$presentOwningLabels = [System.Environment]::GetEnvironmentVariable($VsoOwningLabels)
if ($presentOwningLabels) {
$labels += $presentOwningLabels
}
Write-Host "##vso[task.setvariable variable=$VsoVariable;]$results"
Write-Host "##vso[task.setvariable variable=$VsoOwningLabels;]($labels -join ',')"
}

return $results
Expand Down

0 comments on commit e79f39e

Please sign in to comment.