Fix merge conflict checker for empty file lists and filter *.cs files (#26365)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: TravisEz13 <10873629+TravisEz13@users.noreply.github.com>
This commit is contained in:
Copilot
2025-10-31 22:52:40 +00:00
committed by GitHub
co-authored by TravisEz13
parent 0c40a84abb
commit 8851ab8170
4 changed files with 72 additions and 9 deletions
@@ -56,8 +56,10 @@ jobs:
- **File Handling**:
- Checks only files that were added, modified, or renamed
- Skips deleted files
- **Filters out `*.cs` files** (C# files are excluded from merge conflict checking)
- Skips binary/unreadable files
- Skips directories
- **Empty File List**: Gracefully handles cases where no files need checking (e.g., PRs that only delete files)
## Example Output
@@ -25,7 +25,8 @@ runs:
run: |
# Get changed files from environment variable (secure against injection)
$changedFilesJson = $env:CHANGED_FILES_JSON
$changedFiles = $changedFilesJson | ConvertFrom-Json
# Ensure we always have an array (ConvertFrom-Json returns null for empty JSON arrays)
$changedFiles = @($changedFilesJson | ConvertFrom-Json)
# Import ci.psm1 and run the check
Import-Module "$env:GITHUB_WORKSPACE/tools/ci.psm1" -Force
+10 -4
View File
@@ -30,11 +30,17 @@ Describe "Test-MergeConflictMarker" {
}
Context "When no files are provided" {
It "Should handle empty file array" {
# The function parameter has Mandatory validation which rejects empty arrays by design
# This test verifies that behavior
It "Should handle empty file array gracefully" {
# The function now accepts empty arrays to handle cases like delete-only PRs
$emptyArray = @()
{ Test-MergeConflictMarker -File $emptyArray -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw -ExpectedMessage "*empty array*"
Test-MergeConflictMarker -File $emptyArray -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath
$outputs = Get-Content $script:testOutputPath
$outputs | Should -Contain "files-checked=0"
$outputs | Should -Contain "conflicts-found=0"
$summary = Get-Content $script:testSummaryPath -Raw
$summary | Should -Match "No Files to Check"
}
}
+58 -4
View File
@@ -1039,8 +1039,9 @@ Function Test-MergeConflictMarker
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string[]] $File,
[Parameter()]
[AllowEmptyCollection()]
[string[]] $File = @(),
[Parameter()]
[string] $WorkspacePath = $PWD,
@@ -1054,10 +1055,63 @@ Function Test-MergeConflictMarker
Write-Host "Starting merge conflict marker check..." -ForegroundColor Cyan
Write-Host "Checking $($File.Count) changed files for merge conflict markers" -ForegroundColor Cyan
# Helper function to write outputs when no files to check
function Write-NoFilesOutput {
param(
[string]$Message,
[string]$OutputPath,
[string]$SummaryPath
)
# Output results to GitHub Actions
if ($OutputPath) {
"files-checked=0" | Out-File -FilePath $OutputPath -Append -Encoding utf8
"conflicts-found=0" | Out-File -FilePath $OutputPath -Append -Encoding utf8
}
# Create GitHub Actions job summary
if ($SummaryPath) {
$summaryContent = @"
# Merge Conflict Marker Check Results
## Summary
- **Files Checked:** 0
- **Files with Conflicts:** 0
## ℹ️ No Files to Check
$Message
"@
$summaryContent | Out-File -FilePath $SummaryPath -Encoding utf8
}
}
# Handle empty file list (e.g., when PR only deletes files)
if ($File.Count -eq 0) {
Write-Host "No files to check (empty file list)" -ForegroundColor Yellow
Write-NoFilesOutput -Message "No files were provided for checking (this can happen when a PR only deletes files)." -OutputPath $OutputPath -SummaryPath $SummaryPath
return
}
# Filter out *.cs files from merge conflict checking
$filesToCheck = @($File | Where-Object { $_ -notlike "*.cs" })
$filteredCount = $File.Count - $filesToCheck.Count
if ($filteredCount -gt 0) {
Write-Host "Filtered out $filteredCount *.cs file(s) from merge conflict checking" -ForegroundColor Yellow
}
if ($filesToCheck.Count -eq 0) {
Write-Host "No files to check after filtering (all files were *.cs)" -ForegroundColor Yellow
Write-NoFilesOutput -Message "All $filteredCount file(s) were filtered out (*.cs files are excluded from merge conflict checking)." -OutputPath $OutputPath -SummaryPath $SummaryPath
return
}
Write-Host "Checking $($filesToCheck.Count) changed files for merge conflict markers" -ForegroundColor Cyan
# Convert relative paths to absolute paths for processing
$absolutePaths = $File | ForEach-Object {
$absolutePaths = $filesToCheck | ForEach-Object {
if ([System.IO.Path]::IsPathRooted($_)) {
$_
} else {