name: Itaca stack

# Every Monday: is a newer version of the Itaca stack (create-itaca) out than the one in itaca.json?
# Then it opens an issue saying how to upgrade (AGENTS.md, "Staying on the standard") and closes the
# one for an older version. It never changes code. Only versions published a day ago or more count:
# pnpm doesn't install newer ones (minimumReleaseAge in pnpm-workspace.yaml).

on:
  schedule:
    - cron: "0 7 * * 1"
  workflow_dispatch:

permissions:
  contents: read
  issues: write

jobs:
  check:
    name: Newer stack version
    runs-on: ubuntu-latest
    timeout-minutes: 5
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          persist-credentials: false
      - name: Compare itaca.json with npm
        id: versions
        run: |
          node --input-type=module <<'EOF' >> "$GITHUB_OUTPUT"
          import { readFileSync } from "node:fs";
          const manifest = JSON.parse(readFileSync("itaca.json", "utf8"));
          const current = manifest.version ?? manifest.createdWith.replace(/^create-itaca@/, "");
          const res = await fetch("https://registry.npmjs.org/create-itaca");
          if (!res.ok) throw new Error(`npm registry: ${res.status}`);
          const { time } = await res.json();
          const parse = (v) => v.split(".").map(Number);
          const newer = (a, b) => { const [x, y] = [parse(a), parse(b)]; for (let i = 0; i < 3; i++) if (x[i] !== y[i]) return x[i] > y[i]; return false; };
          const dayAgo = Date.now() - 24 * 60 * 60 * 1000;
          const latest = Object.keys(time)
            .filter((v) => /^\d+\.\d+\.\d+$/.test(v) && Date.parse(time[v]) <= dayAgo)
            .reduce((a, b) => (newer(b, a) ? b : a), current);
          console.log(`current=${current}`);
          console.log(`latest=${latest}`);
          EOF
      - name: Open the upgrade issue
        if: steps.versions.outputs.latest != steps.versions.outputs.current
        env:
          GH_TOKEN: ${{ github.token }}
          CURRENT: ${{ steps.versions.outputs.current }}
          LATEST: ${{ steps.versions.outputs.latest }}
        run: |
          title="Itaca stack $LATEST is available"
          open=$(gh issue list --state open --limit 100 --json number,title --jq '.[] | select(.title | test("^Itaca stack [0-9.]+ is available$")) | "\(.number) \(.title)"')
          if printf '%s\n' "$open" | grep -qF " $title"; then
            echo "Already open: $title"
            exit 0
          fi
          printf '%s\n' "$open" | while read -r number _; do
            [ -n "$number" ] && gh issue close "$number" --comment "Replaced by the issue for $LATEST."
          done
          body=$(cat <<EOF
          This project is on version **$CURRENT** of the Itaca stack (\`itaca.json\`). Version **$LATEST** is out.

          What changed and what an existing project has to do: the migration notes of each version, in [create-itaca's package](https://cdn.jsdelivr.net/npm/create-itaca@$LATEST/migrations/).

          **How to upgrade** (an Itaca engineer, or an agent with Itaca engineering reviewing):

          - With Claude Code: \`/upgrade\`.
          - By hand: on an up-to-date \`main\` with nothing pending, \`pnpm create itaca@$LATEST upgrade\`, then follow \`ITACA-UPGRADE.md\`.

          It's delivered in a pull request that only does the upgrade. See \`AGENTS.md\`, "Staying on the standard".
          EOF
          )
          gh issue create --title "$title" --body "$body"
