Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions tools/publish-template-tag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env tsx
/**
* Syncs the template to the given version (with retry), then commits, tags
* template-vX.X.X, and pushes.
* Waits until npm serves the given version, syncs the template to it (with
* retry), then commits, tags template-vX.X.X, and pushes.
*
* Used by the private secure release repo during the template-sync step.
* Changes here affect the release pipeline.
Expand All @@ -11,6 +11,9 @@ import { spawnSync } from "node:child_process";
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";

import { pushTemplateTag } from "./push-template-tag";
import { waitForNpmVersion } from "./wait-for-npm-version";

const ROOT = process.cwd();
const version = process.argv[2];
if (!version) {
Expand All @@ -34,12 +37,12 @@ function run(
// 1. Update template package.json
const templatePath = join(ROOT, "template", "package.json");
const templateJson = JSON.parse(readFileSync(templatePath, "utf-8"));
const pinned = ["@databricks/appkit", "@databricks/appkit-ui"].filter(
(name) => name in (templateJson.dependencies ?? {}),
);
if (templateJson.dependencies) {
if ("@databricks/appkit" in templateJson.dependencies) {
templateJson.dependencies["@databricks/appkit"] = version;
}
if ("@databricks/appkit-ui" in templateJson.dependencies) {
templateJson.dependencies["@databricks/appkit-ui"] = version;
for (const name of pinned) {
templateJson.dependencies[name] = version;
}
writeFileSync(templatePath, `${JSON.stringify(templateJson, null, 2)}\n`);
console.log(`✓ template/package.json → ${version}`);
Expand Down Expand Up @@ -97,9 +100,20 @@ if (npmConfig.status !== 0 || npmConfig.stdout.trim() === "undefined") {
process.exit(1);
}

// 2a. npm → package-lock.json
// Install only once the registry serves the new version; the retries below
// are too short to outlast registry propagation.
await waitForNpmVersion(pinned, version, { cwd: templateDir }).catch(
(error: Error) => {
console.error(error.message);
process.exit(1);
},
);

// 2a. npm → package-lock.json (--prefer-online: a stale cached packument
// would otherwise fail every attempt with ETARGET)
const npmExit = await runInstallWithRetry("npm install", "npm", [
"install",
"--prefer-online",
`--min-release-age=${RELEASE_AGE_DAYS}`,
`--min-release-age-exclude=${RELEASE_AGE_EXCLUDE}`,
]);
Expand Down Expand Up @@ -144,28 +158,17 @@ if (pnpmExit !== 0) {
console.log("✓ template/pnpm-lock.yaml updated (pnpm install)");

// 3. Git add, commit, tag, push
const commands: [string, string[]][] = [
const pushed = pushTemplateTag(
version,
[
"git",
[
"add",
"template/package.json",
"template/package-lock.json",
"template/pnpm-lock.yaml",
],
"template/package.json",
"template/package-lock.json",
"template/pnpm-lock.yaml",
],
[
"git",
["commit", "-s", "-m", `chore: sync template to v${version} [skip ci]`],
],
["git", ["tag", "-a", `template-v${version}`, "-m", `Template v${version}`]],
["git", ["push", "origin", "HEAD", "--follow-tags"]],
];

for (const [command, args] of commands) {
if (run(command, args) !== 0) {
process.exit(1);
}
ROOT,
);
if (!pushed) {
process.exit(1);
}

console.log(`✓ template tag template-v${version} pushed`);
102 changes: 102 additions & 0 deletions tools/push-template-tag.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { spawnSync } from "node:child_process";
import {
mkdirSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import { afterEach, beforeEach, expect, test, vi } from "vitest";

import { pushTemplateTag } from "./push-template-tag";

let directory: string;

function git(cwd: string, ...args: string[]): string {
const result = spawnSync("git", args, { cwd, encoding: "utf-8" });
if (result.status !== 0)
throw new Error(`git ${args.join(" ")}: ${result.stderr}`);
return result.stdout.trim();
}

function commitFile(
cwd: string,
file: string,
content: string,
message: string,
) {
mkdirSync(join(cwd, "template"), { recursive: true });
writeFileSync(join(cwd, file), content);
git(cwd, "add", file);
git(cwd, "commit", "-m", message);
}

beforeEach(() => {
directory = mkdtempSync(join(tmpdir(), "appkit-template-tag-test-"));
// No user config or hooks; a fixed identity.
writeFileSync(join(directory, "gitconfig"), "");
vi.stubEnv("GIT_CONFIG_GLOBAL", join(directory, "gitconfig"));
vi.stubEnv("GIT_CONFIG_NOSYSTEM", "1");
for (const role of ["AUTHOR", "COMMITTER"]) {
vi.stubEnv(`GIT_${role}_NAME`, "Release Bot");
vi.stubEnv(`GIT_${role}_EMAIL`, "release@example.com");
}
});

afterEach(() => {
vi.unstubAllEnvs();
rmSync(directory, { recursive: true, force: true });
});

test("tags the released code even when the branch moved meanwhile", () => {
const origin = join(directory, "origin.git");
const runner = join(directory, "runner");
const other = join(directory, "other");
git(directory, "init", "-q", "--bare", "-b", "main", origin);
git(directory, "clone", "-q", origin, runner);
commitFile(runner, "template/app.ts", "released\n", "chore: release v2");
git(runner, "push", "-q", "origin", "main");
const released = git(runner, "rev-parse", "HEAD");

git(directory, "clone", "-q", origin, other);
commitFile(other, "template/app.ts", "unreleased\n", "feat: after release");
git(other, "push", "-q", "origin", "main");

writeFileSync(join(runner, "template/package.json"), '{"v":"2.0.0"}\n');
expect(pushTemplateTag("2.0.0", ["template/package.json"], runner)).toBe(
true,
);

const show = (rev: string) => git(origin, "show", rev);
expect(show("template-v2.0.0:template/app.ts")).toBe("released");
expect(show("template-v2.0.0:template/package.json")).toBe('{"v":"2.0.0"}');
expect(git(origin, "rev-parse", "template-v2.0.0^{commit}^")).toBe(released);
expect(show("main:template/app.ts")).toBe("unreleased");
expect(show("main:template/package.json")).toBe('{"v":"2.0.0"}');
});

test("pushes from a checkout with an unrelated modified file", () => {
const origin = join(directory, "origin.git");
const runner = join(directory, "runner");
git(directory, "init", "-q", "--bare", "-b", "main", origin);
git(directory, "clone", "-q", origin, runner);
commitFile(runner, "template/lock.json", "committed\n", "chore: release v2");
git(runner, "push", "-q", "origin", "main");
writeFileSync(join(runner, "template/lock.json"), "rewritten by install\n");

writeFileSync(join(runner, "template/package.json"), '{"v":"2.0.0"}\n');
expect(pushTemplateTag("2.0.0", ["template/package.json"], runner)).toBe(
true,
);

expect(git(origin, "show", "template-v2.0.0:template/package.json")).toBe(
'{"v":"2.0.0"}',
);
expect(git(origin, "show", "main:template/lock.json")).toBe("committed");
expect(readFileSync(join(runner, "template/lock.json"), "utf-8")).toBe(
"rewritten by install\n",
);
});
37 changes: 37 additions & 0 deletions tools/push-template-tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { spawnSync } from "node:child_process";

/**
* Commits the synced template files, tags the commit template-v<version> and
* pushes both. The tag stays on the code the release was cut from; if the
* branch moved meanwhile, only the branch gets the sync replayed on top.
*/
export function pushTemplateTag(
version: string,
files: string[],
cwd: string,
): boolean {
const git = (...args: string[]) =>
spawnSync("git", args, { cwd, stdio: "inherit" }).status === 0;
const branch = spawnSync("git", ["rev-parse", "--abbrev-ref", "HEAD"], {
cwd,
encoding: "utf-8",
}).stdout.trim();
const tag = `template-v${version}`;
return (
git("add", ...files) &&
git(
"commit",
"-s",
"-m",
`chore: sync template to v${version} [skip ci]`,
) &&
// Tag before the pull: commits merged after the release never went
// through the release pipeline, so they must not ship in the template.
git("tag", "-a", tag, "-m", `Template v${version}`) &&
// The branch may have moved while we waited for the registry.
// --autostash: the job's own install can leave unrelated files modified.
git("pull", "--rebase", "--autostash", "origin", branch) &&
// --atomic: never leave the tag pushed without the branch (or the reverse).
git("push", "--atomic", "origin", "HEAD", `refs/tags/${tag}`)
);
}
Loading
Loading