mirror of
https://github.com/hanzoai/turborepo.git
synced 2026-10-30 19:44:51 +00:00
### Why Library Release run 28803106189 got past the build matrix (after #13269/#13270) but failed in `Publish to NPM` at `Commit version bump`: ``` fatal: update_ref failed for ref 'refs/heads/library-release/0.0.1-canary.22': ... nonexistent object ``` `create-github-api-commit.mjs` creates the commit remotely via GraphQL `createCommitOnBranch`, then runs `git update-ref` with that SHA — but the object only exists on GitHub, never in the local object store. The existing test masked this by mocking the API commit SHA as the local HEAD. ### What - Fetch `refs/heads/<branch>` from origin before `git update-ref` when the commit object is missing locally. - Add a regression test that uses a real origin remote holding a commit the local clone doesn't have. ### How The fetch is skipped when the object already exists (keeps tests without a remote working, avoids a needless network call). Verify with `node --test scripts/create-github-api-commit.test.mjs` (7/7 pass), then re-dispatch Library Release with `dry_run: true`. This also fixes the same latent failure in `turborepo-release.yml` and `update-examples-on-release.yml`, which call the same script.
359 lines
11 KiB
JavaScript
359 lines
11 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { execFileSync } from "node:child_process";
|
|
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import test from "node:test";
|
|
|
|
import {
|
|
buildFileChanges,
|
|
parseArgs,
|
|
run,
|
|
} from "./create-github-api-commit.mjs";
|
|
|
|
function git(args) {
|
|
return execFileSync("git", args, { encoding: "utf8" }).trim();
|
|
}
|
|
|
|
async function withTempRepo(callback) {
|
|
const cwd = process.cwd();
|
|
const dir = mkdtempSync(join(tmpdir(), "github-api-commit-"));
|
|
|
|
try {
|
|
process.chdir(dir);
|
|
git(["init"]);
|
|
git(["checkout", "-b", "main"]);
|
|
git(["config", "user.name", "Test User"]);
|
|
git(["config", "user.email", "[email protected]"]);
|
|
writeFileSync("file.txt", "base\n");
|
|
git(["add", "file.txt"]);
|
|
git(["commit", "-m", "initial"]);
|
|
await callback(dir);
|
|
} finally {
|
|
process.chdir(cwd);
|
|
rmSync(dir, { force: true, recursive: true });
|
|
}
|
|
}
|
|
|
|
function mockGitHub({ commitOid = "commit-sha", remoteOid, verified = false }) {
|
|
const requests = [];
|
|
const originalFetch = globalThis.fetch;
|
|
|
|
globalThis.fetch = async (url, options = {}) => {
|
|
const body = options.body ? JSON.parse(options.body) : undefined;
|
|
requests.push({ body, url: String(url) });
|
|
|
|
if (String(url).endsWith("/graphql")) {
|
|
if (body.query.includes("repository(owner:")) {
|
|
return response({
|
|
data: {
|
|
repository: {
|
|
id: "repo-id",
|
|
nameWithOwner: "vercel/turbo",
|
|
ref: remoteOid ? { target: { oid: remoteOid } } : null,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
if (body.query.includes("createRef")) {
|
|
return response({ data: { createRef: { ref: { id: "ref-id" } } } });
|
|
}
|
|
if (body.query.includes("deleteRef")) {
|
|
return response({ data: { deleteRef: { clientMutationId: null } } });
|
|
}
|
|
if (body.query.includes("createCommitOnBranch")) {
|
|
return response({
|
|
data: { createCommitOnBranch: { commit: { oid: commitOid } } },
|
|
});
|
|
}
|
|
}
|
|
|
|
return response({
|
|
commit: { verification: { reason: "unsigned", verified } },
|
|
});
|
|
};
|
|
|
|
return {
|
|
requests,
|
|
restore() {
|
|
globalThis.fetch = originalFetch;
|
|
},
|
|
};
|
|
}
|
|
|
|
function response(data) {
|
|
return { json: async () => data, ok: true, status: 200, statusText: "OK" };
|
|
}
|
|
|
|
test("parseArgs validates branch policy", () => {
|
|
assert.deepEqual(
|
|
parseArgs([
|
|
"--branch",
|
|
"release/test",
|
|
"--message",
|
|
"test",
|
|
"--all-tracked",
|
|
"--if-exists",
|
|
"update",
|
|
]),
|
|
{
|
|
allTracked: true,
|
|
branch: "release/test",
|
|
ifExists: "update",
|
|
includeUntracked: false,
|
|
message: "test",
|
|
paths: [],
|
|
},
|
|
);
|
|
assert.throws(
|
|
() =>
|
|
parseArgs([
|
|
"--branch",
|
|
"release/test",
|
|
"--message",
|
|
"test",
|
|
"--all-tracked",
|
|
"--if-exists",
|
|
"replace",
|
|
]),
|
|
/--if-exists must be 'fail' or 'update'/,
|
|
);
|
|
});
|
|
|
|
test("buildFileChanges refuses sensitive-looking paths", async () => {
|
|
await withTempRepo(() => {
|
|
writeFileSync(".env", "TOKEN=secret\n");
|
|
assert.throws(
|
|
() => buildFileChanges([{ deleted: false, path: ".env" }]),
|
|
/Refusing to commit sensitive-looking file/,
|
|
);
|
|
});
|
|
});
|
|
|
|
test("verification failure preserves a branch after commit creation", async () => {
|
|
await withTempRepo(async () => {
|
|
writeFileSync("file.txt", "changed\n");
|
|
process.env.GITHUB_REPOSITORY = "vercel/turbo";
|
|
process.env.GH_TOKEN = "test-token";
|
|
process.env.CREATE_GITHUB_API_COMMIT_VERIFY_ATTEMPTS = "1";
|
|
process.env.CREATE_GITHUB_API_COMMIT_VERIFY_DELAY_MS = "0";
|
|
|
|
const github = mockGitHub({ remoteOid: null, verified: false });
|
|
try {
|
|
await assert.rejects(
|
|
run({
|
|
allTracked: true,
|
|
branch: "release/test",
|
|
ifExists: "fail",
|
|
includeUntracked: false,
|
|
message: "test commit",
|
|
paths: [],
|
|
}),
|
|
/GitHub did not verify commit commit-sha/,
|
|
);
|
|
assert.equal(
|
|
github.requests.some((request) =>
|
|
request.body?.query.includes("deleteRef"),
|
|
),
|
|
false,
|
|
);
|
|
} finally {
|
|
github.restore();
|
|
delete process.env.GITHUB_REPOSITORY;
|
|
delete process.env.GH_TOKEN;
|
|
delete process.env.CREATE_GITHUB_API_COMMIT_VERIFY_ATTEMPTS;
|
|
delete process.env.CREATE_GITHUB_API_COMMIT_VERIFY_DELAY_MS;
|
|
}
|
|
});
|
|
});
|
|
|
|
test("run reads changed files from repository root when invoked in a subdirectory", async () => {
|
|
await withTempRepo(async () => {
|
|
mkdirSync("cli");
|
|
mkdirSync("packages/create-turbo", { recursive: true });
|
|
writeFileSync("packages/create-turbo/package.json", '{"version":"1.0.0"}\n');
|
|
git(["add", "cli", "packages/create-turbo/package.json"]);
|
|
git(["commit", "-m", "add package"]);
|
|
writeFileSync("packages/create-turbo/package.json", '{"version":"1.0.1"}\n');
|
|
process.chdir("cli");
|
|
const currentHead = git(["rev-parse", "HEAD"]);
|
|
|
|
process.env.GITHUB_REPOSITORY = "vercel/turbo";
|
|
process.env.GH_TOKEN = "test-token";
|
|
process.env.CREATE_GITHUB_API_COMMIT_VERIFY_ATTEMPTS = "1";
|
|
process.env.CREATE_GITHUB_API_COMMIT_VERIFY_DELAY_MS = "0";
|
|
|
|
const github = mockGitHub({
|
|
commitOid: currentHead,
|
|
remoteOid: null,
|
|
verified: true,
|
|
});
|
|
try {
|
|
await run({
|
|
allTracked: true,
|
|
branch: "release/test",
|
|
ifExists: "fail",
|
|
includeUntracked: false,
|
|
message: "test commit",
|
|
paths: [],
|
|
});
|
|
|
|
const createCommitRequest = github.requests.find((request) =>
|
|
request.body?.query.includes("createCommitOnBranch"),
|
|
);
|
|
assert.deepEqual(createCommitRequest.body.variables.input.fileChanges, {
|
|
additions: [
|
|
{
|
|
contents: Buffer.from('{"version":"1.0.1"}\n').toString("base64"),
|
|
path: "packages/create-turbo/package.json",
|
|
},
|
|
],
|
|
});
|
|
} finally {
|
|
github.restore();
|
|
delete process.env.GITHUB_REPOSITORY;
|
|
delete process.env.GH_TOKEN;
|
|
delete process.env.CREATE_GITHUB_API_COMMIT_VERIFY_ATTEMPTS;
|
|
delete process.env.CREATE_GITHUB_API_COMMIT_VERIFY_DELAY_MS;
|
|
}
|
|
});
|
|
});
|
|
|
|
test("fetches the API-created commit before updating the local ref", async () => {
|
|
const cwd = process.cwd();
|
|
const originDir = mkdtempSync(join(tmpdir(), "github-api-commit-origin-"));
|
|
const cloneDir = join(mkdtempSync(join(tmpdir(), "github-api-commit-work-")), "clone");
|
|
|
|
try {
|
|
process.chdir(originDir);
|
|
git(["init"]);
|
|
git(["checkout", "-b", "main"]);
|
|
git(["config", "user.name", "Test User"]);
|
|
git(["config", "user.email", "[email protected]"]);
|
|
writeFileSync("file.txt", "base\n");
|
|
git(["add", "file.txt"]);
|
|
git(["commit", "-m", "initial"]);
|
|
|
|
git(["clone", "--quiet", originDir, cloneDir]);
|
|
|
|
// Simulate createCommitOnBranch: a commit that exists only on the remote,
|
|
// at the tip of the target branch.
|
|
git(["checkout", "-b", "release/test"]);
|
|
writeFileSync("file.txt", "remote change\n");
|
|
git(["add", "file.txt"]);
|
|
git(["commit", "-m", "api commit"]);
|
|
const remoteOnlySha = git(["rev-parse", "HEAD"]);
|
|
git(["checkout", "main"]);
|
|
|
|
process.chdir(cloneDir);
|
|
git(["config", "user.name", "Test User"]);
|
|
git(["config", "user.email", "[email protected]"]);
|
|
writeFileSync("file.txt", "local change\n");
|
|
|
|
process.env.GITHUB_REPOSITORY = "vercel/turbo";
|
|
process.env.GH_TOKEN = "test-token";
|
|
process.env.CREATE_GITHUB_API_COMMIT_VERIFY_ATTEMPTS = "1";
|
|
process.env.CREATE_GITHUB_API_COMMIT_VERIFY_DELAY_MS = "0";
|
|
|
|
const github = mockGitHub({
|
|
commitOid: remoteOnlySha,
|
|
remoteOid: null,
|
|
verified: true,
|
|
});
|
|
try {
|
|
await run({
|
|
allTracked: true,
|
|
branch: "release/test",
|
|
ifExists: "fail",
|
|
includeUntracked: false,
|
|
message: "test commit",
|
|
paths: [],
|
|
});
|
|
|
|
assert.equal(git(["rev-parse", "refs/heads/release/test"]), remoteOnlySha);
|
|
} finally {
|
|
github.restore();
|
|
delete process.env.GITHUB_REPOSITORY;
|
|
delete process.env.GH_TOKEN;
|
|
delete process.env.CREATE_GITHUB_API_COMMIT_VERIFY_ATTEMPTS;
|
|
delete process.env.CREATE_GITHUB_API_COMMIT_VERIFY_DELAY_MS;
|
|
}
|
|
} finally {
|
|
process.chdir(cwd);
|
|
rmSync(originDir, { force: true, recursive: true });
|
|
rmSync(join(cloneDir, ".."), { force: true, recursive: true });
|
|
}
|
|
});
|
|
|
|
test("update policy commits onto an existing remote branch", async () => {
|
|
await withTempRepo(async () => {
|
|
writeFileSync("file.txt", "changed\n");
|
|
process.env.GITHUB_REPOSITORY = "vercel/turbo";
|
|
process.env.GH_TOKEN = "test-token";
|
|
process.env.CREATE_GITHUB_API_COMMIT_VERIFY_ATTEMPTS = "1";
|
|
process.env.CREATE_GITHUB_API_COMMIT_VERIFY_DELAY_MS = "0";
|
|
|
|
const github = mockGitHub({ remoteOid: "remote-sha", verified: false });
|
|
try {
|
|
await assert.rejects(
|
|
run({
|
|
allTracked: true,
|
|
branch: "post-release-bump-examples",
|
|
ifExists: "update",
|
|
includeUntracked: false,
|
|
message: "test commit",
|
|
paths: [],
|
|
}),
|
|
/GitHub did not verify commit commit-sha/,
|
|
);
|
|
const createCommitRequest = github.requests.find((request) =>
|
|
request.body?.query.includes("createCommitOnBranch"),
|
|
);
|
|
assert.equal(
|
|
createCommitRequest.body.variables.input.expectedHeadOid,
|
|
"remote-sha",
|
|
);
|
|
} finally {
|
|
github.restore();
|
|
delete process.env.GITHUB_REPOSITORY;
|
|
delete process.env.GH_TOKEN;
|
|
delete process.env.CREATE_GITHUB_API_COMMIT_VERIFY_ATTEMPTS;
|
|
delete process.env.CREATE_GITHUB_API_COMMIT_VERIFY_DELAY_MS;
|
|
}
|
|
});
|
|
});
|
|
|
|
test("default policy refuses an existing remote branch", async () => {
|
|
await withTempRepo(async () => {
|
|
writeFileSync("file.txt", "changed\n");
|
|
process.env.GITHUB_REPOSITORY = "vercel/turbo";
|
|
process.env.GH_TOKEN = "test-token";
|
|
|
|
const currentHead = git(["rev-parse", "HEAD"]);
|
|
const github = mockGitHub({ remoteOid: currentHead, verified: false });
|
|
try {
|
|
await assert.rejects(
|
|
run({
|
|
allTracked: true,
|
|
branch: "release/test",
|
|
ifExists: "fail",
|
|
includeUntracked: false,
|
|
message: "test commit",
|
|
paths: [],
|
|
}),
|
|
/Remote branch release\/test already exists/,
|
|
);
|
|
assert.equal(
|
|
github.requests.some((request) =>
|
|
request.body?.query.includes("createCommitOnBranch"),
|
|
),
|
|
false,
|
|
);
|
|
} finally {
|
|
github.restore();
|
|
delete process.env.GITHUB_REPOSITORY;
|
|
delete process.env.GH_TOKEN;
|
|
}
|
|
});
|
|
});
|