From fd129923f7e367e060f43b15b296045f84ec87e2 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 10:38:31 -0800 Subject: [PATCH 01/37] Install and Run code owner tools in get-codeowner.ps1 --- eng/common/scripts/get-codeowners.ps1 | 96 ++++++++++++------- .../CODEOWNERS | 2 +- tools/code-owners-parser/ci.yml | 14 +-- 3 files changed, 68 insertions(+), 44 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index fdf8453acee..1ff6fb2644c 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -1,49 +1,71 @@ param ( - $TargetDirectory, # should be in relative form from root of repo. EG: sdk/servicebus - $RootDirectory, # ideally $(Build.SourcesDirectory) - $VsoVariable = "" # target devops output variable + [string]$TargetDirectory, # Code path to code owners. e.g sdk/core/azure-amqp + [string]$RootDirectory = "$env:SYSTEM_DEFAULTWORKINGDIRECTORY", # The repo contains CODEOWNER file. + [string]$CodeOwnerFileLocation = "$PSSCriptRoot/../../../.github/CODEOWNERS", # The absolute path of CODEOWNERS file. + [string]$ToolVersion = "1.0.0-dev.20211118.20", # Placeholder. Will update in next PR + [string]$ToolPath = "$env:AGENT_TOOLSDIRECTORY", # The place to check the tool existence. Put $(Agent.ToolsDirectory) as default + [string]$DevOpsFeed = "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json", # DevOp tool feeds. + [string]$VsoVariable = "" # Option of write code owners into devop 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 +$LocalCodeOwnerPath = ".github/CODEOWNERS" +$ToolCommandName = "retrieve-codeowners" +$ToolName = "Azure.Sdk.Tools.RetrieveCodeOwners" +function New-TemporaryDirectory { + $parent = [System.IO.Path]::GetTempPath() + [string] $name = [System.Guid]::NewGuid() + [string] $newPath = Join-Path $parent $name + New-Item -ItemType Directory -Path $newPath | Out-Null + return $newPath } -$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 "," +function InstallRetrieveCodeOwnersTool() { + # Check if the retrieve-codeowners tool exsits or not. + if (Get-Command "$ToolPath/$ToolCommandName" -errorAction SilentlyContinue) { + return } -} + if (!$ToolPath -or !(Test-Path $ToolPath)) { + $ToolPath = New-TemporaryDirectory + } + Write-Warning "Installing the ToolCommandName tool under $ToolPath... " + dotnet tool install --tool-path $ToolPath --add-source $DevOpsFeed --version $ToolVersion $ToolName | Out-Null -$results = $ownedFolders[$target] + return Join-Path $ToolPath $ToolCommandName +} -if ($results) { - Write-Host "Found a folder $results to match $target" +function GetCodeOwners ([string] $Command) { + # Run code owner tools to retrieve code owners. + if (!(Get-Command $Command -errorAction SilentlyContinue)) { + Write-Error "The ToolCommandName tool is not properly installed. Please check your tool path. $ToolPath" + return "" + } + + # Params $RootDirectory is already in use in cpp release pipeline. + # Will use $CodeOwnerFileLocation and deprecate $RootDirectory once it is ready to retire $RootDirectory. + if ($RootDirectory -and !(Test-Path $CodeOwnerFileLocation)) { + $CodeOwnerFileLocation = Join-Path $RootDirectory $LocalCodeOwnerPath + } + + $codeOwnersString = & $Command --target-directory "$TargetDirectory" --code-owner-file-path "$CodeOwnerFileLocation" + # Failed at the command of fetching code owners. + if ($LASTEXITCODE -ne 0) { + return "" + } + + $codeOwnersJson = $codeOwnersString | ConvertFrom-Json + if (!$codeOwnersJson) { + Write-Host "No code owners returned from the path: $TargetDirectory" + return "" + } + $codeOwners = $codeOwnersJson.Owners -join "," + Write-Host "Code owners are $codeOwners" if ($VsoVariable) { - $alreadyPresent = [System.Environment]::GetEnvironmentVariable($VsoVariable) - - if ($alreadyPresent) { - $results += ",$alreadyPresent" - } - Write-Host "##vso[task.setvariable variable=$VsoVariable;]$results" + Write-Host "##vso[task.setvariable variable=$VsoVariable;]$codeOwners" } - - return $results -} -else { - Write-Host "Unable to match path $target in CODEOWNERS file located at $codeOwnersLocation." - Write-Host ($ownedFolders | ConvertTo-Json) - return "" } + +# Install the tool first +$output = InstallRetrieveCodeOwnersTool + +return GetCodeOwners -Command $output diff --git a/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS b/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS index 8c5af6a868f..47557d88aac 100644 --- a/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS +++ b/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS @@ -27,4 +27,4 @@ # Example for service that does not have the code in the repo but wants issues to be labeled # Notice the use of the moniker // # ServiceLabel: %label %Service Attention -// @person7 @person8 \ No newline at end of file +// @person7 @person8 diff --git a/tools/code-owners-parser/ci.yml b/tools/code-owners-parser/ci.yml index 97068d14b7b..ab8568973f7 100644 --- a/tools/code-owners-parser/ci.yml +++ b/tools/code-owners-parser/ci.yml @@ -27,9 +27,11 @@ extends: ToolDirectory: tools/code-owners-parser DotNetCoreVersion: 5.0.301 TestPostSteps: - # This is a simple test for placeholder. Will replace the test with get-codeowners.ps1 in future. - - pwsh: | - $output = dotnet run --project "tools\code-owners-parser\Azure.Sdk.Tools.RetrieveCodeOwners\Azure.Sdk.Tools.RetrieveCodeOwners.csproj" ` - --code-owner-file-path "$(Build.SourcesDirectory)/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" --target-directory "sdk" - $output | ConvertFrom-Json - displayName: Test on code owner tool output \ No newline at end of file + - task: PowerShell@2 + displayName: Test on code owner tool output + inputs: + pwsh: true + filePath: eng/common/scripts/get-codeowners.ps1 + arguments: > + -TargetDirectory "sdk" + -CodeOwnerFileLocation "$(System.DefaultWorkingDirectory)/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" From 5f6b7239ec2c919004af6b5e6935358947d7cd76 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 10:45:13 -0800 Subject: [PATCH 02/37] return command when exists --- eng/common/scripts/get-codeowners.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 1ff6fb2644c..a91715e2790 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -22,7 +22,7 @@ function New-TemporaryDirectory { function InstallRetrieveCodeOwnersTool() { # Check if the retrieve-codeowners tool exsits or not. if (Get-Command "$ToolPath/$ToolCommandName" -errorAction SilentlyContinue) { - return + return "$ToolPath/$ToolCommandName" } if (!$ToolPath -or !(Test-Path $ToolPath)) { $ToolPath = New-TemporaryDirectory From c454055e48d3b26c2303817b1bf08b4296cc7579 Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Fri, 19 Nov 2021 11:14:17 -0800 Subject: [PATCH 03/37] Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard --- eng/common/scripts/get-codeowners.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index a91715e2790..4c519712fc2 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -33,7 +33,8 @@ function InstallRetrieveCodeOwnersTool() { return Join-Path $ToolPath $ToolCommandName } -function GetCodeOwners ([string] $Command) { +function Get-CodeOwners ([string] $Command) +{ # Run code owner tools to retrieve code owners. if (!(Get-Command $Command -errorAction SilentlyContinue)) { Write-Error "The ToolCommandName tool is not properly installed. Please check your tool path. $ToolPath" From f1767a27e68f4ee446bf6d9e50234800c12a1e04 Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Fri, 19 Nov 2021 11:14:36 -0800 Subject: [PATCH 04/37] Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard --- eng/common/scripts/get-codeowners.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 4c519712fc2..10614715bc9 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -19,7 +19,8 @@ function New-TemporaryDirectory { return $newPath } -function InstallRetrieveCodeOwnersTool() { +function Get-CodeOwnersTool() +{ # Check if the retrieve-codeowners tool exsits or not. if (Get-Command "$ToolPath/$ToolCommandName" -errorAction SilentlyContinue) { return "$ToolPath/$ToolCommandName" From 1cc91a5ba7182278729ac38ed386a4eac698882f Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 11:32:08 -0800 Subject: [PATCH 05/37] Address comments --- eng/common/scripts/get-codeowners.ps1 | 32 ++++++++++----------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 10614715bc9..857ab448489 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -8,16 +8,7 @@ param ( [string]$VsoVariable = "" # Option of write code owners into devop variable ) -$LocalCodeOwnerPath = ".github/CODEOWNERS" $ToolCommandName = "retrieve-codeowners" -$ToolName = "Azure.Sdk.Tools.RetrieveCodeOwners" -function New-TemporaryDirectory { - $parent = [System.IO.Path]::GetTempPath() - [string] $name = [System.Guid]::NewGuid() - [string] $newPath = Join-Path $parent $name - New-Item -ItemType Directory -Path $newPath | Out-Null - return $newPath -} function Get-CodeOwnersTool() { @@ -26,26 +17,26 @@ function Get-CodeOwnersTool() return "$ToolPath/$ToolCommandName" } if (!$ToolPath -or !(Test-Path $ToolPath)) { - $ToolPath = New-TemporaryDirectory + $ToolPath = (Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())) + New-Item -ItemType Directory -Path $newPath | Out-Null } Write-Warning "Installing the ToolCommandName tool under $ToolPath... " - dotnet tool install --tool-path $ToolPath --add-source $DevOpsFeed --version $ToolVersion $ToolName | Out-Null + dotnet tool install --tool-path $ToolPath --add-source $DevOpsFeed --version $ToolVersion "Azure.Sdk.Tools.RetrieveCodeOwners" | Out-Null + # Test to see if the tool properly installed. + if (!(Get-Command $Command -errorAction SilentlyContinue)) { + Write-Error "The ToolCommandName tool is not properly installed. Please check your tool path. $ToolPath" + return "" + } return Join-Path $ToolPath $ToolCommandName } function Get-CodeOwners ([string] $Command) { - # Run code owner tools to retrieve code owners. - if (!(Get-Command $Command -errorAction SilentlyContinue)) { - Write-Error "The ToolCommandName tool is not properly installed. Please check your tool path. $ToolPath" - return "" - } - # Params $RootDirectory is already in use in cpp release pipeline. # Will use $CodeOwnerFileLocation and deprecate $RootDirectory once it is ready to retire $RootDirectory. if ($RootDirectory -and !(Test-Path $CodeOwnerFileLocation)) { - $CodeOwnerFileLocation = Join-Path $RootDirectory $LocalCodeOwnerPath + $CodeOwnerFileLocation = Join-Path $RootDirectory ".github/CODEOWNERS" } $codeOwnersString = & $Command --target-directory "$TargetDirectory" --code-owner-file-path "$CodeOwnerFileLocation" @@ -60,11 +51,12 @@ function Get-CodeOwners ([string] $Command) return "" } - $codeOwners = $codeOwnersJson.Owners -join "," - Write-Host "Code owners are $codeOwners" if ($VsoVariable) { + $codeOwners = $codeOwnersJson.Owners -join "," Write-Host "##vso[task.setvariable variable=$VsoVariable;]$codeOwners" } + + return $codeOwnersJson.Owners } # Install the tool first From 1be01e1b40d480e32c458285e9a0aa374f0e3610 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 11:55:35 -0800 Subject: [PATCH 06/37] Added test to script --- eng/common/scripts/get-codeowners.ps1 | 33 +++++++++++++++++++++------ tools/code-owners-parser/ci.yml | 1 + 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 857ab448489..dcdf5c933af 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -5,7 +5,8 @@ param ( [string]$ToolVersion = "1.0.0-dev.20211118.20", # Placeholder. Will update in next PR [string]$ToolPath = "$env:AGENT_TOOLSDIRECTORY", # The place to check the tool existence. Put $(Agent.ToolsDirectory) as default [string]$DevOpsFeed = "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json", # DevOp tool feeds. - [string]$VsoVariable = "" # Option of write code owners into devop variable + [string]$VsoVariable = "", # Option of write code owners into devop variable + [switch]$Test #Run test functions against the script logic ) $ToolCommandName = "retrieve-codeowners" @@ -39,7 +40,7 @@ function Get-CodeOwners ([string] $Command) $CodeOwnerFileLocation = Join-Path $RootDirectory ".github/CODEOWNERS" } - $codeOwnersString = & $Command --target-directory "$TargetDirectory" --code-owner-file-path "$CodeOwnerFileLocation" + $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $CodeOwnerFileLocation # Failed at the command of fetching code owners. if ($LASTEXITCODE -ne 0) { return "" @@ -47,7 +48,7 @@ function Get-CodeOwners ([string] $Command) $codeOwnersJson = $codeOwnersString | ConvertFrom-Json if (!$codeOwnersJson) { - Write-Host "No code owners returned from the path: $TargetDirectory" + Write-Host "No code owners returned from the path: $targetDirectory" return "" } @@ -55,11 +56,29 @@ function Get-CodeOwners ([string] $Command) $codeOwners = $codeOwnersJson.Owners -join "," Write-Host "##vso[task.setvariable variable=$VsoVariable;]$codeOwners" } - + return $codeOwnersJson.Owners } -# Install the tool first -$output = InstallRetrieveCodeOwnersTool -return GetCodeOwners -Command $output +function TestGetCodeOwner() { + # Install the tool first + $output = InstallRetrieveCodeOwnersTool + $actualReturn = GetCodeOwners -command $output + $expectReturn = @("person1", "person2") + for ($i = 0; $i -lt $expectReturn.Length; $i++) { + if ($actualReturn[$i] -ne $expectReturn[$i]) { + Write-Error "Expect result $expectReturn[$i] is different than actual result $actualReturn[$i]." + exit + } + } +} + +if($Test) { + TestGetCodeOwner +} +else { + # Install the tool first + $output = InstallRetrieveCodeOwnersTool + return GetCodeOwners -command $output +} diff --git a/tools/code-owners-parser/ci.yml b/tools/code-owners-parser/ci.yml index ab8568973f7..0dc884bcd8b 100644 --- a/tools/code-owners-parser/ci.yml +++ b/tools/code-owners-parser/ci.yml @@ -35,3 +35,4 @@ extends: arguments: > -TargetDirectory "sdk" -CodeOwnerFileLocation "$(System.DefaultWorkingDirectory)/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" + -Test From 03633d17be9a1c32d0d772b6027c72c2a8cd0bfc Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 12:43:54 -0800 Subject: [PATCH 07/37] change return type --- eng/common/scripts/get-codeowners.ps1 | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index dcdf5c933af..dd116410121 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -27,13 +27,16 @@ function Get-CodeOwnersTool() # Test to see if the tool properly installed. if (!(Get-Command $Command -errorAction SilentlyContinue)) { Write-Error "The ToolCommandName tool is not properly installed. Please check your tool path. $ToolPath" - return "" + return } return Join-Path $ToolPath $ToolCommandName } -function Get-CodeOwners ([string] $Command) +function Get-CodeOwners ([string] $command) { + if (!$command) { + return @() + } # Params $RootDirectory is already in use in cpp release pipeline. # Will use $CodeOwnerFileLocation and deprecate $RootDirectory once it is ready to retire $RootDirectory. if ($RootDirectory -and !(Test-Path $CodeOwnerFileLocation)) { @@ -43,13 +46,13 @@ function Get-CodeOwners ([string] $Command) $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $CodeOwnerFileLocation # Failed at the command of fetching code owners. if ($LASTEXITCODE -ne 0) { - return "" + return @() } $codeOwnersJson = $codeOwnersString | ConvertFrom-Json if (!$codeOwnersJson) { Write-Host "No code owners returned from the path: $targetDirectory" - return "" + return @() } if ($VsoVariable) { @@ -61,9 +64,10 @@ function Get-CodeOwners ([string] $Command) } -function TestGetCodeOwner() { - # Install the tool first - $output = InstallRetrieveCodeOwnersTool +function TestGetCodeOwner([string] $command) { + if (!$output) { + exit + } $actualReturn = GetCodeOwners -command $output $expectReturn = @("person1", "person2") for ($i = 0; $i -lt $expectReturn.Length; $i++) { @@ -74,11 +78,13 @@ function TestGetCodeOwner() { } } +# Install the tool first +$output = Get-CodeOwnersTool + if($Test) { - TestGetCodeOwner + TestGetCodeOwner -command $output } else { - # Install the tool first - $output = InstallRetrieveCodeOwnersTool + return GetCodeOwners -command $output } From f8408737daaa0ae144a29acd5635b1fddb781306 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 12:54:01 -0800 Subject: [PATCH 08/37] change wrong params --- eng/common/scripts/get-codeowners.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index dd116410121..76040754d55 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -65,10 +65,10 @@ function Get-CodeOwners ([string] $command) function TestGetCodeOwner([string] $command) { - if (!$output) { + if (!$command) { exit } - $actualReturn = GetCodeOwners -command $output + $actualReturn = GetCodeOwners -command $command $expectReturn = @("person1", "person2") for ($i = 0; $i -lt $expectReturn.Length; $i++) { if ($actualReturn[$i] -ne $expectReturn[$i]) { From f67e38220fe1d62860dff6c645de5c7036eeb8a9 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 12:57:18 -0800 Subject: [PATCH 09/37] default to toolpath --- eng/common/scripts/get-codeowners.ps1 | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 76040754d55..b778c5d906a 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -3,7 +3,7 @@ param ( [string]$RootDirectory = "$env:SYSTEM_DEFAULTWORKINGDIRECTORY", # The repo contains CODEOWNER file. [string]$CodeOwnerFileLocation = "$PSSCriptRoot/../../../.github/CODEOWNERS", # The absolute path of CODEOWNERS file. [string]$ToolVersion = "1.0.0-dev.20211118.20", # Placeholder. Will update in next PR - [string]$ToolPath = "$env:AGENT_TOOLSDIRECTORY", # The place to check the tool existence. Put $(Agent.ToolsDirectory) as default + [string]$ToolPath = (Join-Path [System.IO.Path]::GetTempPath(), "codeowners-tool-path"), # The place to check the tool existence. Put temp path as default [string]$DevOpsFeed = "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json", # DevOp tool feeds. [string]$VsoVariable = "", # Option of write code owners into devop variable [switch]$Test #Run test functions against the script logic @@ -17,9 +17,8 @@ function Get-CodeOwnersTool() if (Get-Command "$ToolPath/$ToolCommandName" -errorAction SilentlyContinue) { return "$ToolPath/$ToolCommandName" } - if (!$ToolPath -or !(Test-Path $ToolPath)) { - $ToolPath = (Join-Path ([System.IO.Path]::GetTempPath()) ([System.IO.Path]::GetRandomFileName())) - New-Item -ItemType Directory -Path $newPath | Out-Null + if (!(Test-Path $ToolPath)) { + New-Item -ItemType Directory -Path $ToolPath | Out-Null } Write-Warning "Installing the ToolCommandName tool under $ToolPath... " dotnet tool install --tool-path $ToolPath --add-source $DevOpsFeed --version $ToolVersion "Azure.Sdk.Tools.RetrieveCodeOwners" | Out-Null From ecc3e7e52bbfa03a5723658b50aeb4e50f2d51df Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 13:01:49 -0800 Subject: [PATCH 10/37] typo --- eng/common/scripts/get-codeowners.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index b778c5d906a..3fe033e2906 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -3,7 +3,7 @@ param ( [string]$RootDirectory = "$env:SYSTEM_DEFAULTWORKINGDIRECTORY", # The repo contains CODEOWNER file. [string]$CodeOwnerFileLocation = "$PSSCriptRoot/../../../.github/CODEOWNERS", # The absolute path of CODEOWNERS file. [string]$ToolVersion = "1.0.0-dev.20211118.20", # Placeholder. Will update in next PR - [string]$ToolPath = (Join-Path [System.IO.Path]::GetTempPath(), "codeowners-tool-path"), # The place to check the tool existence. Put temp path as default + [string]$ToolPath = (Join-Path ([System.IO.Path]::GetTempPath()) "codeowners-tool-path"), # The place to check the tool existence. Put temp path as default [string]$DevOpsFeed = "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json", # DevOp tool feeds. [string]$VsoVariable = "", # Option of write code owners into devop variable [switch]$Test #Run test functions against the script logic From 5a547dc3fb04d82c33770110c8c6c236c4a552a2 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 13:09:17 -0800 Subject: [PATCH 11/37] correct exit on Test --- eng/common/scripts/get-codeowners.ps1 | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 3fe033e2906..4de543b005a 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -23,12 +23,13 @@ function Get-CodeOwnersTool() Write-Warning "Installing the ToolCommandName tool under $ToolPath... " dotnet tool install --tool-path $ToolPath --add-source $DevOpsFeed --version $ToolVersion "Azure.Sdk.Tools.RetrieveCodeOwners" | Out-Null + $command = Join-Path $ToolPath $ToolCommandName # Test to see if the tool properly installed. - if (!(Get-Command $Command -errorAction SilentlyContinue)) { + if (!(Get-Command $command -errorAction SilentlyContinue)) { Write-Error "The ToolCommandName tool is not properly installed. Please check your tool path. $ToolPath" return } - return Join-Path $ToolPath $ToolCommandName + return $command } function Get-CodeOwners ([string] $command) @@ -65,16 +66,17 @@ function Get-CodeOwners ([string] $command) function TestGetCodeOwner([string] $command) { if (!$command) { - exit + exit 1 } $actualReturn = GetCodeOwners -command $command $expectReturn = @("person1", "person2") for ($i = 0; $i -lt $expectReturn.Length; $i++) { if ($actualReturn[$i] -ne $expectReturn[$i]) { Write-Error "Expect result $expectReturn[$i] is different than actual result $actualReturn[$i]." - exit + exit 1 } } + exit 0 } # Install the tool first @@ -84,6 +86,5 @@ if($Test) { TestGetCodeOwner -command $output } else { - return GetCodeOwners -command $output } From 64cff3b3a7c4265da3f77502c847920baca11123 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 13:12:33 -0800 Subject: [PATCH 12/37] change to right function name --- eng/common/scripts/get-codeowners.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 4de543b005a..7697dd78777 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -68,7 +68,7 @@ function TestGetCodeOwner([string] $command) { if (!$command) { exit 1 } - $actualReturn = GetCodeOwners -command $command + $actualReturn = Get-CodeOwners -command $command $expectReturn = @("person1", "person2") for ($i = 0; $i -lt $expectReturn.Length; $i++) { if ($actualReturn[$i] -ne $expectReturn[$i]) { From 599f7499dd2a16c1421008e6a87ef892955bb9d7 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 13:16:03 -0800 Subject: [PATCH 13/37] Fixed log message --- eng/common/scripts/get-codeowners.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 7697dd78777..bb811dc6706 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -70,8 +70,12 @@ function TestGetCodeOwner([string] $command) { } $actualReturn = Get-CodeOwners -command $command $expectReturn = @("person1", "person2") + if ($actualReturn.Length -ne $expectReturn.Length) { + Write-Error "The length of actual result is not as expected. Expected length: $expectReturn.Length, Actual length: $actualReturn.Length." + exit 1 + } for ($i = 0; $i -lt $expectReturn.Length; $i++) { - if ($actualReturn[$i] -ne $expectReturn[$i]) { + if ($expectReturn[$i] -ne $actualReturn[$i]) { Write-Error "Expect result $expectReturn[$i] is different than actual result $actualReturn[$i]." exit 1 } From 019103774a75515bdeafeb8342aa3698fc4ba3b0 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 13:29:41 -0800 Subject: [PATCH 14/37] log message --- eng/common/scripts/get-codeowners.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index bb811dc6706..d79d841f33e 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -26,7 +26,7 @@ function Get-CodeOwnersTool() $command = Join-Path $ToolPath $ToolCommandName # Test to see if the tool properly installed. if (!(Get-Command $command -errorAction SilentlyContinue)) { - Write-Error "The ToolCommandName tool is not properly installed. Please check your tool path. $ToolPath" + Write-Error "The retrieve-codeowners tool is not properly installed. Please check your tool path. $ToolPath" return } return $command From b5e163fea462b8cbc9ffd0b1833f4ca52f73fbea Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 14:51:36 -0800 Subject: [PATCH 15/37] Added more test cases --- eng/common/scripts/get-codeowners.ps1 | 43 ++++++++++++--------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index d79d841f33e..851b9f3862f 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -13,9 +13,10 @@ $ToolCommandName = "retrieve-codeowners" function Get-CodeOwnersTool() { + $command = Join-Path $ToolPath $ToolCommandName # Check if the retrieve-codeowners tool exsits or not. - if (Get-Command "$ToolPath/$ToolCommandName" -errorAction SilentlyContinue) { - return "$ToolPath/$ToolCommandName" + if (Get-Command $command -errorAction SilentlyContinue) { + return $command } if (!(Test-Path $ToolPath)) { New-Item -ItemType Directory -Path $ToolPath | Out-Null @@ -23,7 +24,6 @@ function Get-CodeOwnersTool() Write-Warning "Installing the ToolCommandName tool under $ToolPath... " dotnet tool install --tool-path $ToolPath --add-source $DevOpsFeed --version $ToolVersion "Azure.Sdk.Tools.RetrieveCodeOwners" | Out-Null - $command = Join-Path $ToolPath $ToolCommandName # Test to see if the tool properly installed. if (!(Get-Command $command -errorAction SilentlyContinue)) { Write-Error "The retrieve-codeowners tool is not properly installed. Please check your tool path. $ToolPath" @@ -32,27 +32,25 @@ function Get-CodeOwnersTool() return $command } -function Get-CodeOwners ([string] $command) +function Get-CodeOwners ([string]$targetDirectory = $TargetDirectory, [string]$codeOwnerFileLocation = $CodeOwnerFileLocation) { - if (!$command) { - return @() - } + $command = Get-CodeOwnersTool # Params $RootDirectory is already in use in cpp release pipeline. - # Will use $CodeOwnerFileLocation and deprecate $RootDirectory once it is ready to retire $RootDirectory. - if ($RootDirectory -and !(Test-Path $CodeOwnerFileLocation)) { - $CodeOwnerFileLocation = Join-Path $RootDirectory ".github/CODEOWNERS" + # Will use $codeOwnerFileLocation and deprecate $RootDirectory once it is ready to retire $RootDirectory. + if ($RootDirectory -and !(Test-Path $codeOwnerFileLocation)) { + $codeOwnerFileLocation = Join-Path $RootDirectory ".github/CODEOWNERS" } - $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $CodeOwnerFileLocation + $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation # Failed at the command of fetching code owners. if ($LASTEXITCODE -ne 0) { - return @() + return ,@() } $codeOwnersJson = $codeOwnersString | ConvertFrom-Json if (!$codeOwnersJson) { Write-Host "No code owners returned from the path: $targetDirectory" - return @() + return ,@() } if ($VsoVariable) { @@ -63,13 +61,9 @@ function Get-CodeOwners ([string] $command) return $codeOwnersJson.Owners } +function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocation, [string[]]$expectReturn) { + $actualReturn = Get-CodeOwners -targetDirectory "sdk" -codeOwnerFileLocation $testFile -function TestGetCodeOwner([string] $command) { - if (!$command) { - exit 1 - } - $actualReturn = Get-CodeOwners -command $command - $expectReturn = @("person1", "person2") if ($actualReturn.Length -ne $expectReturn.Length) { Write-Error "The length of actual result is not as expected. Expected length: $expectReturn.Length, Actual length: $actualReturn.Length." exit 1 @@ -83,12 +77,13 @@ function TestGetCodeOwner([string] $command) { exit 0 } -# Install the tool first -$output = Get-CodeOwnersTool - if($Test) { - TestGetCodeOwner -command $output + $testFile = "$PSSCriptRoot\..\..\..\tools\code-owners-parser\Azure.Sdk.Tools.RetrieveCodeOwners.Tests\CODEOWNERS" + TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -expectResult @("person1", "person2") + TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -expectResult @("person1", "person2") + TestGetCodeOwner -targetDirectory "/sdk/azconfig/" -codeOwnerFileLocation $testFile -expectResult @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -expectResult @() } else { - return GetCodeOwners -command $output + return Get-CodeOwners } From 25d77786c502934b8eb54accf9c3427ff1890910 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 14:52:49 -0800 Subject: [PATCH 16/37] Correct the parameters --- eng/common/scripts/get-codeowners.ps1 | 2 +- tools/code-owners-parser/ci.yml | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 851b9f3862f..15768b20fc1 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -1,5 +1,5 @@ param ( - [string]$TargetDirectory, # Code path to code owners. e.g sdk/core/azure-amqp + [string]$TargetDirectory = "", # Code path to code owners. e.g sdk/core/azure-amqp [string]$RootDirectory = "$env:SYSTEM_DEFAULTWORKINGDIRECTORY", # The repo contains CODEOWNER file. [string]$CodeOwnerFileLocation = "$PSSCriptRoot/../../../.github/CODEOWNERS", # The absolute path of CODEOWNERS file. [string]$ToolVersion = "1.0.0-dev.20211118.20", # Placeholder. Will update in next PR diff --git a/tools/code-owners-parser/ci.yml b/tools/code-owners-parser/ci.yml index 0dc884bcd8b..5e23a01c7a0 100644 --- a/tools/code-owners-parser/ci.yml +++ b/tools/code-owners-parser/ci.yml @@ -33,6 +33,4 @@ extends: pwsh: true filePath: eng/common/scripts/get-codeowners.ps1 arguments: > - -TargetDirectory "sdk" - -CodeOwnerFileLocation "$(System.DefaultWorkingDirectory)/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" -Test From 05e053a2d8965b232cb1167d276cf1b61cc632bb Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Fri, 19 Nov 2021 21:44:04 -0800 Subject: [PATCH 17/37] Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard --- eng/common/scripts/get-codeowners.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 15768b20fc1..6eb23296b04 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -81,7 +81,8 @@ if($Test) { $testFile = "$PSSCriptRoot\..\..\..\tools\code-owners-parser\Azure.Sdk.Tools.RetrieveCodeOwners.Tests\CODEOWNERS" TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -expectResult @("person1", "person2") TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -expectResult @("person1", "person2") - TestGetCodeOwner -targetDirectory "/sdk/azconfig/" -codeOwnerFileLocation $testFile -expectResult @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -expectResult @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -expectResult @("person3", "person4") TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -expectResult @() } else { From f57247f55f073f568bd97162308eb094dc17fe0f Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Fri, 19 Nov 2021 21:45:00 -0800 Subject: [PATCH 18/37] Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard --- eng/common/scripts/get-codeowners.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 6eb23296b04..dccda99e6d4 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -13,7 +13,7 @@ $ToolCommandName = "retrieve-codeowners" function Get-CodeOwnersTool() { - $command = Join-Path $ToolPath $ToolCommandName + $command = Join-Path $ToolPath "retrieve-codeowners" # Check if the retrieve-codeowners tool exsits or not. if (Get-Command $command -errorAction SilentlyContinue) { return $command From 92fe01065662fa95d4b6f722b37b70e904103b30 Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Fri, 19 Nov 2021 21:45:12 -0800 Subject: [PATCH 19/37] Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard --- eng/common/scripts/get-codeowners.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index dccda99e6d4..f21e4bd5422 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -21,7 +21,7 @@ function Get-CodeOwnersTool() if (!(Test-Path $ToolPath)) { New-Item -ItemType Directory -Path $ToolPath | Out-Null } - Write-Warning "Installing the ToolCommandName tool under $ToolPath... " + Write-Host "Installing the retrieve-codeowners tool under $ToolPath... " dotnet tool install --tool-path $ToolPath --add-source $DevOpsFeed --version $ToolVersion "Azure.Sdk.Tools.RetrieveCodeOwners" | Out-Null # Test to see if the tool properly installed. From 20219f7bf80bcc913bec6657d89d73e31aea6b0e Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 21:46:38 -0800 Subject: [PATCH 20/37] Remove exit 0 in test function --- eng/common/scripts/get-codeowners.ps1 | 3 --- 1 file changed, 3 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index f21e4bd5422..a00f4e415f4 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -9,8 +9,6 @@ param ( [switch]$Test #Run test functions against the script logic ) -$ToolCommandName = "retrieve-codeowners" - function Get-CodeOwnersTool() { $command = Join-Path $ToolPath "retrieve-codeowners" @@ -74,7 +72,6 @@ function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocati exit 1 } } - exit 0 } if($Test) { From 2056b38f70a684903fefc21c12532cb38ab94692 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 21:56:22 -0800 Subject: [PATCH 21/37] change the path delimiter --- eng/common/scripts/get-codeowners.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index a00f4e415f4..b6dd6f204c2 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -75,7 +75,7 @@ function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocati } if($Test) { - $testFile = "$PSSCriptRoot\..\..\..\tools\code-owners-parser\Azure.Sdk.Tools.RetrieveCodeOwners.Tests\CODEOWNERS" + $testFile = "$PSSCriptRoot/../../../tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -expectResult @("person1", "person2") TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -expectResult @("person1", "person2") TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -expectResult @("person3", "person4") From fa2be8a6b645e9b351ba8e93537ea55038501a49 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 22:13:17 -0800 Subject: [PATCH 22/37] More changes on var --- eng/common/scripts/get-codeowners.ps1 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index b6dd6f204c2..927151bbea7 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -60,13 +60,13 @@ function Get-CodeOwners ([string]$targetDirectory = $TargetDirectory, [string]$c } function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocation, [string[]]$expectReturn) { - $actualReturn = Get-CodeOwners -targetDirectory "sdk" -codeOwnerFileLocation $testFile + $actualReturn = Get-CodeOwners -targetDirectory $targetDirectory -codeOwnerFileLocation $codeOwnerFileLocation - if ($actualReturn.Length -ne $expectReturn.Length) { - Write-Error "The length of actual result is not as expected. Expected length: $expectReturn.Length, Actual length: $actualReturn.Length." + if ($actualReturn.Count -ne $expectReturn.Count) { + Write-Error "The length of actual result is not as expected. Expected length: $($expectReturn.Count), Actual length: $($actualReturn.Count)." exit 1 } - for ($i = 0; $i -lt $expectReturn.Length; $i++) { + for ($i = 0; $i -lt $expectReturn.Count; $i++) { if ($expectReturn[$i] -ne $actualReturn[$i]) { Write-Error "Expect result $expectReturn[$i] is different than actual result $actualReturn[$i]." exit 1 @@ -76,11 +76,11 @@ function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocati if($Test) { $testFile = "$PSSCriptRoot/../../../tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" - TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -expectResult @("person1", "person2") - TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -expectResult @("person1", "person2") - TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -expectResult @("person3", "person4") - TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -expectResult @("person3", "person4") - TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -expectResult @() + TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") + TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") + TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -expectReturn @() } else { return Get-CodeOwners From 4e4b72053cf05643656958e7ece50c1b512d21d5 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 22:25:47 -0800 Subject: [PATCH 23/37] Hide error behind --- eng/common/scripts/get-codeowners.ps1 | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 927151bbea7..d0f20fb7587 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -39,9 +39,10 @@ function Get-CodeOwners ([string]$targetDirectory = $TargetDirectory, [string]$c $codeOwnerFileLocation = Join-Path $RootDirectory ".github/CODEOWNERS" } - $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation + $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation 2>&1 # Failed at the command of fetching code owners. if ($LASTEXITCODE -ne 0) { + Write-Host $codeOwnersString return ,@() } @@ -76,10 +77,10 @@ function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocati if($Test) { $testFile = "$PSSCriptRoot/../../../tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" - TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") - TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") - TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") - TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") + # TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") + # TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") + # TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") + # TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -expectReturn @() } else { From a027ec185deb815e91841f03f37d79225bff129a Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 22:27:47 -0800 Subject: [PATCH 24/37] Comment back all tests --- eng/common/scripts/get-codeowners.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index d0f20fb7587..872826cbd35 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -77,10 +77,10 @@ function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocati if($Test) { $testFile = "$PSSCriptRoot/../../../tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" - # TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") - # TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") - # TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") - # TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") + TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") + TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") + TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -expectReturn @() } else { From ab6a03a8c13ccc27671b7df7453bff85307a01d2 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Fri, 19 Nov 2021 22:34:58 -0800 Subject: [PATCH 25/37] exit 0 --- eng/common/scripts/get-codeowners.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 872826cbd35..37e690cbbf5 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -82,6 +82,7 @@ if($Test) { TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -expectReturn @() + exit 0 } else { return Get-CodeOwners From 19df1c0a23a6fa25b4067a1529cc75086ddddba4 Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Mon, 22 Nov 2021 09:24:39 -0800 Subject: [PATCH 26/37] Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard --- eng/common/scripts/get-codeowners.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 37e690cbbf5..32a41df5c5b 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -30,7 +30,7 @@ function Get-CodeOwnersTool() return $command } -function Get-CodeOwners ([string]$targetDirectory = $TargetDirectory, [string]$codeOwnerFileLocation = $CodeOwnerFileLocation) +function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocation) { $command = Get-CodeOwnersTool # Params $RootDirectory is already in use in cpp release pipeline. From 307ab9e6d04de0d839cbff4a9025860b1f6a5e10 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Mon, 22 Nov 2021 10:10:28 -0800 Subject: [PATCH 27/37] remove default value --- eng/common/scripts/get-codeowners.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 32a41df5c5b..92fc4fa1db9 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -85,5 +85,5 @@ if($Test) { exit 0 } else { - return Get-CodeOwners + return Get-CodeOwners -targetDirectory $TargetDirectory -codeOwnerFileLocation $CodeOwnerFileLocation } From 5e26fda3ebfcd06cc727da37191c169138581f2c Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Mon, 22 Nov 2021 10:41:55 -0800 Subject: [PATCH 28/37] changes for new get-codeowners --- eng/common/scripts/get-codeowners.ps1 | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 92fc4fa1db9..0cf5959eedb 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -1,11 +1,11 @@ param ( [string]$TargetDirectory = "", # Code path to code owners. e.g sdk/core/azure-amqp - [string]$RootDirectory = "$env:SYSTEM_DEFAULTWORKINGDIRECTORY", # The repo contains CODEOWNER file. [string]$CodeOwnerFileLocation = "$PSSCriptRoot/../../../.github/CODEOWNERS", # The absolute path of CODEOWNERS file. [string]$ToolVersion = "1.0.0-dev.20211118.20", # Placeholder. Will update in next PR [string]$ToolPath = (Join-Path ([System.IO.Path]::GetTempPath()) "codeowners-tool-path"), # The place to check the tool existence. Put temp path as default [string]$DevOpsFeed = "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json", # DevOp tool feeds. [string]$VsoVariable = "", # Option of write code owners into devop variable + [switch]$FilterNonUser, # Option to filter out the team alias in code owner list. e.g. Azure/azure-sdk-team [switch]$Test #Run test functions against the script logic ) @@ -33,13 +33,13 @@ function Get-CodeOwnersTool() function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocation) { $command = Get-CodeOwnersTool - # Params $RootDirectory is already in use in cpp release pipeline. - # Will use $codeOwnerFileLocation and deprecate $RootDirectory once it is ready to retire $RootDirectory. - if ($RootDirectory -and !(Test-Path $codeOwnerFileLocation)) { - $codeOwnerFileLocation = Join-Path $RootDirectory ".github/CODEOWNERS" + # Filter out the non user alias from code owner list. + if($FilterNonUser) { + $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation --filter-out-non-user-aliases 2>&1 + } + else { + $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation 2>&1 } - - $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation 2>&1 # Failed at the command of fetching code owners. if ($LASTEXITCODE -ne 0) { Write-Host $codeOwnersString From c3a51379187e8c0f369c4614828845b2d791024c Mon Sep 17 00:00:00 2001 From: Sima Zhu <48036328+sima-zhu@users.noreply.github.com> Date: Mon, 22 Nov 2021 11:14:28 -0800 Subject: [PATCH 29/37] Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard --- eng/common/scripts/get-codeowners.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 0cf5959eedb..35cbd102e9b 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -5,7 +5,7 @@ param ( [string]$ToolPath = (Join-Path ([System.IO.Path]::GetTempPath()) "codeowners-tool-path"), # The place to check the tool existence. Put temp path as default [string]$DevOpsFeed = "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json", # DevOp tool feeds. [string]$VsoVariable = "", # Option of write code owners into devop variable - [switch]$FilterNonUser, # Option to filter out the team alias in code owner list. e.g. Azure/azure-sdk-team + [switch]$IncludeNonUserAliases, # Option to filter out the team alias in code owner list. e.g. Azure/azure-sdk-team [switch]$Test #Run test functions against the script logic ) From 509d9f3cb1aa4d7790ffd9a4bc28fa1fe35d8865 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Mon, 22 Nov 2021 11:25:31 -0800 Subject: [PATCH 30/37] update version and parameter --- eng/common/scripts/get-codeowners.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 35cbd102e9b..d2038727148 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -1,7 +1,7 @@ param ( [string]$TargetDirectory = "", # Code path to code owners. e.g sdk/core/azure-amqp [string]$CodeOwnerFileLocation = "$PSSCriptRoot/../../../.github/CODEOWNERS", # The absolute path of CODEOWNERS file. - [string]$ToolVersion = "1.0.0-dev.20211118.20", # Placeholder. Will update in next PR + [string]$ToolVersion = "1.0.0-dev.20211122.14", # Placeholder. Will update in next PR [string]$ToolPath = (Join-Path ([System.IO.Path]::GetTempPath()) "codeowners-tool-path"), # The place to check the tool existence. Put temp path as default [string]$DevOpsFeed = "https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json", # DevOp tool feeds. [string]$VsoVariable = "", # Option of write code owners into devop variable @@ -34,11 +34,11 @@ function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocatio { $command = Get-CodeOwnersTool # Filter out the non user alias from code owner list. - if($FilterNonUser) { - $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation --filter-out-non-user-aliases 2>&1 + if($IncludeNonUserAliases) { + $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation 2>&1 } else { - $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation 2>&1 + $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation --filter-out-non-user-aliases 2>&1 } # Failed at the command of fetching code owners. if ($LASTEXITCODE -ne 0) { From e12595239b53422dbd4778f1f18f74a6267c0dc5 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Mon, 22 Nov 2021 12:06:20 -0800 Subject: [PATCH 31/37] add test on nonuser alias --- .../Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS b/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS index 47557d88aac..e1627b9095b 100644 --- a/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS +++ b/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS @@ -24,6 +24,9 @@ # PRLabel: %label /sdk/eventhubs/ @person7 @person8 +# This is for testing non user aliases case. +/sdk/testUser/ @Azure/azure-sdk-teams + # Example for service that does not have the code in the repo but wants issues to be labeled # Notice the use of the moniker // # ServiceLabel: %label %Service Attention From 98ea9c85ba830ac02b219737116de69f16727327 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Mon, 22 Nov 2021 12:09:19 -0800 Subject: [PATCH 32/37] add user --- eng/common/scripts/get-codeowners.ps1 | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index d2038727148..9b48cc7e912 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -30,11 +30,11 @@ function Get-CodeOwnersTool() return $command } -function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocation) +function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocation, [bool]$useNonUserAlias = $false) { $command = Get-CodeOwnersTool # Filter out the non user alias from code owner list. - if($IncludeNonUserAliases) { + if($useNonUserAlias) { $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation 2>&1 } else { @@ -60,8 +60,8 @@ function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocatio return $codeOwnersJson.Owners } -function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocation, [string[]]$expectReturn) { - $actualReturn = Get-CodeOwners -targetDirectory $targetDirectory -codeOwnerFileLocation $codeOwnerFileLocation +function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocation, [bool]$useNonUserAlias = $false, [string[]]$expectReturn) { + $actualReturn = Get-CodeOwners -targetDirectory $targetDirectory -codeOwnerFileLocation $codeOwnerFileLocation -useNonUserAlias $useNonUserAlias if ($actualReturn.Count -ne $expectReturn.Count) { Write-Error "The length of actual result is not as expected. Expected length: $($expectReturn.Count), Actual length: $($actualReturn.Count)." @@ -77,13 +77,14 @@ function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocati if($Test) { $testFile = "$PSSCriptRoot/../../../tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" - TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") - TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -expectReturn @("person1", "person2") - TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") - TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -expectReturn @("person3", "person4") - TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -expectReturn @() + TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile $useNonUserAlias $true -expectReturn @("person1", "person2") + TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile $useNonUserAlias $true -expectReturn @("person1", "person2") + TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile $useNonUserAlias $true -expectReturn @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $useNonUserAlias $true $testFile -expectReturn @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile $useNonUserAlias $true -expectReturn @() + TestGetCodeOwner -targetDirectory "/sdk/testUser/" -codeOwnerFileLocation $testFile -expectReturn @() exit 0 } else { - return Get-CodeOwners -targetDirectory $TargetDirectory -codeOwnerFileLocation $CodeOwnerFileLocation + return Get-CodeOwners -targetDirectory $TargetDirectory -codeOwnerFileLocation $CodeOwnerFileLocation -useNonUserAlias $IncludeNonUserAliases } From b7de54a88ad8fb0e8bc0673c60c3ad6673922ee2 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Mon, 22 Nov 2021 12:18:11 -0800 Subject: [PATCH 33/37] more fix --- eng/common/scripts/get-codeowners.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 9b48cc7e912..e917900c188 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -77,11 +77,11 @@ function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocati if($Test) { $testFile = "$PSSCriptRoot/../../../tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" - TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile $useNonUserAlias $true -expectReturn @("person1", "person2") - TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile $useNonUserAlias $true -expectReturn @("person1", "person2") - TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile $useNonUserAlias $true -expectReturn @("person3", "person4") - TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $useNonUserAlias $true $testFile -expectReturn @("person3", "person4") - TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile $useNonUserAlias $true -expectReturn @() + TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @("person1", "person2") + TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @("person1", "person2") + TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -useNonUserAlias $true $testFile -expectReturn @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @() TestGetCodeOwner -targetDirectory "/sdk/testUser/" -codeOwnerFileLocation $testFile -expectReturn @() exit 0 } From 5a62919fe39db7fc08810a7da97610bd4c13e9d1 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Mon, 22 Nov 2021 12:31:26 -0800 Subject: [PATCH 34/37] add messages --- eng/common/scripts/get-codeowners.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index e917900c188..1eec94eee2c 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -34,6 +34,7 @@ function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocatio { $command = Get-CodeOwnersTool # Filter out the non user alias from code owner list. + Write-Host "Testing on $targetDirectory..." if($useNonUserAlias) { $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation 2>&1 } From caa3cec34c6a335e888ba01f2a741210ad74e051 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Mon, 22 Nov 2021 14:06:28 -0800 Subject: [PATCH 35/37] Added real cases --- eng/common/scripts/get-codeowners.ps1 | 2 +- .../Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index 1eec94eee2c..d0aa8b23baa 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -83,7 +83,7 @@ if($Test) { TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @("person3", "person4") TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -useNonUserAlias $true $testFile -expectReturn @("person3", "person4") TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @() - TestGetCodeOwner -targetDirectory "/sdk/testUser/" -codeOwnerFileLocation $testFile -expectReturn @() + TestGetCodeOwner -targetDirectory "/sdk/testUser/" -codeOwnerFileLocation $testFile -expectReturn @("azuer-sdk") exit 0 } else { diff --git a/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS b/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS index e1627b9095b..d4a23d1bbb1 100644 --- a/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS +++ b/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS @@ -25,7 +25,7 @@ /sdk/eventhubs/ @person7 @person8 # This is for testing non user aliases case. -/sdk/testUser/ @Azure/azure-sdk-teams +/sdk/testUser/ @azure/azure-sdk-eng @azure-sdk # Example for service that does not have the code in the repo but wants issues to be labeled # Notice the use of the moniker // From e72f7c4e2aa37b3755886a4f9ab2ad93dc09145d Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Mon, 22 Nov 2021 14:13:09 -0800 Subject: [PATCH 36/37] fixed typo --- eng/common/scripts/get-codeowners.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index d0aa8b23baa..cd967691eae 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -83,7 +83,7 @@ if($Test) { TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @("person3", "person4") TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -useNonUserAlias $true $testFile -expectReturn @("person3", "person4") TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @() - TestGetCodeOwner -targetDirectory "/sdk/testUser/" -codeOwnerFileLocation $testFile -expectReturn @("azuer-sdk") + TestGetCodeOwner -targetDirectory "/sdk/testUser/" -codeOwnerFileLocation $testFile -expectReturn @("azure-sdk") exit 0 } else { From c2fa08b61262b3a8e43aa2edcec5ea15e0a671d1 Mon Sep 17 00:00:00 2001 From: sima-zhu Date: Mon, 22 Nov 2021 15:00:46 -0800 Subject: [PATCH 37/37] Make program work --- eng/common/scripts/get-codeowners.ps1 | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/eng/common/scripts/get-codeowners.ps1 b/eng/common/scripts/get-codeowners.ps1 index cd967691eae..dff01ecab60 100644 --- a/eng/common/scripts/get-codeowners.ps1 +++ b/eng/common/scripts/get-codeowners.ps1 @@ -30,12 +30,12 @@ function Get-CodeOwnersTool() return $command } -function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocation, [bool]$useNonUserAlias = $false) +function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocation, [bool]$includeNonUserAliases = $false) { $command = Get-CodeOwnersTool # Filter out the non user alias from code owner list. Write-Host "Testing on $targetDirectory..." - if($useNonUserAlias) { + if($includeNonUserAliases) { $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation 2>&1 } else { @@ -58,11 +58,11 @@ function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocatio Write-Host "##vso[task.setvariable variable=$VsoVariable;]$codeOwners" } - return $codeOwnersJson.Owners + return ,@($codeOwnersJson.Owners) } -function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocation, [bool]$useNonUserAlias = $false, [string[]]$expectReturn) { - $actualReturn = Get-CodeOwners -targetDirectory $targetDirectory -codeOwnerFileLocation $codeOwnerFileLocation -useNonUserAlias $useNonUserAlias +function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocation, [bool]$includeNonUserAliases = $false, [string[]]$expectReturn) { + $actualReturn = Get-CodeOwners -targetDirectory $targetDirectory -codeOwnerFileLocation $codeOwnerFileLocation -includeNonUserAliases $IncludeNonUserAliases if ($actualReturn.Count -ne $expectReturn.Count) { Write-Error "The length of actual result is not as expected. Expected length: $($expectReturn.Count), Actual length: $($actualReturn.Count)." @@ -78,14 +78,14 @@ function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocati if($Test) { $testFile = "$PSSCriptRoot/../../../tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" - TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @("person1", "person2") - TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @("person1", "person2") - TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @("person3", "person4") - TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -useNonUserAlias $true $testFile -expectReturn @("person3", "person4") - TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -useNonUserAlias $true -expectReturn @() + TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -includeNonUserAliases $true -expectReturn @("person1", "person2") + TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -includeNonUserAliases $true -expectReturn @("person1", "person2") + TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -includeNonUserAliases $true -expectReturn @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -includeNonUserAliases $true $testFile -expectReturn @("person3", "person4") + TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -includeNonUserAliases $true -expectReturn @() TestGetCodeOwner -targetDirectory "/sdk/testUser/" -codeOwnerFileLocation $testFile -expectReturn @("azure-sdk") exit 0 } else { - return Get-CodeOwners -targetDirectory $TargetDirectory -codeOwnerFileLocation $CodeOwnerFileLocation -useNonUserAlias $IncludeNonUserAliases + return Get-CodeOwners -targetDirectory $TargetDirectory -codeOwnerFileLocation $CodeOwnerFileLocation -includeNonUserAliases $IncludeNonUserAliases }