Skip to content

Commit ca187f5

Browse files
committed
1 parent 5f56199 commit ca187f5

7 files changed

Lines changed: 121 additions & 70 deletions

File tree

‎scripts/lib/plain-gh.mjs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,28 @@ export function execGhJson(args, options = {}, params = {}) {
186186
return JSON.parse(execGhRead(args, { ...options, encoding: "utf8" }, params));
187187
}
188188

189+
/**
190+
* @param {string} repo
191+
* @param {string} sha
192+
* @param {string} event
193+
* @param {number} perPage
194+
* @returns {string[]}
195+
*/
196+
export function workflowRunsApiArgs(repo, sha, event, perPage) {
197+
return [
198+
"api",
199+
"--method",
200+
"GET",
201+
`repos/${repo}/actions/workflows/ci.yml/runs`,
202+
"-f",
203+
`event=${event}`,
204+
"-f",
205+
`head_sha=${sha}`,
206+
"-f",
207+
`per_page=${perPage}`,
208+
];
209+
}
210+
189211
/**
190212
* @overload
191213
* @param {string} endpoint

‎scripts/lib/plain-gh.sh‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,22 @@ plain_gh_search_path() {
7373
printf '%s\n' "$output"
7474
}
7575

76+
bridge_plain_gh_auth() {
77+
if [ -n "${GH_TOKEN:-}" ] || [ -n "${GITHUB_TOKEN:-}" ]; then
78+
return 0
79+
fi
80+
81+
local gh_path
82+
local token
83+
gh_path=$(type -P gh 2>/dev/null) || return 0
84+
if token=$(plain_gh_env "$gh_path" auth token 2>/dev/null) && [ -n "$token" ]; then
85+
export GH_TOKEN="$token"
86+
fi
87+
}
88+
7689
gh_plain() {
7790
local gh_bin
7891
gh_bin=$(resolve_plain_gh_bin) || return 1
92+
bridge_plain_gh_auth
7993
plain_gh_env "$gh_bin" "$@"
8094
}

‎scripts/pr-lib/ci-dispatch.mjs‎

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { spawnSync } from "node:child_process";
44
import { isDirectRunUrl } from "../lib/direct-run.mjs";
5-
import { execGhJson, execGhRead, execPlainGh } from "../lib/plain-gh.mjs";
5+
import { execGhJson, execGhRead, execPlainGh, workflowRunsApiArgs } from "../lib/plain-gh.mjs";
66

77
const SHA_PATTERN = /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/u;
88

@@ -41,23 +41,9 @@ function buildCiDispatchArgs(record) {
4141
}
4242

4343
function listCiRuns(headRefOid) {
44-
return execGhJson(
45-
[
46-
"run",
47-
"list",
48-
"--commit",
49-
headRefOid,
50-
"--workflow",
51-
"ci.yml",
52-
"--event",
53-
"workflow_dispatch",
54-
"--limit",
55-
"20",
56-
"--json",
57-
"databaseId,url,headSha,createdAt,status",
58-
],
59-
{ stdio: ["ignore", "pipe", "pipe"] },
60-
);
44+
return execGhJson(workflowRunsApiArgs("openclaw/openclaw", headRefOid, "workflow_dispatch", 20), {
45+
stdio: ["ignore", "pipe", "pipe"],
46+
}).workflow_runs;
6147
}
6248

6349
function readCurrentPrHeadOid(pr) {
@@ -86,7 +72,7 @@ async function dispatchCiForPr(
8672
} = {},
8773
) {
8874
requirePrRecord(record);
89-
const priorRunIds = new Set(listRuns(record.headRefOid).map((run) => run.databaseId));
75+
const priorRunIds = new Set(listRuns(record.headRefOid).map((run) => run.id));
9076
const headBeforeDispatch = readHeadOid(record.pr);
9177
if (headBeforeDispatch !== record.headRefOid) {
9278
throw new Error(
@@ -98,10 +84,10 @@ async function dispatchCiForPr(
9884
for (let attempt = 1; attempt <= pollAttempts; attempt += 1) {
9985
const run = listRuns(record.headRefOid).find(
10086
(candidate) =>
101-
candidate.headSha === record.headRefOid &&
102-
!priorRunIds.has(candidate.databaseId) &&
103-
typeof candidate.url === "string" &&
104-
candidate.url.length > 0,
87+
candidate.head_sha === record.headRefOid &&
88+
!priorRunIds.has(candidate.id) &&
89+
typeof candidate.html_url === "string" &&
90+
candidate.html_url.length > 0,
10591
);
10692
if (run) {
10793
const headAtObservation = readHeadOid(record.pr);
@@ -168,7 +154,7 @@ async function main(argv = process.argv.slice(2)) {
168154
console.log(
169155
"Observed a new exact-SHA manual run after dispatch; GitHub does not expose a dispatch correlation ID, so concurrent requests cannot be distinguished.",
170156
);
171-
console.log(`observed_run_url=${run.url}`);
157+
console.log(`observed_run_url=${run.html_url}`);
172158
} else {
173159
console.log(
174160
`Requested CI for PR #${record.pr} at unchanged remote head ${record.headRefOid} (${record.headRefName}).`,
@@ -177,7 +163,7 @@ async function main(argv = process.argv.slice(2)) {
177163
"run_url=pending (GitHub accepted the dispatch, but Actions has not indexed it yet)",
178164
);
179165
console.log(
180-
`inspect_with=gh run list --commit ${record.headRefOid} --workflow ci.yml --event workflow_dispatch`,
166+
`inspect_with=gh api --method GET repos/openclaw/openclaw/actions/workflows/ci.yml/runs -f event=workflow_dispatch -f head_sha=${record.headRefOid} -f per_page=20`,
181167
);
182168
}
183169
}

‎scripts/watch-pr-ci.mts‎

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { setTimeout as sleep } from "node:timers/promises";
33
import { parseArgs as parseNodeArgs } from "node:util";
44
import { z } from "zod";
55
import { isDirectRunUrl } from "./lib/direct-run.mjs";
6-
import { execGhJson } from "./lib/plain-gh.mjs";
6+
import { execGhJson, workflowRunsApiArgs } from "./lib/plain-gh.mjs";
77

88
const USAGE =
99
"Usage: node scripts/watch-pr-ci.mjs <pr-number> <head-sha> [--repo owner/repo] [--after run-id] [--attach-timeout 900] [--timeout 3600] [--interval 120] [--completion rollup|ci-run]";
@@ -66,8 +66,11 @@ const RollupResponseSchema = z.object({
6666
repository: z.object({ pullRequest: RollupPageSchema.nullish() }).nullish(),
6767
}),
6868
});
69-
const RunListItemSchema = z.object({ databaseId: z.number(), createdAt: z.string() });
70-
const RunListSchema = validArray(RunListItemSchema).catch([]);
69+
const RunListItemSchema = z.object({ id: z.number(), created_at: z.string() });
70+
const RunListSchema = z
71+
.object({ workflow_runs: validArray(RunListItemSchema) })
72+
.transform((response) => response.workflow_runs)
73+
.catch([]);
7174
const RunStatusSchema = z
7275
.object({ status: optionalString, conclusion: optionalNullable(z.string()) })
7376
.catch({});
@@ -298,24 +301,10 @@ const readPr = (pr: number, repo: string) =>
298301
GH_READ_OPTIONS,
299302
),
300303
);
301-
export const buildFindRunArgs = (repo: string, sha: string) => [
302-
"run",
303-
"list",
304-
"--repo",
305-
repo,
306-
"--commit",
307-
sha,
308-
"--workflow",
309-
"ci.yml",
310-
"--event",
311-
"pull_request",
312-
"--limit",
313-
"1",
314-
"--json",
315-
"createdAt,databaseId",
316-
];
304+
export const buildFindRunArgs = (repo: string, sha: string) =>
305+
workflowRunsApiArgs(repo, sha, "pull_request", 1);
317306
export const selectRunAfter = (runs: RunListItem[], after?: number) =>
318-
runs.find((run) => after === undefined || run.databaseId > after);
307+
runs.find((run) => after === undefined || run.id > after);
319308
const findRun = (repo: string, sha: string, after?: number) =>
320309
selectRunAfter(
321310
RunListSchema.parse(execGhJson(buildFindRunArgs(repo, sha), GH_READ_OPTIONS)),
@@ -492,15 +481,15 @@ async function main(argv = process.argv.slice(2)) {
492481
const candidate = findRun(args.repo, args.headSha, args.after);
493482
if (candidate) {
494483
const classification = classifyRunAttachment(
495-
candidate.databaseId,
496-
readRun(args.repo, candidate.databaseId),
484+
candidate.id,
485+
readRun(args.repo, candidate.id),
497486
args.after,
498487
);
499488
if (classification.attach) {
500489
if (classification.warning) {
501490
console.log(classification.warning);
502491
}
503-
return { runId: candidate.databaseId };
492+
return { runId: candidate.id };
504493
}
505494
}
506495
} catch (error) {

‎test/scripts/plain-gh.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ printf 'CLICOLOR=%s\\n' "\${CLICOLOR-}"
3939
printf 'CLICOLOR_FORCE=%s\\n' "\${CLICOLOR_FORCE-}"
4040
printf 'COLORTERM_SET=%s\\n' "\${COLORTERM+x}"
4141
printf 'OPENCLAW_GH_BIN_SET=%s\\n' "\${OPENCLAW_GH_BIN+x}"
42+
printf 'GH_TOKEN_SET=%s\\n' "\${GH_TOKEN:+1}"
4243
`,
4344
);
4445
chmodSync(ghPath, 0o755);
@@ -188,6 +189,7 @@ describe("plain gh helpers", () => {
188189
CLICOLOR_FORCE: "1",
189190
COLORTERM: "truecolor",
190191
FORCE_COLOR: "3",
192+
GH_TOKEN: "existing-test-token",
191193
},
192194
});
193195

@@ -201,6 +203,46 @@ describe("plain gh helpers", () => {
201203
expect(readFileSync(outputPath, "utf8")).toContain("COLORTERM_SET=");
202204
});
203205

206+
it("bridges PATH-shim credentials to the selected plain gh", () => {
207+
const ghPath = makeFakeGh();
208+
const shimDir = mkdtempSync(path.join(tmpdir(), "plain-gh-auth-shim-"));
209+
tempDirs.push(shimDir);
210+
const shimPath = path.join(shimDir, "gh");
211+
writeFileSync(
212+
shimPath,
213+
`#!/usr/bin/env bash
214+
if [ "$*" = "auth token" ]; then
215+
printf 'bridged-test-token\\n'
216+
exit 0
217+
fi
218+
exit 2
219+
`,
220+
);
221+
chmodSync(shimPath, 0o755);
222+
const script = [
223+
"set -euo pipefail",
224+
"source scripts/lib/plain-gh.sh",
225+
`OPENCLAW_GH_BIN=${JSON.stringify(ghPath)}`,
226+
"export OPENCLAW_GH_BIN",
227+
"gh_plain api rate_limit",
228+
].join("\n");
229+
230+
const result = spawnSync("bash", ["-c", script], {
231+
encoding: "utf8",
232+
env: {
233+
...process.env,
234+
GH_TOKEN: "",
235+
GITHUB_TOKEN: "",
236+
PATH: `${shimDir}${path.delimiter}/usr/bin:/bin`,
237+
},
238+
});
239+
240+
expect(result.status, result.stderr).toBe(0);
241+
expect(result.stdout).toContain("argv=api rate_limit");
242+
expect(result.stdout).toContain("GH_TOKEN_SET=1");
243+
expect(result.stdout).not.toContain("bridged-test-token");
244+
});
245+
204246
it("captures large gh payloads by default", () => {
205247
const ghPath = makeLargeFakeGh();
206248
const bytes = 2 * 1024 * 1024;

‎test/scripts/pr-ci-dispatch.test.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ function createFakeGh() {
2323
set -euo pipefail
2424
printf '%s\\t%s\\n' "$(basename "$0")" "$*" >> "$OPENCLAW_TEST_GH_CALLS"
2525
case "$1 $2" in
26-
"run list")
26+
"api --method")
2727
if [ "\${OPENCLAW_TEST_GH_MODE:-}" = "pending-head-change" ]; then
28-
printf '[]\\n'
28+
printf '{"workflow_runs":[]}\\n'
2929
elif [ -e "$OPENCLAW_TEST_GH_SEEN_RUN_LIST" ]; then
30-
printf '[{"databaseId":99,"url":"https://github.com/openclaw/openclaw/actions/runs/99","headSha":"%s","createdAt":"2026-01-01T00:00:00Z","status":"queued"}]\\n' "$OPENCLAW_TEST_HEAD_SHA"
30+
printf '{"workflow_runs":[{"id":99,"html_url":"https://github.com/openclaw/openclaw/actions/runs/99","head_sha":"%s","created_at":"2026-01-01T00:00:00Z","status":"queued"}]}\\n' "$OPENCLAW_TEST_HEAD_SHA"
3131
else
3232
: > "$OPENCLAW_TEST_GH_SEEN_RUN_LIST"
33-
printf '[]\\n'
33+
printf '{"workflow_runs":[]}\\n'
3434
fi
3535
;;
3636
"pr view")
@@ -128,9 +128,11 @@ describePosix("scripts/pr ci-dispatch", () => {
128128
expect(callLines).toContain(
129129
`real-gh\tworkflow run ci.yml --ref contributor/fix-hosted-gates -f target_ref=${sha} -f release_gate=true -f pull_request_number=12345`,
130130
);
131-
expect(callLines.some((call) => call.startsWith(`gh\trun list --commit ${sha}`))).toBe(true);
131+
expect(callLines).toContain(
132+
`gh\tapi --method GET repos/openclaw/openclaw/actions/workflows/ci.yml/runs -f event=workflow_dispatch -f head_sha=${sha} -f per_page=20`,
133+
);
132134
expect(callLines.some((call) => call.startsWith("gh\tpr view 12345"))).toBe(true);
133-
expect(callLines.some((call) => /^real-gh\t(?:run list|pr view)/u.test(call))).toBe(false);
135+
expect(callLines.some((call) => /^real-gh\t(?:api|pr view)/u.test(call))).toBe(false);
134136
});
135137

136138
it("refuses a fork-local branch name before invoking GitHub", () => {

‎test/scripts/watch-pr-ci.test.ts‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,22 @@ describe("watch-pr-ci", () => {
6868

6969
it("builds a pull-request-only run attachment query", () => {
7070
expect(buildFindRunArgs("openclaw/openclaw", sha)).toEqual([
71-
"run",
72-
"list",
73-
"--repo",
74-
"openclaw/openclaw",
75-
"--commit",
76-
sha,
77-
"--workflow",
78-
"ci.yml",
79-
"--event",
80-
"pull_request",
81-
"--limit",
82-
"1",
83-
"--json",
84-
"createdAt,databaseId",
71+
"api",
72+
"--method",
73+
"GET",
74+
"repos/openclaw/openclaw/actions/workflows/ci.yml/runs",
75+
"-f",
76+
"event=pull_request",
77+
"-f",
78+
`head_sha=${sha}`,
79+
"-f",
80+
"per_page=1",
8581
]);
8682
});
8783

8884
it("filters run ids at and before --after", () => {
89-
const newer = { databaseId: 102, createdAt: "2026-07-23T02:00:00Z" };
90-
const runs = [newer, { databaseId: 101, createdAt: "2026-07-23T01:00:00Z" }];
85+
const newer = { id: 102, created_at: "2026-07-23T02:00:00Z" };
86+
const runs = [newer, { id: 101, created_at: "2026-07-23T01:00:00Z" }];
9187
expect(selectRunAfter(runs, 101)).toBe(newer);
9288
expect(selectRunAfter(runs, 102)).toBeUndefined();
9389
expect(selectRunAfter(runs)).toBe(newer);

0 commit comments

Comments
 (0)