From d28260c67998c7a9a223d2dfd9bda4b8e2022f8b Mon Sep 17 00:00:00 2001 From: Lina Wolf <48202465+linawolf@users.noreply.github.com> Date: Fri, 27 Dec 2024 06:02:22 +0100 Subject: [PATCH] [TASK] Automatically fix whitespace issues (#1208) * [TASK] Automatically fix whitespace issues Releases: main, 13.4, 12.4 * Update apply-precommit.yml --- .github/workflows/apply-precommit.yml | 70 +++++++++++++++++++++++++++ .pre-commit-config.yaml | 6 +++ 2 files changed, 76 insertions(+) create mode 100644 .github/workflows/apply-precommit.yml create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/apply-precommit.yml b/.github/workflows/apply-precommit.yml new file mode 100644 index 00000000..831486e7 --- /dev/null +++ b/.github/workflows/apply-precommit.yml @@ -0,0 +1,70 @@ +name: Check and Fix Whitespace on Schedule + +on: + schedule: + - cron: '0 0 * * *' + workflow_dispatch: + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + + - name: Install pre-commit + run: pip install pre-commit + + - name: Run pre-commit hooks and apply fixes + id: pre-commit + run: | + # Run pre-commit with auto-fix and ignore exit code + pre-commit run --all-files --hook-stage=manual --show-diff-on-failure || true + # Check if any files were modified + git diff --exit-code || echo "FIX_NEEDED=true" >> $GITHUB_ENV + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Check for existing PR + if: env.FIX_NEEDED == 'true' + id: check_pr + run: | + EXISTING_PR=$(gh pr list --state open --search "Fix whitespace issues" --json number -q '.[].number') + if [[ -n "$EXISTING_PR" ]]; then + echo "EXISTING_PR=true" >> $GITHUB_ENV + fi + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Create and push changes if needed + if: env.FIX_NEEDED == 'true' && env.EXISTING_PR != 'true' + id: create_branch + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + # Create a new branch for the changes + BRANCH_NAME="fix/whitespace-$(date +'%Y%m%d%H%M%S')" + git checkout -b "$BRANCH_NAME" + git add . + git commit -m "fix: apply whitespace fixes" + git push origin "$BRANCH_NAME" + echo "branch_name=$BRANCH_NAME" >> $GITHUB_ENV + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Open Pull Request + if: env.FIX_NEEDED == 'true' && env.EXISTING_PR != 'true' + uses: repo-sync/pull-request@v2 + with: + source_branch: ${{ env.branch_name }} + destination_branch: ${{ github.ref_name }} + pr_title: "Fix whitespace issues" + pr_body: "This PR automatically applies whitespace fixes." + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..7be14764 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,6 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 # Use the latest version + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer