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
5 changes: 5 additions & 0 deletions .changeset/every-seals-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mynameistito/codex-usage": patch
---

Fix npm release staging with trusted publishing by skipping the unsupported staged-package listing request.
8 changes: 5 additions & 3 deletions __tests__/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ describe("release helpers", () => {
expect(calls).toHaveLength(1);
});

test("checks stage-list status and uses supported stage-publish args", async () => {
test("stages directly with trusted publishing", async () => {
const calls: string[][] = [];
const responses = [
result(1, "", "npm error code E404"),
result(0, "[]"),
result(0, "staged"),
];

Expand All @@ -58,6 +57,9 @@ describe("release helpers", () => {
{ name: "example", version: "1.0.0" }
);

expect(calls[2]).toEqual(["npm", "stage", "publish", "."]);
expect(calls).toEqual([
["npm", "view", "example@1.0.0", "version"],
["npm", "stage", "publish", "."],
]);
});
});
65 changes: 0 additions & 65 deletions scripts/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,51 +131,6 @@ export const runCommand: CommandRunner = async (command, args) => {
return { exitCode, stderr, stdout };
};

const isStagedEntryForVersion = (entry: unknown, version: string) => {
if (typeof entry === "string") {
return entry === version || entry.endsWith(`@${version}`);
}

if (!entry || typeof entry !== "object") {
return false;
}

const record = entry as Record<string, unknown>;
const packageRecord = record["package"];

return (
record["version"] === version ||
(Boolean(packageRecord) &&
typeof packageRecord === "object" &&
(packageRecord as Record<string, unknown>)["version"] === version)
);
};

/** Returns whether npm staged-version output contains the package version. */
export const hasStagedVersion = (input: string, version: string) => {
const trimmed = input.trim();

if (!trimmed) {
return false;
}

const parsed = JSON.parse(trimmed) as unknown;

if (!parsed || typeof parsed !== "object") {
return false;
}

if ("error" in parsed) {
return false;
}

const staged = Array.isArray(parsed)
? parsed
: Object.values(parsed as Record<string, unknown>);

return staged.some((entry) => isStagedEntryForVersion(entry, version));
};

/** Stages the package with npm or reports that it is already released. */
export const runNpmRelease = async (
runner: CommandRunner = runCommand,
Expand All @@ -198,26 +153,6 @@ export const runNpmRelease = async (
);
}

const stagedList = await runner("npm", [
"stage",
"list",
releasePackage.name,
"--json",
]);

if (stagedList.exitCode !== 0) {
const stagedOutput = `${stagedList.stdout}${stagedList.stderr}`;
throw new ReleaseError(
`npm stage list failed with ${stagedList.exitCode}: ${stagedOutput.trim()}`
);
}

if (hasStagedVersion(stagedList.stdout, releasePackage.version)) {
console.log(`${spec} is already staged for approval`);
writeGithubOutputs({ ...baseOutputs, staged: "true" });
return;
}

const stagePublish = await runner("npm", ["stage", "publish", "."]);
const publishOutput = `${stagePublish.stdout}${stagePublish.stderr}`;

Expand Down
Loading