1
+ function Get-CodeownersTool ([string ] $ToolPath , [string ] $DevOpsFeed , [string ] $ToolVersion )
2
+ {
3
+ $codeownersToolCommand = Join-Path $ToolPath " retrieve-codeowners"
4
+ # Check if the retrieve-codeowners tool exists or not.
5
+ if (Get-Command $codeownersToolCommand - errorAction SilentlyContinue) {
6
+ return $codeownersToolCommand
7
+ }
8
+ if (! (Test-Path $ToolPath )) {
9
+ New-Item - ItemType Directory - Path $ToolPath | Out-Null
10
+ }
11
+ Write-Host " Installing the retrieve-codeowners tool under tool path: $ToolPath ..."
12
+
13
+ # Run command under tool path to avoid dotnet tool install command checking .csproj files.
14
+ # This is a bug for dotnet tool command. Issue: https://github.com/dotnet/sdk/issues/9623
15
+ Push-Location $ToolPath
16
+ dotnet tool install -- tool- path $ToolPath -- add-source $DevOpsFeed -- version $ToolVersion " Azure.Sdk.Tools.RetrieveCodeOwners" | Out-Null
17
+ Pop-Location
18
+ # Test to see if the tool properly installed.
19
+ if (! (Get-Command $codeownersToolCommand - errorAction SilentlyContinue)) {
20
+ Write-Error " The retrieve-codeowners tool is not properly installed. Please check your tool path: $ToolPath "
21
+ return
22
+ }
23
+ return $codeownersToolCommand
24
+ }
25
+
26
+ <#
27
+ . SYNOPSIS
28
+ A function that given as input $TargetPath param, returns the owners
29
+ of that path, as determined by CODEOWNERS file passed in $CodeownersFileLocation
30
+ param.
31
+
32
+ . PARAMETER TargetPath
33
+ Required*. Path to file or directory whose owners are to be determined from a
34
+ CODEOWNERS file. e.g. sdk/core/azure-amqp/ or sdk/core/foo.txt.
35
+
36
+ *for backward compatibility, you might provide $TargetDirectory instead.
37
+
38
+ . PARAMETER TargetDirectory
39
+ Obsolete. Replaced by $TargetPath. Kept for backward-compatibility.
40
+ If both $TargetPath and $TargetDirectory are provided, $TargetDirectory is
41
+ ignored.
42
+
43
+ . PARAMETER CodeownersFileLocation
44
+ Optional. An absolute path to the CODEOWNERS file against which the $TargetPath param
45
+ will be checked to determine its owners.
46
+
47
+ . PARAMETER ToolVersion
48
+ Optional. The NuGet package version of the package containing the "retrieve-codeowners"
49
+ tool, around which this script is a wrapper.
50
+
51
+ . PARAMETER ToolPath
52
+ Optional. The place to check the "retrieve-codeowners" tool existence.
53
+
54
+ . PARAMETER DevOpsFeed
55
+ Optional. The NuGet package feed from which the "retrieve-codeowners" tool is to be installed.
56
+
57
+ NuGet feed:
58
+ https://dev.azure.com/azure-sdk/public/_artifacts/feed/azure-sdk-for-net/NuGet/Azure.Sdk.Tools.RetrieveCodeOwners
59
+
60
+ Pipeline publishing the NuGet package to the feed, "tools - code-owners-parser":
61
+ https://dev.azure.com/azure-sdk/internal/_build?definitionId=3188
62
+
63
+ . PARAMETER VsoVariable
64
+ Optional. If provided, the determined owners, based on $TargetPath matched against CODEOWNERS file at $CodeownersFileLocation,
65
+ will be output to Azure DevOps pipeline log as variable named $VsoVariable.
66
+
67
+ Reference:
68
+ https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch
69
+ https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=bash#logging-command-format
70
+
71
+ . PARAMETER IncludeNonUserAliases
72
+ Optional. Whether to include in the returned owners list aliases that are team aliases, e.g. Azure/azure-sdk-team
73
+
74
+ . PARAMETER Test
75
+ Optional. Whether to run the script against hard-coded tests.
76
+
77
+ #>
78
+ function Get-Codeowners (
79
+ [string ] $TargetPath ,
80
+ [string ] $TargetDirectory ,
81
+ [string ] $ToolPath = (Join-Path ([System.IO.Path ]::GetTempPath()) " codeowners-tool" ),
82
+ [string ] $DevOpsFeed = " https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json" ,
83
+ [string ] $ToolVersion = " 1.0.0-dev.20230306.3" ,
84
+ [string ] $VsoVariable = " " ,
85
+ [string ] $CodeownersFileLocation = " " ,
86
+ [switch ] $IncludeNonUserAliases
87
+ )
88
+ {
89
+ if ([string ]::IsNullOrWhiteSpace($CodeownersFileLocation )) {
90
+ # The $PSScriptRoot is assumed to be azure-sdk-tools/eng/common/scripts/get-codeowners.ps1
91
+ $CodeownersFileLocation = (Resolve-Path $PSScriptRoot / ../ ../ ../ .github/ CODEOWNERS)
92
+ }
93
+
94
+ # Backward compatibility: if $TargetPath is not provided, fall-back to the legacy $TargetDirectory
95
+ if ([string ]::IsNullOrWhiteSpace($TargetPath )) {
96
+ $TargetPath = $TargetDirectory
97
+ }
98
+ if ([string ]::IsNullOrWhiteSpace($TargetPath )) {
99
+ Write-Error " TargetPath (or TargetDirectory) parameter must be neither null nor whitespace."
100
+ return , @ ()
101
+ }
102
+
103
+ $codeownersToolCommand = Get-CodeownersTool - ToolPath $ToolPath - DevOpsFeed $DevOpsFeed - ToolVersion $ToolVersion
104
+ Write-Host " Executing: & $codeownersToolCommand --target-path $TargetPath --codeowners-file-path-or-url $CodeownersFileLocation --exclude-non-user-aliases:$ ( ! $IncludeNonUserAliases ) "
105
+ $commandOutput = & $codeownersToolCommand `
106
+ -- target- path $TargetPath `
107
+ -- codeowners- file- path- or- url $CodeownersFileLocation `
108
+ -- exclude- non- user- aliases:$ (! $IncludeNonUserAliases ) `
109
+ 2>&1
110
+
111
+ if ($LASTEXITCODE -ne 0 ) {
112
+ Write-Host " Command $codeownersToolCommand execution failed (exit code = $LASTEXITCODE ). Output string: $commandOutput "
113
+ return , @ ()
114
+ } else
115
+ {
116
+ Write-Host " Command $codeownersToolCommand executed successfully (exit code = 0). Command output string length: $ ( $commandOutput.length ) "
117
+ }
118
+
119
+ # Assert: $commandOutput is a valid JSON representing:
120
+ # - a single CodeownersEntry, if the $TargetPath was a single path
121
+ # - or a dictionary of CodeownerEntries, keyes by each path resolved from a $TargetPath glob path.
122
+ #
123
+ # For implementation details, see Azure.Sdk.Tools.RetrieveCodeOwners.Program.Main
124
+
125
+ $codeownersJson = $commandOutput | ConvertFrom-Json
126
+
127
+ if ($VsoVariable ) {
128
+ $codeowners = $codeownersJson.Owners -join " ,"
129
+ Write-Host " ##vso[task.setvariable variable=$VsoVariable ;]$codeowners "
130
+ }
131
+
132
+ return , @ ($codeownersJson.Owners )
133
+ }
0 commit comments