test js typos #10
Workflow file for this run
This file contains 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 Typos in Pull Request | |
on: [pull_request] | |
jobs: | |
check-typos: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v3 | |
with: | |
python-version: '3.14.0-alpha.1' | |
- name: Install typos | |
run: | | |
python3 -m pip install --upgrade pip | |
pip install typos | |
- name: Get changed files | |
id: changed-files | |
run: | | |
set -e | |
echo "Listing changed files..." | |
git fetch --all | |
base_commit=$(git rev-parse origin/${{ github.base_ref }}) | |
current_commit=$(git rev-parse HEAD) | |
echo "Base commit: $base_commit" | |
echo "Current commit: $current_commit" | |
changed_files=$(git diff --name-only $base_commit $current_commit || true) | |
echo "Changed files: $changed_files" | |
if [ -z "$changed_files" ]; then | |
echo "CHANGED_FILES=false" >> $GITHUB_ENV | |
else | |
echo "CHANGED_FILES=true" >> $GITHUB_ENV | |
echo "$changed_files" | tr ' ' '\n' > changed_files.txt | |
echo "CHANGED_FILE_LIST=changed_files.txt" >> $GITHUB_ENV | |
fi | |
- name: Check if files have changed | |
id: check-files | |
run: | | |
if [ "${{ env.CHANGED_FILES }}" == "true" ]; then | |
echo "Files have changed." | |
echo "RUN_SCRIPTS=true" >> $GITHUB_ENV | |
else | |
echo "No files changed." | |
echo "RUN_SCRIPTS=false" >> $GITHUB_ENV | |
fi | |
- name: Check typos in changed files and comment on PR | |
if: ${{ env.RUN_SCRIPTS == 'true' }} | |
run: | | |
set -e | |
echo "Checking typos in changed files..." | |
typo_found=false | |
typos_output=$(mktemp) | |
while IFS= read -r file; do | |
echo "Checking typos in $file" | |
typos "$file" > "$typos_output" || true | |
# Log the typos output for debugging | |
echo "Typos output for $file:" | |
cat "$typos_output" | |
# Check if typos were found | |
if [ -s "$typos_output" ]; then | |
typo_found=true | |
while IFS= read -r line; do | |
# Debug the line before matching | |
echo "Processing line: $line" | |
# Correct way to use regular expressions in bash | |
if [[ "$line" =~ error:\ \`([^`]*)\`\ should\ be\ \`([^`]*)\` ]]; then | |
typo_word="${BASH_REMATCH[1]}" | |
correction="${BASH_REMATCH[2]}" | |
line_number=$(echo "$line" | grep -oP '\d+') | |
# Debug the extracted information | |
echo "Found typo: '$typo_word' should be '$correction' at line $line_number" | |
# Comment on the PR for each error found | |
comment_body="**Typo found**: \`$typo_word\` (line $line_number) should be \`$correction\`." | |
pr_number=${{ github.event.pull_request.number }} | |
echo "Creating PR comment for typo: $typo_word" | |
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-X POST \ | |
-d "{\"body\": \"$comment_body\"}" \ | |
"https://api.github.com/repos/${{ github.repository }}/issues/$pr_number/comments" | |
fi | |
done < "$typos_output" | |
fi | |
done < ${{ env.CHANGED_FILE_LIST }} | |
# Clean up temporary file | |
rm "$typos_output" | |
- name: Comment if no typos found | |
if: ${{ env.TYPO_FOUND == 'false' }} | |
run: | | |
echo "No typos found, skipping PR comment." |