Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Automated Spell Check #1432

Merged
merged 7 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add copyright header


# 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
shaynafinocchiaro marked this conversation as resolved.
Show resolved Hide resolved

on:
workflow_dispatch:
repository_dispatch:
shaynafinocchiaro marked this conversation as resolved.
Show resolved Hide resolved
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
shaynafinocchiaro marked this conversation as resolved.
Show resolved Hide resolved
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
shaynafinocchiaro marked this conversation as resolved.
Show resolved Hide resolved
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
shaynafinocchiaro marked this conversation as resolved.
Show resolved Hide resolved
run: |
echo "Changes detected: ${{ env.CHANGES_DETECTED }}"

- name: Commit and Push Changes
shaynafinocchiaro marked this conversation as resolved.
Show resolved Hide resolved
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
shaynafinocchiaro marked this conversation as resolved.
Show resolved Hide resolved
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
Loading