Skip to content

Commit

Permalink
feat: added spell check workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mgandharva committed Jan 28, 2025
1 parent 5144383 commit bce9b1a
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/autofix-scripts/codespell-autofix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# Determine the directory of the script
script_dir=$(dirname "$(readlink -f "$0")")
echo "Script directory: $script_dir"

# Define the target directory
target_dir=$(realpath "$script_dir/../../..")
echo "Target directory: $target_dir"

# Run codespell and capture output, targeting the test-csm-docs directory
output=$(find "$target_dir" -type f -name "*.md" -exec codespell \
--ignore-words="$script_dir/codespell.txt" --builtin clear,rare,informal \
--write-changes --quiet-level=0 {} +)

# Print each line of the output separately for readability
echo "Codespell Output:"
echo "$output" | while IFS= read -r line; do
echo "$line"
done

# Process the output and apply the first suggestion
echo "Processing changes..."
echo "$output" | while IFS= read -r line; do
if [[ $line == *" ==>"* ]]; then
# Extract file, line number, original word, and suggestion
file=$(echo "$line" | cut -d':' -f1)
error_line=$(echo "$line" | cut -d':' -f2)
original=$(echo "$line" | awk '{print $2}')
suggestion=$(echo "$line" | awk -F'[ ]+==> ' '{print $2}' | cut -d',' -f1)

# Ensure the file is inside the test-csm-docs directory
if [[ $file == $target_dir/* ]]; then
# Apply the first suggestion
if [ -n "$suggestion" ] && [ -f "$file" ]; then
sed -i "${error_line}s/\b$original\b/$suggestion/" "$file"
echo "Fixed $original to $suggestion in $file on line $error_line"
fi
else
echo "Skipping file not in target directory: $file"
fi
fi
done
4 changes: 4 additions & 0 deletions .github/workflows/autofix-scripts/codespell.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
VAs
IAM
MKE
master
1 change: 1 addition & 0 deletions .github/workflows/autofix-scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
codespell
88 changes: 88 additions & 0 deletions .github/workflows/spellcheck-autofix.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
name: Automatic Spell Check and Correction

on:
workflow_dispatch:
repository_dispatch:
types: [trigger-spell-check-autofix]

jobs:
codespell:
name: Perform Spell Check and Apply Fixes
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.8] # Define the Python version here

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Import GPG Key
uses: crazy-max/ghaction-import-gpg@v6
with:
gpg_private_key: ${{ secrets.CSM_GPG_PRIVATE_KEY }}
git_user_signingkey: true
git_commit_gpgsign: true
git_tag_gpgsign: true
git_config_global: true

- name: Set Up Python Environment
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install Codespell
run: |
python -m pip install --upgrade pip
pip install codespell
- name: Run Codespell Autofix
run: |
chmod +x .github/workflows/autofix-scripts/codespell-autofix.sh
./.github/workflows/autofix-scripts/codespell-autofix.sh
- name: Check for Uncommitted Changes
id: check_changes
run: |
git status
if [ -n "$(git status --porcelain)" ]; then
echo "CHANGES_DETECTED=true" >> $GITHUB_ENV
else
echo "CHANGES_DETECTED=false" >> $GITHUB_ENV
fi
- name: Print Changes Detected
run: |
echo "Changes detected: ${{ env.CHANGES_DETECTED }}"
- name: Commit and Push Changes
if: env.CHANGES_DETECTED == 'true'
run: |
git add .
git commit -m "Autofix spelling issues"
- name: Generate GitHub App Token
uses: actions/[email protected]
id: generate-token
if: env.CHANGES_DETECTED == 'true'
with:
app-id: ${{ vars.CSM_RELEASE_APP_ID }}
private-key: ${{ secrets.CSM_RELEASE_APP_PRIVATE_KEY }}

- name: Create Pull Request with Autofixes
if: env.CHANGES_DETECTED == 'true'
uses: peter-evans/create-pull-request@v5
with:
token: ${{ steps.generate-token.outputs.token }}
branch: spellcheck-fixes
title: 'Automated Spelling Corrections Applied'
body: |
This pull request was created automatically by a GitHub Action that fixes spelling errors across multiple files.
**Summary of changes:**
- Identified and corrected common spelling mistakes
- Ensured consistency and accuracy in documentation files
Please review the changes to confirm their accuracy and approve the PR.
sign-commits: true
delete-branch: true

0 comments on commit bce9b1a

Please sign in to comment.