Skip to content

Commit 842ae4e

Browse files
Update Repair-PullRequestTest.ps1
1 parent f372c02 commit 842ae4e

File tree

1 file changed

+81
-10
lines changed

1 file changed

+81
-10
lines changed

.aitools/module/Repair-PullRequestTest.ps1

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,56 @@ function Repair-PullRequestTest {
122122
}
123123

124124
process {
125+
# Helper function to get modified files from git diff
126+
function Get-ModifiedTestFiles {
127+
param(
128+
[string]$BaseBranch = "origin/development",
129+
[string]$TargetBranch = "HEAD",
130+
[string]$CommitSha = $null
131+
)
132+
133+
$modifiedFiles = @()
134+
$changedTestFiles = @()
135+
$changedCommandFiles = @()
136+
137+
try {
138+
if ($CommitSha) {
139+
# Get files modified in a specific commit
140+
$gitOutput = git diff --name-only "$CommitSha^..$CommitSha" 2>$null
141+
} else {
142+
# Get files modified between branches
143+
$gitOutput = git diff --name-only "$BaseBranch...$TargetBranch" 2>$null
144+
}
145+
146+
if ($gitOutput) {
147+
$modifiedFiles = $gitOutput | Where-Object { $_ }
148+
Write-Verbose "Found $($modifiedFiles.Count) modified files via git diff"
149+
150+
foreach ($file in $modifiedFiles) {
151+
Write-Verbose "Processing modified file: $file"
152+
153+
if ($file -like "*Tests.ps1" -or $file -like "tests/*.Tests.ps1") {
154+
$testFileName = [System.IO.Path]::GetFileName($file)
155+
$changedTestFiles += $testFileName
156+
Write-Verbose "Added test file: $testFileName"
157+
} elseif ($file -like "public/*.ps1") {
158+
$commandName = [System.IO.Path]::GetFileNameWithoutExtension($file)
159+
$testFileName = "$commandName.Tests.ps1"
160+
$changedCommandFiles += $testFileName
161+
Write-Verbose "Added command test file: $testFileName (from command - $commandName)"
162+
}
163+
}
164+
}
165+
} catch {
166+
Write-Warning "Failed to get modified files via git diff: $_"
167+
}
168+
169+
# Combine and deduplicate
170+
$relevantTestFiles = ($changedTestFiles + $changedCommandFiles) | Sort-Object -Unique
171+
Write-Verbose "Relevant test files from git diff: $($relevantTestFiles -join ', ')"
172+
173+
return $relevantTestFiles
174+
}
125175
try {
126176
# Create temp directory for working test files (cross-platform)
127177
$tempDir = if ($IsWindows -or $env:OS -eq "Windows_NT") {
@@ -228,8 +278,17 @@ function Repair-PullRequestTest {
228278
return
229279
}
230280

231-
# For build-specific mode, we don't filter by PR files - process all failures
232-
$allRelevantTestFiles = @()
281+
# For build-specific mode, determine modified files via git diff from the build commit
282+
Write-Verbose "BuildId mode: Determining modified files for build #$BuildId"
283+
284+
# Try to get the commit SHA from the build (this would require additional AppVeyor API calls)
285+
# For now, use current branch comparison as fallback
286+
$allRelevantTestFiles = Get-ModifiedTestFiles -BaseBranch "origin/development" -TargetBranch "HEAD"
287+
288+
if ($allRelevantTestFiles.Count -eq 0) {
289+
Write-Warning "No modified test files found for BuildId mode. Cannot determine which tests to repair."
290+
return
291+
}
233292

234293
# Use the first PR for branch operations (or current branch if no PR specified)
235294
$selectedPR = $prs | Select-Object -First 1
@@ -256,8 +315,14 @@ function Repair-PullRequestTest {
256315
return
257316
}
258317

259-
# For branch-specific mode, we don't filter by PR files - process all failures
260-
$allRelevantTestFiles = @()
318+
# For branch-specific mode, determine modified files via git diff
319+
Write-Verbose "Branch mode: Determining modified files for branch '$Branch'"
320+
$allRelevantTestFiles = Get-ModifiedTestFiles -BaseBranch "origin/development" -TargetBranch "origin/$Branch"
321+
322+
if ($allRelevantTestFiles.Count -eq 0) {
323+
Write-Warning "No modified test files found for branch '$Branch'. Cannot determine which tests to repair."
324+
return
325+
}
261326

262327
# Create a pseudo-PR object for the specified branch
263328
$selectedPR = @{
@@ -272,8 +337,14 @@ function Repair-PullRequestTest {
272337
# Use the auto-detected failures
273338
$allFailedTestsAcrossPRs = $autoDetectedFailures
274339

275-
# For auto-detected mode, we don't filter by PR files - process all failures
276-
$allRelevantTestFiles = @()
340+
# For auto-detected mode, determine modified files via git diff
341+
Write-Verbose "Auto-detection mode: Determining modified files for current branch '$originalBranch'"
342+
$allRelevantTestFiles = Get-ModifiedTestFiles -BaseBranch "origin/development" -TargetBranch "HEAD"
343+
344+
if ($allRelevantTestFiles.Count -eq 0) {
345+
Write-Warning "No modified test files found for current branch '$originalBranch'. Cannot determine which tests to repair."
346+
return
347+
}
277348

278349
# Use the pseudo-PR object we already created
279350
$selectedPR = $prs[0]
@@ -355,10 +426,10 @@ function Repair-PullRequestTest {
355426
continue
356427
}
357428

358-
# Filter tests for this PR and add to collection
429+
# Filter tests for this PR and add to collection - only include files that were actually changed
359430
foreach ($test in $prFailedTests) {
360431
$testFileName = [System.IO.Path]::GetFileName($test.TestFile)
361-
if ($relevantTestFiles.Count -eq 0 -or $testFileName -in $relevantTestFiles) {
432+
if ($relevantTestFiles.Count -gt 0 -and $testFileName -in $relevantTestFiles) {
362433
$allFailedTestsAcrossPRs += $test
363434
}
364435
}
@@ -382,8 +453,8 @@ function Repair-PullRequestTest {
382453

383454
foreach ($test in $allFailedTestsAcrossPRs) {
384455
$fileName = [System.IO.Path]::GetFileName($test.TestFile)
385-
# ONLY include files that are actually in the PR changes
386-
if ($allRelevantTestFiles.Count -eq 0 -or $fileName -in $allRelevantTestFiles) {
456+
# ONLY include files that are actually in the PR/branch changes
457+
if ($fileName -in $allRelevantTestFiles) {
387458
if (-not $fileErrorMap.ContainsKey($fileName)) {
388459
$fileErrorMap[$fileName] = @()
389460
}

0 commit comments

Comments
 (0)