Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/skill-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ jobs:
ref: ${{ steps.clawhub_source.outputs.ref }}
path: clawhub-source

# actions/checkout only writes inside the caller's workspace. A skill published from the
# repository root (skill_path: ".") would then contain this checkout and upload it as
# skill files, so move it out before anything is installed into it.
- name: Move ClawHub source out of the caller workspace
run: mv "$GITHUB_WORKSPACE/clawhub-source" "$RUNNER_TEMP/clawhub-source"

- name: Install ClawHub CLI dependencies
working-directory: clawhub-source
working-directory: ${{ runner.temp }}/clawhub-source
run: bun install --frozen-lockfile

- name: Validate publish mode inputs
Expand Down Expand Up @@ -224,7 +230,7 @@ jobs:
from pathlib import Path

workspace = Path(os.environ["GITHUB_WORKSPACE"]).resolve()
cli_entry = workspace / "clawhub-source" / "packages" / "clawhub" / "src" / "cli.ts"
cli_entry = Path(os.environ["RUNNER_TEMP"]) / "clawhub-source" / "packages" / "clawhub" / "src" / "cli.ts"
if not cli_entry.is_file():
raise SystemExit(f"Missing ClawHub CLI entrypoint at {cli_entry}")

Expand Down
117 changes: 79 additions & 38 deletions scripts/security/skill-publish-workflow.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @vitest-environment node */
import { spawnSync } from "node:child_process";
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { join, relative } from "node:path";
import { describe, expect, it } from "vitest";
import { parse as parseYaml } from "yaml";

