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@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0

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

      - name: Setup Nix
        uses: cachix/install-nix-action@19effe9fe722874e6d46dd7182e4b8b7a43c4a99 # v31.10.0
        with:
          nix_path: nixpkgs=channel:nixos-unstable
      - uses: cachix/cachix-action@3ba601ff5bbb07c7220846facfa2cd81eeee15a1 # v16
        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@c0f553fe549906ede9cf27b5156039d195d2ece0 # v8.1.0
        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
