-
Notifications
You must be signed in to change notification settings - Fork 47
feature(github): added github action to check formatting in Python files #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
21561c8
71b3971
e66cec2
24cdda4
c658d03
0961f88
be6112e
c06a71f
a7d6710
4bf599b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| name: Python Code Quality Check | ||
| name: Python Check | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
| paths: | ||
| - '**.py' | ||
|
|
||
| jobs: | ||
| lint: | ||
| code-quality-check: | ||
| name: Code Quality | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
|
|
@@ -15,11 +17,32 @@ jobs: | |
| - name: Set up Python | ||
| uses: actions/setup-python@v5 | ||
| with: | ||
| python-version: '3.10' | ||
| python-version: '3.12' | ||
|
|
||
| - name: Install dependencies | ||
| run: | | ||
| pip install flake8 | ||
| pip install flake8 black | ||
|
|
||
| - name: Run black and generate diff | ||
| id: black_check | ||
| run: | | ||
| echo "Running black in check mode with diff output..." | ||
| black --check --diff --line-length 99 . > formatting.diff || true | ||
|
|
||
| if [ -s formatting.diff ]; then | ||
| echo "Formatting issues detected:" | ||
| cat formatting.diff | ||
| echo "black_failed=true" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "No formatting issues found." | ||
|
varundhall marked this conversation as resolved.
|
||
| fi | ||
|
|
||
| - name: Upload formatting.diff | ||
| if: steps.black_check.outputs.black_failed == 'true' | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: python-formatting-diff | ||
| path: formatting.diff | ||
|
|
||
| - name: Detect changed Python files | ||
| id: detect_changes | ||
|
|
@@ -42,7 +65,6 @@ jobs: | |
| fi | ||
|
|
||
| - name: Run Flake8 on changed files | ||
| if: env.SKIP != 'true' | ||
| run: | | ||
| echo "Running flake8 on: $CHANGED" | ||
| flake8 $CHANGED > flake8_output.txt || true | ||
|
Comment on lines
67
to
70
|
||
|
|
@@ -53,12 +75,8 @@ jobs: | |
| import os | ||
|
|
||
| try: | ||
| skip = os.getenv("SKIP") == "true" | ||
| if skip: | ||
| lines = [] | ||
| else: | ||
| with open("flake8_output.txt") as f: | ||
| lines = f.read().splitlines() | ||
| with open("flake8_output.txt") as f: | ||
| lines = f.read().splitlines() | ||
|
|
||
| except FileNotFoundError: | ||
| lines = [] | ||
|
|
@@ -86,7 +104,7 @@ jobs: | |
| EOF | ||
|
|
||
| - name: Upload full report | ||
| if: env.SKIP != 'true' && env.FLAKE8_ISSUE_PRESENT == 'true' | ||
| if: env.FLAKE8_ISSUE_PRESENT == 'true' | ||
| uses: actions/upload-artifact@v4 | ||
| with: | ||
| name: python-code-quality-report | ||
|
|
@@ -108,10 +126,25 @@ jobs: | |
| body-path: flake8_summary.md | ||
| edit-mode: replace | ||
|
|
||
| - name: Fail if Flake8 issues found | ||
| if: env.SKIP != 'true' | ||
| - name: Fail if both black and flake8 failed | ||
| if: steps.black_check.outputs.black_failed == 'true' && env.FLAKE8_ISSUE_PRESENT == 'true' | ||
| run: | | ||
| if [ "$FLAKE8_ISSUE_PRESENT" == "true" ]; then | ||
| echo "❌ Issues detected in Python files. Please fix them before merging." | ||
| exit 1 | ||
| fi | ||
| echo "Python formatting check failed. See formatting.diff artifact for details." | ||
| echo "To fix the error(s) run the command below from the root of the repo and commit the changes:" | ||
| echo "./scripts/fix-python-formatting.sh" | ||
| echo "We also detected Linting Issues in Python files. Please fix them before merging." | ||
| exit 1 | ||
|
|
||
| - name: Fail if only black failed | ||
| if: steps.black_check.outputs.black_failed == 'true' && env.FLAKE8_ISSUE_PRESENT != 'true' | ||
| run: | | ||
| echo "Python formatting check failed. See formatting.diff artifact for details." | ||
| echo "To fix the error(s) run the command below from the root of the repo and commit the changes:" | ||
| echo "./scripts/fix-python-formatting.sh" | ||
| exit 1 | ||
|
|
||
| - name: Fail if only flake8 failed | ||
| if: steps.black_check.outputs.black_failed != 'true' && env.FLAKE8_ISSUE_PRESENT == 'true' | ||
| run: | | ||
| echo "❌ Issues detected in Python files. Please fix them before merging." | ||
| exit 1 | ||
|
varundhall marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,7 +70,8 @@ jobs: | |
| '.circleci', | ||
| '.devops', | ||
| 'ci', | ||
| '.ci' | ||
| '.ci', | ||
| 'scripts' | ||
| ]; | ||
|
|
||
| // Get all files changed in this PR | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -euo pipefail | ||
|
|
||
| echo "Checking if 'black' is installed..." | ||
| if ! command -v black &> /dev/null; then | ||
| echo "Installing black via pip..." | ||
| if command -v pip3 &> /dev/null; then | ||
| pip3 install --user black | ||
| export PATH="$HOME/.local/bin:$PATH" | ||
| elif command -v pip &> /dev/null; then | ||
| pip install --user black | ||
| export PATH="$HOME/.local/bin:$PATH" | ||
| else | ||
| echo "pip not found. Please install pip to continue." | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| echo "Finding all Python files..." | ||
| py_files=$(find . -type f -name '*.py') | ||
|
|
||
| if [ -z "$py_files" ]; then | ||
| echo "No Python files found to format." | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "Formatting all Python files with black (line length = 99)..." | ||
| black --line-length 99 $py_files | ||
|
|
||
|
varundhall marked this conversation as resolved.
|
||
| echo "All Python files are now formatted!" | ||
Uh oh!
There was an error while loading. Please reload this page.