Skip to content

Commit eb96378

Browse files
azure-sdksima-zhuweshaggard
authored
Sync eng/common directory with azure-sdk-tools for PR 2322 (Azure#21851)
* Install and Run code owner tools in get-codeowner.ps1 * return command when exists * Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard <[email protected]> * Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard <[email protected]> * Address comments * Added test to script * change return type * change wrong params * default to toolpath * typo * correct exit on Test * change to right function name * Fixed log message * log message * Added more test cases * Correct the parameters * Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard <[email protected]> * Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard <[email protected]> * Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard <[email protected]> * Remove exit 0 in test function * change the path delimiter * More changes on var * Hide error behind * Comment back all tests * exit 0 * Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard <[email protected]> * remove default value * changes for new get-codeowners * Update eng/common/scripts/get-codeowners.ps1 Co-authored-by: Wes Haggard <[email protected]> * update version and parameter * add user * more fix * add messages * Added real cases * fixed typo * Make program work Co-authored-by: sima-zhu <[email protected]> Co-authored-by: Sima Zhu <[email protected]> Co-authored-by: Wes Haggard <[email protected]>
1 parent 08efbe2 commit eb96378

File tree

1 file changed

+75
-33
lines changed

1 file changed

+75
-33
lines changed

eng/common/scripts/get-codeowners.ps1

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,91 @@
11
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
510
)
6-
$target = $TargetDirectory.ToLower().Trim("/")
7-
$codeOwnersLocation = Join-Path $RootDirectory -ChildPath ".github/CODEOWNERS"
8-
$ownedFolders = @{}
911

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
1624

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
2629
}
30+
return $command
2731
}
2832

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+
}
3355

3456
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
3666

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
3975
}
40-
Write-Host "##vso[task.setvariable variable=$VsoVariable;]$results"
4176
}
77+
}
4278

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
4488
}
4589
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
4991
}

0 commit comments

Comments
 (0)