Sync eng/common directory with azure-sdk-tools for PR 1421 (#1720)
* 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 <djurek@microsoft.com>
This commit is contained in:
parent
b7de24483a
commit
824853d948
27
eng/common/pipelines/templates/steps/check-spelling.yml
Normal file
27
eng/common/pipelines/templates/steps/check-spelling.yml
Normal file
@ -0,0 +1,27 @@
|
||||
# Checks spelling of files that changed between the current state of the repo
|
||||
# and some ref (branch, tag, etc.) or commit hash. Only runs on PRs.
|
||||
# ContinueOnError - true: Pipeline warns on spelling error
|
||||
# false: Pipeline fails on spelling error
|
||||
# TargetBranch - Target ref (e.g. master) to compare to create file change
|
||||
# list.
|
||||
# CspellConfigPath - Path to cspell.json config location
|
||||
|
||||
parameters:
|
||||
ContinueOnError: true
|
||||
TargetBranch: $(System.PullRequest.TargetBranch)
|
||||
SourceBranch: HEAD
|
||||
CspellConfigPath: ./.vscode/cspell.json
|
||||
|
||||
steps:
|
||||
- ${{ if eq(variables['Build.Reason'], 'PullRequest') }}:
|
||||
- task: PowerShell@2
|
||||
displayName: Check spelling (cspell)
|
||||
continueOnError: ${{ parameters.ContinueOnError }}
|
||||
inputs:
|
||||
targetType: filePath
|
||||
filePath: eng/common/scripts/check-spelling-in-changed-files.ps1
|
||||
arguments: >-
|
||||
-TargetBranch "origin/$("${{ parameters.TargetBranch }}" -replace "refs/heads/")"
|
||||
-SourceBranch ${{ parameters.SourceBranch }}
|
||||
-CspellConfigPath ${{ parameters.CspellConfigPath }}
|
||||
pwsh: true
|
||||
99
eng/common/scripts/check-spelling-in-changed-files.ps1
Normal file
99
eng/common/scripts/check-spelling-in-changed-files.ps1
Normal file
@ -0,0 +1,99 @@
|
||||
[CmdletBinding()]
|
||||
Param (
|
||||
[Parameter()]
|
||||
[string] $TargetBranch,
|
||||
|
||||
[Parameter()]
|
||||
[string] $SourceBranch,
|
||||
|
||||
[Parameter()]
|
||||
[string] $CspellConfigPath = "./.vscode/cspell.json"
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Continue"
|
||||
. $PSScriptRoot/logging.ps1
|
||||
|
||||
if ((Get-Command git | Measure-Object).Count -eq 0) {
|
||||
LogError "Could not locate git. Install git https://git-scm.com/downloads"
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ((Get-Command npx | Measure-Object).Count -eq 0) {
|
||||
LogError "Could not locate npx. Install NodeJS (includes npm and npx) https://nodejs.org/en/download/"
|
||||
exit 1
|
||||
}
|
||||
|
||||
if (!(Test-Path $CspellConfigPath)) {
|
||||
LogError "Could not locate config file $CspellConfigPath"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Lists names of files that were in some way changed between the
|
||||
# current $SourceBranch and $TargetBranch. Excludes files that were deleted to
|
||||
# prevent errors in Resolve-Path
|
||||
Write-Host "git diff --diff-filter=d --name-only $TargetBranch $SourceBranch"
|
||||
$changedFiles = git diff --diff-filter=d --name-only $TargetBranch $SourceBranch `
|
||||
| Resolve-Path
|
||||
|
||||
$changedFilesCount = ($changedFiles | Measure-Object).Count
|
||||
Write-Host "Git Detected $changedFilesCount changed file(s). Files checked by cspell may exclude files according to cspell.json"
|
||||
|
||||
if ($changedFilesCount -eq 0) {
|
||||
Write-Host "No changes detected"
|
||||
exit 0
|
||||
}
|
||||
|
||||
$changedFilesString = $changedFiles -join ' '
|
||||
|
||||
Write-Host "npx cspell --config $CspellConfigPath $changedFilesString"
|
||||
$spellingErrors = Invoke-Expression "npx cspell --config $CspellConfigPath $changedFilesString"
|
||||
|
||||
if ($spellingErrors) {
|
||||
foreach ($spellingError in $spellingErrors) {
|
||||
LogWarning $spellingError
|
||||
}
|
||||
LogWarning "Spelling errors detected. To correct false positives or learn about spell checking see: https://aka.ms/azsdk/engsys/spellcheck"
|
||||
}
|
||||
|
||||
exit 0
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Uses cspell (from NPM) to check spelling of recently changed files
|
||||
|
||||
.DESCRIPTION
|
||||
This script checks files that have changed relative to a base branch (default
|
||||
`master`) for spelling errors. Dictionaries and spelling configurations reside
|
||||
in a configurable `cspell.json` location.
|
||||
|
||||
This script uses `npx` and assumes that NodeJS (and by extension `npm` and `npx`
|
||||
) are installed on the machine. If it does not detect `npx` it will warn the
|
||||
user and exit with an error.
|
||||
|
||||
The entire file is scanned, not just changed sections. Spelling errors in parts
|
||||
of the file not touched will still be shown.
|
||||
|
||||
Running this on the local machine will trigger tests
|
||||
|
||||
.PARAMETER TargetBranch
|
||||
Git ref to compare changes. This is usually the "base" (GitHub) or "target"
|
||||
(DevOps) branch for which a pull request would be opened.
|
||||
|
||||
.PARAMETER SouceBranch
|
||||
Git ref to use instead of changes in current repo state. Use `HEAD` here to
|
||||
check spelling of files that have been committed and exlcude any new files or
|
||||
modified files that are not committed. This is most useful in CI scenarios where
|
||||
builds may have modified the state of the repo. Leaving this parameter blank
|
||||
includes files for whom changes have not been committed.
|
||||
|
||||
.PARAMETER CspellConfigPath
|
||||
Optional location to use for cspell.json path. Default value is
|
||||
`./.vscode/cspell.json`
|
||||
|
||||
.EXAMPLE
|
||||
./eng/common/scripts/check-spelling-in-changed-files.ps1 -TargetBranch 'target_branch_name'
|
||||
|
||||
This will run spell check with changes in the current branch with respect to
|
||||
`target_branch_name`
|
||||
|
||||
#>
|
||||
Loading…
Reference in New Issue
Block a user