name: Update flake.lock

on:
  push:
    branches: [master]
    paths: [go.sum]
  workflow_dispatch:

permissions:
  contents: write
  pull-requests: write

jobs:
  update:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7

      - uses: DeterminateSystems/nix-installer-action@main

      - name: Update flake.lock
        run: nix flake update

      - name: Create Pull Request
        env:
          GH_TOKEN: ${{ secrets.HOMEBREW_GITHUB_TOKEN }}
        run: |
          git diff --quiet flake.lock && exit 0
          BRANCH="chore/update-flake-lock"
          git config user.name "Floatpane Bot"
          git config user.email "us@floatpane.com"
          git checkout -b "$BRANCH"
          git add flake.lock
          git commit -m "chore: update flake.lock"
          git push -f origin "$BRANCH"
          if ! gh pr list --head "$BRANCH" --state open | grep -q .; then
            BODY=$(cat <<'EOF'
          ## What?

          Updates `flake.lock` to the latest revisions of all flake inputs (`nixpkgs`, `flake-utils`, etc.).

          ## Why?

          Keeps Nix inputs current so contributors and CI build against fresh `nixpkgs`. Picks up upstream security and toolchain fixes. Generated automatically by the flake-lock update workflow on changes to `go.sum`.
          EOF
          )
            gh pr create --title "chore: update flake.lock" --body "$BODY" --base master --label chore --label area/nix
          fi
