|
| 1 | +param ( |
| 2 | + # The root repo we scaned with. |
| 3 | + [string] $RootRepo = '$PSScriptRoot/../../..', |
| 4 | + # The target branch to compare with. |
| 5 | + [string] $targetBranch = "origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" |
| 6 | +) |
| 7 | +$deletedFiles = (git diff $targetBranch HEAD --name-only --diff-filter=D) |
| 8 | +$renamedFiles = (git diff $targetBranch HEAD --diff-filter=R) |
| 9 | +$changedMarkdowns = (git diff $targetBranch HEAD --name-only -- '*.md') |
| 10 | + |
| 11 | +$beforeRenameFiles = @() |
| 12 | +# Retrieve the 'renamed from' files. Git command only returns back the files after rename. |
| 13 | +# In order to have the files path before rename, it has to do some regex checking. |
| 14 | +# It is better to be replaced by more reliable commands if any. |
| 15 | +foreach ($file in $renamedFiles) { |
| 16 | + if ($file -match "^rename from (.*)$") { |
| 17 | + $beforeRenameFiles += $file -replace "^rename from (.*)$", '$1' |
| 18 | + } |
| 19 | +} |
| 20 | +# A combined list of deleted and renamed files. |
| 21 | +$relativePathLinks = ($deletedFiles + $beforeRenameFiles) |
| 22 | +# Removed the deleted markdowns. |
| 23 | +$changedMarkdowns = $changedMarkdowns | Where-Object { $deletedFiles -notcontains $_ } |
| 24 | +# Scan all markdowns and find if it contains the deleted or renamed files. |
| 25 | +$markdownContainLinks = @() |
| 26 | +$allMarkdownFiles = Get-ChildItem -Path $RootRepo -Recurse -Include *.md |
| 27 | +foreach ($f in $allMarkdownFiles) { |
| 28 | + $filePath = $f.FullName |
| 29 | + $content = Get-Content -Path $filePath -Raw |
| 30 | + foreach ($l in $relativePathLinks) { |
| 31 | + if ($content -match $l) { |
| 32 | + $markdownContainLinks += $filePath |
| 33 | + break |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +# Convert markdowns path of the PR to absolute path. |
| 39 | +$adjustedReadmes = $changedMarkdowns | Foreach-Object { Resolve-Path $_ } |
| 40 | +$markdownContainLinks += $adjustedReadmes |
| 41 | + |
| 42 | +# Get rid of any duplicated ones. |
| 43 | +$allMarkdowns = [string[]]($markdownContainLinks | Sort-Object | Get-Unique) |
| 44 | + |
| 45 | +Write-Host "Here are all markdown files we need to check based on the changed files:" |
| 46 | +foreach ($file in $allMarkdowns) { |
| 47 | + Write-Host " $file" |
| 48 | +} |
| 49 | +return $allMarkdowns |
0 commit comments