test js typos #9
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: Verify typos installation | |
run: | | |
typos --help # This will show the available commands | |
- 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 there are no changed files, set the environment variable to false | |
if [ -z "$changed_files" ]; then | |
echo "CHANGED_FILES=false" >> $GITHUB_ENV | |
else | |
echo "CHANGED_FILES=true" >> $GITHUB_ENV | |
# Use newline-separated file list to avoid issues with special characters or spaces | |
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 | |
# Temporary file to store the typos output | |
typos_output=$(mktemp) | |
# Read the list of changed files from the text file | |
while IFS= read -r file; do | |
echo "Checking typos in $file" | |
# Capture typos output into a temp file | |
typos "$file" > "$typos_output" || true | |
# If typos were found, set flag and parse the output | |
if [ -s "$typos_output" ]; then | |
typo_found=true | |
# Read each typo and create a comment for it | |
while IFS= read -r line; do | |
# Match the line that indicates an error and extract info | |
if [[ "$line" =~ error:\ \`([^`]*)\`\ should\ be\ \`([^`]*)\` ]]; then | |
typo_word="${BASH_REMATCH[1]}" | |
correction="${BASH_REMATCH[2]}" | |
line_number=$(echo "$line" | grep -oP '\d+') | |
# Create a 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" |