From 83f300357ebe10efc6a016162e48c389d2d36a3b Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Tue, 9 Nov 2021 13:20:54 -0800 Subject: [PATCH] Re-add get-codeowners.ps1 --- eng/common/scripts/get-codeowners.ps1 | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 eng/common/scripts/get-codeowners.ps1 diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 new file mode 100644 index 000000000000..fdf8453aceeb --- /dev/null +++ b/eng/common/scripts/get-codeowners.ps1 @@ -0,0 +1,49 @@ +param ( + $TargetDirectory, # should be in relative form from root of repo. EG: sdk/servicebus + $RootDirectory, # ideally $(Build.SourcesDirectory) + $VsoVariable = "" # target devops output variable +) +$target = $TargetDirectory.ToLower().Trim("/") +$codeOwnersLocation = Join-Path $RootDirectory -ChildPath ".github/CODEOWNERS" +$ownedFolders = @{} + +if (!(Test-Path $codeOwnersLocation)) { + Write-Host "Unable to find CODEOWNERS file in target directory $RootDirectory" + exit 1 +} + +$codeOwnersContent = Get-Content $codeOwnersLocation + +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 "," + } +} + +$results = $ownedFolders[$target] + +if ($results) { + Write-Host "Found a folder $results to match $target" + + if ($VsoVariable) { + $alreadyPresent = [System.Environment]::GetEnvironmentVariable($VsoVariable) + + if ($alreadyPresent) { + $results += ",$alreadyPresent" + } + Write-Host "##vso[task.setvariable variable=$VsoVariable;]$results" + } + + return $results +} +else { + Write-Host "Unable to match path $target in CODEOWNERS file located at $codeOwnersLocation." + Write-Host ($ownedFolders | ConvertTo-Json) + return "" +}