OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
refactor(publication): separate Git transport from review providers (#1551)
Michael D'Angelo83040eeecf0e
diff --git a/sdk/typescript/scripts/package-dist-files.mjs b/sdk/typescript/scripts/package-dist-files.mjsindex 3a12548a8..f4f5c4432 100644--- a/sdk/typescript/scripts/package-dist-files.mjs+++ b/sdk/typescript/scripts/package-dist-files.mjs@@ -51,6 +51,7 @@ const distModules = ["multiscan","mock-scan","owner-evidence",+ "patch-publication","patch-tui","publication","publication-events",diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex 2e6cfc165..31b820dd5 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -95,6 +95,10 @@ import { isRecord as isJsonObject } from "./record.js";import { suggestOwnersInternal } from "./suggest-owners.js";import { parseImportedFindings } from "./findings-import.js";import { publishScanToCustom } from "./custom-publish.js";+import {+ publishPatchReview,+ resolvePatchReviewPublisher,+} from "./patch-publication.js";import { DEFAULT_DEDUPE_CONCURRENCY } from "./deduplication/deduplication.js";import { savedScanWorkbench } from "./saved-scan-bootstrap.js";import { deduplicateScanInternal } from "./deduplication/scan.js";@@ -6862,93 +6866,25 @@ async function publishPatchBranch(stderr: Writable,dependencies: CliDependencies,): Promise<{ branch: string; url: string }> {- const run = (command: "git" | "gh" | "glab", args: string[]) =>+ const run = (command: "git" | "gh" | "glab", args: readonly string[]) =>dependencies.runRepositoryCommand(command, args, repository);try {const remote = await run("git", ["remote", "get-url", "--push", "origin"]);- const host = patchRemoteHost(remote);- const gitlabHost =- dependencies.environment["GITLAB_HOST"] ||- dependencies.environment["GITLAB_URI"] ||- dependencies.environment["GL_HOST"];- const gitlab =- host === "gitlab.com" ||- (host !== undefined &&- gitlabHost !== undefined &&- host ===- patchRemoteHost(- gitlabHost.includes("://") ? gitlabHost : `https://${gitlabHost}`,- ));- const command = gitlab ? "glab" : "gh";- const gitlabRepository = remote.includes("://")- ? remote- : `ssh://${remote.replace(":", "/")}`;- let url = await run(- command,- gitlab- ? [- "mr",- "list",- "--all",- "--source-branch",- branch,- "--output",- "json",- "--jq",- "map(select(.source_project_id == .target_project_id))[0].web_url // empty",- "--repo",- gitlabRepository,- ]- : [- "pr",- "list",- "--head",- branch,- "--state",- "all",- "--json",- "url",- "--jq",- ".[0].url // empty",- ],- );- if (!url) {- await run("git", ["push", "--set-upstream", "origin", branch]);- url = await run(- command,- gitlab- ? [- "mr",- "create",- "--draft",- "--head",- gitlabRepository,- "--source-branch",- branch,- "--title",- PATCH_PR_TITLE,- "--description",- gitlabPatchDescription(body),- "--yes",- "--repo",- gitlabRepository,- ]- : [- "pr",- "create",- "--draft",- "--head",- branch,- "--title",- PATCH_PR_TITLE,- "--body",- body,- ],- );- }- stderr.write(- `${gitlab ? "Merge" : "Pull"} request: ${safePatchText(url)}\n`,+ const publisher = resolvePatchReviewPublisher(+ remote,+ dependencies.environment,+ run,);+ const url = await publishPatchReview({+ branch,+ title: PATCH_PR_TITLE,+ body,+ publisher,+ pushBranch: async (branch) => {+ await run("git", ["push", "--set-upstream", "origin", branch]);+ },+ });+ stderr.write(`${publisher.label}: ${safePatchText(url)}\n`);return { branch, url };} catch (error) {stderr.write(@@ -6958,21 +6894,6 @@ async function publishPatchBranch(}}-function gitlabPatchDescription(body: string): string {- // GitLab ignores quick actions inside its native fenced blockquotes. Choose- // a fence the report cannot close after quick-action CR removal; preserve its- // original bytes and end the outer quote at EOF.- let fenceLength = 3;- for (const match of body.replaceAll("\r", "").matchAll(/^[ \t]*(>+)/gmu))- fenceLength = Math.max(fenceLength, match[1]!.length + 1);- return `${">".repeat(fenceLength)}\n${body}`;-}--function patchRemoteHost(remote: string): string | undefined {- if (remote.includes("://")) return new URL(remote).hostname.toLowerCase();- return /^(?:[^@/]+@)?([^:/]+):[^/]/u.exec(remote)?.[1]?.toLowerCase();-}-async function resumePatchPullRequest(repository: string,branch: string,diff --git a/sdk/typescript/src/patch-publication.ts b/sdk/typescript/src/patch-publication.tsnew file mode 100644index 000000000..a7b834124--- /dev/null+++ b/sdk/typescript/src/patch-publication.ts@@ -0,0 +1,151 @@+export interface PatchReviewRequest {+ branch: string;+ title: string;+ body: string;+}++/** Internal provider operations; Git transport is supplied separately. */+export interface PatchReviewPublisher {+ readonly label: string;+ /** Return the existing review URL, or an empty string when none exists. */+ findExisting(branch: string): Promise<string>;+ createDraft(request: PatchReviewRequest): Promise<string>;+}++export type RunPatchReviewCommand = (+ command: "gh" | "glab",+ args: readonly string[],+) => Promise<string>;++export async function publishPatchReview({+ publisher,+ pushBranch,+ ...request+}: PatchReviewRequest & {+ publisher: PatchReviewPublisher;+ pushBranch(branch: string): Promise<void>;+}): Promise<string> {+ const existing = await publisher.findExisting(request.branch);+ if (existing) return existing;+ await pushBranch(request.branch);+ return publisher.createDraft(request);+}++/** Preserve the CLI's default routing, including native gh repository selection. */+export function resolvePatchReviewPublisher(+ remote: string,+ environment: NodeJS.ProcessEnv,+ run: RunPatchReviewCommand,+): PatchReviewPublisher {+ const host = remoteHost(remote);+ const gitlabHost =+ environment["GITLAB_HOST"] ||+ environment["GITLAB_URI"] ||+ environment["GL_HOST"];+ const gitlab =+ host === "gitlab.com" ||+ (host !== undefined &&+ gitlabHost !== undefined &&+ host ===+ remoteHost(+ gitlabHost.includes("://") ? gitlabHost : `https://${gitlabHost}`,+ ));+ return gitlab+ ? createGitLabPatchPublisher({ project: remote, run })+ : createGitHubPatchPublisher(run);+}++export function createGitHubPatchPublisher(+ run: RunPatchReviewCommand,+): PatchReviewPublisher {+ return {+ label: "Pull request",+ findExisting: (branch) =>+ run("gh", [+ "pr",+ "list",+ "--head",+ branch,+ "--state",+ "all",+ "--json",+ "url",+ "--jq",+ ".[0].url // empty",+ ]),+ createDraft: ({ branch, title, body }) =>+ run("gh", [+ "pr",+ "create",+ "--draft",+ "--head",+ branch,+ "--title",+ title,+ "--body",+ body,+ ]),+ };+}++export function createGitLabPatchPublisher({+ project,+ run,+}: {+ /** Review project URL, independent of the Git push destination. */+ project: string;+ run: RunPatchReviewCommand;+}): PatchReviewPublisher {+ const repository = project.includes("://")+ ? project+ : `ssh://${project.replace(":", "/")}`;+ return {+ label: "Merge request",+ findExisting: (branch) =>+ run("glab", [+ "mr",+ "list",+ "--all",+ "--source-branch",+ branch,+ "--output",+ "json",+ "--jq",+ "map(select(.source_project_id == .target_project_id))[0].web_url // empty",+ "--repo",+ repository,+ ]),+ createDraft: ({ branch, title, body }) =>+ run("glab", [+ "mr",+ "create",+ "--draft",+ "--head",+ repository,+ "--source-branch",+ branch,+ "--title",+ title,+ "--description",+ gitlabPatchDescription(body),+ "--yes",+ "--repo",+ repository,+ ]),+ };+}++function gitlabPatchDescription(body: string): string {+ // GitLab ignores quick actions inside its native fenced blockquotes. Choose+ // a fence the report cannot close after quick-action CR removal; preserve its+ // original bytes and end the outer quote at EOF.+ let fenceLength = 3;+ for (const match of body.replaceAll("\r", "").matchAll(/^[ \t]*(>+)/gmu))+ fenceLength = Math.max(fenceLength, match[1]!.length + 1);+ return `${">".repeat(fenceLength)}\n${body}`;+}++function remoteHost(remote: string): string | undefined {+ if (remote.includes("://")) return new URL(remote).hostname.toLowerCase();+ return /^(?:[^@/]+@)?([^:/]+):[^/]/u.exec(remote)?.[1]?.toLowerCase();+}diff --git a/sdk/typescript/tests-ts/patch-publication.test.ts b/sdk/typescript/tests-ts/patch-publication.test.tsnew file mode 100644index 000000000..22ed03f41--- /dev/null+++ b/sdk/typescript/tests-ts/patch-publication.test.ts@@ -0,0 +1,204 @@+import { expect, test } from "bun:test";+import { execFileSync } from "node:child_process";+import { mkdir, rm, writeFile } from "node:fs/promises";+import { join } from "node:path";+import {+ createGitLabPatchPublisher,+ publishPatchReview,+ resolvePatchReviewPublisher,+ type PatchReviewPublisher,+} from "../src/patch-publication.js";+import { temporaryDirectory } from "./support/temporary-directories.js";++const request = {+ branch: "patch/example",+ title: "Fix a synthetic issue",+ body: "Verified with a synthetic regression test.",+};++test("publishes a local Git branch using a separate review project and browser URL", async () => {+ const root = await temporaryDirectory("patch-publication-");+ const repository = join(root, "working tree");+ const transport = join(root, "bare remote.git");+ const output = join(root, "provider output");+ const project = "https://forge.example.test/team/subgroup/project.git";+ const url = "https://reviews.example.test/review/42";+ const events: string[] = [];+ const git = (...args: string[]) =>+ execFileSync("git", args, {+ cwd: repository,+ encoding: "utf8",+ stdio: ["ignore", "pipe", "pipe"],+ }).trim();+ try {+ await mkdir(repository);+ await mkdir(output);+ git("init", "--initial-branch=main");+ git("init", "--bare", transport);+ git("config", "user.name", "Synthetic User");+ git("config", "user.email", "[email protected]");+ git(+ "-c",+ "commit.gpgsign=false",+ "commit",+ "--allow-empty",+ "-m",+ "Fixture",+ );+ git("checkout", "-b", request.branch);+ git("remote", "add", "origin", transport);++ const client = join(output, "review-client.mjs");+ await writeFile(+ client,+ `import assert from "node:assert/strict";+const [serialized, project, url] = process.argv.slice(2);+const args = JSON.parse(serialized);+assert.equal(args[0], "mr");+assert.equal(args[args.indexOf("--repo") + 1], project);+if (args[1] === "create") {+ assert.equal(args[args.indexOf("--head") + 1], project);+ process.stdout.write(url);+} else {+ assert.equal(args[1], "list");+}+`,+ );+ const publisher = createGitLabPatchPublisher({+ project,+ run: async (command, args) => {+ expect(command).toBe("glab");+ events.push(args[1]!);+ if (args[1] === "create")+ expect(git("--git-dir", transport, "rev-parse", request.branch)).toBe(+ git("rev-parse", "HEAD"),+ );+ return execFileSync(+ process.execPath,+ [client, JSON.stringify(args), project, url],+ { encoding: "utf8" },+ );+ },+ });++ const result = await publishPatchReview({+ ...request,+ publisher,+ pushBranch: async (branch) => {+ events.push("push");+ git("push", "--set-upstream", "origin", branch);+ },+ });++ expect(result).toBe(url);+ expect(git("remote", "get-url", "--push", "origin")).toBe(transport);+ expect(events).toEqual(["list", "push", "create"]);+ } finally {+ await rm(root, { recursive: true, force: true });+ }+});++test("reuses an existing review without pushing or creating another", async () => {+ const events: string[] = [];+ const url = "https://reviews.example.test/review/42";+ const result = await publishPatchReview({+ ...request,+ publisher: {+ label: "Review request",+ findExisting: async (branch) => {+ events.push(`find ${branch}`);+ return url;+ },+ createDraft: async () => {+ events.push("create");+ return "";+ },+ },+ pushBranch: async () => {+ events.push("push");+ },+ });+ expect(result).toBe(url);+ expect(events).toEqual([`find ${request.branch}`]);+});++test("stops before pushing when review lookup fails", async () => {+ const failure = new Error("Synthetic review service unavailable");+ const events: string[] = [];+ await expect(+ publishPatchReview({+ ...request,+ publisher: {+ label: "Review request",+ findExisting: async () => {+ events.push("find");+ throw failure;+ },+ createDraft: async () => {+ events.push("create");+ return "";+ },+ },+ pushBranch: async () => {+ events.push("push");+ },+ }),+ ).rejects.toBe(failure);+ expect(events).toEqual(["find"]);+});++test("rechecks the provider after a create response is lost", async () => {+ const url = "https://reviews.example.test/review/42";+ const failure = new Error("Synthetic connection closed after creation");+ let existing = "";+ const events: string[] = [];+ const publisher: PatchReviewPublisher = {+ label: "Review request",+ findExisting: async (branch) => {+ expect(branch).toBe(request.branch);+ events.push("find");+ return existing;+ },+ createDraft: async (received) => {+ expect(received).toEqual(request);+ events.push("create");+ existing = url;+ throw failure;+ },+ };+ const options = {+ ...request,+ publisher,+ pushBranch: async (branch: string) => {+ expect(branch).toBe(request.branch);+ events.push("push");+ },+ };++ await expect(publishPatchReview(options)).rejects.toBe(failure);+ expect(await publishPatchReview(options)).toBe(url);+ expect(events).toEqual(["find", "push", "create", "find"]);+});++test("keeps native gh repository resolution for an unrecognized remote host", async () => {+ const url = "https://forge.example.test/team/project/pull/42";+ const calls: Array<readonly string[]> = [];+ const publisher = resolvePatchReviewPublisher(+ "ssh://[email protected]/team/project.git",+ {},+ async (command, args) => {+ expect(command).toBe("gh");+ calls.push(args);+ return args[1] === "create" ? url : "";+ },+ );+ expect(await publisher.findExisting(request.branch)).toBe("");+ expect(await publisher.createDraft(request)).toBe(url);+ expect(calls.map((args) => args.slice(0, 2))).toEqual([+ ["pr", "list"],+ ["pr", "create"],+ ]);+ for (const args of calls) expect(args).not.toContain("--repo");+ const create = calls[1]!;+ expect(create[create.indexOf("--body") + 1]).toBe(request.body);+});