Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install and Run code owner tools in get-codeowner.ps1 #2322

Merged
38 commits merged into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
fd12992
Install and Run code owner tools in get-codeowner.ps1
sima-zhu Nov 19, 2021
5f6b723
return command when exists
sima-zhu Nov 19, 2021
c454055
Update eng/common/scripts/get-codeowners.ps1
sima-zhu Nov 19, 2021
f1767a2
Update eng/common/scripts/get-codeowners.ps1
sima-zhu Nov 19, 2021
1cc91a5
Address comments
sima-zhu Nov 19, 2021
1be01e1
Added test to script
sima-zhu Nov 19, 2021
03633d1
change return type
sima-zhu Nov 19, 2021
f840873
change wrong params
sima-zhu Nov 19, 2021
f67e382
default to toolpath
sima-zhu Nov 19, 2021
ecc3e7e
typo
sima-zhu Nov 19, 2021
5a547dc
correct exit on Test
sima-zhu Nov 19, 2021
64cff3b
change to right function name
sima-zhu Nov 19, 2021
599f749
Fixed log message
sima-zhu Nov 19, 2021
0191037
log message
sima-zhu Nov 19, 2021
b5e163f
Added more test cases
sima-zhu Nov 19, 2021
25d7778
Correct the parameters
sima-zhu Nov 19, 2021
05e053a
Update eng/common/scripts/get-codeowners.ps1
sima-zhu Nov 20, 2021
f57247f
Update eng/common/scripts/get-codeowners.ps1
sima-zhu Nov 20, 2021
92fe010
Update eng/common/scripts/get-codeowners.ps1
sima-zhu Nov 20, 2021
20219f7
Remove exit 0 in test function
sima-zhu Nov 20, 2021
2056b38
change the path delimiter
sima-zhu Nov 20, 2021
fa2be8a
More changes on var
sima-zhu Nov 20, 2021
4e4b720
Hide error behind
sima-zhu Nov 20, 2021
a027ec1
Comment back all tests
sima-zhu Nov 20, 2021
ab6a03a
exit 0
sima-zhu Nov 20, 2021
19df1c0
Update eng/common/scripts/get-codeowners.ps1
sima-zhu Nov 22, 2021
307ab9e
remove default value
sima-zhu Nov 22, 2021
5e26fda
changes for new get-codeowners
sima-zhu Nov 22, 2021
c3a5137
Update eng/common/scripts/get-codeowners.ps1
sima-zhu Nov 22, 2021
509d9f3
update version and parameter
sima-zhu Nov 22, 2021
e125952
add test on nonuser alias
sima-zhu Nov 22, 2021
98ea9c8
add user
sima-zhu Nov 22, 2021
1732c84
Merge branch 'main' of https://github.com/Azure/azure-sdk-tools into …
sima-zhu Nov 22, 2021
b7de54a
more fix
sima-zhu Nov 22, 2021
5a62919
add messages
sima-zhu Nov 22, 2021
caa3cec
Added real cases
sima-zhu Nov 22, 2021
e72f7c4
fixed typo
sima-zhu Nov 22, 2021
c2fa08b
Make program work
sima-zhu Nov 22, 2021
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
108 changes: 75 additions & 33 deletions eng/common/scripts/get-codeowners.ps1
Original file line number Diff line number Diff line change
@@ -1,49 +1,91 @@
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]$CodeOwnerFileLocation = "$PSSCriptRoot/../../../.github/CODEOWNERS", # The absolute path of CODEOWNERS file.
[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
[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
)
$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
function Get-CodeOwnersTool()
{
$command = Join-Path $ToolPath "retrieve-codeowners"
# Check if the retrieve-codeowners tool exsits or not.
if (Get-Command $command -errorAction SilentlyContinue) {
return $command
}
if (!(Test-Path $ToolPath)) {
New-Item -ItemType Directory -Path $ToolPath | Out-Null
}
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

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 ","
# 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"
return
}
return $command
}

$results = $ownedFolders[$target]

if ($results) {
Write-Host "Found a folder $results to match $target"
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($includeNonUserAliases) {
$codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation 2>&1
}
else {
$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) {
Write-Host $codeOwnersString
return ,@()
}

$codeOwnersJson = $codeOwnersString | ConvertFrom-Json
if (!$codeOwnersJson) {
Write-Host "No code owners returned from the path: $targetDirectory"
return ,@()
}

if ($VsoVariable) {
$alreadyPresent = [System.Environment]::GetEnvironmentVariable($VsoVariable)
$codeOwners = $codeOwnersJson.Owners -join ","
Write-Host "##vso[task.setvariable variable=$VsoVariable;]$codeOwners"
}

return ,@($codeOwnersJson.Owners)
}
weshaggard marked this conversation as resolved.
Show resolved Hide resolved

function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocation, [bool]$includeNonUserAliases = $false, [string[]]$expectReturn) {
$actualReturn = Get-CodeOwners -targetDirectory $targetDirectory -codeOwnerFileLocation $codeOwnerFileLocation -includeNonUserAliases $IncludeNonUserAliases

if ($alreadyPresent) {
$results += ",$alreadyPresent"
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.Count; $i++) {
if ($expectReturn[$i] -ne $actualReturn[$i]) {
Write-Error "Expect result $expectReturn[$i] is different than actual result $actualReturn[$i]."
exit 1
}
Write-Host "##vso[task.setvariable variable=$VsoVariable;]$results"
}
}

return $results
if($Test) {
$testFile = "$PSSCriptRoot/../../../tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS"
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 {
Write-Host "Unable to match path $target in CODEOWNERS file located at $codeOwnersLocation."
Write-Host ($ownedFolders | ConvertTo-Json)
return ""
return Get-CodeOwners -targetDirectory $TargetDirectory -codeOwnerFileLocation $CodeOwnerFileLocation -includeNonUserAliases $IncludeNonUserAliases
}
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
# PRLabel: %label
/sdk/eventhubs/ @person7 @person8

# This is for testing non user aliases case.
/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 /<NotInRepo>/
# ServiceLabel: %label %Service Attention
/<NotInRepo>/ @person7 @person8
/<NotInRepo>/ @person7 @person8
13 changes: 7 additions & 6 deletions tools/code-owners-parser/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ 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
- task: PowerShell@2
displayName: Test on code owner tool output
inputs:
pwsh: true
filePath: eng/common/scripts/get-codeowners.ps1
sima-zhu marked this conversation as resolved.
Show resolved Hide resolved
arguments: >
-Test