Skip to content

Instantly share code, notes, and snippets.

@luzpaz
Last active July 14, 2019 08:02
Show Gist options
  • Save luzpaz/76c7cbbb7edcb916f1ce69a14453478a to your computer and use it in GitHub Desktop.
Save luzpaz/76c7cbbb7edcb916f1ce69a14453478a to your computer and use it in GitHub Desktop.
Bash script to invoke codespell on newly submitted PRs. Results will not interrupt build process but instead will be sent to the PR comment thread.
#!/bin/bash
# Inspired from https://blog.eleven-labs.com/en/how-to-check-the-spelling-of-your-docs-from-travis-ci/
# Tips:
# 1. use "exit 0;" to not interrupt build process
# 2. Needs a token as an ENV variable that is hidden (DONE: $GH_TOKEN)
# 3. requires apt package: codespell (we want to install the most up to date codespell)
# pip install --user --upgrade git+https://github.com/lucasdemarchi/codespell.git
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;36m'
NC='\033[0m' # No Color
CODESPELL_SKIPS="*.po,*.ts,./.git,./src/3rdParty,./src/zipios++,./src/CXX"
# Round up all changed content
CHANGED_FILES=($(git diff --name-only $TRAVIS_COMMIT_RANGE))
echo -e "$BLUE>> Following files were changed in this pull request (commit range: $TRAVIS_COMMIT_RANGE):$NC"
echo "$CHANGED_FILES"
# cat all files that changed
TEXT_CONTENT=`cat $(echo "$CHANGED_FILES")`
echo -e "$BLUE>> Text content that will be checked:$NC"
echo "$TEXT_CONTENT"
# Download FreeCAD's codespell whitelist in to ./fc-word-whitelist.txt
echo -e "$BLUE>> Downloading whitelist files:$NC"
curl -Os https://gist.githubusercontent.com/luzpaz/7ac1bf4412b9c1e5acde715ef9cb612c/raw/fc-word-whitelist.txt`
# Run codespell
echo -e "$BLUE>> Run codespell:$NC"
codespell -d -q 3 -S "$CODESPELL_SKIPS" -I ./fc-word-whitelist.txt | tee codespell_results.txt
CODESPELL_RESULTS=`cat codespell_results.txt`
# Send results back to the PR comment
echo -e "$BLUE>> Sending results in a comment on the Github pull request #$TRAVIS_PULL_REQUEST:$NC"
curl -i -H "Authorization: token $GH_TOKEN" \
-H "Content-Type: application/json" \
-X POST -d "{\"body\":\"$CODESPELL_RESULTS\"}" \
https://api.github.com/repos/FreeCAD/FreeCAD/issues/$TRAVIS_PULL_REQUEST/comments
# Clean up
rm ./fc-word-whitelist.txt codespell_results.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment