|
8 | 8 |
|
9 | 9 | jobs: |
10 | 10 | pyspelling: |
11 | | - runs-on: ubuntu-latest |
| 11 | + runs-on: ubuntu-20.04 |
12 | 12 | steps: |
13 | | - - name: Get changed files and check skip label |
14 | | - id: files |
| 13 | + - name: Check for skip label and get changed files |
| 14 | + id: check-files |
15 | 15 | uses: actions/github-script@v6 |
16 | 16 | with: |
17 | 17 | script: | |
| 18 | + let skipCheck = false; |
| 19 | + let changedFiles = []; |
| 20 | + |
18 | 21 | if (context.eventName === 'pull_request') { |
19 | 22 | // Check for skip label |
20 | 23 | const { data: labels } = await github.rest.issues.listLabelsOnIssue({ |
21 | | - ...context.repo, |
| 24 | + owner: context.repo.owner, |
| 25 | + repo: context.repo.repo, |
22 | 26 | issue_number: context.issue.number |
23 | 27 | }); |
24 | | - if (labels.some(label => label.name === 'skip-spell-check')) { |
25 | | - core.setOutput('skip', 'true'); |
26 | | - return; |
| 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); |
27 | 41 | } |
| 42 | + } else { |
| 43 | + // For push events, we'll still need to use git diff |
| 44 | + // We'll handle this after checkout |
28 | 45 | } |
29 | 46 | |
30 | | - core.setOutput('skip', 'false'); |
| 47 | + core.setOutput('skip', skipCheck.toString()); |
| 48 | + core.setOutput('files', changedFiles.join('\n')); |
| 49 | + core.setOutput('is-pr', (context.eventName === 'pull_request').toString()); |
31 | 50 |
|
32 | 51 | - uses: actions/checkout@v4 |
33 | | - if: steps.files.outputs.skip != 'true' |
| 52 | + if: steps.check-files.outputs.skip != 'true' |
34 | 53 | with: |
35 | 54 | fetch-depth: 0 |
36 | 55 |
|
37 | | - - name: Get files to check |
38 | | - if: steps.files.outputs.skip != 'true' |
| 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' |
39 | 69 | id: check |
40 | 70 | run: | |
41 | | - if [[ ${{ github.event_name }} == 'pull_request' ]]; then |
42 | | - FILES=$(gh pr view ${{ github.event.pull_request.number }} --json files --jq '.files[].path' | grep -E '\.(py|rst|md)$' || true) |
| 71 | + if [ "${{ steps.check-files.outputs.is-pr }}" == "true" ]; then |
| 72 | + FILES="${{ steps.check-files.outputs.files }}" |
43 | 73 | else |
44 | | - FILES=$(git diff --name-only HEAD^..HEAD | grep -E '\.(py|rst|md)$' || true) |
| 74 | + FILES="${{ steps.push-files.outputs.files }}" |
45 | 75 | fi |
46 | 76 | |
47 | 77 | if [ -z "$FILES" ]; then |
48 | 78 | echo "skip=true" >> $GITHUB_OUTPUT |
49 | | - echo "No relevant files changed" |
| 79 | + echo "No relevant files changed (*.py, *.rst, *.md), skipping spell check" |
50 | 80 | else |
51 | | - echo "files<<EOF" >> $GITHUB_OUTPUT |
52 | | - echo "$FILES" >> $GITHUB_OUTPUT |
53 | | - echo "EOF" >> $GITHUB_OUTPUT |
54 | 81 | echo "skip=false" >> $GITHUB_OUTPUT |
55 | | - echo "Files to check:" |
| 82 | + echo "Found changed files to check:" |
56 | 83 | echo "$FILES" |
57 | 84 | fi |
58 | | - env: |
59 | | - GH_TOKEN: ${{ github.token }} |
60 | 85 |
|
61 | 86 | - uses: actions/setup-python@v4 |
62 | | - if: steps.files.outputs.skip != 'true' && steps.check.outputs.skip != 'true' |
| 87 | + if: | |
| 88 | + steps.check-files.outputs.skip != 'true' && |
| 89 | + steps.check.outputs.skip != 'true' |
63 | 90 | with: |
64 | 91 | python-version: '3.9' |
65 | 92 | cache: 'pip' |
66 | 93 |
|
67 | 94 | - name: Install dependencies |
68 | | - if: steps.files.outputs.skip != 'true' && steps.check.outputs.skip != 'true' |
| 95 | + if: | |
| 96 | + steps.check-files.outputs.skip != 'true' && |
| 97 | + steps.check.outputs.skip != 'true' |
69 | 98 | run: | |
70 | 99 | pip install pyspelling |
71 | 100 | sudo apt-get install aspell aspell-en |
72 | 101 |
|
73 | | - - name: Run spell check |
74 | | - if: steps.files.outputs.skip != 'true' && steps.check.outputs.skip != 'true' |
| 102 | + - name: Run spell check on each file |
| 103 | + if: | |
| 104 | + steps.check-files.outputs.skip != 'true' && |
| 105 | + steps.check.outputs.skip != 'true' |
75 | 106 | run: | |
76 | | - while IFS= read -r file; do |
| 107 | + # Get the list of files into an array |
| 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 | + for file in "${FILES[@]}"; do |
77 | 117 | if [ -n "$file" ]; then |
78 | | - echo "Checking $file" |
79 | | - python -c " |
80 | | - import yaml |
81 | | - with open('.pyspelling.yml') as config_file: |
82 | | - config = yaml.safe_load(config_file) |
83 | | - for matrix_entry in config['matrix']: |
84 | | - matrix_entry['sources'] = ['$file'] |
85 | | - with open('temp_config.yml', 'w') as output_file: |
86 | | - yaml.dump(config, output_file) |
87 | | - " |
88 | | - pyspelling -c temp_config.yml || exit 1 |
| 118 | + echo "Checking spelling in $file" |
| 119 | + # Create a temporary config file based on the existing one |
| 120 | + python3 - <<EOF > temp_config.yml |
| 121 | + import yaml |
| 122 | + |
| 123 | + with open('.pyspelling.yml', 'r') as f: |
| 124 | + config = yaml.safe_load(f) |
| 125 | + |
| 126 | + # Modify only the sources in each matrix entry |
| 127 | + for matrix in config['matrix']: |
| 128 | + matrix['sources'] = ['$file'] |
| 129 | + |
| 130 | + with open('temp_config.yml', 'w') as f: |
| 131 | + yaml.dump(config, f, default_flow_style=False) |
| 132 | + EOF |
| 133 | + |
| 134 | + if ! pyspelling -c temp_config.yml; then |
| 135 | + FINAL_EXIT_CODE=1 |
| 136 | + fi |
89 | 137 | fi |
90 | | - done <<< "${{ steps.check.outputs.files }}" |
| 138 | + done |
| 139 | + |
| 140 | + if [ $FINAL_EXIT_CODE -ne 0 ]; then |
| 141 | + echo "Spell check failed!" |
| 142 | + exit 1 |
| 143 | + fi |
0 commit comments