Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update get-codeowners powershell file
  • Loading branch information
chidozieononiwu committed Sep 24, 2020
commit d4f6b4bca001418c86dddee34c602102cf9c1306
82 changes: 70 additions & 12 deletions eng/common/scripts/get-codeowners.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
param (
$TargetDirectory, # should be in relative form from root of repo. EG: sdk/servicebus
$RootDirectory, # ideally $(Build.SourcesDirectory)
$VsoVariable = "" # target devops output variable
$AuthToken,
$OwningUsers = "", # target devops output variable
$OwningTeams = "",
$OwningLabels = ""
)
$target = $TargetDirectory.ToLower().Trim("/")
$codeOwnersLocation = Join-Path $RootDirectory -ChildPath ".github/CODEOWNERS"
Expand All @@ -13,34 +16,89 @@ if (!(Test-Path $codeOwnersLocation)) {
}

$codeOwnersContent = Get-Content $codeOwnersLocation
$previousLine

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 ","
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that you split on "," below we should just avoid the join here and just keep an array.

$labels = ""

if ($null -ne $previousLine -and $previousLine.StartsWith("# PRLabel:"))
{
$previousLine = $previousLine.Replace("# PRLabel: ","")
$splitPrevLine = $previousLine -split "%"
$labels = ($splitPrevLine[1..$($splitPrevLine.Length)] | % { return $_.Trim() }) -join ","
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets make remove the join here and keep it an array.

}

$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

if ($AuthToken) {
$headers = @{
Authorization = "bearer $AuthToken"
}
}

foreach ($str in $aliases)
{
$usersApiUrl = "https://api.github.com/users/$str"
try
{
$response = Invoke-RestMethod -Headers $headers $usersApiUrl
if ($users) { $users += ("," + $str) } else { $users += $str }
}
catch {
if ($_.Exception.Response.StatusCode.Value__ -eq 404) # Consider it a team Alias
{
if ($teams) { $teams += ("," + $str) } else { $teams += $str }
}
else{
LogError "Invoke-RestMethod ${usersApiUrl} failed with exception:`n$_"
exit 1
}
}
}

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

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

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

return $results
return @{ Users = $users; Teams = $teams; Labels = $results.Labels }
}
else {
Write-Host "Unable to match path $target in CODEOWNERS file located at $codeOwnersLocation."
Expand Down