Skip to content

Commit c28d11f

Browse files
authored
fix(gateway): allow managed publication with repo hooks (#129576)
1 parent fbd0677 commit c28d11f

6 files changed

Lines changed: 100 additions & 12 deletions

‎src/gateway/github-publication-base.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function githubPublicationUnsafeConfigArgs(scope: "--local" | "--worktree
5555
scope,
5656
"--includes",
5757
"--get-regexp",
58-
"^(core\\.(alternaterefscommand|askpass|fsmonitor|gitproxy|hookspath|sshcommand|worktree)|credential\\..*helper|filter\\..*|http\\..*|include(if)?\\..*|push\\..*|remote\\..*\\.(proxy|receivepack|uploadpack|vcs)|uploadpack\\.packobjectshook|url\\..*\\.(insteadof|pushinsteadof))$",
58+
"^(core\\.(alternaterefscommand|askpass|fsmonitor|gitproxy|sshcommand|worktree)|credential\\..*helper|filter\\..*|http\\..*|include(if)?\\..*|push\\..*|remote\\..*\\.(proxy|receivepack|uploadpack|vcs)|uploadpack\\.packobjectshook|url\\..*\\.(insteadof|pushinsteadof))$",
5959
];
6060
}
6161

‎src/gateway/github-publication-executor.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -555,12 +555,7 @@ export async function executeGitHubPublication(params: {
555555
GIT_CONFIG_GLOBAL: os.devNull,
556556
GIT_CONFIG_SYSTEM: os.devNull,
557557
};
558-
const pushArgs = [
559-
"git",
560-
"-c",
561-
`core.hooksPath=${os.devNull}`,
562-
...githubPublicationPushArgs(httpsRemote, headCommit, branch).slice(1),
563-
];
558+
const pushArgs = githubPublicationPushArgs(httpsRemote, headCommit, branch);
564559
const observeRemoteHead = async () => {
565560
const observed = await requireCommand(githubPublicationRemoteHeadArgs(httpsRemote, branch), {
566561
cwd: worktree.path,

‎src/gateway/github-publication-git-index.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "node:fs/promises";
2+
import os from "node:os";
23
import path from "node:path";
34
import { afterEach, beforeEach, describe, expect, it } from "vitest";
45
import { runCommandBuffered } from "../process/exec.js";
@@ -14,6 +15,8 @@ import {
1415
import {
1516
assertGitHubPublicationTreeHasNoFilters,
1617
assertSafeGitPublicationWorkspace,
18+
githubPublicationPushArgs,
19+
githubPublicationUpdateRefArgs,
1720
} from "./github-publication-git-transport.js";
1821

1922
let testState: OpenClawTestState;
@@ -114,6 +117,92 @@ describe("GitHub publication index update", () => {
114117
).resolves.toBeUndefined();
115118
});
116119

120+
it.each([
121+
{ scope: "--local" as const, label: "repository-local" },
122+
{ scope: "--worktree" as const, label: "worktree-scoped" },
123+
])("publishes and recovers with $label hooks without executing them", async ({ scope }) => {
124+
const fixture = await createFixture();
125+
const remote = await makeDirectory("remote");
126+
const hooks = await makeDirectory("hooks");
127+
const marker = path.join(hooks, "invoked");
128+
const hookEnv = { ...process.env, OPENCLAW_PUBLICATION_HOOK_MARKER: marker };
129+
await git(remote, ["init", "--bare"]);
130+
await Promise.all(
131+
["pre-push", "post-index-change", "reference-transaction"].map(
132+
async (hook) =>
133+
await fs.writeFile(
134+
path.join(hooks, hook),
135+
'#!/bin/sh\nprintf invoked > "$OPENCLAW_PUBLICATION_HOOK_MARKER"\nexit 97\n',
136+
{ mode: 0o755 },
137+
),
138+
),
139+
);
140+
if (scope === "--worktree") {
141+
await git(fixture.cwd, ["config", "--local", "extensions.worktreeConfig", "true"]);
142+
}
143+
await git(fixture.cwd, ["config", scope, "core.hooksPath", hooks]);
144+
145+
await expect(
146+
assertSafeGitPublicationWorkspace(
147+
fixture.cwd,
148+
async (argv, options) =>
149+
await runCommandBuffered(argv, {
150+
cwd: options?.cwd ?? fixture.cwd,
151+
env: options?.env,
152+
timeoutMs: 10_000,
153+
maxOutputBytes: 64 * 1024,
154+
}),
155+
),
156+
).resolves.toBeUndefined();
157+
158+
await expect(
159+
updateGitHubPublicationBranchAndIndex({
160+
...publicationIndexParams(fixture),
161+
env: hookEnv,
162+
updateRef: async () => {
163+
await git(
164+
fixture.cwd,
165+
githubPublicationUpdateRefArgs("main", fixture.headCommit, fixture.previousHead).slice(
166+
1,
167+
),
168+
undefined,
169+
hookEnv,
170+
);
171+
throw new Error("response lost");
172+
},
173+
}),
174+
).rejects.toThrow("workspace recovery is pending");
175+
await fs.rm(path.join(fixture.cwd, ".git", "index.lock"));
176+
const hardenedGit = ["-c", `core.hooksPath=${os.devNull}`, "-c", "core.fsmonitor=false"];
177+
await git(
178+
fixture.cwd,
179+
[...hardenedGit, "update-index", "--force-remove", "artifact.txt"],
180+
undefined,
181+
hookEnv,
182+
);
183+
await git(fixture.cwd, [...hardenedGit, "add", "artifact.txt"], undefined, hookEnv);
184+
await recoverGitHubPublicationBranchAndIndex({
185+
cwd: fixture.cwd,
186+
requestId: REQUEST_ID,
187+
branch: "main",
188+
sourceHeadCommit: fixture.previousHead,
189+
workspaceTree: fixture.workspaceTree,
190+
assertCurrent: () => undefined,
191+
run: async (argv, options) =>
192+
await git(fixture.cwd, argv.slice(1), options?.input, options?.env ?? hookEnv),
193+
});
194+
await expect(fs.access(marker)).rejects.toThrow();
195+
await git(
196+
fixture.cwd,
197+
githubPublicationPushArgs(remote, fixture.headCommit, "publication").slice(1),
198+
undefined,
199+
hookEnv,
200+
);
201+
202+
expect(await git(remote, ["rev-parse", "refs/heads/publication"])).toBe(fixture.headCommit);
203+
await expect(fs.access(marker)).rejects.toThrow();
204+
});
205+
117206
it("rejects a filter from Git's default global attributes file", async () => {
118207
const cwd = await makeDirectory("attributes");
119208
const globalAttributes = path.join(cwd, "global-attributes");

‎src/gateway/github-publication-git-index.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import path from "node:path";
66

77
type GitCommandOptions = { cwd?: string; env?: NodeJS.ProcessEnv; input?: string };
88

9+
const HARDENED_GIT = ["git", "-c", `core.hooksPath=${os.devNull}`, "-c", "core.fsmonitor=false"];
10+
911
class GitHubPublicationRefCasRejectedError extends Error {}
1012
export class GitHubPublicationRecoveryPendingError extends Error {}
1113

@@ -121,7 +123,7 @@ export async function recoverGitHubPublicationBranchAndIndex(params: {
121123
["git", "rev-parse", "--verify", `refs/heads/${params.branch}`],
122124
{ cwd: params.cwd },
123125
);
124-
const indexTree = await params.run(["git", "write-tree"], { cwd: params.cwd });
126+
const indexTree = await params.run([...HARDENED_GIT, "write-tree"], { cwd: params.cwd });
125127
if (
126128
branchHead === params.sourceHeadCommit ||
127129
(indexTree === params.workspaceTree && (await publicationCommitMatches(params, branchHead)))
@@ -206,8 +208,7 @@ export async function updateGitHubPublicationBranchAndIndex(params: {
206208
GIT_CONFIG_GLOBAL: os.devNull,
207209
GIT_CONFIG_SYSTEM: os.devNull,
208210
};
209-
const hardenedGit = ["git", "-c", `core.hooksPath=${os.devNull}`, "-c", "core.fsmonitor=false"];
210-
await params.run([...hardenedGit, "read-tree", params.headCommit], {
211+
await params.run([...HARDENED_GIT, "read-tree", params.headCommit], {
211212
cwd: params.cwd,
212213
env: { ...gitEnv, GIT_INDEX_FILE: replacementIndex },
213214
});
@@ -275,7 +276,7 @@ export async function updateGitHubPublicationBranchAndIndex(params: {
275276
});
276277
}
277278
await fs.copyFile(indexPath, observedIndex);
278-
const currentIndexTree = await params.run([...hardenedGit, "write-tree"], {
279+
const currentIndexTree = await params.run([...HARDENED_GIT, "write-tree"], {
279280
cwd: params.cwd,
280281
env: { ...gitEnv, GIT_INDEX_FILE: observedIndex },
281282
});

‎src/gateway/github-publication-git-transport.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ export function githubPublicationPushArgs(
173173
): string[] {
174174
return [
175175
...GITHUB_CREDENTIAL_ARGS,
176+
"-c",
177+
`core.hooksPath=${os.devNull}`,
176178
"push",
177179
"--porcelain",
178180
"--no-follow-tags",

‎src/gateway/github-publication.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ describe("Gateway GitHub publication", () => {
7474
candidate.includes("update-ref") ||
7575
candidate.includes("read-tree") ||
7676
candidate.includes("add") ||
77-
candidate.includes("reset"),
77+
candidate.includes("reset") ||
78+
candidate.includes("push"),
7879
)) {
7980
expect(argv, argv.join(" ")).toContain(`core.hooksPath=${os.devNull}`);
8081
}

0 commit comments

Comments
 (0)