Skip to content

Commit da98bd9

Browse files
committed
fix: validate opengrep rulepack-only changes
1 parent 4e79917 commit da98bd9

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎scripts/run-opengrep.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ if (( PATHS_PASSED == 0 )); then
119119
git ls-files --others --exclude-standard
120120
} | awk '/^(src|extensions|apps|packages|scripts)\// { print }' | sort -u
121121
)
122+
mapfile -t RULEPACK_CHANGED_PATHS < <(
123+
{
124+
git diff --name-only --diff-filter=ACMRTUXB "${OPENCLAW_OPENGREP_BASE_REF:-origin/main...HEAD}" 2>/dev/null || true
125+
git diff --name-only --diff-filter=ACMRTUXB -- 2>/dev/null || true
126+
git ls-files --others --exclude-standard
127+
} | awk '/^(security\/opengrep\/|scripts\/run-opengrep\.sh$|\.semgrepignore$|\.github\/workflows\/opengrep-)/ { print }' | sort -u
128+
)
129+
if (( ${#SCAN_PATHS[@]} == 0 && ${#RULEPACK_CHANGED_PATHS[@]} > 0 )); then
130+
SCAN_PATHS=( "security/opengrep/precise.yml" )
131+
fi
122132
if (( ${#SCAN_PATHS[@]} == 0 )); then
123133
echo "→ No changed first-party paths for opengrep." >&2
124134
exit 0

‎test/scripts/run-opengrep.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { execFileSync } from "node:child_process";
2+
import fs from "node:fs";
3+
import path from "node:path";
4+
import { describe, expect, it } from "vitest";
5+
import { createScriptTestHarness } from "./test-helpers.js";
6+
7+
const { createTempDir } = createScriptTestHarness();
8+
9+
function git(cwd: string, ...args: string[]): string {
10+
return execFileSync("git", args, { cwd, encoding: "utf8" }).trim();
11+
}
12+
13+
function writeFile(filePath: string, content: string): void {
14+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
15+
fs.writeFileSync(filePath, content);
16+
}
17+
18+
describe("run-opengrep.sh", () => {
19+
it("validates the rulepack when only OpenGrep rulepack files changed", () => {
20+
const repo = createTempDir("openclaw-run-opengrep-");
21+
git(repo, "init", "-q");
22+
git(repo, "config", "user.email", "[email protected]");
23+
git(repo, "config", "user.name", "Test User");
24+
25+
const scriptSource = path.resolve("scripts/run-opengrep.sh");
26+
writeFile(path.join(repo, "scripts/run-opengrep.sh"), fs.readFileSync(scriptSource, "utf8"));
27+
fs.chmodSync(path.join(repo, "scripts/run-opengrep.sh"), 0o755);
28+
writeFile(path.join(repo, "security/opengrep/precise.yml"), "rules: []\n");
29+
git(repo, "add", ".");
30+
git(repo, "commit", "-qm", "initial");
31+
32+
fs.appendFileSync(path.join(repo, "security/opengrep/precise.yml"), "# changed\n");
33+
const argsPath = path.join(repo, "opengrep-args.txt");
34+
const binDir = path.join(repo, "bin");
35+
fs.mkdirSync(binDir);
36+
writeFile(
37+
path.join(binDir, "opengrep"),
38+
[
39+
"#!/usr/bin/env bash",
40+
`printf '%s\\n' "$@" > ${JSON.stringify(argsPath)}`,
41+
"exit 0",
42+
"",
43+
].join("\n"),
44+
);
45+
fs.chmodSync(path.join(binDir, "opengrep"), 0o755);
46+
47+
execFileSync("bash", ["scripts/run-opengrep.sh", "--changed"], {
48+
cwd: repo,
49+
env: {
50+
...process.env,
51+
PATH: `${binDir}${path.delimiter}${process.env.PATH ?? ""}`,
52+
OPENCLAW_OPENGREP_BASE_REF: "HEAD",
53+
},
54+
encoding: "utf8",
55+
});
56+
57+
const args = fs.readFileSync(path.join(repo, "opengrep-args.txt"), "utf8");
58+
expect(args).toContain("security/opengrep/precise.yml");
59+
});
60+
});

0 commit comments

Comments
 (0)