Gate dispatched GitHub CLI mutations
This commit is contained in:
parent
d93267eb47
commit
01bc5558af
10 changed files with 423 additions and 28 deletions
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