name: merge_flake_lock_update
on:
  workflow_dispatch:
  schedule:
    - cron: "0 2 * * 6"

jobs:
  merge:
    runs-on: ubuntu-latest

    permissions:
      contents: write
      pull-requests: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: merge_flake_lock_update
        run: |
          pr_number=$(gh pr list --state open --author RichieCahill --label flake_lock_update --json number --jq '.[0].number')
          echo "pr_number=$pr_number" >> $GITHUB_ENV
          if [ -n "$pr_number" ]; then
            gh pr merge "$pr_number" --rebase
          else
            echo "No open PR found with label flake_lock_update"
          fi
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN_FOR_UPDATES }}
