@@ -122,6 +122,56 @@ function Repair-PullRequestTest {
122
122
}
123
123
124
124
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
+ }
125
175
try {
126
176
# Create temp directory for working test files (cross-platform)
127
177
$tempDir = if ($IsWindows -or $env: OS -eq " Windows_NT" ) {
@@ -228,8 +278,17 @@ function Repair-PullRequestTest {
228
278
return
229
279
}
230
280
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
+ }
233
292
234
293
# Use the first PR for branch operations (or current branch if no PR specified)
235
294
$selectedPR = $prs | Select-Object - First 1
@@ -256,8 +315,14 @@ function Repair-PullRequestTest {
256
315
return
257
316
}
258
317
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
+ }
261
326
262
327
# Create a pseudo-PR object for the specified branch
263
328
$selectedPR = @ {
@@ -272,8 +337,14 @@ function Repair-PullRequestTest {
272
337
# Use the auto-detected failures
273
338
$allFailedTestsAcrossPRs = $autoDetectedFailures
274
339
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
+ }
277
348
278
349
# Use the pseudo-PR object we already created
279
350
$selectedPR = $prs [0 ]
@@ -355,10 +426,10 @@ function Repair-PullRequestTest {
355
426
continue
356
427
}
357
428
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
359
430
foreach ($test in $prFailedTests ) {
360
431
$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 ) {
362
433
$allFailedTestsAcrossPRs += $test
363
434
}
364
435
}
@@ -382,8 +453,8 @@ function Repair-PullRequestTest {
382
453
383
454
foreach ($test in $allFailedTestsAcrossPRs ) {
384
455
$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 ) {
387
458
if (-not $fileErrorMap.ContainsKey ($fileName )) {
388
459
$fileErrorMap [$fileName ] = @ ()
389
460
}
0 commit comments