name: Update iTerm2 colorschemes
on:
  schedule:
    # Once a week
    - cron: "0 0 * * 0"
  workflow_dispatch:
jobs:
  update-iterm2-schemes:
    if: github.repository == 'ghostty-org/ghostty'
    runs-on: namespace-profile-ghostty-sm
    permissions:
      # Needed for create-pull-request action
      contents: write
      pull-requests: write
    env:
      ZIG_LOCAL_CACHE_DIR: /zig/local-cache
      ZIG_GLOBAL_CACHE_DIR: /zig/global-cache
    steps:
      - name: Checkout code
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          fetch-depth: 0

      - name: Setup Cache
        uses: namespacelabs/nscloud-cache-action@58bf6e08898e88803c098e2b522668541cd3b2e3 # v1.6.0
        with:
          path: |
            /nix
            /zig

      - name: Setup Nix
        uses: cachix/install-nix-action@a49548c11d9846ad46ecc0115273879b045f001c # v31.10.7
        with:
          nix_path: nixpkgs=channel:nixos-unstable
      - uses: cachix/cachix-action@5f2d7c5294214f71b873db4b969586b980625e71 # v17
        with:
          name: ghostty
          authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"

      - name: Download colorschemes
        id: download
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          # Get the latest release from iTerm2-Color-Schemes
          RELEASE_INFO=$(gh api repos/mbadolato/iTerm2-Color-Schemes/releases/latest)
          TAG_NAME=$(echo "$RELEASE_INFO" | jq -r '.tag_name')
          FILENAME="ghostty-themes-${TAG_NAME}.tgz"
          mkdir -p upload
          curl -L -o "upload/${FILENAME}" "https://github.com/mbadolato/iTerm2-Color-Schemes/releases/download/${TAG_NAME}/ghostty-themes.tgz"
          echo "tag_name=$TAG_NAME" >> $GITHUB_OUTPUT
          echo "filename=$FILENAME" >> $GITHUB_OUTPUT

      - name: Upload to R2
        uses: ryand56/r2-upload-action@b801a390acbdeb034c5e684ff5e1361c06639e7c # v1.4
        with:
          r2-account-id: ${{ secrets.CF_R2_DEPS_ACCOUNT_ID }}
          r2-access-key-id: ${{ secrets.CF_R2_DEPS_AWS_KEY }}
          r2-secret-access-key: ${{ secrets.CF_R2_DEPS_SECRET_KEY }}
          r2-bucket: ghostty-deps
          source-dir: upload
          destination-dir: ./

      - name: Run zig fetch
        run: |
          nix develop -c zig fetch --save="iterm2_themes" "https://deps.files.ghostty.org/${{ steps.download.outputs.filename }}"

      - name: Update zig cache hash
        run: |
          # Only proceed if build.zig.zon has changed
          if ! git diff --exit-code build.zig.zon; then
            nix develop -c ./nix/build-support/check-zig-cache.sh --update
            nix develop -c ./nix/build-support/check-zig-cache.sh
          fi

      # Verify the build still works. We choose an arbitrary build type
      # as a canary instead of testing all build types.
      - name: Test Build
        run: nix build .#ghostty

      - name: Create pull request
        uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1
        with:
          title: Update iTerm2 colorschemes
          base: main
          branch: iterm2_colors_action
          commit-message: "deps: Update iTerm2 color schemes"
          add-paths: |
            build.zig.zon
            build.zig.zon.nix
            build.zig.zon.txt
            build.zig.zon.json
            flatpak/zig-packages.json
          body: |
            Upstream release: https://github.com/mbadolato/iTerm2-Color-Schemes/releases/tag/${{ steps.download.outputs.tag_name }}
          labels: dependencies
