Skip to content

Update spellcheck to run only on changed files #4155

Update spellcheck to run only on changed files

Update spellcheck to run only on changed files #4155

Workflow file for this run

name: Check spelling
on:
pull_request:
push:
branches:
- main
jobs:
pyspelling:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get changed files and check for skip label
id: changes
uses: actions/github-script@v6
with:
script: |
let skipCheck = false;
let changedFiles = [];
if (context.eventName === 'pull_request') {
// Check for skip label
const { data: labels } = await github.rest.issues.listLabelsOnIssue({
...context.repo,
issue_number: context.issue.number
});
skipCheck = labels.some(label => label.name === 'skip-spell-check');
if (!skipCheck) {
const { data: files } = await github.rest.pulls.listFiles({
...context.repo,
pull_number: context.issue.number
});
changedFiles = files
.filter(file => file.filename.match(/\.(py|rst|md)$/))
.map(file => file.filename);
}
} else {
// For push events
const exec = require('child_process').execSync;
changedFiles = exec('git diff --name-only HEAD^..HEAD -- "*.py" "*.rst" "*.md"')
.toString()
.trim()
.split('\n')
.filter(Boolean);
}
const shouldRun = !skipCheck && changedFiles.length > 0;
core.setOutput('should-run', shouldRun);
core.setOutput('files', changedFiles.join('\n'));
- uses: actions/setup-python@v4
if: steps.changes.outputs.should-run == 'true'
with:
python-version: '3.9'
cache: 'pip'
- name: Install dependencies
if: steps.changes.outputs.should-run == 'true'
run: |
pip install pyspelling
sudo apt-get install aspell aspell-en
- name: Run spell check
if: steps.changes.outputs.should-run == 'true'
run: |
# Get the list of files
mapfile -t FILES <<< "${{ steps.changes.outputs.files }}"
# Check each file
for file in "${FILES[@]}"; do
echo "Checking spelling in $file"
python3 - <<EOF > temp_config.yml
import yaml
with open('.pyspelling.yml', 'r') as f:
config = yaml.safe_load(f)
for matrix in config['matrix']:
matrix['sources'] = ['$file']
with open('temp_config.yml', 'w') as f:
yaml.dump(config, f)
EOF
pyspelling -c temp_config.yml || exit 1
done