Gate dispatched GitHub CLI mutations
This commit is contained in:
parent
d93267eb47
commit
01bc5558af
10 changed files with 423 additions and 28 deletions
|
|
@ -129,7 +129,11 @@ async function createGitHubDraftPr(options) {
|
|||
base,
|
||||
"--head",
|
||||
branch,
|
||||
], repoRoot);
|
||||
], repoRoot, {
|
||||
env: {
|
||||
BIFROST_GITHUB_MUTATION_AUTHORIZED: "true",
|
||||
},
|
||||
});
|
||||
prUrl = pr.stdout.trim();
|
||||
await bridgeAction?.complete({
|
||||
receiptUrl: prUrl,
|
||||
|
|
@ -218,6 +222,11 @@ async function commentGitHubPr(options) {
|
|||
"--input", payloadPath,
|
||||
],
|
||||
repoRoot,
|
||||
{
|
||||
env: {
|
||||
BIFROST_GITHUB_MUTATION_AUTHORIZED: "true",
|
||||
},
|
||||
},
|
||||
).stdout,
|
||||
"GitHub PR comment response",
|
||||
);
|
||||
|
|
@ -867,7 +876,7 @@ async function readOptionText(options, inlineName, fileName, fallback) {
|
|||
}
|
||||
|
||||
function git(args, cwd, options = {}) {
|
||||
return run("git", args, cwd, options);
|
||||
return run(resolveGitCommand(), args, cwd, options);
|
||||
}
|
||||
|
||||
function runGitHubCli(options, args, cwd, runOptions = {}) {
|
||||
|
|
@ -876,16 +885,25 @@ function runGitHubCli(options, args, cwd, runOptions = {}) {
|
|||
}
|
||||
|
||||
function run(command, args, cwd, options = {}) {
|
||||
const result = spawnSync(command, args, {
|
||||
cwd,
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
env: {
|
||||
...process.env,
|
||||
...(options.env ?? {}),
|
||||
},
|
||||
windowsHide: true,
|
||||
});
|
||||
const env = {
|
||||
...process.env,
|
||||
...(options.env ?? {}),
|
||||
};
|
||||
const result = isWindowsCommandScript(command)
|
||||
? spawnSync("cmd.exe", ["/d", "/s", "/c", command, ...args], {
|
||||
cwd,
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
env,
|
||||
windowsHide: true,
|
||||
})
|
||||
: spawnSync(command, args, {
|
||||
cwd,
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
env,
|
||||
windowsHide: true,
|
||||
});
|
||||
|
||||
if (result.status !== 0 && !options.allowFailure) {
|
||||
throw new Error(`${command} ${args.join(" ")} failed with ${result.status ?? "unknown"}:\n${result.stderr || result.stdout}`);
|
||||
|
|
@ -936,6 +954,10 @@ function optionalString(value) {
|
|||
return trimmed.length > 0 ? trimmed : undefined;
|
||||
}
|
||||
|
||||
function resolveGitCommand() {
|
||||
return optionalString(process.env.BIFROST_GIT_EXECUTABLE) ?? "git";
|
||||
}
|
||||
|
||||
async function beginBridgeAction(options, action) {
|
||||
const baseUrl = optionalString(options["bifrost-base-url"]) ?? optionalString(process.env.BIFROST_BRIDGE_BASE_URL);
|
||||
if (!baseUrl) {
|
||||
|
|
@ -1085,6 +1107,10 @@ function resolveGitHubCommand(options) {
|
|||
};
|
||||
}
|
||||
|
||||
function isWindowsCommandScript(command) {
|
||||
return /\.cmd$/i.test(command) || /\.bat$/i.test(command);
|
||||
}
|
||||
|
||||
function splitCommandArgs(value) {
|
||||
if (typeof value !== "string" || value.trim().length === 0) {
|
||||
return [];
|
||||
|
|
|
|||
|
|
@ -561,12 +561,19 @@ function renderDispatchReceiptContent(request) {
|
|||
|
||||
function buildBridgeContextEnv(request) {
|
||||
const hooksPath = resolve(bifrostRoot, "tools", "git-hooks");
|
||||
const gitGatePath = resolve(bifrostRoot, "tools", "git-gate");
|
||||
return {
|
||||
BIFROST_BRIDGE_SOURCE_KIND: "bifrost_agent_transport_request",
|
||||
BIFROST_BRIDGE_SOURCE_ID: request.id,
|
||||
BIFROST_BRIDGE_AUTHORITY_REF: "bifrost_dispatch_execution",
|
||||
BIFROST_ENFORCE_GITHUB_GATE: "true",
|
||||
BIFROST_GIT_EXECUTABLE: resolve(gitGatePath, "git.cmd"),
|
||||
BIFROST_GH_EXECUTABLE: resolve(gitGatePath, "gh.cmd"),
|
||||
BIFROST_REAL_GIT: resolveRealGitExecutable(),
|
||||
...buildRealGitHubCliEnv(),
|
||||
BIFROST_NODE_EXECUTABLE: process.execPath,
|
||||
...buildGitHooksConfigEnv(hooksPath),
|
||||
...buildPathPrependEnv(gitGatePath),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -587,6 +594,50 @@ function buildGitHooksConfigEnv(hooksPath) {
|
|||
};
|
||||
}
|
||||
|
||||
function buildPathPrependEnv(directory) {
|
||||
const currentPath = process.env.Path ?? process.env.PATH ?? "";
|
||||
const nextPath = currentPath ? `${directory};${currentPath}` : directory;
|
||||
return {
|
||||
PATH: nextPath,
|
||||
Path: nextPath,
|
||||
};
|
||||
}
|
||||
|
||||
function buildRealGitHubCliEnv() {
|
||||
const realGh = tryResolveExecutable("gh");
|
||||
return realGh ? { BIFROST_REAL_GH: realGh } : {};
|
||||
}
|
||||
|
||||
function resolveRealGitExecutable() {
|
||||
const resolved = tryResolveExecutable("git");
|
||||
if (!resolved) {
|
||||
throw new Error("Could not resolve the real git executable for dispatched turn gating.");
|
||||
}
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
function tryResolveExecutable(command) {
|
||||
const locator = process.platform === "win32" ? "where.exe" : "which";
|
||||
const result = spawnSync(locator, [command], {
|
||||
cwd: bifrostRoot,
|
||||
encoding: "utf8",
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
windowsHide: true,
|
||||
});
|
||||
|
||||
if (result.status !== 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const matches = `${result.stdout ?? ""}`
|
||||
.split(/\r?\n/)
|
||||
.map((line) => line.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
return matches[0] ?? "";
|
||||
}
|
||||
|
||||
class CodexAppServerClient {
|
||||
constructor({ command, env, logPath }) {
|
||||
this.command = command;
|
||||
|
|
|
|||
118
tools/git-gate/gh-proxy.mjs
Normal file
118
tools/git-gate/gh-proxy.mjs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
#!/usr/bin/env node
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const realGh = process.env.BIFROST_REAL_GH;
|
||||
const realGhArgs = splitCommandArgs(process.env.BIFROST_REAL_GH_ARGS ?? "");
|
||||
const mutatingCommands = new Set([
|
||||
"gist create",
|
||||
"gist delete",
|
||||
"gist edit",
|
||||
"issue close",
|
||||
"issue comment",
|
||||
"issue create",
|
||||
"issue delete",
|
||||
"issue edit",
|
||||
"issue lock",
|
||||
"issue reopen",
|
||||
"pr close",
|
||||
"pr comment",
|
||||
"pr create",
|
||||
"pr edit",
|
||||
"pr lock",
|
||||
"pr merge",
|
||||
"pr ready",
|
||||
"pr reopen",
|
||||
"pr review",
|
||||
"release create",
|
||||
"release delete",
|
||||
"release edit",
|
||||
"repo archive",
|
||||
"repo create",
|
||||
"repo delete",
|
||||
"repo edit",
|
||||
"repo fork",
|
||||
"repo rename",
|
||||
"secret delete",
|
||||
"secret set",
|
||||
"variable delete",
|
||||
"variable set",
|
||||
]);
|
||||
|
||||
if (!realGh) {
|
||||
process.stderr.write("Bifrost GitHub gate has no real GitHub CLI executable configured.\n");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (
|
||||
process.env.BIFROST_ENFORCE_GITHUB_GATE === "true" &&
|
||||
isMutatingGitHubCliCommand(args) &&
|
||||
process.env.BIFROST_GITHUB_MUTATION_AUTHORIZED !== "true"
|
||||
) {
|
||||
process.stderr.write(
|
||||
"Bifrost blocked GitHub CLI mutation in this dispatched turn. Use tools/bifrost-bridge.mjs so GitHub publication is gated and receipted.\n",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const result = isWindowsCommandScript(realGh)
|
||||
? spawnSync("cmd.exe", ["/d", "/s", "/c", realGh, ...realGhArgs, ...args], {
|
||||
stdio: "inherit",
|
||||
env: process.env,
|
||||
windowsHide: true,
|
||||
})
|
||||
: spawnSync(realGh, [...realGhArgs, ...args], {
|
||||
stdio: "inherit",
|
||||
env: process.env,
|
||||
windowsHide: true,
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
process.exit(result.status ?? 1);
|
||||
|
||||
function isWindowsCommandScript(command) {
|
||||
return /\.cmd$/i.test(command) || /\.bat$/i.test(command);
|
||||
}
|
||||
|
||||
function isMutatingGitHubCliCommand(input) {
|
||||
const [scope = "", action = ""] = input;
|
||||
if (!scope) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (scope === "api") {
|
||||
return apiMethod(input) !== "GET";
|
||||
}
|
||||
|
||||
return mutatingCommands.has(`${scope} ${action}`);
|
||||
}
|
||||
|
||||
function apiMethod(input) {
|
||||
for (let index = 0; index < input.length; index += 1) {
|
||||
const token = input[index];
|
||||
if (token === "--method") {
|
||||
return `${input[index + 1] ?? "GET"}`.trim().toUpperCase();
|
||||
}
|
||||
|
||||
if (token.startsWith("--method=")) {
|
||||
return token.slice("--method=".length).trim().toUpperCase();
|
||||
}
|
||||
}
|
||||
|
||||
return input.includes("--input") ? "POST" : "GET";
|
||||
}
|
||||
|
||||
function splitCommandArgs(value) {
|
||||
if (!value) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const matches = value.match(/(?:[^\s"]+|"[^"]*")+/g) ?? [];
|
||||
return matches
|
||||
.map((part) => part.trim())
|
||||
.filter(Boolean)
|
||||
.map((part) => (part.startsWith("\"") && part.endsWith("\"")) ? part.slice(1, -1) : part);
|
||||
}
|
||||
18
tools/git-gate/gh.cmd
Normal file
18
tools/git-gate/gh.cmd
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
@echo off
|
||||
setlocal
|
||||
|
||||
set "REAL_GH=%BIFROST_REAL_GH%"
|
||||
if not defined REAL_GH (
|
||||
echo Bifrost GitHub gate has no real GitHub CLI executable configured.>&2
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set "NODE_EXE=%BIFROST_NODE_EXECUTABLE%"
|
||||
if not defined NODE_EXE (
|
||||
echo Bifrost GitHub gate has no Node executable configured.>&2
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set "NODE_SCRIPT=%~dp0gh-proxy.mjs"
|
||||
"%NODE_EXE%" "%NODE_SCRIPT%" %*
|
||||
exit /b %ERRORLEVEL%
|
||||
64
tools/git-gate/git-proxy.mjs
Normal file
64
tools/git-gate/git-proxy.mjs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
#!/usr/bin/env node
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const realGit = process.env.BIFROST_REAL_GIT;
|
||||
|
||||
if (!realGit) {
|
||||
process.stderr.write("Bifrost Git gate has no real git executable configured.\n");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const subcommand = findGitSubcommand(args);
|
||||
if (
|
||||
process.env.BIFROST_ENFORCE_GITHUB_GATE === "true" &&
|
||||
subcommand === "push" &&
|
||||
process.env.BIFROST_GITHUB_PUSH_AUTHORIZED !== "true"
|
||||
) {
|
||||
process.stderr.write(
|
||||
"Bifrost blocked git push in this dispatched turn. Use tools/bifrost-bridge.mjs so GitHub publication is gated and receipted.\n",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const result = spawnSync(realGit, args, {
|
||||
stdio: "inherit",
|
||||
env: process.env,
|
||||
windowsHide: true,
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
throw result.error;
|
||||
}
|
||||
|
||||
process.exit(result.status ?? 1);
|
||||
|
||||
function findGitSubcommand(input) {
|
||||
const optionsWithSeparateValues = new Set([
|
||||
"-C",
|
||||
"-c",
|
||||
"--exec-path",
|
||||
"--git-dir",
|
||||
"--work-tree",
|
||||
"--namespace",
|
||||
"--super-prefix",
|
||||
"--config-env",
|
||||
]);
|
||||
|
||||
for (let index = 0; index < input.length; index += 1) {
|
||||
const token = input[index];
|
||||
if (token === "--") {
|
||||
return input[index + 1] ?? "";
|
||||
}
|
||||
|
||||
if (!token.startsWith("-")) {
|
||||
return token;
|
||||
}
|
||||
|
||||
if (optionsWithSeparateValues.has(token)) {
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
18
tools/git-gate/git.cmd
Normal file
18
tools/git-gate/git.cmd
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
@echo off
|
||||
setlocal
|
||||
|
||||
set "REAL_GIT=%BIFROST_REAL_GIT%"
|
||||
if not defined REAL_GIT (
|
||||
echo Bifrost Git gate has no real git executable configured.>&2
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set "NODE_EXE=%BIFROST_NODE_EXECUTABLE%"
|
||||
if not defined NODE_EXE (
|
||||
echo Bifrost Git gate has no Node executable configured.>&2
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set "NODE_SCRIPT=%~dp0git-proxy.mjs"
|
||||
"%NODE_EXE%" "%NODE_SCRIPT%" %*
|
||||
exit /b %ERRORLEVEL%
|
||||
Loading…
Reference in a new issue