diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e3ce1e5..fa0c456 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -31,6 +31,7 @@ jobs: timeout-minutes: 10 permissions: contents: read + actions: read env: # The build otherwise reports usage to telemetry.astro.build. ASTRO_TELEMETRY_DISABLED: 1 @@ -58,6 +59,11 @@ jobs: working-directory: site - run: npm test working-directory: site + # The home page's "Last count" is when Likes were last counted, not when the site was built. + - name: Find the last Likes count + run: echo "LIKES_AT=$(gh run list -R "$GITHUB_REPOSITORY" -w likes.yml -s success -L 1 --json updatedAt --jq '.[0].updatedAt // ""')" >> "$GITHUB_ENV" + env: + GH_TOKEN: ${{ github.token }} - run: npm run build working-directory: site - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 diff --git a/site/src/lib/format.ts b/site/src/lib/format.ts index 10ce83d..2c42061 100644 --- a/site/src/lib/format.ts +++ b/site/src/lib/format.ts @@ -49,3 +49,10 @@ export const pct = (likes: number, delta: number) => { const before = likes - delta; return before > 0 ? Math.round((delta / before) * 100) : null; }; + +// When Likes were last counted: deploy passes the last successful Sync Likes run. +// Without it (local and PR builds) the build time stands in. +export function lastCount(v: string | undefined, now = new Date()): Date { + const ok = v && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/.test(v); + return ok ? new Date(v) : now; +} diff --git a/site/src/pages/index.astro b/site/src/pages/index.astro index 449f00d..087808f 100644 --- a/site/src/pages/index.astro +++ b/site/src/pages/index.astro @@ -3,12 +3,12 @@ import Base from "../layouts/Base.astro"; import Leaderboard from "../components/Leaderboard.astro"; import Explain from "../components/Explain.astro"; import { loadAll, liveList, trending } from "../lib/data.ts"; -import { centralStamp, icon, num } from "../lib/format.ts"; +import { centralStamp, icon, lastCount, num } from "../lib/format.ts"; const { entries, categories } = loadAll(); const live = liveList(entries); const climber = trending(entries)[0]; -const built = new Date(); +const built = lastCount(process.env.LIKES_AT); const stamp = centralStamp(built); --- diff --git a/site/tests/format.test.mjs b/site/tests/format.test.mjs index 17f7d15..8a4fb08 100644 --- a/site/tests/format.test.mjs +++ b/site/tests/format.test.mjs @@ -1,6 +1,6 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import { centralStamp } from "../src/lib/format.ts"; +import { centralStamp, lastCount } from "../src/lib/format.ts"; test("centralStamp shows CST in winter", () => { assert.equal( @@ -15,3 +15,14 @@ test("centralStamp shows CDT in summer", () => { "Jul 1, 12:00 PM CDT", ); }); + +test("lastCount uses the last Likes run, else the build time", () => { + const now = new Date("2026-11-07T12:00:00Z"); + assert.equal( + lastCount("2026-11-07T09:17:42Z", now).toISOString(), + "2026-11-07T09:17:42.000Z", + ); + for (const bad of [undefined, "", "null", "yesterday", "2026-11-07"]) { + assert.equal(lastCount(bad, now), now); + } +});