Skip to content

Commit c050c31

Browse files
authored
Merge branch 'main' into fix-onpremauth-changes-clean
2 parents 66f57ed + a29b75b commit c050c31

99 files changed

Lines changed: 12579 additions & 715 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.build/Build.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ New-Item -Path $distFolder -ItemType Directory | Out-Null
4343
are referenced.
4444
#>
4545

46-
$excludedFolders = @(".build", "dist")
46+
$excludedFolders = @(".build", "dist", ".github")
4747

4848
$allScriptFiles = Get-ChildItem -Path $repoRoot -Directory |
4949
Where-Object { -not $excludedFolders.Contains($_.Name) } |

.build/CodeFormatter.ps1

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,9 @@ param(
1313

1414
Set-StrictMode -Version Latest
1515

16-
. $PSScriptRoot\Load-Module.ps1
17-
. $PSScriptRoot\CodeFormatterChecks\CheckContainsCurlyQuotes.ps1
18-
. $PSScriptRoot\CodeFormatterChecks\CheckFileHasNewlineAtEndOfFile.ps1
19-
. $PSScriptRoot\CodeFormatterChecks\CheckMarkdownFileHasNoBOM.ps1
20-
. $PSScriptRoot\CodeFormatterChecks\CheckMultipleEmptyLines.ps1
21-
. $PSScriptRoot\CodeFormatterChecks\CheckScriptFileHasBOM.ps1
22-
. $PSScriptRoot\CodeFormatterChecks\CheckScriptFileHasComplianceHeader.ps1
23-
. $PSScriptRoot\CodeFormatterChecks\CheckScriptFormat.ps1
24-
. $PSScriptRoot\CodeFormatterChecks\CheckTokenTypeCasing.ps1
16+
. $PSScriptRoot\Invoke-CodeFormatterOnFiles.ps1
2517
. $PSScriptRoot\HelpFunctions\Get-CommitFilesOnBranch.ps1
2618

27-
if (-not (Load-Module -Name PSScriptAnalyzer -MinimumVersion "1.24")) {
28-
throw "PSScriptAnalyzer module could not be loaded"
29-
}
30-
31-
if (-not (Load-Module -Name EncodingAnalyzer)) {
32-
throw "EncodingAnalyzer module could not be loaded"
33-
}
34-
3519
$repoRoot = Get-Item "$PSScriptRoot\.."
3620

3721
$optimizeCodeFormatter = [string]::IsNullOrEmpty($Branch) -eq $false
@@ -61,61 +45,7 @@ if ($optimizeCodeFormatter) {
6145
}
6246
}
6347

64-
$errorCount = 0
65-
66-
foreach ($fileInfo in $filesToCheck) {
67-
$errorCount += (CheckFileHasNewlineAtEndOfFile $fileInfo $Save) ? 1 : 0
68-
$errorCount += (CheckMarkdownFileHasNoBOM $fileInfo $Save) ? 1 : 0
69-
$errorCount += (CheckScriptFileHasBOM $fileInfo $Save) ? 1 : 0
70-
$errorCount += (CheckScriptFileHasComplianceHeader $fileInfo $Save) ? 1 : 0
71-
$errorCount += (CheckTokenTypeCasing $fileInfo $Save "Keyword") ? 1 : 0
72-
$errorCount += (CheckTokenTypeCasing $fileInfo $Save "Operator") ? 1 : 0
73-
$errorCount += (CheckMultipleEmptyLines $fileInfo $Save) ? 1 : 0
74-
$errorCount += (CheckContainsCurlyQuotes $fileInfo $Save) ? 1 : 0
75-
76-
# This one is tricky. It returns $true or $false like the others, but in the case
77-
# of an error, we also want to get the diff output. Piping to Out-Host from within
78-
# the function loses the colorization, as does redirection. I can't find any way
79-
# for the function to output the diff while preserving the color. So we unfortunately
80-
# have to handle the output here.
81-
$results = @(CheckScriptFormat $fileInfo $Save)
82-
if ($results.Length -gt 0 -and $results[0] -eq $true) {
83-
$errorCount++
84-
if ($results.Length -gt 2) {
85-
git -c color.status=always diff ($($results[1]) | git hash-object -w --stdin) ($($results[2]) | git hash-object -w --stdin)
86-
}
87-
}
88-
}
89-
90-
$maxRetries = 5
91-
92-
foreach ($fileInfo in $filesToCheck) {
93-
for ($i = 0; $i -lt $maxRetries; $i++) {
94-
try {
95-
$params = @{
96-
Path = ($fileInfo.FullName)
97-
Settings = "$repoRoot\PSScriptAnalyzerSettings.psd1"
98-
CustomRulePath = "$repoRoot\.build\CodeFormatterChecks\CustomRules.psm1"
99-
IncludeDefaultRules = $true
100-
}
101-
$analyzerResults = Invoke-ScriptAnalyzer @params
102-
if ($null -ne $analyzerResults) {
103-
$errorCount++
104-
$analyzerResults | Format-Table -AutoSize
105-
}
106-
break
107-
} catch {
108-
Write-Warning "Invoke-ScriptAnalyzer failed on $($fileInfo.FullName). Error:"
109-
$_.Exception | Format-List | Out-Host
110-
Write-Warning "Retrying in 5 seconds."
111-
Start-Sleep -Seconds 5
112-
}
113-
}
114-
115-
if ($i -eq $maxRetries) {
116-
throw "Invoke-ScriptAnalyzer failed $maxRetries times. Giving up."
117-
}
118-
}
48+
$errorCount = Invoke-CodeFormatterOnFiles -FilePaths ($filesToCheck | ForEach-Object { $_.FullName }) -Save:$Save
11949

