...

Text file src/github.com/Azure/azure-sdk-for-go/eng/common/scripts/get-codeowners.ps1

Documentation: github.com/Azure/azure-sdk-for-go/eng/common/scripts

     1param (
     2  [string]$TargetDirectory = "", # Code path to code owners. e.g sdk/core/azure-amqp
     3  [string]$CodeOwnerFileLocation = (Resolve-Path $PSScriptRoot/../../../.github/CODEOWNERS), # The absolute path of CODEOWNERS file. 
     4  [string]$ToolVersion = "1.0.0-dev.20220121.1", 
     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
    10)
    11
    12function 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
    24  # Run command under tool path to avoid dotnet tool install command checking .csproj files. 
    25  # This is a bug for dotnet tool command. Issue: https://github.com/dotnet/sdk/issues/9623
    26  Push-Location $ToolPath
    27  dotnet tool install --tool-path $ToolPath --add-source $DevOpsFeed --version $ToolVersion "Azure.Sdk.Tools.RetrieveCodeOwners" | Out-Null
    28  Pop-Location
    29  # Test to see if the tool properly installed.
    30  if (!(Get-Command $command -errorAction SilentlyContinue)) {
    31    Write-Error "The retrieve-codeowners tool is not properly installed. Please check your tool path. $ToolPath"
    32    return 
    33  }
    34  return $command
    35}
    36
    37function Get-CodeOwners ([string]$targetDirectory, [string]$codeOwnerFileLocation, [bool]$includeNonUserAliases = $false)
    38{
    39  $command = Get-CodeOwnersTool
    40  # Filter out the non user alias from code owner list.
    41  if($includeNonUserAliases) {
    42    $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation 2>&1
    43  }
    44  else {
    45    $codeOwnersString = & $command --target-directory $targetDirectory --code-owner-file-path $codeOwnerFileLocation --filter-out-non-user-aliases 2>&1
    46  }
    47  # Failed at the command of fetching code owners.
    48  if ($LASTEXITCODE -ne 0) {
    49    Write-Host $codeOwnersString
    50    return ,@()
    51  }
    52  
    53  $codeOwnersJson = $codeOwnersString | ConvertFrom-Json
    54  if (!$codeOwnersJson) {
    55    Write-Host "No code owners returned from the path: $targetDirectory"
    56    return ,@()
    57  }
    58  
    59  if ($VsoVariable) {
    60    $codeOwners = $codeOwnersJson.Owners -join ","
    61    Write-Host "##vso[task.setvariable variable=$VsoVariable;]$codeOwners"
    62  }
    63
    64  return ,@($codeOwnersJson.Owners)
    65}
    66
    67function TestGetCodeOwner([string]$targetDirectory, [string]$codeOwnerFileLocation, [bool]$includeNonUserAliases = $false, [string[]]$expectReturn) {
    68  Write-Host "Testing on $targetDirectory..."
    69  $actualReturn = Get-CodeOwners -targetDirectory $targetDirectory -codeOwnerFileLocation $codeOwnerFileLocation -includeNonUserAliases $IncludeNonUserAliases
    70
    71  if ($actualReturn.Count -ne $expectReturn.Count) {
    72    Write-Error "The length of actual result is not as expected. Expected length: $($expectReturn.Count), Actual length: $($actualReturn.Count)."
    73    exit 1
    74  }
    75  for ($i = 0; $i -lt $expectReturn.Count; $i++) {
    76    if ($expectReturn[$i] -ne $actualReturn[$i]) {
    77      Write-Error "Expect result $expectReturn[$i] is different than actual result $actualReturn[$i]."
    78      exit 1
    79    }
    80  }
    81}
    82
    83if($Test) {
    84  $testFile = (Resolve-Path $PSScriptRoot/../../../tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS)
    85  TestGetCodeOwner -targetDirectory "sdk" -codeOwnerFileLocation $testFile -includeNonUserAliases $true -expectReturn @("person1", "person2")
    86  TestGetCodeOwner -targetDirectory "sdk/noPath" -codeOwnerFileLocation $testFile -includeNonUserAliases $true -expectReturn @("person1", "person2")
    87  TestGetCodeOwner -targetDirectory "/sdk/azconfig" -codeOwnerFileLocation $testFile -includeNonUserAliases $true -expectReturn @("person3", "person4")
    88  TestGetCodeOwner -targetDirectory "/sdk/azconfig/package" -codeOwnerFileLocation $testFile -includeNonUserAliases $true  $testFile -expectReturn @("person3", "person4")
    89  TestGetCodeOwner -targetDirectory "/sd" -codeOwnerFileLocation $testFile -includeNonUserAliases $true  -expectReturn @()
    90  TestGetCodeOwner -targetDirectory "/sdk/testUser/" -codeOwnerFileLocation $testFile -expectReturn @("azure-sdk") 
    91  exit 0
    92}
    93else {
    94  return Get-CodeOwners -targetDirectory $TargetDirectory -codeOwnerFileLocation $CodeOwnerFileLocation -includeNonUserAliases $IncludeNonUserAliases
    95}

View as plain text