Skip to content

Commit 7950c54

Browse files
committed
fix(ci): preserve Periphery scope contracts
1 parent f0a8c40 commit 7950c54

5 files changed

Lines changed: 149 additions & 116 deletions

File tree

‎.github/workflows/ios-periphery.yml‎

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -40,43 +40,45 @@ jobs:
4040

4141
- name: Detect changed paths
4242
id: scope
43-
shell: bash
44-
env:
45-
BASE_SHA: ${{ github.event.pull_request.base.sha }}
46-
EVENT_NAME: ${{ github.event_name }}
47-
PR_DRAFT: ${{ github.event.pull_request.draft && 'true' || 'false' }}
48-
run: |
49-
set -euo pipefail
50-
if [ "$EVENT_NAME" = "workflow_dispatch" ]; then
51-
echo "should-scan=true" >> "$GITHUB_OUTPUT"
52-
exit 0
53-
fi
54-
if [ "$PR_DRAFT" = "true" ]; then
55-
echo "should-scan=false" >> "$GITHUB_OUTPUT"
56-
exit 0
57-
fi
43+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
44+
with:
45+
script: |
46+
if (context.eventName === "workflow_dispatch") {
47+
core.setOutput("should-scan", "true");
48+
return;
49+
}
50+
if (context.payload.pull_request?.draft) {
51+
core.setOutput("should-scan", "false");
52+
return;
53+
}
5854
59-
if git diff --quiet "$BASE_SHA" HEAD -- \
60-
apps/ios/ \
61-
apps/shared/OpenClawKit/Sources/ \
62-
apps/swabble/Sources/SwabbleKit/ \
63-
.github/workflows/ios-periphery.yml \
64-
.github/workflows/ios-periphery-comment.yml \
65-
config/swiftformat \
66-
config/swiftlint.yml \
67-
scripts/check-swift-tools.sh \
68-
scripts/format-swift.sh \
69-
scripts/install-swift-tools.sh \
70-
scripts/ios-write-swift-filelist.mjs \
71-
scripts/lint-swift.sh; then
72-
echo "should-scan=false" >> "$GITHUB_OUTPUT"
73-
else
74-
diff_status="$?"
75-
if [ "$diff_status" -ne 1 ]; then
76-
exit "$diff_status"
77-
fi
78-
echo "should-scan=true" >> "$GITHUB_OUTPUT"
79-
fi
55+
const baseSha = context.payload.pull_request?.base?.sha;
56+
if (!baseSha) {
57+
throw new Error("missing pull request base SHA");
58+
}
59+
const result = await exec.getExecOutput("git", [
60+
"diff",
61+
"--quiet",
62+
baseSha,
63+
"HEAD",
64+
"--",
65+
"apps/ios/",
66+
"apps/shared/OpenClawKit/Sources/",
67+
"apps/swabble/Sources/SwabbleKit/",
68+
".github/workflows/ios-periphery.yml",
69+
".github/workflows/ios-periphery-comment.yml",
70+
"config/swiftformat",
71+
"config/swiftlint.yml",
72+
"scripts/check-swift-tools.sh",
73+
"scripts/format-swift.sh",
74+
"scripts/install-swift-tools.sh",
75+
"scripts/ios-write-swift-filelist.mjs",
76+
"scripts/lint-swift.sh",
77+
], { ignoreReturnCode: true, silent: true });
78+
if (result.exitCode !== 0 && result.exitCode !== 1) {
79+
throw new Error(`git diff failed with exit code ${result.exitCode}`);
80+
}
81+
core.setOutput("should-scan", String(result.exitCode === 1));
8082
8183
scan:
8284
name: Scan iOS dead code

‎.github/workflows/macos-periphery.yml‎

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,37 @@ jobs:
4040

