diff --git a/.github/workflows/format-python.yml b/.github/workflows/format-python.yml deleted file mode 100644 index f5f84e1..0000000 --- a/.github/workflows/format-python.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Format Python -on: - pull_request: - types: [ opened, edited, reopened, synchronize, ready_for_review ] - workflow_dispatch: -jobs: - format: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - with: - repository: ${{ github.event.pull_request.head.repo.full_name }} - ref: ${{ github.event.pull_request.head.ref }} - - name: Format code with black - run: | - pip install black - black --line-length 120 . - - name: Commit changes - uses: EndBug/add-and-commit@v9 - with: - default_author: github_actions - message: "Formatted code with black --line-length 120" - add: "." diff --git a/.github/workflows/python-checks.yml b/.github/workflows/python-checks.yml new file mode 100644 index 0000000..eb4b3dd --- /dev/null +++ b/.github/workflows/python-checks.yml @@ -0,0 +1,61 @@ +name: Ensure formatted code +on: + pull_request: + types: [ opened, edited, reopened, synchronize, ready_for_review ] + workflow_dispatch: + +permissions: + contents: read + pull-requests: write + +jobs: + format_check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install ruff + run: pip install ruff + - name: Check formatting for Python code + env: + GH_TOKEN: ${{ github.token }} + run: | + set +e + format_output=$(ruff format --check) + exit_code=$? + if [ $exit_code -eq 0 ]; then + echo "All Python code is properly formatted." + gh pr comment ${{ github.event.pull_request.number }} --body "All Python code is properly formatted." + else + echo "$format_output" + echo "Errors:" > format_output.txt + echo '```' >> format_output.txt + echo "$format_output" >> format_output.txt + echo '```' >> format_output.txt + echo "Some Python code is not properly formatted." >> format_output.txt + echo "Please run 'ruff format' to format the code." >> format_output.txt + gh pr comment ${{ github.event.pull_request.number }} --bodyfile format_output.txt + exit 1 + fi + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install ruff + run: pip install ruff + - name: Lint Python code + env: + GH_TOKEN: ${{ github.token }} + run: | + set +e + check_output=$(ruff check) + exit_code=$? + if [ $exit_code -eq 0 ]; then + echo "All Python code is properly formatted." + gh pr comment ${{ github.event.pull_request.number }} --body "No linting errors found." + else + echo "$check_output" > check_output.txt + echo "Some Python code has linting errors." >> check_output.txt + echo "Please run 'ruff check --fix' to fix the errors." >> check_output.txt + gh pr comment ${{ github.event.pull_request.number }} --body "```\n$(cat check_output.txt)\n```" + exit 1 + fi