diff --git a/apps/desktop/vite.config.ts b/apps/desktop/vite.config.ts index ba37576815097..e758deab63588 100644 --- a/apps/desktop/vite.config.ts +++ b/apps/desktop/vite.config.ts @@ -3,6 +3,7 @@ import { defaultInclude, defineConfig } from "vite-plus"; import { isDesktopRuntimeExternalDependency } from "../../scripts/lib/desktop-external-packages.ts"; import { loadRepoEnv } from "../../scripts/lib/public-config.ts"; +import { releaseRepositoryDefine } from "../../scripts/lib/release-repository.ts"; const repoEnv = loadRepoEnv(); @@ -11,6 +12,7 @@ const isMainProcessExternal = (id: string) => id === "electron" || id.startsWith("electron/") || isDesktopRuntimeExternalDependency(id); const shouldLaunchElectronAfterPack = process.env.T3CODE_DESKTOP_DEV === "1"; const publicConfigDefine = { + ...releaseRepositoryDefine(), __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: JSON.stringify( repoEnv.T3CODE_CLERK_PUBLISHABLE_KEY?.trim() ?? "", ), diff --git a/apps/server/vite.config.ts b/apps/server/vite.config.ts index 02fe582953abd..ba47cc4e57418 100644 --- a/apps/server/vite.config.ts +++ b/apps/server/vite.config.ts @@ -5,6 +5,7 @@ import { defineConfig, mergeConfig } from "vite-plus"; import baseConfig from "../../vite.config.ts"; import { loadRepoEnv } from "../../scripts/lib/public-config.ts"; +import { releaseRepositoryDefine } from "../../scripts/lib/release-repository.ts"; import rootPackageJson from "../../package.json" with { type: "json" }; import packageJson from "./package.json" with { type: "json" }; import { WeightedShardSequencer } from "./src/testUtils/weightedShardSequencer.ts"; @@ -126,6 +127,7 @@ export default mergeConfig( js: "#!/usr/bin/env node\n", }, define: { + ...releaseRepositoryDefine(), __T3CODE_BUILD_CHANNEL__: JSON.stringify(cliBuildChannel), __T3CODE_TELEMETRY_CHANNEL__: JSON.stringify(telemetryChannel), __T3CODE_BUILD_RELAY_URL__: JSON.stringify(repoEnv.T3CODE_RELAY_URL?.trim() ?? ""), diff --git a/apps/web/tsconfig.json b/apps/web/tsconfig.json index b9cd3ad6b5e47..f64bf416a53a2 100644 --- a/apps/web/tsconfig.json +++ b/apps/web/tsconfig.json @@ -34,6 +34,7 @@ "scripts/verify-built-boot.ts", "scripts/warm-dep-cache.ts", "../../scripts/lib/public-config.ts", + "../../scripts/lib/release-repository.ts", "../../scripts/lib/third-party-licenses.ts" ] } diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index 217294de3762c..ba34eb17d7d99 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -19,6 +19,7 @@ import { } from "@t3tools/shared/devProxy"; import { loadRepoEnv } from "../../scripts/lib/public-config"; +import { releaseRepositoryDefine } from "../../scripts/lib/release-repository"; import { thirdPartyLicensesPlugin } from "../../scripts/lib/third-party-licenses"; import { tailwindPlugins } from "./vite/tailwind"; import { warmBundledDevPlugin } from "./vite/warmBundledDev"; @@ -298,6 +299,7 @@ export default defineConfig(() => { ], }, define: { + ...releaseRepositoryDefine(), "import.meta.env.VITE_WS_URL": JSON.stringify(configuredWsUrl ?? ""), // Override .env URLs with empty values in single-origin dev so clients use window.location.origin. "import.meta.env.VITE_HTTP_URL": JSON.stringify(configuredHttpUrl ?? ""), diff --git a/docs/operations/release.md b/docs/operations/release.md index be74b2ad2982e..4268bcc90d4a7 100644 --- a/docs/operations/release.md +++ b/docs/operations/release.md @@ -477,6 +477,7 @@ available. - Repository slug source: - `T3CODE_DESKTOP_UPDATE_REPOSITORY` (format `owner/repo`), if set. - otherwise `GITHUB_REPOSITORY` from GitHub Actions. + - The server, `t3 update` and desktop SSH installs bake in the same slug (`scripts/lib/release-repository.ts`). - Required release assets for updater: - platform installers (`.exe`, `.dmg`, `.AppImage`, `.deb`, plus macOS `.zip` for Squirrel.Mac update payloads) - channel metadata: `latest*.yml` for stable releases, `nightly*.yml` for nightly releases diff --git a/packages/shared/src/cliRelease.test.ts b/packages/shared/src/cliRelease.test.ts index b0aa61765e45a..5b59e3b767035 100644 --- a/packages/shared/src/cliRelease.test.ts +++ b/packages/shared/src/cliRelease.test.ts @@ -1,6 +1,11 @@ -import { describe, expect, it } from "vite-plus/test"; +import { afterEach, describe, expect, it, vi } from "vite-plus/test"; -import { cliArchiveTarCommand, newestCliReleaseVersion, parseChecksums } from "./cliRelease.ts"; +import { + cliArchiveTarCommand, + newestCliReleaseVersion, + parseChecksums, + resolveCliReleaseRepository, +} from "./cliRelease.ts"; describe("cliRelease", () => { it("parses sha256sum output including binary-mode markers", () => { @@ -40,3 +45,43 @@ describe("cliRelease", () => { expect(newestCliReleaseVersion([{ tag_name: "v1.2.3" }], "preview")).toBeUndefined(); }); }); + +describe("cliRelease repository", () => { + afterEach(() => { + vi.unstubAllGlobals(); + vi.resetModules(); + }); + + it("defaults to upstream when the build names no repository", () => { + expect(resolveCliReleaseRepository(undefined)).toBe("pingdotgg/t3code"); + expect(resolveCliReleaseRepository("")).toBe("pingdotgg/t3code"); + expect(resolveCliReleaseRepository(" ")).toBe("pingdotgg/t3code"); + }); + + it("lists and downloads releases only from the repository the build names", async () => { + vi.stubGlobal("__T3CODE_RELEASE_REPOSITORY__", "fork-owner/t3code"); + vi.resetModules(); + const release = await import("./cliRelease.ts"); + expect(release.cliReleaseIndexPageUrl(2)).toBe( + "https://api.github.com/repos/fork-owner/t3code/releases?per_page=100&page=2", + ); + expect(release.cliReleaseDownloadBaseUrl("0.1.9-fork.1")).toBe( + "https://github.com/fork-owner/t3code/releases/download/v0.1.9-fork.1", + ); + expect(release.cliReleaseAssetUrl("0.1.9-fork.1", "SHA256SUMS")).toBe( + "https://github.com/fork-owner/t3code/releases/download/v0.1.9-fork.1/SHA256SUMS", + ); + expect(release.CLI_RELEASES_PAGE_URL).toBe("https://github.com/fork-owner/t3code/releases"); + }); + + it("keeps upstream URLs in a build without the setting", async () => { + vi.resetModules(); + const release = await import("./cliRelease.ts"); + expect(release.cliReleaseIndexPageUrl(1)).toBe( + "https://api.github.com/repos/pingdotgg/t3code/releases?per_page=100&page=1", + ); + expect(release.cliReleaseDownloadBaseUrl("0.1.9")).toBe( + "https://github.com/pingdotgg/t3code/releases/download/v0.1.9", + ); + }); +}); diff --git a/packages/shared/src/cliRelease.ts b/packages/shared/src/cliRelease.ts index 07a0c73b17947..9c12b4cc426d6 100644 --- a/packages/shared/src/cliRelease.ts +++ b/packages/shared/src/cliRelease.ts @@ -3,7 +3,19 @@ import * as Schema from "effect/Schema"; import { compareSemverVersions } from "./semver.ts"; -const CLI_RELEASE_REPOSITORY = "pingdotgg/t3code"; +// Set at build time from T3CODE_DESKTOP_UPDATE_REPOSITORY (scripts/lib/release-repository.ts), the +// desktop updater's setting, so a fork's builds list and download only the fork's releases. +declare const __T3CODE_RELEASE_REPOSITORY__: string | undefined; +const UPSTREAM_RELEASE_REPOSITORY = "pingdotgg/t3code"; + +/** The repository releases come from: the build's setting, or upstream when it has none. */ +export function resolveCliReleaseRepository(configured: string | undefined): string { + return configured?.trim() || UPSTREAM_RELEASE_REPOSITORY; +} + +const CLI_RELEASE_REPOSITORY = resolveCliReleaseRepository( + typeof __T3CODE_RELEASE_REPOSITORY__ === "undefined" ? undefined : __T3CODE_RELEASE_REPOSITORY__, +); export const CLI_RELEASES_PAGE_URL = `https://github.com/${CLI_RELEASE_REPOSITORY}/releases`; export const CLI_RELEASE_CHECKSUMS_FILE = "SHA256SUMS"; export const CLI_RELEASE_BASE_URL_ENV = "T3CODE_RELEASE_BASE_URL"; diff --git a/scripts/lib/release-repository.test.ts b/scripts/lib/release-repository.test.ts new file mode 100644 index 0000000000000..46a285b29425e --- /dev/null +++ b/scripts/lib/release-repository.test.ts @@ -0,0 +1,72 @@ +import { it as effectIt } from "@effect/vitest"; +import * as ConfigProvider from "effect/ConfigProvider"; +import * as Effect from "effect/Effect"; +import { describe, expect, it } from "vite-plus/test"; + +import { resolveGitHubPublishConfig } from "../build-desktop-artifact.ts"; +import { releaseRepositoryDefine, resolveReleaseRepository } from "./release-repository.ts"; + +const desktopFeedRepository = (env: Record) => + resolveGitHubPublishConfig("latest").pipe( + Effect.map((config) => (config ? `${config.owner}/${config.repo}` : "")), + Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))), + ); + +describe("release repository build setting", () => { + it("is empty (the upstream default) when unset or blank", () => { + expect(resolveReleaseRepository({})).toBe(""); + expect(resolveReleaseRepository({ T3CODE_DESKTOP_UPDATE_REPOSITORY: " " })).toBe(""); + }); + + it("is upstream's desktop update setting, so the server follows the desktop feed", () => { + const env = { T3CODE_DESKTOP_UPDATE_REPOSITORY: " some-owner/t3code " }; + expect(resolveReleaseRepository(env)).toBe("some-owner/t3code"); + expect(releaseRepositoryDefine(env)).toEqual({ + __T3CODE_RELEASE_REPOSITORY__: JSON.stringify("some-owner/t3code"), + }); + }); + + it("names the repository a GitHub Actions build runs in, unless the setting overrides it", () => { + const actions = { GITHUB_ACTIONS: "true", GITHUB_REPOSITORY: "fork-owner/t3code" }; + expect(resolveReleaseRepository(actions)).toBe("fork-owner/t3code"); + expect( + resolveReleaseRepository({ ...actions, T3CODE_DESKTOP_UPDATE_REPOSITORY: "other/t3code" }), + ).toBe("other/t3code"); + }); + + effectIt.effect("names the same repository as the desktop updater feed for every setting", () => + Effect.gen(function* () { + const cases: Array> = [ + {}, + { T3CODE_DESKTOP_UPDATE_REPOSITORY: "fork-owner/t3code" }, + { GITHUB_REPOSITORY: "fork-owner/t3code" }, + { T3CODE_DESKTOP_UPDATE_REPOSITORY: "a/t3code", GITHUB_REPOSITORY: "b/t3code" }, + { T3CODE_RELEASE_REPOSITORY: "b/t3code", GITHUB_REPOSITORY: "b/t3code" }, + ]; + for (const env of cases) { + const feed = yield* desktopFeedRepository(env); + expect(resolveReleaseRepository(env), JSON.stringify(env)).toBe(feed); + } + }), + ); + + it("fails the build when the former setting disagrees with the desktop updater's", () => { + for (const env of [ + { T3CODE_RELEASE_REPOSITORY: "fork-owner/t3code" }, + { T3CODE_RELEASE_REPOSITORY: "b/t3code", T3CODE_DESKTOP_UPDATE_REPOSITORY: "a/t3code" }, + { T3CODE_RELEASE_REPOSITORY: "b/t3code", GITHUB_REPOSITORY: "a/t3code" }, + ]) { + expect(() => resolveReleaseRepository(env), JSON.stringify(env)).toThrow( + /T3CODE_RELEASE_REPOSITORY=".+" disagrees.*Set T3CODE_DESKTOP_UPDATE_REPOSITORY/, + ); + } + }); + + it("fails the build on a malformed value instead of falling back to upstream", () => { + for (const value of ["owner", "owner/repo/extra", "owner/..", "https://github.com/o/r"]) { + expect(() => resolveReleaseRepository({ T3CODE_DESKTOP_UPDATE_REPOSITORY: value })).toThrow( + /T3CODE_DESKTOP_UPDATE_REPOSITORY/, + ); + } + }); +}); diff --git a/scripts/lib/release-repository.ts b/scripts/lib/release-repository.ts new file mode 100644 index 0000000000000..ee0d22b7f4cee --- /dev/null +++ b/scripts/lib/release-repository.ts @@ -0,0 +1,45 @@ +/** + * The repository a build updates from, for the server, CLI and desktop SSH installs. It is the one + * upstream already uses for the desktop updater feed and release links (docs/operations/release.md): + * T3CODE_DESKTOP_UPDATE_REPOSITORY, else GITHUB_REPOSITORY, else "" for upstream. + */ +export const RELEASE_REPOSITORY_ENV = "T3CODE_DESKTOP_UPDATE_REPOSITORY"; +// The setting's first name in this fork; still accepted only when it names the same repository. +const FORMER_RELEASE_REPOSITORY_ENV = "T3CODE_RELEASE_REPOSITORY"; + +const SEGMENT = /^[A-Za-z0-9._-]+$/u; + +function isValidSegment(value: string | undefined): value is string { + return value !== undefined && value !== "." && value !== ".." && SEGMENT.test(value); +} + +/** The configured repository, or "" for the upstream default. Throws on a malformed value or a + * disagreeing former setting, so no build ships the desktop and the server on different feeds. */ +export function resolveReleaseRepository( + env: Readonly> = process.env, +): string { + // The same order as apps/web/vite.config.ts and resolveGitHubPublishConfig read them. + const configured = env[RELEASE_REPOSITORY_ENV]?.trim() || env.GITHUB_REPOSITORY?.trim() || ""; + const former = env[FORMER_RELEASE_REPOSITORY_ENV]?.trim() || ""; + if (former !== "" && former !== configured) { + throw new Error( + `${FORMER_RELEASE_REPOSITORY_ENV}="${former}" disagrees with the repository the desktop ` + + `updater uses ("${configured || "pingdotgg/t3code"}"). Set ${RELEASE_REPOSITORY_ENV} instead.`, + ); + } + if (configured === "") return ""; + const [owner, name, ...rest] = configured.split("/"); + if (!isValidSegment(owner) || !isValidSegment(name) || rest.length > 0) { + throw new Error( + `${RELEASE_REPOSITORY_ENV} (or GITHUB_REPOSITORY) must be "/", got "${configured}".`, + ); + } + return configured; +} + +/** The bundler `define` entry every build that bundles `@t3tools/shared/cliRelease` carries. */ +export function releaseRepositoryDefine( + env: Readonly> = process.env, +): Record { + return { __T3CODE_RELEASE_REPOSITORY__: JSON.stringify(resolveReleaseRepository(env)) }; +}