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
28 changes: 27 additions & 1 deletion scripts/catalog-publication.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,17 @@ const endpoint = args[1];
const method = args[args.indexOf("--method") + 1];
const body = args.includes("--input") ? JSON.parse(fs.readFileSync(0, "utf8")) : undefined;
fs.appendFileSync(process.env.API_CALLS, JSON.stringify({ endpoint, method, body }) + "\\n");
const mainAdvanced = () =>
process.env.MAIN_ADVANCES === "before" ||
(process.env.MAIN_ADVANCES === "after-commit" && fs.readFileSync(process.env.API_CALLS, "utf8").includes('"graphql"'));
if (!process.env.PUBLISH_SUCCESS && (endpoint === "graphql" || method === "DELETE")) {
console.error(endpoint === "graphql" ? "commit rejected" : "cleanup rejected");
process.exit(1);
}
if (endpoint === "graphql") console.log("c".repeat(40));
else if (endpoint.includes("/runs?")) console.log(JSON.stringify({ workflow_runs: JSON.parse(process.env.WORKFLOW_RUNS || "[]") }));
else if (endpoint.endsWith("/dispatches") && process.env.DISPATCH_FAIL) process.exit(1);
else if (endpoint.endsWith("/ref/heads/main")) console.log(process.env.BASE);
else if (endpoint.endsWith("/ref/heads/main")) console.log(mainAdvanced() ? "b".repeat(40) : process.env.BASE);
else if (endpoint.includes("/matching-refs/")) console.log(process.env.NO_BRANCH ? "" : "refs/heads/bot/catalog-publish");
else if (endpoint.includes("/ref/heads/bot/catalog-publish")) console.log("a".repeat(40));
else if (endpoint.includes("/pulls?")) console.log(process.env.NO_OPEN_PR ? "" : "42");
Expand Down Expand Up @@ -191,6 +194,29 @@ test("reverting unpublished sources closes the obsolete PR and resets its branch
);
});

test("a run that main overtook before it started ends clean and writes nothing", (t) => {
const fixture = publicationFixture(t, true, { MAIN_ADVANCES: "before" });
assert.match(fixture.run(), /^::notice::Main advanced during publication/m);
assert.deepEqual(
fixture.calls().filter((call) => call.method !== "GET"),
[],
);
});

test("a run that main overtook after its commits ends clean and leaves the publication alone", (t) => {
const fixture = publicationFixture(t, true, {
MAIN_ADVANCES: "after-commit",
PUBLISH_SUCCESS: "1",
});
assert.match(fixture.run(), /^::notice::Main advanced during publication/m);
const writes = fixture.calls().filter((call) => call.method !== "GET");
assert.equal(writes.at(-1).method, "DELETE");
assert.equal(
writes.some((call) => call.method === "PATCH" || call.endpoint.endsWith("/dispatches")),
false,
);
});

test("a failed staging cleanup preserves the publication failure as well", (t) => {
const fixture = publicationFixture(t, true);
assert.throws(fixture.run, (error) => {
Expand Down
15 changes: 14 additions & 1 deletion scripts/publish-catalog.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@ function actionEnvironment() {
return { repository, run, endpoint: `repos/${repository}/git` };
}

class MainAdvanced extends Error {}

function assertCurrentMain(endpoint, base) {
const current = commitOid(api(`${endpoint}/ref/heads/main`, "GET", undefined, ".object.sha"));
if (current !== base)
throw new Error("Main advanced during publication; the next push run will regenerate it.");
throw new MainAdvanced(
"Main advanced during publication; the next push run will regenerate it.",
);
}

function updateStandingBranch(endpoint, exists, head) {
Expand Down Expand Up @@ -334,6 +338,15 @@ function publishSnapshot(context, base, exists, batches) {
}

export function publish(root) {
try {
publishMain(root);
} catch (error) {
if (!(error instanceof MainAdvanced)) throw error;
console.log(`::notice::${error.message}`);
}
}

function publishMain(root) {
const base = commitOid(git(root, ["rev-parse", "HEAD"]));
const changes = catalogChanges(root, base);
const batches = commitBatches(changes);
Expand Down
Loading