(step, overrides = {})
| 48 | const VERSION_GATE = stepText("npm-publish", "Ensure npm supports trusted publishing"); |
| 49 | |
| 50 | function execute(step, overrides = {}) { |
| 51 | const directory = fs.mkdtempSync(path.join(os.tmpdir(), "ocr-release-contract-")); |
| 52 | const bin = path.join(directory, "bin"); |
| 53 | fs.mkdirSync(bin); |
| 54 | fs.symlinkSync(process.execPath, path.join(bin, "node")); |
| 55 | fs.writeFileSync(path.join(directory, "package.json"), JSON.stringify(MANIFEST)); |
| 56 | const mock = `#!/usr/bin/env node |
| 57 | const fs = require("fs"); |
| 58 | const path = require("path"); |
| 59 | const command = path.basename(process.argv[1]); |
| 60 | const args = process.argv.slice(2); |
| 61 | fs.appendFileSync("calls.jsonl", JSON.stringify({ command, args }) + "\\n"); |
| 62 | if (command === "npm") { |
| 63 | if (args[0] === "--version") { |
| 64 | console.log(process.env.TEST_NPM_VERSION); |
| 65 | } else if (args[0] === "install") { |
| 66 | process.exit(Number(process.env.TEST_INSTALL_STATUS || 0)); |
| 67 | } else { |
| 68 | throw new Error("Unexpected npm operation: " + args.join(" ")); |
| 69 | } |
| 70 | } else if (args[args.length - 1].startsWith("https://oidc.invalid/")) { |
| 71 | console.log(JSON.stringify({ value: process.env.TEST_ID_TOKEN })); |
| 72 | } else { |
| 73 | if (process.env.TEST_CURL_FAILURE === "1") process.exit(7); |
| 74 | const failed = process.env.TEST_FAILED_PACKAGE && |
| 75 | args[args.length - 1].endsWith(process.env.TEST_FAILED_PACKAGE.split("/").join("%2f")); |
| 76 | const body = failed ? JSON.stringify({ message: "Not authorized" }) : process.env.TEST_RESPONSE; |
| 77 | const outputIndex = args.indexOf("-o"); |
| 78 | if (outputIndex < 0) throw new Error("Expected a curl output file"); |
| 79 | fs.writeFileSync(args[outputIndex + 1], body); |
| 80 | process.stdout.write(failed ? "401" : process.env.TEST_HTTP_STATUS); |
| 81 | } |
| 82 | `; |
| 83 | for (const command of ["curl", "npm"]) { |
| 84 | fs.writeFileSync(path.join(bin, command), mock, { mode: 0o755 }); |
| 85 | } |
| 86 | const claims = Buffer.from(JSON.stringify({ |
| 87 | repository: "alibaba/open-code-review", |
| 88 | workflow_ref: "alibaba/open-code-review/.github/workflows/release.yml@refs/heads/main", |
| 89 | repository_visibility: "public", |
| 90 | })).toString("base64url"); |
| 91 | try { |
| 92 | const result = spawnSync("/bin/bash", ["--noprofile", "--norc", "-e", "-c", runBlock(step)], { |
| 93 | cwd: directory, |
| 94 | encoding: "utf8", |
| 95 | timeout: 30000, |
| 96 | env: { |
| 97 | PATH: `${bin}:${process.env.PATH}`, |
| 98 | HOME: directory, |
| 99 | LC_ALL: "C", |
| 100 | ACTIONS_ID_TOKEN_REQUEST_URL: "https://oidc.invalid/?request=fixture", |
| 101 | ACTIONS_ID_TOKEN_REQUEST_TOKEN: "fixture-request-token", |
| 102 | TEST_ID_TOKEN: `header.${claims}.signature`, |
| 103 | TEST_HTTP_STATUS: "201", |
| 104 | TEST_RESPONSE: JSON.stringify({ token: "fixture-npm-token" }), |
| 105 | TEST_NPM_VERSION: "11.15.0", |
| 106 | ...overrides, |
| 107 | }, |
no test coverage detected