From 4349494820e408a07f8242bb85016fcaa016f97f Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Wed, 11 Nov 2020 15:50:11 -0800 Subject: [PATCH 1/2] Added the preprocess scripts. --- eng/common/scripts/Verify-Links.ps1 | 1 + .../get-markdown-files-from-changed-files.ps1 | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 eng/common/scripts/get-markdown-files-from-changed-files.ps1 diff --git a/eng/common/scripts/Verify-Links.ps1 b/eng/common/scripts/Verify-Links.ps1 index c9991a0588942..60065c980890e 100644 --- a/eng/common/scripts/Verify-Links.ps1 +++ b/eng/common/scripts/Verify-Links.ps1 @@ -30,6 +30,7 @@ function NormalizeUrl([string]$url){ $url = "file://" + (Resolve-Path $url).ToString(); } + Write-Verbose "The url to check against: $url." $uri = [System.Uri]$url; if ($script:baseUrl -eq "") { diff --git a/eng/common/scripts/get-markdown-files-from-changed-files.ps1 b/eng/common/scripts/get-markdown-files-from-changed-files.ps1 new file mode 100644 index 0000000000000..be084fbf56f68 --- /dev/null +++ b/eng/common/scripts/get-markdown-files-from-changed-files.ps1 @@ -0,0 +1,49 @@ +param ( + # The root repo we scaned with. + [string[]] $RootRepo = '$PSScriptRoot/../../..', + # The target branch to compare with. + [string] $targetBranch = "origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" +) +$deletedFiles = (git diff $targetBranch HEAD --name-only --diff-filter=D) +$renamedFiles = (git diff $targetBranch HEAD --diff-filter=R) +$changedMarkdowns = (git diff $targetBranch HEAD --name-only -- '*.md') + +$beforeRenameFiles = @() +# Retrieve the 'renamed from' files. Git command only returns back the files after rename. +# In order to have the files path before rename, it has to do some regex checking. +# It is better to be replaced by more reliable commands if any. +foreach ($file in $renamedFiles) { + if ($file -match "^rename from (.*)$") { + $beforeRenameFiles += $file -replace "^rename from (.*)$", '$1' + } +} +# A combined list of deleted and renamed files. +$relativePathLinks = ($deletedFiles + $beforeRenameFiles) +# Removed the deleted markdowns. +$changedMarkdowns = $changedMarkdowns | Where-Object { $deletedFiles -notcontains $_ } +# Scan all markdowns and find if it contains the deleted or renamed files. +$markdownContainLinks = @() +$allMarkdownFiles = Get-ChildItem -Path $RootRepo -Recurse -Include *.md +foreach ($f in $allMarkdownFiles) { + $filePath = $f.FullName + $content = Get-Content -Path $filePath -Raw + foreach ($l in $relativePathLinks) { + if ($content -match $l) { + $markdownContainLinks += $filePath + break + } + } +} + +# Convert markdowns path of the PR to absolute path. +$adjustedReadmes = $changedMarkdowns | Foreach-Object { Resolve-Path $_ } +$markdownContainLinks += $adjustedReadmes + +# Get rid of any duplicated ones. +$allMarkdowns = [string[]]($markdownContainLinks | Sort-Object | Get-Unique) + +Write-Host "Here are all markdown files we need to check based on the changed files:" +foreach ($file in $allMarkdowns) { + Write-Host " $file" +} +return $allMarkdowns From d629a7def521f2c4ba071b1aede14d82d193fb16 Mon Sep 17 00:00:00 2001 From: Sima Zhu Date: Wed, 11 Nov 2020 16:30:28 -0800 Subject: [PATCH 2/2] string array to string --- eng/common/scripts/get-markdown-files-from-changed-files.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/common/scripts/get-markdown-files-from-changed-files.ps1 b/eng/common/scripts/get-markdown-files-from-changed-files.ps1 index be084fbf56f68..6316fd1ce26eb 100644 --- a/eng/common/scripts/get-markdown-files-from-changed-files.ps1 +++ b/eng/common/scripts/get-markdown-files-from-changed-files.ps1 @@ -1,6 +1,6 @@ param ( # The root repo we scaned with. - [string[]] $RootRepo = '$PSScriptRoot/../../..', + [string] $RootRepo = '$PSScriptRoot/../../..', # The target branch to compare with. [string] $targetBranch = "origin/${env:SYSTEM_PULLREQUEST_TARGETBRANCH}" )