|
1 | 1 | import assert from "node:assert/strict"; |
2 | 2 | import test from "node:test"; |
3 | 3 |
|
4 | | -import { githubLimitedPagePath, githubPaginatedPath } from "../../dist/repair/github-cli.js"; |
| 4 | +import { |
| 5 | + ghJson, |
| 6 | + ghJsonWithRetryAsync, |
| 7 | + ghSpawn, |
| 8 | + githubLimitedPagePath, |
| 9 | + githubPaginatedPath, |
| 10 | +} from "../../dist/repair/github-cli.js"; |
5 | 11 |
|
6 | 12 | test("githubPaginatedPath requests maximum REST page size by default", () => { |
7 | 13 | assert.equal( |
@@ -36,3 +42,92 @@ test("githubLimitedPagePath caps one REST page and preserves existing filters", |
36 | 42 | "repos/openclaw/openclaw/pulls/123/files?per_page=1&page=1", |
37 | 43 | ); |
38 | 44 | }); |
| 45 | + |
| 46 | +test("public target reads preserve GitHub App mutations and every explicit credential", async () => { |
| 47 | + const fixtureEnv = { |
| 48 | + GH_BIN: process.execPath, |
| 49 | + GH_BIN_ARGS: JSON.stringify([ |
| 50 | + "--eval", |
| 51 | + "process.stdout.write(JSON.stringify({ token: process.env.GH_TOKEN, args: process.argv.slice(1) }))", |
| 52 | + "--", |
| 53 | + ]), |
| 54 | + GH_TOKEN: "app-mutation-token", |
| 55 | + CLAWSWEEPER_PUBLIC_GH_TOKEN: "public-read-token", |
| 56 | + }; |
| 57 | + const previous = Object.fromEntries( |
| 58 | + Object.keys(fixtureEnv).map((key) => [key, process.env[key]]), |
| 59 | + ); |
| 60 | + Object.assign(process.env, fixtureEnv); |
| 61 | + |
| 62 | + const observed = (args: string[], options?: { env?: NodeJS.ProcessEnv; input?: string }) => |
| 63 | + ghJson<{ token: string; args: string[] }>(args, options); |
| 64 | + |
| 65 | + try { |
| 66 | + for (const args of [ |
| 67 | + ["api", "repos/openclaw/openclaw/issues/comments/123"], |
| 68 | + ["api", "repos/openclaw/openclaw/issues/123"], |
| 69 | + ["api", "repos/openclaw/openclaw/issues?state=open&per_page=100"], |
| 70 | + ["api", "repos/openclaw/openclaw/issues/123/comments?per_page=100", "--paginate", "--slurp"], |
| 71 | + ["api", "repos/openclaw/openclaw/pulls/123/reviews?per_page=100"], |
| 72 | + ["api", "repos/openclaw/openclaw/pulls/123", "--jq", ".requested_reviewers"], |
| 73 | + ]) { |
| 74 | + assert.equal(observed(args).token, "public-read-token", args.join(" ")); |
| 75 | + } |
| 76 | + |
| 77 | + assert.equal( |
| 78 | + (await ghJsonWithRetryAsync<{ token: string }>(["api", "repos/openclaw/openclaw/issues/123"])) |
| 79 | + .token, |
| 80 | + "public-read-token", |
| 81 | + ); |
| 82 | + |
| 83 | + for (const args of [ |
| 84 | + ["api", "user"], |
| 85 | + ["api", "repos/openclaw/openclaw/collaborators/person/permission"], |
| 86 | + ["api", "repos/openclaw/clawsweeper/issues/123"], |
| 87 | + ["api", "repos/private/secret/issues/123"], |
| 88 | + ["api", "https://api.github.com/repos/openclaw/openclaw/issues/123"], |
| 89 | + ["api", "repos/openclaw/openclaw/issues/../../clawsweeper/issues/123"], |
| 90 | + ["api", "repos/openclaw/openclaw/issues/%2e%2e/clawsweeper"], |
| 91 | + ["api", "repos/openclaw/openclaw/issues%2f123"], |
| 92 | + ["api", "repos/openclaw/openclaw/issues\\123"], |
| 93 | + ["api", "repos/openclaw/openclaw/issues/123", "--method", "GET"], |
| 94 | + ["api", "repos/openclaw/openclaw/issues/123", "--method", "POST"], |
| 95 | + ["api", "repos/openclaw/openclaw/issues/123", "--method=DELETE"], |
| 96 | + ["api", "repos/openclaw/openclaw/issues/123", "-X", "PATCH"], |
| 97 | + ["api", "repos/openclaw/openclaw/issues/123", "-XDELETE"], |
| 98 | + ["api", "repos/openclaw/openclaw/issues/123", "-f", "body=mutated"], |
| 99 | + ["api", "repos/openclaw/openclaw/issues/123", "-F", "body=mutated"], |
| 100 | + ["api", "repos/openclaw/openclaw/issues/123", "--field=body=mutated"], |
| 101 | + ["api", "repos/openclaw/openclaw/issues/123", "--raw-field", "body=mutated"], |
| 102 | + ["api", "repos/openclaw/openclaw/issues/123", "--input", "payload.json"], |
| 103 | + ["api", "repos/openclaw/openclaw/issues/123", "--header", "X-HTTP-Method-Override: DELETE"], |
| 104 | + ["api", "repos/openclaw/openclaw/issues/123", "--hostname", "example.invalid"], |
| 105 | + ["api", "repos/openclaw/openclaw/issues/123", "--jq"], |
| 106 | + ["pr", "view", "123", "--repo", "openclaw/openclaw"], |
| 107 | + ]) { |
| 108 | + assert.equal(observed(args).token, "app-mutation-token", args.join(" ")); |
| 109 | + } |
| 110 | + |
| 111 | + const publicArgs = ["api", "repos/openclaw/openclaw/issues/comments/123"]; |
| 112 | + assert.equal( |
| 113 | + observed(publicArgs, { env: { GH_TOKEN: "explicit-dispatch-token" } }).token, |
| 114 | + "explicit-dispatch-token", |
| 115 | + ); |
| 116 | + assert.equal( |
| 117 | + observed(publicArgs, { env: { GITHUB_TOKEN: "explicit-token" } }).token, |
| 118 | + "app-mutation-token", |
| 119 | + ); |
| 120 | + assert.equal(observed(publicArgs, { input: "request body" }).token, "app-mutation-token"); |
| 121 | + |
| 122 | + const spawned = ghSpawn(publicArgs); |
| 123 | + assert.equal(JSON.parse(spawned.stdout).token, "app-mutation-token"); |
| 124 | + |
| 125 | + delete process.env.CLAWSWEEPER_PUBLIC_GH_TOKEN; |
| 126 | + assert.equal(observed(publicArgs).token, "app-mutation-token"); |
| 127 | + } finally { |
| 128 | + for (const [key, value] of Object.entries(previous)) { |
| 129 | + if (value === undefined) delete process.env[key]; |
| 130 | + else process.env[key] = value; |
| 131 | + } |
| 132 | + } |
| 133 | +}); |
0 commit comments