Update spellcheck to run only on changed files #4151
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Check spelling | |
on: | |
pull_request: | |
push: | |
branches: | |
- main | |
jobs: | |
pyspelling: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Get changed files and check skip label | |
id: files | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
if (context.eventName === 'pull_request') { | |
// Check for skip label | |
const { data: labels } = await github.rest.issues.listLabelsOnIssue({ | |
...context.repo, | |
issue_number: context.issue.number | |
}); | |
if (labels.some(label => label.name === 'skip-spell-check')) { | |
core.setOutput('skip', 'true'); | |
return; | |
} | |
} | |
core.setOutput('skip', 'false'); | |
- uses: actions/checkout@v4 | |
if: steps.files.outputs.skip != 'true' | |
with: | |
fetch-depth: 0 | |
- name: Get files to check | |
if: steps.files.outputs.skip != 'true' | |
id: check | |
run: | | |
if [[ ${{ github.event_name }} == 'pull_request' ]]; then | |
FILES=$(gh pr view ${{ github.event.pull_request.number }} --json files --jq '.files[].path' | grep -E '\.(py|rst|md)$' || true) | |
else | |
FILES=$(git diff --name-only HEAD^..HEAD | grep -E '\.(py|rst|md)$' || true) | |
fi | |
if [ -z "$FILES" ]; then | |
echo "skip=true" >> $GITHUB_OUTPUT | |
echo "No relevant files changed" | |
else | |
echo "files<<EOF" >> $GITHUB_OUTPUT | |
echo "$FILES" >> $GITHUB_OUTPUT | |
echo "EOF" >> $GITHUB_OUTPUT | |
echo "skip=false" >> $GITHUB_OUTPUT | |
echo "Files to check:" | |
echo "$FILES" | |
fi | |
env: | |
GH_TOKEN: ${{ github.token }} | |
- uses: actions/setup-python@v4 | |
if: steps.files.outputs.skip != 'true' && steps.check.outputs.skip != 'true' | |
with: | |
python-version: '3.9' | |
cache: 'pip' | |
- name: Install dependencies | |
if: steps.files.outputs.skip != 'true' && steps.check.outputs.skip != 'true' | |
run: | | |
pip install pyspelling | |
sudo apt-get install aspell aspell-en | |
- name: Run spell check | |
if: steps.files.outputs.skip != 'true' && steps.check.outputs.skip != 'true' | |
run: | | |
while IFS= read -r file; do | |
if [ -n "$file" ]; then | |
echo "Checking $file" | |
python -c " | |
import yaml | |
with open('.pyspelling.yml') as config_file: | |
config = yaml.safe_load(config_file) | |
for matrix_entry in config['matrix']: | |
matrix_entry['sources'] = ['$file'] | |
with open('temp_config.yml', 'w') as output_file: | |
yaml.dump(config, output_file) | |
" | |
pyspelling -c temp_config.yml || exit 1 | |
fi | |
done <<< "${{ steps.check.outputs.files }}" |