diff --git a/eng/common/pipelines/templates/steps/get-pr-owners.yml b/eng/common/pipelines/templates/steps/get-pr-owners.yml index 2abe10b81a52..a80d5b83b2de 100644 --- a/eng/common/pipelines/templates/steps/get-pr-owners.yml +++ b/eng/common/pipelines/templates/steps/get-pr-owners.yml @@ -39,7 +39,7 @@ steps: - pwsh: | $originalValue = "$(${{ parameters.TargetVariable }})" - $result = $(Build.SourcesDirectory)/eng/common/scripts/get-codeowners.ps1 -TargetDirectory sdk/${{ parameters.ServiceDirectory }}/ -RootDirectory $(Build.SourcesDirectory) + $result = $(Build.SourcesDirectory)/eng/common/scripts/get-codeowners.ps1 -TargetDirectory /sdk/${{ parameters.ServiceDirectory }}/ -RootDirectory $(Build.SourcesDirectory) if ($result) { Write-Output "##vso[task.setvariable variable=${{ parameters.TargetVariable }}]$originalValue,$result" } diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index edbae4cb18c6..8452f9fd650b 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -2,8 +2,9 @@ param ( $TargetDirectory, # should be in relative form from root of repo. EG: sdk/servicebus $RootDirectory # ideally $(Build.SourcesDirectory) ) - +$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" @@ -12,29 +13,27 @@ if (!(Test-Path $codeOwnersLocation)) { $codeOwnersContent = Get-Content $codeOwnersLocation -$ownedFolders = @{} - 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()] = ($splitLine[1..$($splitLine.Length)] ` + $ownedFolders[$splitLine[0].ToLower().Trim("/")] = ($splitLine[1..$($splitLine.Length)] ` | ? { $_.StartsWith("@") } ` | % { return $_.substring(1) }) -join "," } } -$results = $ownedFolders[$TargetDirectory.ToLower()] +$results = $ownedFolders[$target] if ($results) { - Write-Host "Discovered code owners for path $TargetDirectory are $results." + Write-Host "Found a folder $results to match $target" return $results } else { - Write-Host "Unable to match path $TargetDirectory in CODEOWNERS file located at $codeOwnersLocation." - Write-Host $ownedFolders | ConvertTo-Json + Write-Host "Unable to match path $target in CODEOWNERS file located at $codeOwnersLocation." + Write-Host ($ownedFolders | ConvertTo-Json) return "" }