Add ruff linting and formatting CI #1
Workflow file for this run
This file contains hidden or 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: code analysis | |
| on: pull_request | |
| permissions: | |
| contents: read | |
| jobs: | |
| ruff: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Get the list of changed files | |
| id: diff | |
| run: | | |
| git fetch --depth=1 origin $GITHUB_BASE_REF | |
| git diff --name-only origin/$GITHUB_BASE_REF > changed_files.txt | |
| - name: Install ruff | |
| uses: astral-sh/ruff-action@v3 | |
| with: | |
| args: "--version" | |
| - name: Lint code | |
| run: | | |
| files=$(cat changed_files.txt | grep '\.py$' || echo "") | |
| if [ -n "$files" ]; then | |
| echo "$files" | xargs ruff check --diff || true | |
| echo "$files" | xargs ruff check | |
| else | |
| echo "No python files to lint" | |
| fi | |
| - name: Format code | |
| if: always() | |
| run: | | |
| files=$(cat changed_files.txt | grep '\.py$' || echo "") | |
| if [ -n "$files" ]; then | |
| diff_command="" | |
| apply_command="" | |
| for file in $files; do | |
| while IFS=- read -r start length; do | |
| [ -z "$start" ] && continue | |
| length=${length:-1} | |
| # Skip invalid ranges | |
| if [ "$start" -eq 0 ] || [ "$length" -eq 0 ]; then | |
| continue | |
| fi | |
| end=$((start + length)) | |
| diff_command+="ruff format --diff --range $start-$end $file && " | |
| apply_command+="ruff format --range $start-$end $file && " | |
| done < <(git diff --unified=0 origin/$GITHUB_BASE_REF "$file" | grep '^@@' | sed -E 's/^@@ -[0-9]+(,[0-9]+)? \+([0-9]+)(,([0-9]+))? @@.*/\2-\4/') | |
| done | |
| if [ -n "$diff_command" ]; then | |
| diff_command=${diff_command% && } | |
| if ! eval "$diff_command"; then | |
| apply_command=${apply_command% && } | |
| echo -e "::error::Formatting failed. To apply the changes locally, run the following command:\n$apply_command" | |
| exit 123 | |
| fi | |
| else | |
| echo "No ranges detected to format." | |
| fi | |
| else | |
| echo "No python files to format" | |
| fi |