4141
- name: Detect changed paths
4242
id: scope
43-
shell: bash
44-
env:
45-
BASE_SHA: ${{ github.event.pull_request.base.sha }}
46-
EVENT_NAME: ${{ github.event_name }}
47-
PR_DRAFT: ${{ github.event.pull_request.draft && 'true' || 'false' }}
48-
run: |
49-
set -euo pipefail
50-
if [ "$EVENT_NAME" = "workflow_dispatch" ]; then
51-
echo "should-scan=true" >> "$GITHUB_OUTPUT"
52-
exit 0
53-
fi
54-
if [ "$PR_DRAFT" = "true" ]; then
55-
echo "should-scan=false" >> "$GITHUB_OUTPUT"
56-
exit 0
57-
fi
58-
59-
if git diff --quiet "$BASE_SHA" HEAD -- \
60-
apps/macos/ \
61-
.github/workflows/macos-periphery.yml \
62-
.github/workflows/ios-periphery-comment.yml \
63-
apps/macos/.periphery.yml; then
64-
echo "should-scan=false" >> "$GITHUB_OUTPUT"
65-
else
66-
diff_status="$?"
67-
if [ "$diff_status" -ne 1 ]; then
68-
exit "$diff_status"
69-
fi
70-
echo "should-scan=true" >> "$GITHUB_OUTPUT"
71-
fi
43+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
44+
with:
45+
script: |
46+
if (context.eventName === "workflow_dispatch") {
47+
core.setOutput("should-scan", "true");
48+
return;
49+
}
50+
if (context.payload.pull_request?.draft) {
51+
core.setOutput("should-scan", "false");
52+
return;
53+
}
54+
55+
const baseSha = context.payload.pull_request?.base?.sha;
56+
if (!baseSha) {
57+
throw new Error("missing pull request base SHA");
58+
}
59+
const result = await exec.getExecOutput("git", [
60+
"diff",
61+
"--quiet",
62+
baseSha,
63+
"HEAD",
64+
"--",
65+
"apps/macos/",
66+
".github/workflows/macos-periphery.yml",
67+
".github/workflows/ios-periphery-comment.yml",
68+
"apps/macos/.periphery.yml",
69+
], { ignoreReturnCode: true, silent: true });
70+
if (result.exitCode !== 0 && result.exitCode !== 1) {
71+
throw new Error(`git diff failed with exit code ${result.exitCode}`);
72+
}
73+
core.setOutput("should-scan", String(result.exitCode === 1));
7274
7375
scan:
7476
name: Scan macOS dead code

‎.github/workflows/shared-openclawkit-periphery.yml‎

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,40 +40,42 @@ jobs:
4040

4141
- name: Detect changed paths
4242
id: scope
43-
shell: bash
44-
env:
45-
BASE_SHA: ${{ github.event.pull_request.base.sha }}
46-
EVENT_NAME: ${{ github.event_name }}
47-
PR_DRAFT: ${{ github.event.pull_request.draft && 'true' || 'false' }}
48-
run: |
49-
set -euo pipefail
50-
if [ "$EVENT_NAME" = "workflow_dispatch" ]; then
51-
echo "should-scan=true" >> "$GITHUB_OUTPUT"
52-
exit 0
53-
fi
54-
if [ "$PR_DRAFT" = "true" ]; then
55-
echo "should-scan=false" >> "$GITHUB_OUTPUT"
56-
exit 0
57-
fi
43+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
44+
with:
45+
script: |
46+
if (context.eventName === "workflow_dispatch") {
47+
core.setOutput("should-scan", "true");
48+
return;
49+
}
50+
if (context.payload.pull_request?.draft) {
51+
core.setOutput("should-scan", "false");
52+
return;
53+
}
5854
59-
if git diff --quiet "$BASE_SHA" HEAD -- \
60-
apps/ios/ \
61-
apps/macos/ \
62-
apps/shared/OpenClawKit/ \
63-
.github/workflows/shared-openclawkit-periphery.yml \
64-
scripts/periphery-intersection.mjs \
65-
scripts/ios-configure-signing.sh \
66-
scripts/ios-write-swift-filelist.mjs \
67-
scripts/ios-write-version-xcconfig.sh \
68-
test/scripts/periphery-intersection.test.ts; then
69-
echo "should-scan=false" >> "$GITHUB_OUTPUT"
70-
else
71-
diff_status="$?"
72-
if [ "$diff_status" -ne 1 ]; then
73-
exit "$diff_status"
74-
fi
75-
echo "should-scan=true" >> "$GITHUB_OUTPUT"
76-
fi
55+
const baseSha = context.payload.pull_request?.base?.sha;
56+
if (!baseSha) {
57+
throw new Error("missing pull request base SHA");
58+
}
59+
const result = await exec.getExecOutput("git", [
60+
"diff",
61+
"--quiet",
62+
baseSha,
63+
"HEAD",
64+
"--",
65+
"apps/ios/",
66+
"apps/macos/",
67+
"apps/shared/OpenClawKit/",
68+
".github/workflows/shared-openclawkit-periphery.yml",
69+
"scripts/periphery-intersection.mjs",
70+
"scripts/ios-configure-signing.sh",
71+
"scripts/ios-write-swift-filelist.mjs",
72+
"scripts/ios-write-version-xcconfig.sh",
73+
"test/scripts/periphery-intersection.test.ts",
74+
], { ignoreReturnCode: true, silent: true });
75+
if (result.exitCode !== 0 && result.exitCode !== 1) {
76+
throw new Error(`git diff failed with exit code ${result.exitCode}`);
77+
}
78+
core.setOutput("should-scan", String(result.exitCode === 1));
7779
7880
scan-ios:
7981
name: Scan shared kit from iOS

‎test/scripts/ios-periphery-comment-workflow.test.ts‎

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ function scopeScript(): string {
9797
}
9898

