Skip to content

Commit d38fc3e

Browse files
authored
fix(ci): keep Periphery scope checks off shared API quota (#106090)
* fix(ci): avoid API quota in Periphery scope checks * fix(ci): preserve Periphery scope contracts
1 parent 61a9b49 commit d38fc3e

5 files changed

Lines changed: 163 additions & 88 deletions

File tree

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

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ env:
1414

1515
permissions:
1616
contents: read
17-
pull-requests: read
1817

1918
jobs:
2019
scope:
@@ -23,6 +22,22 @@ jobs:
2322
outputs:
2423
should-scan: ${{ steps.scope.outputs.should-scan }}
2524
steps:
25+
- name: Checkout
26+
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false
27+
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
28+
with:
29+
fetch-depth: 1
30+
fetch-tags: false
31+
persist-credentials: false
32+
submodules: false
33+
34+
- name: Ensure base commit
35+
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false
36+
uses: ./.github/actions/ensure-base-commit
37+
with:
38+
base-sha: ${{ github.event.pull_request.base.sha }}
39+
fetch-ref: ${{ github.event.pull_request.base.ref }}
40+
2641
- name: Detect changed paths
2742
id: scope
2843
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
@@ -37,32 +52,33 @@ jobs:
3752
return;
3853
}
3954
40-
const files = await github.paginate(github.rest.pulls.listFiles, {
41-
owner: context.repo.owner,
42-
repo: context.repo.repo,
43-
pull_number: context.payload.pull_request.number,
44-
per_page: 100,
45-
});
46-
const isScanPath = (filename) =>
47-
typeof filename === "string" && (
48-
filename.startsWith("apps/ios/") ||
49-
filename.startsWith("apps/shared/OpenClawKit/Sources/") ||
50-
filename.startsWith("apps/swabble/Sources/SwabbleKit/") ||
51-
filename === ".github/workflows/ios-periphery.yml" ||
52-
filename === ".github/workflows/ios-periphery-comment.yml" ||
53-
filename === "config/swiftformat" ||
54-
filename === "config/swiftlint.yml" ||
55-
filename === "scripts/check-swift-tools.sh" ||
56-
filename === "scripts/format-swift.sh" ||
57-
filename === "scripts/install-swift-tools.sh" ||
58-
filename === "scripts/ios-write-swift-filelist.mjs" ||
59-
filename === "scripts/lint-swift.sh"
60-
);
61-
const shouldScan = files.some(
62-
({ filename, previous_filename: previousFilename }) =>
63-
isScanPath(filename) || isScanPath(previousFilename)
64-
);
65-
core.setOutput("should-scan", String(shouldScan));
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));
6682
6783
scan:
6884
name: Scan iOS dead code

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

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ env:
1414

1515
permissions:
1616
contents: read
17-
pull-requests: read
1817

1918
jobs:
2019
scope:
@@ -23,6 +22,22 @@ jobs:
2322
outputs:
2423
should-scan: ${{ steps.scope.outputs.should-scan }}
2524
steps:
25+
- name: Checkout
26+
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false
27+
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
28+
with:
29+
fetch-depth: 1
30+
fetch-tags: false
31+
persist-credentials: false
32+
submodules: false
33+
34+
- name: Ensure base commit
35+
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false
36+
uses: ./.github/actions/ensure-base-commit
37+
with:
38+
base-sha: ${{ github.event.pull_request.base.sha }}
39+
fetch-ref: ${{ github.event.pull_request.base.ref }}
40+
2641
- name: Detect changed paths
2742
id: scope
2843
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
@@ -37,24 +52,25 @@ jobs:
3752
return;
3853
}
3954
40-
const files = await github.paginate(github.rest.pulls.listFiles, {
41-
owner: context.repo.owner,
42-
repo: context.repo.repo,
43-
pull_number: context.payload.pull_request.number,
44-
per_page: 100,
45-
});
46-
const isScanPath = (filename) =>
47-
typeof filename === "string" && (
48-
filename.startsWith("apps/macos/") ||
49-
filename === ".github/workflows/macos-periphery.yml" ||
50-
filename === ".github/workflows/ios-periphery-comment.yml" ||
51-
filename === "apps/macos/.periphery.yml"
52-
);
53-
const shouldScan = files.some(
54-
({ filename, previous_filename: previousFilename }) =>
55-
isScanPath(filename) || isScanPath(previousFilename)
56-
);
57-
core.setOutput("should-scan", String(shouldScan));
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));
5874
5975
scan:
6076
name: Scan macOS dead code

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

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ env:
1414

1515
permissions:
1616
contents: read
17-
pull-requests: read
1817

1918
jobs:
2019
scope:
@@ -23,6 +22,22 @@ jobs:
2322
outputs:
2423
should-scan: ${{ steps.scope.outputs.should-scan }}
2524
steps:
25+
- name: Checkout
26+
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false
27+
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
28+
with:
29+
fetch-depth: 1
30+
fetch-tags: false
31+
persist-credentials: false
32+
submodules: false
33+
34+
- name: Ensure base commit
35+
if: github.event_name == 'pull_request' && github.event.pull_request.draft == false
36+
uses: ./.github/actions/ensure-base-commit
37+
with:
38+
base-sha: ${{ github.event.pull_request.base.sha }}
39+
fetch-ref: ${{ github.event.pull_request.base.ref }}
40+
2641
- name: Detect changed paths
2742
id: scope
2843
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
@@ -37,29 +52,30 @@ jobs:
3752
return;
3853
}
3954
40-
const files = await github.paginate(github.rest.pulls.listFiles, {
41-
owner: context.repo.owner,
42-
repo: context.repo.repo,
43-
pull_number: context.payload.pull_request.number,
44-
per_page: 100,
45-
});
46-
const isScanPath = (filename) =>
47-
typeof filename === "string" && (
48-
filename.startsWith("apps/ios/") ||
49-
filename.startsWith("apps/macos/") ||
50-
filename.startsWith("apps/shared/OpenClawKit/") ||
51-
filename === ".github/workflows/shared-openclawkit-periphery.yml" ||
52-
filename === "scripts/periphery-intersection.mjs" ||
53-
filename === "scripts/ios-configure-signing.sh" ||
54-
filename === "scripts/ios-write-swift-filelist.mjs" ||
55-
filename === "scripts/ios-write-version-xcconfig.sh" ||
56-
filename === "test/scripts/periphery-intersection.test.ts"
57-
);
58-
const shouldScan = files.some(
59-
({ filename, previous_filename: previousFilename }) =>
60-
isScanPath(filename) || isScanPath(previousFilename)
61-
);
62-
core.setOutput("should-scan", String(shouldScan));
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));
6379
6480
scan-ios:
6581
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)