|
5 | 5 | push:
|
6 | 6 | branches:
|
7 | 7 | - main
|
| 8 | + |
8 | 9 | jobs:
|
9 | 10 | pyspelling:
|
10 | 11 | runs-on: ubuntu-20.04
|
11 | 12 | steps:
|
12 |
| - - uses: actions/checkout@v3 |
| 13 | + - name: Check for skip label and get changed files |
| 14 | + id: check-files |
| 15 | + uses: actions/github-script@v6 |
| 16 | + with: |
| 17 | + script: | |
| 18 | + let skipCheck = false; |
| 19 | + let changedFiles = []; |
| 20 | + |
| 21 | + if (context.eventName === 'pull_request') { |
| 22 | + // Check for skip label |
| 23 | + const { data: labels } = await github.rest.issues.listLabelsOnIssue({ |
| 24 | + owner: context.repo.owner, |
| 25 | + repo: context.repo.repo, |
| 26 | + issue_number: context.issue.number |
| 27 | + }); |
| 28 | + skipCheck = labels.some(label => label.name === 'skip-spell-check'); |
| 29 | + |
| 30 | + if (!skipCheck) { |
| 31 | + // Get changed files in PR |
| 32 | + const { data: files } = await github.rest.pulls.listFiles({ |
| 33 | + owner: context.repo.owner, |
| 34 | + repo: context.repo.repo, |
| 35 | + pull_number: context.issue.number |
| 36 | + }); |
| 37 | + |
| 38 | + changedFiles = files |
| 39 | + .filter(file => file.filename.match(/\.(py|rst|md)$/)) |
| 40 | + .map(file => file.filename); |
| 41 | + } |
| 42 | + } else { |
| 43 | + // For push events, we'll still need to use git diff |
| 44 | + // We'll handle this after checkout |
| 45 | + } |
| 46 | + |
| 47 | + core.setOutput('skip', skipCheck.toString()); |
| 48 | + core.setOutput('files', changedFiles.join('\n')); |
| 49 | + core.setOutput('is-pr', (context.eventName === 'pull_request').toString()); |
| 50 | +
|
| 51 | + - uses: actions/checkout@v4 |
| 52 | + if: steps.check-files.outputs.skip != 'true' |
| 53 | + with: |
| 54 | + fetch-depth: 0 |
| 55 | + |
| 56 | + - name: Get changed files for push event |
| 57 | + if: | |
| 58 | + steps.check-files.outputs.skip != 'true' && |
| 59 | + steps.check-files.outputs.is-pr != 'true' |
| 60 | + id: push-files |
| 61 | + run: | |
| 62 | + CHANGED_FILES=$(git diff --name-only HEAD^..HEAD -- '*.py' '*.rst' '*.md') |
| 63 | + echo "files<<EOF" >> $GITHUB_OUTPUT |
| 64 | + echo "$CHANGED_FILES" >> $GITHUB_OUTPUT |
| 65 | + echo "EOF" >> $GITHUB_OUTPUT |
| 66 | +
|
| 67 | + - name: Check if relevant files changed |
| 68 | + if: steps.check-files.outputs.skip != 'true' |
| 69 | + id: check |
| 70 | + run: | |
| 71 | + if [ "${{ steps.check-files.outputs.is-pr }}" == "true" ]; then |
| 72 | + FILES="${{ steps.check-files.outputs.files }}" |
| 73 | + else |
| 74 | + FILES="${{ steps.push-files.outputs.files }}" |
| 75 | + fi |
| 76 | + |
| 77 | + if [ -z "$FILES" ]; then |
| 78 | + echo "skip=true" >> $GITHUB_OUTPUT |
| 79 | + echo "No relevant files changed (*.py, *.rst, *.md), skipping spell check" |
| 80 | + else |
| 81 | + echo "skip=false" >> $GITHUB_OUTPUT |
| 82 | + echo "Found changed files to check:" |
| 83 | + echo "$FILES" |
| 84 | + fi |
| 85 | +
|
13 | 86 | - uses: actions/setup-python@v4
|
| 87 | + if: | |
| 88 | + steps.check-files.outputs.skip != 'true' && |
| 89 | + steps.check.outputs.skip != 'true' |
14 | 90 | with:
|
15 | 91 | python-version: '3.9'
|
16 | 92 | cache: 'pip'
|
17 |
| - - run: pip install pyspelling |
18 |
| - - run: sudo apt-get install aspell aspell-en |
19 |
| - - run: pyspelling |
20 | 93 |
|
| 94 | + - name: Install dependencies |
| 95 | + if: | |
| 96 | + steps.check-files.outputs.skip != 'true' && |
| 97 | + steps.check.outputs.skip != 'true' |
| 98 | + run: | |
| 99 | + pip install pyspelling |
| 100 | + sudo apt-get install aspell aspell-en |
| 101 | +
|
| 102 | + - name: Run spell check on each file |
| 103 | + id: spellcheck |
| 104 | + if: | |
| 105 | + steps.check-files.outputs.skip != 'true' && |
| 106 | + steps.check.outputs.skip != 'true' |
| 107 | + run: | |
| 108 | + if [ "${{ steps.check-files.outputs.is-pr }}" == "true" ]; then |
| 109 | + mapfile -t FILES <<< "${{ steps.check-files.outputs.files }}" |
| 110 | + else |
| 111 | + mapfile -t FILES <<< "${{ steps.push-files.outputs.files }}" |
| 112 | + fi |
| 113 | + |
| 114 | + # Check each file individually |
| 115 | + FINAL_EXIT_CODE=0 |
| 116 | + SPELLCHECK_LOG="" |
| 117 | + for file in "${FILES[@]}"; do |
| 118 | + if [ -n "$file" ]; then |
| 119 | + echo "Checking spelling in $file" |
| 120 | + python3 -c "import yaml; config = yaml.safe_load(open('.pyspelling.yml')); new_matrix = [matrix.copy() for matrix in config['matrix'] if (('python' in matrix['name'].lower() and '$file'.endswith('.py')) or ('rest' in matrix['name'].lower() and '$file'.endswith('.rst')) or ('markdown' in matrix['name'].lower() and '$file'.endswith('.md'))) and not matrix.update({'sources': ['$file']})]; config['matrix'] = new_matrix; yaml.dump(config, open('temp_config.yml', 'w'))" |
| 121 | +
|
| 122 | + if OUTPUT=$(pyspelling -c temp_config.yml 2>&1); then |
| 123 | + echo "No spelling errors found in $file" |
| 124 | + else |
| 125 | + FINAL_EXIT_CODE=1 |
| 126 | + echo "Spelling errors found in $file:" |
| 127 | + echo "$OUTPUT" |
| 128 | + SPELLCHECK_LOG+="### $file\n$OUTPUT\n\n" |
| 129 | + fi |
| 130 | + fi |
| 131 | + done |
| 132 | +
|
| 133 | + # Save the results to GITHUB_OUTPUT |
| 134 | + echo "spell_failed=$FINAL_EXIT_CODE" >> $GITHUB_OUTPUT |
| 135 | + echo "spell_log<<SPELLEOF" >> $GITHUB_OUTPUT |
| 136 | + echo "$SPELLCHECK_LOG" >> $GITHUB_OUTPUT |
| 137 | + echo "SPELLEOF" >> $GITHUB_OUTPUT |
| 138 | +
|
| 139 | + if [ $FINAL_EXIT_CODE -ne 0 ]; then |
| 140 | + echo "Spell check failed! See above for details." |
| 141 | + echo |
| 142 | + echo "Here are a few tips:" |
| 143 | + echo "- All PyTorch API objects must be in double backticks or use an intersphinx directive." |
| 144 | + echo " Example: ``torch.nn``, :func:" |
| 145 | + echo "- Consult en-wordlist.txt for spellings of some of the words." |
| 146 | + echo " You can add a word to en-wordlist.txt if:" |
| 147 | + echo " 1) It's a common abbreviation, like RNN." |
| 148 | + echo " 2) It's a word widely accepted in the industry." |
| 149 | + echo "- Please do not add words like 'dtype', 'torch.nn.Transformer' to pass spellcheck." |
| 150 | + echo " Instead wrap it in double backticks or use an intersphinx directive." |
| 151 | + echo |
| 152 | + exit 1 |
| 153 | + fi |
0 commit comments