9999
async function runScope(options: {
100+
diffExitCode?: number;
100101
draft?: boolean;
101102
eventName?: string;
102103
files?: Array<string | { filename: string; previous_filename?: string }>;
@@ -111,6 +112,7 @@ async function runScope(options: {
111112
eventName: options.eventName ?? "pull_request",
112113
payload: {
113114
pull_request: {
115+
base: { sha: "base-sha" },
114116
draft: options.draft ?? false,
115117
number: 123,
116118
},
@@ -120,25 +122,35 @@ async function runScope(options: {
120122
repo: "openclaw",
121123
},
122124
};
123-
const github = {
124-
rest: {
125-
pulls: {
126-
listFiles() {},
127-
},
128-
},
129-
async paginate() {
130-
return (options.files ?? []).map((file) =>
131-
typeof file === "string" ? { filename: file } : file,
125+
const exec = {
126+
async getExecOutput(command: string, args: string[]) {
127+
if (command !== "git" || args.slice(0, 5).join(" ") !== "diff --quiet base-sha HEAD --") {
128+
throw new Error(`unexpected scope command: ${command} ${args.join(" ")}`);
129+
}
130+
if (options.diffExitCode !== undefined) {
131+
return { exitCode: options.diffExitCode };
132+
}
133+
const pathspecs = args.slice(5);
134+
const filenames = (options.files ?? []).flatMap((file) =>
135+
typeof file === "string"
136+
? [file]
137+
: [file.filename, file.previous_filename].filter((name): name is string => Boolean(name)),
132138
);
139+
const changed = filenames.some((filename) =>
140+
pathspecs.some((pathspec) =>
141+
pathspec.endsWith("/") ? filename.startsWith(pathspec) : filename === pathspec,
142+
),
143+
);
144+
return { exitCode: changed ? 1 : 0 };
133145
},
134146
};
135147
const execute = compileFunction(`return (async () => {\n${scopeScript()}\n})();`, [
136148
"context",
137149
"core",
138-
"github",
139-
]) as (context: unknown, core: unknown, github: unknown) => Promise<void>;
150+
"exec",
151+
]) as (context: unknown, core: unknown, exec: unknown) => Promise<void>;
140152

141-
await execute(context, core, github);
153+
await execute(context, core, exec);
142154
return outputs.get("should-scan");
143155
}
144156

@@ -465,9 +477,11 @@ describe("iOS Periphery comment workflow", () => {
465477
compileFunction(`return (async () => {\n${scopeScript()}\n})();`, [
466478
"context",
467479
"core",
468-
"github",
480+
"exec",
469481
]),
470482
).not.toThrow();
483+
expect(scopeScript()).toContain("exec.getExecOutput");
484+
expect(scopeScript()).not.toContain("pulls.listFiles");
471485
});
472486

473487
it.each([
@@ -510,6 +524,12 @@ describe("iOS Periphery comment workflow", () => {
510524
await expect(runScope({ ...options, files })).resolves.toBe(expected);
511525
});
512526

527+
it("fails closed when git cannot compare the base and head", async () => {
528+
await expect(runScope({ diffExitCode: 128 })).rejects.toThrow(
529+
"git diff failed with exit code 128",
530+
);
531+
});
532+
513533
it("accepts a valid small Periphery artifact", async () => {
514534
const archive = makeZip({
515535
"periphery.json": "[]\n",

‎test/scripts/periphery-intersection.test.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ describe("shared OpenClawKit Periphery workflow", () => {
129129
const execute = compileFunction(`return (async () => {\n${script}\n})();`, [
130130
"context",
131131
"core",
132-
"github",
133-
]) as (context: unknown, core: unknown, github: unknown) => Promise<void>;
132+
"exec",
133+
]) as (context: unknown, core: unknown, exec: unknown) => Promise<void>;
134134

135135
for (const filename of [
136136
"apps/ios/Sources/App.swift",
@@ -142,13 +142,20 @@ describe("shared OpenClawKit Periphery workflow", () => {
142142
await execute(
143143
{
144144
eventName: "pull_request",
145-
payload: { pull_request: { draft: false, number: 1 } },
145+
payload: { pull_request: { base: { sha: "base-sha" }, draft: false, number: 1 } },
146146
repo: {},
147147
},
148148
{ setOutput: (name: string, value: string) => outputs.set(name, value) },
149149
{
150-
paginate: async () => [{ filename }],
151-
rest: { pulls: { listFiles() {} } },
150+
async getExecOutput(command: string, args: string[]) {
151+
expect(command).toBe("git");
152+
expect(args.slice(0, 5)).toEqual(["diff", "--quiet", "base-sha", "HEAD", "--"]);
153+
const pathspecs = args.slice(5);
154+
const changed = pathspecs.some((pathspec) =>
155+
pathspec.endsWith("/") ? filename.startsWith(pathspec) : filename === pathspec,
156+
);
157+
return { exitCode: changed ? 1 : 0 };
158+
},
152159
},
153160
);
154161
expect(outputs.get("should-scan"), filename).toBe("true");

0 commit comments

Comments
 (0)