12050
if ($errorCount -gt 0) {
12151
throw "Failed to match formatting requirements"
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
#Requires -Version 7
5+
6+
. $PSScriptRoot\Load-Module.ps1
7+
. $PSScriptRoot\CodeFormatterChecks\CheckContainsCurlyQuotes.ps1
8+
. $PSScriptRoot\CodeFormatterChecks\CheckFileHasNewlineAtEndOfFile.ps1
9+
. $PSScriptRoot\CodeFormatterChecks\CheckMarkdownFileHasNoBOM.ps1
10+
. $PSScriptRoot\CodeFormatterChecks\CheckMultipleEmptyLines.ps1
11+
. $PSScriptRoot\CodeFormatterChecks\CheckScriptFileHasBOM.ps1
12+
. $PSScriptRoot\CodeFormatterChecks\CheckScriptFileHasComplianceHeader.ps1
13+
. $PSScriptRoot\CodeFormatterChecks\CheckScriptFormat.ps1
14+
. $PSScriptRoot\CodeFormatterChecks\CheckTokenTypeCasing.ps1
15+
16+
function Invoke-CodeFormatterOnFiles {
17+
[CmdletBinding()]
18+
[OutputType([int])]
19+
param(
20+
[Parameter(Mandatory = $true)]
21+
[string[]]
22+
$FilePaths,
23+
24+
[switch]
25+
$Save
26+
)
27+
28+
if (-not (Load-Module -Name PSScriptAnalyzer -MinimumVersion "1.24")) {
29+
throw "PSScriptAnalyzer module could not be loaded"
30+
}
31+
32+
if (-not (Load-Module -Name EncodingAnalyzer)) {
33+
throw "EncodingAnalyzer module could not be loaded"
34+
}
35+
36+
$repoRoot = Get-Item "$PSScriptRoot\.."
37+
$errorCount = 0
38+
$filesToCheck = New-Object System.Collections.Generic.List[object]
39+
40+
foreach ($path in $FilePaths) {
41+
if (Test-Path -Path $path) {
42+
$filesToCheck.Add((Get-Item -Path $path))
43+
} else {
44+
Write-Warning "File not found, skipping: $path"
45+
}
46+
}
47+
48+
if ($filesToCheck.Count -eq 0) {
49+
Write-Host "No valid files to check."
50+
return 0
51+
}
52+
53+
foreach ($fileInfo in $filesToCheck) {
54+
$errorCount += (CheckFileHasNewlineAtEndOfFile $fileInfo $Save) ? 1 : 0
55+
$errorCount += (CheckMarkdownFileHasNoBOM $fileInfo $Save) ? 1 : 0
56+
$errorCount += (CheckScriptFileHasBOM $fileInfo $Save) ? 1 : 0
57+
$errorCount += (CheckScriptFileHasComplianceHeader $fileInfo $Save) ? 1 : 0
58+
$errorCount += (CheckTokenTypeCasing $fileInfo $Save "Keyword") ? 1 : 0
59+
$errorCount += (CheckTokenTypeCasing $fileInfo $Save "Operator") ? 1 : 0
60+
$errorCount += (CheckMultipleEmptyLines $fileInfo $Save) ? 1 : 0
61+
$errorCount += (CheckContainsCurlyQuotes $fileInfo $Save) ? 1 : 0
62+
63+
$results = @(CheckScriptFormat $fileInfo $Save)
64+
if ($results.Length -gt 0 -and $results[0] -eq $true) {
65+
$errorCount++
66+
if ($results.Length -gt 2) {
67+
git -c color.status=always diff ($($results[1]) | git hash-object -w --stdin) ($($results[2]) | git hash-object -w --stdin) | Out-Host
68+
}
69+
}
70+
}
71+
72+
$maxRetries = 5
73+
74+
foreach ($fileInfo in $filesToCheck) {
75+
for ($i = 0; $i -lt $maxRetries; $i++) {
76+
try {
77+
$params = @{
78+
Path = ($fileInfo.FullName)
79+
Settings = "$repoRoot\PSScriptAnalyzerSettings.psd1"
80+
CustomRulePath = "$repoRoot\.build\CodeFormatterChecks\CustomRules.psm1"
81+
IncludeDefaultRules = $true
82+
}
83+
$analyzerResults = Invoke-ScriptAnalyzer @params
84+
if ($null -ne $analyzerResults) {
85+
$errorCount++
86+
$analyzerResults | Format-Table -AutoSize | Out-Host
87+
}
88+
break
89+
} catch {
90+
Write-Warning "Invoke-ScriptAnalyzer failed on $($fileInfo.FullName). Error:"
91+
$_.Exception | Format-List | Out-Host
92+
Write-Warning "Retrying in 5 seconds."
93+
Start-Sleep -Seconds 5
94+
}
95+
}
96+
97+
if ($i -eq $maxRetries) {
98+
throw "Invoke-ScriptAnalyzer failed $maxRetries times. Giving up."
99+
}
100+
}
101+
102+
return $errorCount
103+
}

.build/Pester.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ begin {
4242

4343
$root = Get-Item "$PSScriptRoot\.."
4444
$scripts = @(Get-ChildItem -Recurse $root |
45-
Where-Object { $_.Name -like "*.Tests.ps1" })
45+
Where-Object { $_.Name -like "*.Tests.ps1" -and $_.FullName -notmatch "\.github" })
4646

4747
$parentProgress = @{
4848
Id = 0

.build/cspell-words.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
!scriptblock
12
adfs
23
Adsi
34
amsi
4-
Antispam
55
anob
6+
Antispam
67
asmx
78
Authenticode
89
authsspi
@@ -43,11 +44,12 @@ exfiltration
4344
fabrikam
4445
FIPFS
4546
fips
46-
Fsis
4747
fltmc
4848
freb
49+
Fsis
4950
FYDIBOHF
5051
GCDO
52+
github
5153
globalsequence
5254
Hashtable
5355
HHMM
@@ -112,8 +114,8 @@ notin
112114
notlike
113115
notmatch
114116
nspi
115-
ntlm
116117
NTFS
118+
ntlm
117119
NUMA
118120
nupkg
119121
odata
@@ -142,7 +144,6 @@ Runtimes
142144
sccm
143145
Schannel
144146
SCOM
145-
!scriptblock
146147
SERVERNAME
147148
Servicehost
148149
servicelet
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
<#
5+
.SYNOPSIS
6+
Blocks commits when test files exceed the max pipeline run count.
7+
.DESCRIPTION
8+
Scans staged .Tests.ps1 files for SetDefaultRunOfHealthChecker calls.
9+
Blocks the commit if any file exceeds 5 runs (the benchmarked optimal maximum).
10+
#>
11+
[CmdletBinding()]
12+
param(
13+
[Parameter(Mandatory)]
14+
[string[]]$Files
15+
)
16+
17+
$maxRunsPerFile = 5
18+
$failed = $false
19+
20+
foreach ($file in $Files) {
21+
if (-not (Test-Path $file)) { continue }
22+
23+
# Count actual calls only, excluding comments and strings
24+
try {
25+
$content = Get-Content -Path $file -Raw -ErrorAction Stop
26+
} catch {
27+
Write-Host " BLOCKED: $file - Unable to read file: $($_.Exception.Message)" -ForegroundColor Red
28+
$failed = $true
29+
continue
30+
}
31+
$parseErrors = $null
32+
$tokens = [System.Management.Automation.PSParser]::Tokenize($content, [ref]$parseErrors)
33+
34+
if ($null -ne $parseErrors -and $parseErrors.Count -gt 0) {
35+
Write-Host " WARN: $file has $($parseErrors.Count) parse error(s) - run count may be inaccurate" -ForegroundColor Yellow
36+
}
37+
38+
$runCount = @($tokens | Where-Object {
39+
$_.Type -eq 'Command' -and $_.Content -eq 'SetDefaultRunOfHealthChecker'
40+
}).Count
41+
42+
if ($runCount -gt $maxRunsPerFile) {
43+
Write-Host " BLOCKED: $file has $runCount pipeline runs (max: $maxRunsPerFile)" -ForegroundColor Red
44+
Write-Host " Consider splitting this file. Balance runs evenly (e.g., 4+2 not 5+1)." -ForegroundColor Yellow
45+
$failed = $true
46+
} elseif ($runCount -gt 0) {
47+
Write-Host " OK: $file - $runCount pipeline run(s)" -ForegroundColor Green
48+
}
49+
}
50+
51+
# Use 'git commit --no-verify' to bypass if needed
52+
if ($failed) {
53+
Write-Host "`nTest run count check found issues. See above." -ForegroundColor Red
54+
return 1
55+
}
56+
57+
return 0

0 commit comments

Comments
 (0)