Skip to content

Commit 90c8e77

Browse files
Sync eng/common directory with azure-sdk-tools for PR 1421 (Azure#16865)
* Add common tools for spell check * Updates from further iteration * Review feedback: Add check for git as well * Review feedback * Use common approach to resolving base branch name * Eliminate default base branch "master" as this will be changed later, providing no default and using a mandatory parameter means a dev must provide the value * Check for existence of $CspellConfigPath * No need to Set-Location if run from the root of the repo (most common scenario) * -join * Review feedback: Rename TargetRef -> TargetBranch, add SourceBranch, Add reference to spelling docs, exit 0, Rename to check-spelling-in-changed-files.ps1, * Review feedback: Remove ValidateNotNullOrEmpty (we do more definitive validation farther down that will also catch these errors), Update comments and script name Co-authored-by: Daniel Jurek <[email protected]>
1 parent 29f4d2c commit 90c8e77

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Checks spelling of files that changed between the current state of the repo
2+
# and some ref (branch, tag, etc.) or commit hash. Only runs on PRs.
3+
# ContinueOnError - true: Pipeline warns on spelling error
4+
# false: Pipeline fails on spelling error
5+
# TargetBranch - Target ref (e.g. master) to compare to create file change
6+
# list.
7+
# CspellConfigPath - Path to cspell.json config location
8+
9+
parameters:
10+
ContinueOnError: true
11+
TargetBranch: $(System.PullRequest.TargetBranch)
12+
SourceBranch: HEAD
13+
CspellConfigPath: ./.vscode/cspell.json
14+
15+
steps:
16+
- ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
17+
- task: PowerShell@2
18+
displayName: Check spelling (cspell)
19+
continueOnError: ${{ parameters.ContinueOnError }}
20+
inputs:
21+
targetType: filePath
22+
filePath: eng/common/scripts/check-spelling-in-changed-files.ps1
23+
arguments: >-
24+
-TargetBranch "origin/$("${{ parameters.TargetBranch }}" -replace "refs/heads/")"
25+
-SourceBranch ${{ parameters.SourceBranch }}
26+
-CspellConfigPath ${{ parameters.CspellConfigPath }}
27+
pwsh: true
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
[CmdletBinding()]
2+
Param (
3+
[Parameter()]
4+
[string] $TargetBranch,
5+
6+
[Parameter()]
7+
[string] $SourceBranch,
8+
9+
[Parameter()]
10+
[string] $CspellConfigPath = "./.vscode/cspell.json"
11+
)
12+
13+
$ErrorActionPreference = "Continue"
14+
. $PSScriptRoot/logging.ps1
15+
16+
if ((Get-Command git | Measure-Object).Count -eq 0) {
17+
LogError "Could not locate git. Install git https://git-scm.com/downloads"
18+
exit 1
19+
}
20+
21+
if ((Get-Command npx | Measure-Object).Count -eq 0) {
22+
LogError "Could not locate npx. Install NodeJS (includes npm and npx) https://nodejs.org/en/download/"
23+
exit 1
24+
}
25+
26+
if (!(Test-Path $CspellConfigPath)) {
27+
LogError "Could not locate config file $CspellConfigPath"
28+
exit 1
29+
}
30+
31+
# Lists names of files that were in some way changed between the
32+
# current $SourceBranch and $TargetBranch. Excludes files that were deleted to
33+
# prevent errors in Resolve-Path
34+
Write-Host "git diff --diff-filter=d --name-only $TargetBranch $SourceBranch"
35+
$changedFiles = git diff --diff-filter=d --name-only $TargetBranch $SourceBranch `
36+
| Resolve-Path
37+
38+
$changedFilesCount = ($changedFiles | Measure-Object).Count
39+
Write-Host "Git Detected $changedFilesCount changed file(s). Files checked by cspell may exclude files according to cspell.json"
40+
41+
if ($changedFilesCount -eq 0) {
42+
Write-Host "No changes detected"
43+
exit 0
44+
}
45+
46+
$changedFilesString = $changedFiles -join ' '
47+
48+
Write-Host "npx cspell --config $CspellConfigPath $changedFilesString"
49+
$spellingErrors = Invoke-Expression "npx cspell --config $CspellConfigPath $changedFilesString"
50+
51+
if ($spellingErrors) {
52+
foreach ($spellingError in $spellingErrors) {
53+
LogWarning $spellingError
54+
}
55+
LogWarning "Spelling errors detected. To correct false positives or learn about spell checking see: https://aka.ms/azsdk/engsys/spellcheck"
56+
}
57+
58+
exit 0
59+
60+
<#
61+
.SYNOPSIS
62+
Uses cspell (from NPM) to check spelling of recently changed files
63+
64+
.DESCRIPTION
65+
This script checks files that have changed relative to a base branch (default
66+
`master`) for spelling errors. Dictionaries and spelling configurations reside
67+
in a configurable `cspell.json` location.
68+
69+
This script uses `npx` and assumes that NodeJS (and by extension `npm` and `npx`
70+
) are installed on the machine. If it does not detect `npx` it will warn the
71+
user and exit with an error.
72+
73+
The entire file is scanned, not just changed sections. Spelling errors in parts
74+
of the file not touched will still be shown.
75+
76+
Running this on the local machine will trigger tests
77+
78+
.PARAMETER TargetBranch
79+
Git ref to compare changes. This is usually the "base" (GitHub) or "target"
80+
(DevOps) branch for which a pull request would be opened.
81+
82+
.PARAMETER SouceBranch
83+
Git ref to use instead of changes in current repo state. Use `HEAD` here to
84+
check spelling of files that have been committed and exlcude any new files or
85+
modified files that are not committed. This is most useful in CI scenarios where
86+
builds may have modified the state of the repo. Leaving this parameter blank
87+
includes files for whom changes have not been committed.
88+
89+
.PARAMETER CspellConfigPath
90+
Optional location to use for cspell.json path. Default value is
91+
`./.vscode/cspell.json`
92+
93+
.EXAMPLE
94+
./eng/common/scripts/check-spelling-in-changed-files.ps1 -TargetBranch 'target_branch_name'
95+
96+
This will run spell check with changes in the current branch with respect to
97+
`target_branch_name`
98+
99+
#>

0 commit comments

Comments
 (0)