Expand All @@ -11,56 +11,80 @@ const workflow = parseYaml(readFileSync(".github/workflows/skill-publish.yml", "
const publishStep = workflow.jobs.publish.steps.find(
(step: { name?: string }) => step.name === "Run skill publishes",
);
const moveStep = workflow.jobs.publish.steps.find(
(step: { name?: string }) => step.name === "Move ClawHub source out of the caller workspace",
);

function runPublish(stdout: string) {
function runPublish(stdout: string, { skillPath = "" } = {}) {
mkdirSync(".artifacts", { recursive: true });
const workspace = mkdtempSync(join(process.cwd(), ".artifacts/skill-publish-test-"));
const root = mkdtempSync(join(process.cwd(), ".artifacts/skill-publish-test-"));
try {
const bin = join(workspace, "bin");
// Separate dirs, as on a real runner. actions/checkout puts the ClawHub source inside the
// caller's workspace; the workflow must move it to RUNNER_TEMP before publishing, or a skill
// at the repository root uploads it as its own files.
const workspace = join(root, "workspace");
const runnerTemp = join(root, "runner-temp");
mkdirSync(runnerTemp, { recursive: true });
const bin = join(root, "bin");
mkdirSync(bin);
const fakeBun = join(bin, "bun");
writeFileSync(
fakeBun,
'#!/usr/bin/env python3\nimport os\nprint(os.environ["PUBLISH_TEST_STDOUT"])\n',
[
"#!/usr/bin/env python3",
"import json, os, sys",
'seen = {"argv": sys.argv[1:], "checkoutInWorkspace": os.path.exists("clawhub-source")}',
'open(os.environ["PUBLISH_TEST_SEEN"], "w").write(json.dumps(seen))',
'print(os.environ["PUBLISH_TEST_STDOUT"])',
"",
].join("\n"),
);
chmodSync(fakeBun, 0o755);
mkdirSync(join(workspace, "skills", "demo"), { recursive: true });
writeFileSync(join(workspace, "skills", "demo", "SKILL.md"), "# Demo\n");
const skillDir = skillPath ? join(workspace, skillPath) : join(workspace, "skills", "demo");
mkdirSync(skillDir, { recursive: true });
writeFileSync(join(skillDir, "SKILL.md"), "# Demo\n");
const seenFile = join(runnerTemp, "bun-seen.json");
const cliDir = join(workspace, "clawhub-source", "packages", "clawhub", "src");
mkdirSync(cliDir, { recursive: true });
writeFileSync(join(cliDir, "cli.ts"), "// CLI subprocess is a receipt fixture.\n");
if (!publishStep?.run) throw new Error("Missing skill publish workflow step");
const child = spawnSync("bash", ["-c", publishStep.run], {
cwd: workspace,
encoding: "utf8",
env: {
...process.env,
PATH: `${bin}:${process.env.PATH}`,
GITHUB_WORKSPACE: workspace,
RUNNER_TEMP: workspace,
INPUT_SKILL_PATH: "",
INPUT_ROOT: "skills",
INPUT_DRY_RUN: "false",
INPUT_OWNER: "",
INPUT_TAGS: "",
INPUT_CHANGELOG: "",
INPUT_CATEGORIES: "",
INPUT_TOPICS: "",
INPUT_CLEAR_CATEGORIES: "false",
INPUT_CLEAR_TOPICS: "false",
INPUT_SITE: "https://example.invalid",
INPUT_REGISTRY: "https://example.invalid",
INPUT_REF: "",
SOURCE_REF: "refs/heads/main",
SOURCE_REPOSITORY: "fixture/demo",
PUBLISH_TEST_STDOUT: stdout,
},
});
if (child.error) throw child.error;
const output = JSON.parse(readFileSync(join(workspace, "skill-publish.json"), "utf8"));
return { status: child.status, output };
if (!publishStep?.run || !moveStep?.run) throw new Error("Missing skill publish workflow step");
const env = {
...process.env,
PATH: `${bin}:${process.env.PATH}`,
GITHUB_WORKSPACE: workspace,
RUNNER_TEMP: runnerTemp,
INPUT_SKILL_PATH: skillPath,
INPUT_ROOT: "skills",
INPUT_DRY_RUN: "false",
INPUT_OWNER: "",
INPUT_TAGS: "",
INPUT_CHANGELOG: "",
INPUT_CATEGORIES: "",
INPUT_TOPICS: "",
INPUT_CLEAR_CATEGORIES: "false",
INPUT_CLEAR_TOPICS: "false",
INPUT_SITE: "https://example.invalid",
INPUT_REGISTRY: "https://example.invalid",
INPUT_REF: "",
SOURCE_REF: "refs/heads/main",
SOURCE_REPOSITORY: "fixture/demo",
PUBLISH_TEST_STDOUT: stdout,
PUBLISH_TEST_SEEN: seenFile,
};
let status: number | null = null;
for (const step of [moveStep, publishStep]) {
const child = spawnSync("bash", ["-c", step.run], { cwd: workspace, encoding: "utf8", env });
if (child.error) throw child.error;
if (step === publishStep) status = child.status;
}
const output = JSON.parse(readFileSync(join(runnerTemp, "skill-publish.json"), "utf8"));
const seen = JSON.parse(readFileSync(seenFile, "utf8")) as {
argv: string[];
checkoutInWorkspace: boolean;
};
return { status, output, seen, workspace };
} finally {
rmSync(workspace, { recursive: true, force: true });
rmSync(root, { recursive: true, force: true });
}
}

Expand All @@ -87,4 +111,21 @@ describe("skill publish workflow receipts", () => {
expect(result.output.ok).toBe(false);
expect(result.output.summary.failed).toBe(1);
});

it("publishes a repository-root skill with the CLI outside the caller workspace", () => {
const receipt = {
status: "published",
slug: "demo",
version: "1.0.0",
attemptId: "fixture-attempt",
};
const result = runPublish(JSON.stringify(receipt), { skillPath: "." });
expect(result.status).toBe(0);
expect(result.output.published).toEqual([receipt]);
expect(result.seen.checkoutInWorkspace).toBe(false);
const [cliEntry, , workdir] = result.seen.argv;
expect(workdir).toBe(result.workspace);
expect(relative(result.workspace, cliEntry).startsWith("..")).toBe(true);
expect(result.seen.argv[result.seen.argv.indexOf("publish") + 1]).toBe(".");
});
});
20 changes: 20 additions & 0 deletions src/__tests__/skill-publish-workflow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,24 @@ describe("skill publish workflow", () => {
expect(workflow).toContain("uses: actions/upload-artifact@v7\n if: ${{ always() }}");
expect(workflow).toContain("if-no-files-found: ignore");
});

it("keeps the ClawHub checkout out of a repository-root skill", () => {
const workflow = readFileSync(resolve(".github/workflows/skill-publish.yml"), "utf8");
const steps = (
parseYaml(workflow) as {
jobs: {
publish: { steps: Array<{ name?: string; run?: string; "working-directory"?: string }> };
};
}
).jobs.publish.steps;
const names = steps.map((step) => step.name);
const move = steps.find(
(step) => step.name === "Move ClawHub source out of the caller workspace",
);
const install = steps.find((step) => step.name === "Install ClawHub CLI dependencies");

expect(move).toBeDefined();
expect(names.indexOf(move?.name)).toBeLessThan(names.indexOf(install?.name));
expect(install?.["working-directory"]).toBe("${{ runner.temp }}/clawhub-source");
});
});
Loading