name: Nightly rustfmt
on:
  schedule:
    - cron: "0 0 * * *" # runs daily at 00:00 UTC
  workflow_dispatch: # allows manual triggering

permissions: {}

jobs:
  format:
    name: Nightly rustfmt
    runs-on: self-hosted
    permissions:
      contents: write
      pull-requests: write
    steps:
      - uses: actions/checkout@v4
        with:
          ref: main
          token: ${{ secrets.BACKPORT_TOKEN }}
      - uses: cachix/cachix-action@v16
        with:
          name: cashudevkit
          authToken: ${{ secrets.CACHIX_AUTH_TOKEN }}
          useDaemon: false
        continue-on-error: true
      - name: Run Nightly rustfmt
        run: |
          nix develop -i -L .#nightly --command bash -c '
            # Force use of Nix-provided rustfmt
            export RUSTFMT=$(command -v rustfmt)
            cargo fmt
          '
          # Manually remove trailing whitespace
          find . -name '*.rs' -type f -exec sed -E -i 's/[[:space:]]+$//' {} +
      - name: Get the current date
        run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_ENV
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@271a8d0340265f705b14b6d32b9829c1cb33d45e # v7.0.8
        env:
          PRE_COMMIT_ALLOW_NO_CONFIG: 1
        with:
          token: ${{ secrets.BACKPORT_TOKEN }}
          author: Fmt Bot <bot@cashudevkit.org>
          title: Automated nightly rustfmt (${{ env.date }})
          body: |
            Automated nightly `rustfmt` changes by [create-pull-request](https://github.com/peter-evans/create-pull-request) GitHub action
          commit-message: ${{ env.date }} automated rustfmt nightly
          labels: rustfmt
          branch: automated-rustfmt-${{ env.date }}
