|
1 | 1 | param (
|
2 |
| - $TargetDirectory, # should be in relative form from root of repo. EG: sdk/servicebus |
3 |
| - $RootDirectory, # ideally $(Build.SourcesDirectory) |
4 |
| - $VsoVariable = "" # target devops output variable |
| 2 | + [string]$TargetDirectory = "", # Code path to code owners. e.g sdk/core/azure-amqp |
| 3 | + [string]$CodeOwnerFileLocation = "$PSSCriptRoot/../../../.github/CODEOWNERS", # The absolute path of CODEOWNERS file. |
| 4 | + [string]$ToolVersion = "1.0.0-dev.20211122.14", # Placeholder. Will update in next PR |
| 5 | + [string]$ToolPath = (Join-Path ([System.IO.Path]::GetTempPath()) "codeowners-tool-path"), # The place to check the tool existence. Put temp path as default |
| 6 | + [string]$DevOpsFeed = "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json", # DevOp tool feeds. |
| 7 | + [string]$VsoVariable = "", # Option of write code owners into devop variable |
| 8 | + [switch]$IncludeNonUserAliases, # Option to filter out the team alias in code owner list. e.g. Azure/azure-sdk-team |
| 9 | + [switch]$Test #Run test functions against the script logic |
5 | 10 | )
|
6 |
| -$target = $TargetDirectory.ToLower().Trim("/") |
7 |
| -$codeOwnersLocation = Join-Path $RootDirectory -ChildPath ".github/CODEOWNERS" |
8 |
| -$ownedFolders = @{} |
9 | 11 |
|
10 |
| -if (!(Test-Path $codeOwnersLocation)) { |
11 |
| - Write-Host "Unable to find CODEOWNERS file in target directory $RootDirectory" |
12 |
| - exit 1 |
13 |
| -} |
14 |
| - |
15 |
| -$codeOwnersContent = Get-Content $codeOwnersLocation |
| 12 | +function Get-CodeOwnersTool() |
| 13 | +{ |
| 14 | + $command = Join-Path $ToolPath "retrieve-codeowners" |
| 15 | + # Check if the retrieve-codeowners tool exsits or not. |
| 16 | + if (Get-Command $command -errorAction SilentlyContinue) { |
| 17 | + return $command |
| 18 | + } |
| 19 | + if (!(Test-Path $ToolPath)) { |
| 20 | + New-Item -ItemType Directory -Path $ToolPath | Out-Null |
| 21 | + } |
| 22 | + Write-Host "Installing the retrieve-codeowners tool under $ToolPath... " |
| 23 | + dotnet tool install --tool-path $ToolPath --add-source $DevOpsFeed --version $ToolVersion "Azure.Sdk.Tools.RetrieveCodeOwners" | Out-Null |
16 | 24 |
|
17 |
| -foreach ($contentLine in $codeOwnersContent) { |
18 |
| - if (-not $contentLine.StartsWith("#") -and $contentLine){ |
19 |
| - $splitLine = $contentLine -split "\s+" |
20 |
| - |
21 |
| - # CODEOWNERS file can also have labels present after the owner aliases |
22 |
| - # gh aliases start with @ in codeowners. don't pass on to API calls |
23 |
| - $ownedFolders[$splitLine[0].ToLower().Trim("/")] = ($splitLine[1..$($splitLine.Length)] ` |
24 |
| - | ? { $_.StartsWith("@") } ` |
25 |
| - | % { return $_.substring(1) }) -join "," |
| 25 | + # Test to see if the tool properly installed. |
| 26 | + if (!(Get-Command $command -errorAction SilentlyContinue)) { |
| 27 | + Write-Error "The retrieve-codeowners tool is not properly installed. Please check your tool path. $ToolPath" |
| 28 | + return |
26 | 29 | }
|
| 30 | + return $command |
27 | 31 | }
|
28 | 32 |
|
29 |
| -$results = $ownedFolders[$target] |
30 |
| - |
31 |
| -if ($results) { |
32 |
| - Write-Host "Found a folder $results to match $target" |
| 33 | +function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocation, [bool]$includeNonUserAliases = $false) |
| 34 | +{ |
| 35 | + $command = Get-CodeOwnersTool |
| 36 | + # Filter out the non user alias from code owner list. |
| 37 | + Write-Host "Testing on $targetDirectory..." |
| 38 | + if($includeNonUserAliases) { |
| 39 | + $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation 2>&1 |
| 40 | + } |
| 41 | + else { |
| 42 | + $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation --filter-out-non-user-aliases 2>&1 |
| 43 | + } |
| 44 | + # Failed at the command of fetching code owners. |
| 45 | + if ($LASTEXITCODE -ne 0) { |
| 46 | + Write-Host $codeOwnersString |
| 47 | + return ,@() |
| 48 | + } |
| 49 | + |
| 50 | + $codeOwnersJson = $codeOwnersString | ConvertFrom-Json |
| 51 | + if (!$codeOwnersJson) { |
| 52 | + Write-Host "No code owners returned from the path: $targetDirectory" |
| 53 | + return ,@() |
| 54 | + } |
33 | 55 |
|
34 | 56 | if ($VsoVariable) {
|
35 |
| - $alreadyPresent = [System.Environment]::GetEnvironmentVariable($VsoVariable) |
| 57 | + $codeOwners = $codeOwnersJson.Owners -join "," |
| 58 | + Write-Host "##vso[task.setvariable variable=$VsoVariable;]$codeOwners" |
| 59 | + } |
| 60 | + |
| 61 | + return ,@($codeOwnersJson.Owners) |
| 62 | +} |
| 63 | + |
| 64 | +function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocation, [bool]$includeNonUserAliases = $false, [string[]]$expectReturn) { |
| 65 | + $actualReturn = Get-CodeOwners -targetDirectory $targetDirectory -codeOwnerFileLocation $codeOwnerFileLocation -includeNonUserAliases $IncludeNonUserAliases |
36 | 66 |
|
37 |
| - if ($alreadyPresent) { |
38 |
| - $results += ",$alreadyPresent" |
| 67 | + if ($actualReturn.Count -ne $expectReturn.Count) { |
| 68 | + Write-Error "The length of actual result is not as expected. Expected length: $($expectReturn.Count), Actual length: $($actualReturn.Count)." |
| 69 | + exit 1 |
| 70 | + } |
| 71 | + for ($i = 0; $i -lt $expectReturn.Count; $i++) { |
| 72 | + if ($expectReturn[$i] -ne $actualReturn[$i]) { |
| 73 | + Write-Error "Expect result $expectReturn[$i] is different than actual result $actualReturn[$i]." |
| 74 | + exit 1 |
39 | 75 | }
|
40 |
| - Write-Host "##vso[task.setvariable variable=$VsoVariable;]$results" |
41 | 76 | }
|
| 77 | +} |
42 | 78 |
|
43 |
| - return $results |
| 79 | +if($Test) { |
| 80 | + $testFile = "$PSSCriptRoot/../../../tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" |
| 81 | + TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -includeNonUserAliases $true -expectReturn @("person1", "person2") |
| 82 | + TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -includeNonUserAliases $true -expectReturn @("person1", "person2") |
| 83 | + TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -includeNonUserAliases $true -expectReturn @("person3", "person4") |
| 84 | + TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -includeNonUserAliases $true $testFile -expectReturn @("person3", "person4") |
| 85 | + TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -includeNonUserAliases $true -expectReturn @() |
| 86 | + TestGetCodeOwner -targetDirectory "/sdk/testUser/" -codeOwnerFileLocation $testFile -expectReturn @("azure-sdk") |
| 87 | + exit 0 |
44 | 88 | }
|
45 | 89 | else {
|
46 |
| - Write-Host "Unable to match path $target in CODEOWNERS file located at $codeOwnersLocation." |
47 |
| - Write-Host ($ownedFolders | ConvertTo-Json) |
48 |
| - return "" |
| 90 | + return Get-CodeOwners -targetDirectory $TargetDirectory -codeOwnerFileLocation $CodeOwnerFileLocation -includeNonUserAliases $IncludeNonUserAliases |
49 | 91 | }
|
0 commit comments