|
| 1 | +name: Check and Fix Whitespace on Schedule |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + - cron: '0 0 * * *' |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + lint: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - name: Checkout code |
| 13 | + uses: actions/checkout@v2 |
| 14 | + with: |
| 15 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 16 | + |
| 17 | + - name: Set up Python |
| 18 | + uses: actions/setup-python@v2 |
| 19 | + with: |
| 20 | + python-version: '3.x' |
| 21 | + |
| 22 | + - name: Install pre-commit |
| 23 | + run: pip install pre-commit |
| 24 | + |
| 25 | + - name: Run pre-commit hooks and apply fixes |
| 26 | + id: pre-commit |
| 27 | + run: | |
| 28 | + # Run pre-commit with auto-fix and ignore exit code |
| 29 | + pre-commit run --all-files --hook-stage=manual --show-diff-on-failure || true |
| 30 | + # Check if any files were modified |
| 31 | + git diff --exit-code || echo "FIX_NEEDED=true" >> $GITHUB_ENV |
| 32 | + env: |
| 33 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + |
| 35 | + - name: Create and push changes if needed |
| 36 | + if: env.FIX_NEEDED == 'true' |
| 37 | + id: create_branch |
| 38 | + run: | |
| 39 | + git config user.name "github-actions[bot]" |
| 40 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 41 | + # Create a new branch for the changes |
| 42 | + BRANCH_NAME="fix/whitespace-$(date +'%Y%m%d%H%M%S')" |
| 43 | + git checkout -b "$BRANCH_NAME" |
| 44 | + git add . |
| 45 | + git commit -m "fix: apply whitespace fixes" |
| 46 | + git push origin "$BRANCH_NAME" |
| 47 | + echo "branch_name=$BRANCH_NAME" >> $GITHUB_ENV |
| 48 | + env: |
| 49 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 50 | + |
| 51 | + - name: Open Pull Request |
| 52 | + if: env.FIX_NEEDED == 'true' |
| 53 | + uses: repo-sync/pull-request@v2 |
| 54 | + with: |
| 55 | + source_branch: ${{ env.branch_name }} |
| 56 | + destination_branch: ${{ github.ref_name }} |
| 57 | + pr_title: "Fix whitespace issues" |
| 58 | + pr_body: "This PR automatically applies whitespace fixes." |
| 59 | + token: ${{ secrets.GITHUB_TOKEN }} |
0 commit comments