test js typos #27
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: | |
permissions: write-all | |
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..." | |
# Loop through each changed file | |
while IFS= read -r file; do | |
echo "Checking typos in $file" | |
# Run typos checker and capture output, including errors | |
typos_output=$(typos "$file" --format brief 2>&1) || true | |
# Check if typos output exists | |
if [[ -n "$typos_output" ]]; then | |
echo "Typos found in $file:" | |
echo "$typos_output" | |
# Parse the typos output line by line | |
while IFS= read -r line; do | |
echo "line: $line" | |
if [[ $line =~ ^(.*):([0-9]+):([0-9]+):\ \`([^\`]+)\`\ \-\>\ \`([^\`]+)\`$ ]]; then | |
file_path="${BASH_REMATCH[1]}" | |
line_number="${BASH_REMATCH[2]}" | |
char_index="${BASH_REMATCH[3]}" | |
typo="${BASH_REMATCH[4]}" | |
suggestion="${BASH_REMATCH[5]}" | |
# Format the comment for each typo | |
comment="**Typo**: \`$typo\` should be \`$suggestion\`." | |
# Ensure proper escaping of special characters for JSON-like submission | |
file_path_escaped=$(echo "$file_path" | jq -R .) | |
comment_escaped=$(echo "$comment" | jq -R .) | |
line_escaped=$(echo "$line" | jq -R .) | |
# Prepare the review comment using the `--form` type (multipart/form-data) | |
comment_body="```suggestion\n$line_escaped\n```" | |
# Ensure that the JSON structure for comments is correctly formed | |
comments_array="[{ | |
\"path\": $file_path_escaped, | |
\"position\": $line_number, | |
\"side\": \"RIGHT\", | |
\"body\": \"```suggestion\n$line_escaped\n```\" | |
}]" | |
# Debug output to ensure the payload is correctly formatted | |
echo "comment_body: $comment_body" | |
echo "comments_array: $comments_array" | |
# Send the review comment using `--form` | |
curl -X POST \ | |
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
--form "commit_id=${{ github.sha }}" \ | |
--form "body=${comment_escaped}" \ | |
--form "event=REQUEST_CHANGES" \ | |
--form "comments=${comments_array}" \ | |
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/reviews" | |
fi | |
done <<< "$typos_output" | |
else | |
echo "No typos found in $file." | |
fi | |
done < ${{ env.CHANGED_FILE_LIST }} |