Skip to content

Commit 59567a8

Browse files
committed
ci: move changed-scope logic into tested script
1 parent 8ee357f commit 59567a8

4 files changed

Lines changed: 201 additions & 65 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -57,71 +57,7 @@ jobs:
5757
BASE="${{ github.event.pull_request.base.sha }}"
5858
fi
5959
60-
CHANGED="$(git diff --name-only "$BASE" HEAD 2>/dev/null || echo "UNKNOWN")"
61-
if [ "$CHANGED" = "UNKNOWN" ] || [ -z "$CHANGED" ]; then
62-
# Fail-safe: run broad checks if detection fails.
63-
echo "run_node=true" >> "$GITHUB_OUTPUT"
64-
echo "run_macos=true" >> "$GITHUB_OUTPUT"
65-
echo "run_android=true" >> "$GITHUB_OUTPUT"
66-
exit 0
67-
fi
68-
69-
run_node=false
70-
run_macos=false
71-
run_android=false
72-
has_non_docs=false
73-
has_non_native_non_docs=false
74-
75-
while IFS= read -r path; do
76-
[ -z "$path" ] && continue
77-
case "$path" in
78-
docs/*|*.md|*.mdx)
79-
continue
80-
;;
81-
*)
82-
has_non_docs=true
83-
;;
84-
esac
85-
86-
case "$path" in
87-
# Generated protocol models are already covered by protocol:check and
88-
# should not force the full native macOS lane.
89-
apps/macos/Sources/OpenClawProtocol/*|apps/shared/OpenClawKit/Sources/OpenClawProtocol/*)
90-
;;
91-
apps/macos/*|apps/ios/*|apps/shared/*|Swabble/*)
92-
run_macos=true
93-
;;
94-
esac
95-
96-
case "$path" in
97-
apps/android/*|apps/shared/*)
98-
run_android=true
99-
;;
100-
esac
101-
102-
case "$path" in
103-
src/*|test/*|extensions/*|packages/*|scripts/*|ui/*|.github/*|openclaw.mjs|package.json|pnpm-lock.yaml|pnpm-workspace.yaml|tsconfig*.json|vitest*.ts|tsdown.config.ts|.oxlintrc.json|.oxfmtrc.jsonc)
104-
run_node=true
105-
;;
106-
esac
107-
108-
case "$path" in
109-
apps/android/*|apps/ios/*|apps/macos/*|apps/shared/*|Swabble/*|appcast.xml)
110-
;;
111-
*)
112-
has_non_native_non_docs=true
113-
;;
114-
esac
115-
done <<< "$CHANGED"
116-
117-
# If there are non-doc files outside native app trees, keep Node checks enabled.
118-
if [ "$run_node" = false ] && [ "$has_non_docs" = true ] && [ "$has_non_native_non_docs" = true ]; then
119-
run_node=true
120-
fi
121-
122-
echo "run_node=${run_node}" >> "$GITHUB_OUTPUT"
123-
echo "run_macos=${run_macos}" >> "$GITHUB_OUTPUT"
124-
echo "run_android=${run_android}" >> "$GITHUB_OUTPUT"
60+
node scripts/ci-changed-scope.mjs --base "$BASE" --head HEAD
12561
12662
# Build dist once for Node-relevant changes and share it with downstream jobs.
12763
build-artifacts:

‎docs/ci.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Jobs are ordered so cheap checks fail before expensive ones run:
3636
2. `build-artifacts` (blocked on above)
3737
3. `checks`, `checks-windows`, `macos`, `android` (blocked on build)
3838

39+
Scope logic lives in `scripts/ci-changed-scope.mjs` and is covered by unit tests in `src/scripts/ci-changed-scope.test.ts`.
40+
3941
## Runners
4042

4143
| Runner | Jobs |

‎scripts/ci-changed-scope.mjs‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import { execSync } from "node:child_process";
2+
import { appendFileSync } from "node:fs";
3+
4+
/** @typedef {{ runNode: boolean; runMacos: boolean; runAndroid: boolean }} ChangedScope */
5+
6+
const DOCS_PATH_RE = /^(docs\/|.*\.mdx?$)/;
7+
const MACOS_PROTOCOL_GEN_RE =
8+
/^(apps\/macos\/Sources\/OpenClawProtocol\/|apps\/shared\/OpenClawKit\/Sources\/OpenClawProtocol\/)/;
9+
const MACOS_NATIVE_RE = /^(apps\/macos\/|apps\/ios\/|apps\/shared\/|Swabble\/)/;
10+
const ANDROID_NATIVE_RE = /^(apps\/android\/|apps\/shared\/)/;
11+
const NODE_SCOPE_RE =
12+
/^(src\/|test\/|extensions\/|packages\/|scripts\/|ui\/|\.github\/|openclaw\.mjs$|package\.json$|pnpm-lock\.yaml$|pnpm-workspace\.yaml$|tsconfig.*\.json$|vitest.*\.ts$|tsdown\.config\.ts$|\.oxlintrc\.json$|\.oxfmtrc\.jsonc$)/;
13+
const NATIVE_ONLY_RE =
14+
/^(apps\/android\/|apps\/ios\/|apps\/macos\/|apps\/shared\/|Swabble\/|appcast\.xml$)/;
15+
16+
/**
17+
* @param {string[]} changedPaths
18+
* @returns {ChangedScope}
19+
*/
20+
export function detectChangedScope(changedPaths) {
21+
if (!Array.isArray(changedPaths) || changedPaths.length === 0) {
22+
return { runNode: true, runMacos: true, runAndroid: true };
23+
}
24+
25+
let runNode = false;
26+
let runMacos = false;
27+
let runAndroid = false;
28+
let hasNonDocs = false;
29+
let hasNonNativeNonDocs = false;
30+
31+
for (const rawPath of changedPaths) {
32+
const path = String(rawPath).trim();
33+
if (!path) {
34+
continue;
35+
}
36+
37+
if (DOCS_PATH_RE.test(path)) {
38+
continue;
39+
}
40+
41+
hasNonDocs = true;
42+
43+
if (!MACOS_PROTOCOL_GEN_RE.test(path) && MACOS_NATIVE_RE.test(path)) {
44+
runMacos = true;
45+
}
46+
47+
if (ANDROID_NATIVE_RE.test(path)) {
48+
runAndroid = true;
49+
}
50+
51+
if (NODE_SCOPE_RE.test(path)) {
52+
runNode = true;
53+
}
54+
55+
if (!NATIVE_ONLY_RE.test(path)) {
56+
hasNonNativeNonDocs = true;
57+
}
58+
}
59+
60+
if (!runNode && hasNonDocs && hasNonNativeNonDocs) {
61+
runNode = true;
62+
}
63+
64+
return { runNode, runMacos, runAndroid };
65+
}
66+
67+
/**
68+
* @param {string} base
69+
* @param {string} [head]
70+
* @returns {string[]}
71+
*/
72+
export function listChangedPaths(base, head = "HEAD") {
73+
if (!base) {
74+
return [];
75+
}
76+
const output = execSync(`git diff --name-only ${base} ${head}`, {
77+
stdio: ["ignore", "pipe", "pipe"],
78+
encoding: "utf8",
79+
});
80+
return output
81+
.split("\n")
82+
.map((line) => line.trim())
83+
.filter((line) => line.length > 0);
84+
}
85+
86+
/**
87+
* @param {ChangedScope} scope
88+
* @param {string} [outputPath]
89+
*/
90+
export function writeGitHubOutput(scope, outputPath = process.env.GITHUB_OUTPUT) {
91+
if (!outputPath) {
92+
throw new Error("GITHUB_OUTPUT is required");
93+
}
94+
appendFileSync(outputPath, `run_node=${scope.runNode}\n`, "utf8");
95+
appendFileSync(outputPath, `run_macos=${scope.runMacos}\n`, "utf8");
96+
appendFileSync(outputPath, `run_android=${scope.runAndroid}\n`, "utf8");
97+
}
98+
99+
function isDirectRun() {
100+
const direct = process.argv[1];
101+
return Boolean(direct && import.meta.url.endsWith(direct));
102+
}
103+
104+
/** @param {string[]} argv */
105+
function parseArgs(argv) {
106+
const args = { base: "", head: "HEAD" };
107+
for (let i = 0; i < argv.length; i += 1) {
108+
if (argv[i] === "--base") {
109+
args.base = argv[i + 1] ?? "";
110+
i += 1;
111+
continue;
112+
}
113+
if (argv[i] === "--head") {
114+
args.head = argv[i + 1] ?? "HEAD";
115+
i += 1;
116+
}
117+
}
118+
return args;
119+
}
120+
121+
if (isDirectRun()) {
122+
const args = parseArgs(process.argv.slice(2));
123+
try {
124+
const changedPaths = listChangedPaths(args.base, args.head);
125+
if (changedPaths.length === 0) {
126+
writeGitHubOutput({ runNode: true, runMacos: true, runAndroid: true });
127+
process.exit(0);
128+
}
129+
writeGitHubOutput(detectChangedScope(changedPaths));
130+
} catch {
131+
writeGitHubOutput({ runNode: true, runMacos: true, runAndroid: true });
132+
}
133+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { describe, expect, it } from "vitest";
2+
import { detectChangedScope } from "../../scripts/ci-changed-scope.mjs";
3+
4+
describe("detectChangedScope", () => {
5+
it("fails safe when no paths are provided", () => {
6+
expect(detectChangedScope([])).toEqual({
7+
runNode: true,
8+
runMacos: true,
9+
runAndroid: true,
10+
});
11+
});
12+
13+
it("keeps all lanes off for docs-only changes", () => {
14+
expect(detectChangedScope(["docs/ci.md", "README.md"])).toEqual({
15+
runNode: false,
16+
runMacos: false,
17+
runAndroid: false,
18+
});
19+
});
20+
21+
it("enables node lane for node-relevant files", () => {
22+
expect(detectChangedScope(["src/plugins/runtime/index.ts"])).toEqual({
23+
runNode: true,
24+
runMacos: false,
25+
runAndroid: false,
26+
});
27+
});
28+
29+
it("keeps node lane off for native-only changes", () => {
30+
expect(detectChangedScope(["apps/macos/Sources/Foo.swift"])).toEqual({
31+
runNode: false,
32+
runMacos: true,
33+
runAndroid: false,
34+
});
35+
expect(detectChangedScope(["apps/shared/OpenClawKit/Sources/Foo.swift"])).toEqual({
36+
runNode: false,
37+
runMacos: true,
38+
runAndroid: true,
39+
});
40+
});
41+
42+
it("does not force macOS for generated protocol model-only changes", () => {
43+
expect(detectChangedScope(["apps/macos/Sources/OpenClawProtocol/GatewayModels.swift"])).toEqual(
44+
{
45+
runNode: false,
46+
runMacos: false,
47+
runAndroid: false,
48+
},
49+
);
50+
});
51+
52+
it("enables node lane for non-native non-doc files by fallback", () => {
53+
expect(detectChangedScope(["README.md"])).toEqual({
54+
runNode: false,
55+
runMacos: false,
56+
runAndroid: false,
57+
});
58+
59+
expect(detectChangedScope(["assets/icon.png"])).toEqual({
60+
runNode: true,
61+
runMacos: false,
62+
runAndroid: false,
63+
});
64+
});
65+
});

0 commit comments

Comments
 (0)