File: get-markdown-files-from-changed-files.ps1

package info (click to toggle)
python-azure 20201208%2Bgit-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,437,920 kB
  • sloc: python: 4,287,452; javascript: 269; makefile: 198; sh: 187; xml: 106
file content (49 lines) | stat: -rw-r--r-- 1,930 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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