Skip to content

Commit d0dc9e9

Browse files
azure-sdkKonrad Jamrozik
andauthored
Sync eng/common directory with azure-sdk-tools for PR 5608 (Azure#20348)
* ongoing * Move get-codeowners scripts and tests to their own dirs * address PR remarks * Fix CodeOwnerFileLocation path, fix casing, and dedup param defaults * fix param names * add todos on needed changes in cpp repo * Add CodeownersFileLocation to Get-Codeowners and use $null for default param values * move get-codeowners back to scripts/ and rename -functions to .lib * fix: use empty string as defaults instead of $nulls, to fix invocation * fix bug with invocation of Get-Codeowners + add support for passing IncludeNonUserAliases as switch * fix path iin Metadata-Helpers.ps1 * fix typo --------- Co-authored-by: Konrad Jamrozik <[email protected]>
1 parent d967e0f commit d0dc9e9

File tree

4 files changed

+162
-185
lines changed

4 files changed

+162
-185
lines changed

eng/common/pipelines/templates/stages/archetype-sdk-tool-pwsh.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@ variables:
1515
- template: /eng/pipelines/templates/variables/globals.yml
1616

1717
stages:
18-
- stage: 'eng_script_tests'
18+
- stage:
19+
displayName: Run PowerShell Tests
1920
jobs:
20-
- job: 'Test'
21+
- job: Test
2122
strategy:
2223
matrix:
2324
Windows:
24-
Pool: 'azsdk-pool-mms-win-2022-general'
25-
Image: 'windows-2022'
25+
Pool: azsdk-pool-mms-win-2022-general
26+
Image: windows-2022
2627
Linux:
2728
Pool: azsdk-pool-mms-ubuntu-2204-general
2829
Image: MMSUbuntu22.04
2930
Mac:
30-
Pool: 'Azure Pipelines'
31-
Image: 'macos-11'
31+
Pool: Azure Pipelines
32+
Image: macos-11
3233

3334
pool:
3435
name: $(Pool)

eng/common/pipelines/templates/steps/run-pester-tests.yml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ parameters:
1313

1414
steps:
1515
- pwsh: |
16-
Install-Module -Name Pester -Force
16+
Install-Module -Name Pester -Force
1717
displayName: Install Pester
1818
1919
# default test steps
@@ -30,26 +30,28 @@ steps:
3030
$config.Filter.Tag = $tags
3131
}
3232
33+
Write-Host "Calling 'Invoke-Pester' in workingDirectory '$(Build.SourcesDirectory)/${{ parameters.TargetDirectory }}'. Pester tags filter: '$tags'"
34+
# https://pester.dev/docs/commands/Invoke-Pester
3335
Invoke-Pester -Configuration $config
34-
displayName: Run Tests
36+
displayName: Run Pester Tests
3537
env: ${{ parameters.EnvVars }}
3638
workingDirectory: $(Build.SourcesDirectory)/${{ parameters.TargetDirectory }}
3739
3840
- ${{ if not(eq(length(parameters.CustomTestSteps), 0)) }}:
3941
- ${{ parameters.CustomTestSteps }}
4042

4143
- task: PublishTestResults@2
42-
displayName: 'Publish Test Results'
44+
displayName: Publish Test Results
4345
condition: succeededOrFailed()
4446
inputs:
45-
testResultsFormat: 'NUnit'
47+
testResultsFormat: NUnit
4648
testResultsFiles: $(Build.SourcesDirectory)/${{ parameters.TargetDirectory }}/testResults.xml
47-
testRunTitle: '$(System.StageName)_$(Agent.JobName)_Tests'
49+
testRunTitle: $(System.StageName)_$(Agent.JobName)_Tests
4850

4951
- task: PublishCodeCoverageResults@1
50-
displayName: 'Publish Code Coverage to Azure DevOps'
52+
displayName: Publish Code Coverage to Azure DevOps
5153
condition: succeededOrFailed()
5254
inputs:
53-
codeCoverageTool: 'JaCoCo'
54-
summaryFileLocation: '$(Build.SourcesDirectory)/${{ parameters.TargetDirectory }}/coverage.xml'
55-
pathToSources: '$(Build.SourcesDirectory)/${{ parameters.TargetDirectory }}'
55+
codeCoverageTool: JaCoCo
56+
summaryFileLocation: $(Build.SourcesDirectory)/${{ parameters.TargetDirectory }}/coverage.xml
57+
pathToSources: $(Build.SourcesDirectory)/${{ parameters.TargetDirectory }}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)