name: Update devenv

on:
  schedule:
    - cron: "0 3 * * 2" # Weekly on Tuesdays
  push:
    branches:
      - master
    paths:
      - '.github/workflows/update-devenv.yml'
  workflow_dispatch:

permissions:
  contents: write
  pull-requests: write

jobs:
  update-devenv:
    if: github.repository == 'crystal-lang/crystal'
    runs-on: ubuntu-latest

    steps:
      - name: Download source
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
        with:
          ref: master
          persist-credentials: false

      - uses: cachix/install-nix-action@616559265b40713947b9c190a8ff4b507b5df49b # v31.10.4

      - uses: cachix/cachix-action@1eb2ef646ac0255473d23a5907ad7b04ce94065c # v17
        with:
          name: devenv

      - name: Install devenv.sh
        run: nix profile add nixpkgs#devenv

      - name: Update devenv.lock
        run: devenv update

      - name: Check for changes
        id: changes
        run: |
          git add devenv.lock
          if git diff --cached --quiet; then
            echo "changed=false" >>"$GITHUB_OUTPUT"
          else
            echo "changed=true" >>"$GITHUB_OUTPUT"
          fi

      - name: Create PR
        if: steps.changes.outputs.changed == 'true'
        uses: peter-evans/create-pull-request@c0f553fe549906ede9cf27b5156039d195d2ece0 # v8
        with:
          commit-message: "Update `devenv.lock`"
          title: "Update `devenv.lock`"
          body: ""
          labels: "topic:infrastructure"
          branch: infra/update-devenv
          delete-branch: true
