|
1 | 1 | #!/usr/bin/env node |
2 | 2 | // Applies GHSA patch payloads to advisory branches. |
3 | | -import { execFileSync, spawnSync } from "node:child_process"; |
| 3 | +import { execFileSync } from "node:child_process"; |
4 | 4 | import crypto from "node:crypto"; |
5 | 5 | import fs from "node:fs"; |
6 | 6 | import os from "node:os"; |
7 | 7 | import path from "node:path"; |
8 | | - |
9 | | -const GHSA_PATCH_TIMEOUT_MS = 120_000; |
| 8 | +import { GHSA_COMMAND_TIMEOUT_MS, runGhCommand } from "./lib/ghsa-patch-subprocess.mjs"; |
10 | 9 |
|
11 | 10 | function usage() { |
12 | 11 | console.error( |
@@ -46,15 +45,19 @@ function parseArgs(argv) { |
46 | 45 | } |
47 | 46 |
|
48 | 47 | function runGh(args) { |
49 | | - const proc = spawnSync("gh", args, { encoding: "utf8" }); |
50 | | - if (proc.status !== 0) { |
51 | | - fail(proc.stderr.trim() || proc.stdout.trim() || `gh ${args.join(" ")} failed`); |
| 48 | + try { |
| 49 | + return runGhCommand(args); |
| 50 | + } catch (error) { |
| 51 | + return fail(error instanceof Error ? error.message : String(error)); |
52 | 52 | } |
53 | | - return proc.stdout; |
54 | 53 | } |
55 | 54 |
|
56 | 55 | function deriveRepoFromOrigin() { |
57 | | - const remote = execFileSync("git", ["remote", "get-url", "origin"], { encoding: "utf8", timeout: GHSA_PATCH_TIMEOUT_MS, killSignal: "SIGKILL"}).trim(); |
| 56 | + const remote = execFileSync("git", ["remote", "get-url", "origin"], { |
| 57 | + encoding: "utf8", |
| 58 | + killSignal: "SIGKILL", |
| 59 | + timeout: GHSA_COMMAND_TIMEOUT_MS, |
| 60 | + }).trim(); |
58 | 61 | const httpsMatch = remote.match(/github\.com[/:]([^/]+)\/([^/.]+)(?:\.git)?$/); |
59 | 62 | if (!httpsMatch) { |
60 | 63 | fail(`Could not parse origin remote: ${remote}`); |
@@ -127,16 +130,20 @@ const payload = { |
127 | 130 | }; |
128 | 131 |
|
129 | 132 | const patchFile = writeTempJson(payload); |
130 | | -runGh([ |
131 | | - "api", |
132 | | - "-H", |
133 | | - "X-GitHub-Api-Version: 2022-11-28", |
134 | | - "-X", |
135 | | - "PATCH", |
136 | | - advisoryPath, |
137 | | - "--input", |
138 | | - patchFile, |
139 | | -]); |
| 133 | +try { |
| 134 | + runGh([ |
| 135 | + "api", |
| 136 | + "-H", |
| 137 | + "X-GitHub-Api-Version: 2022-11-28", |
| 138 | + "-X", |
| 139 | + "PATCH", |
| 140 | + advisoryPath, |
| 141 | + "--input", |
| 142 | + patchFile, |
| 143 | + ]); |
| 144 | +} finally { |
| 145 | + fs.rmSync(patchFile, { force: true }); |
| 146 | +} |
140 | 147 |
|
141 | 148 | if (restoredCvss) { |
142 | 149 | runGh([ |
|
0 commit comments