@cnc-tools/storm 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/dist/build-info.js +1 -1
  2. package/dist/commands/exec/cleanup.d.ts +3 -3
  3. package/dist/commands/exec/cleanup.js +1 -1
  4. package/dist/commands/exec/command.d.ts +771 -1
  5. package/dist/commands/exec/command.js +1 -1
  6. package/dist/commands/exec/live-view.js +3 -2
  7. package/dist/commands/exec/metadata.d.ts +1 -1
  8. package/dist/commands/exec/metadata.js +4 -0
  9. package/dist/commands/exec/mint-failed.d.ts +28 -0
  10. package/dist/commands/exec/outcomes.d.ts +476 -0
  11. package/dist/commands/exec/progress.js +3 -5
  12. package/dist/commands/exec/run-command.d.ts +2 -2
  13. package/dist/commands/exec/run-metadata.d.ts +1 -1
  14. package/dist/commands/exec/run-metadata.js +1 -1
  15. package/dist/commands/exec/tests-summary.d.ts +1 -1
  16. package/dist/commands/exec/tests-summary.js +1 -1
  17. package/dist/commands/runs/list/command.d.ts +129 -1
  18. package/dist/commands/runs/list/command.js +1 -1
  19. package/dist/commands/runs/list/outcomes.d.ts +16 -0
  20. package/dist/commands/runs/logs/command.d.ts +263 -1
  21. package/dist/commands/runs/logs/command.js +1 -1
  22. package/dist/commands/runs/logs/outcomes.d.ts +105 -0
  23. package/dist/commands/runs/show/command.d.ts +329 -1
  24. package/dist/commands/runs/show/command.js +1 -1
  25. package/dist/commands/runs/show/outcomes.d.ts +71 -0
  26. package/dist/commands/vitest/command.d.ts +886 -1
  27. package/dist/commands/vitest/command.js +1 -1
  28. package/dist/commands/vitest/metadata.d.ts +1 -1
  29. package/dist/commands/vitest/metadata.js +4 -0
  30. package/dist/commands/vitest/outcomes.d.ts +295 -0
  31. package/dist/config/errors.d.ts +73 -0
  32. package/dist/config/schema.d.ts +1 -1
  33. package/dist/config-file/define.d.ts +4 -3
  34. package/dist/config-file/define.js +1 -1
  35. package/dist/config-file/toolchain.js +1 -1
  36. package/dist/core/command-slots.d.ts +8 -2
  37. package/dist/core/command-slots.js +6 -2
  38. package/dist/core/index.d.ts +1 -1
  39. package/dist/credential/index.d.ts +1 -0
  40. package/dist/credential/index.js +1 -0
  41. package/dist/credential/settle.d.ts +20 -0
  42. package/dist/credential/target.d.ts +6 -0
  43. package/dist/credential/target.js +7 -0
  44. package/dist/resources/github/command.d.ts +54 -2
  45. package/dist/resources/github/config.js +1 -1
  46. package/dist/resources/github/index.js +1 -1
  47. package/dist/resources/github/resource.js +2 -2
  48. package/dist/resources/testserver/command.d.ts +208 -10
  49. package/dist/resources/testserver/command.js +7 -0
  50. package/dist/resources/testserver/commands/create/command.d.ts +808 -1
  51. package/dist/resources/testserver/commands/create/command.js +64 -23
  52. package/dist/resources/testserver/commands/create/metadata.js +6 -2
  53. package/dist/resources/testserver/commands/create/outcomes.d.ts +347 -0
  54. package/dist/resources/testserver/commands/create/outcomes.js +14 -1
  55. package/dist/resources/testserver/commands/list/command.d.ts +84 -1
  56. package/dist/resources/testserver/commands/list/command.js +1 -1
  57. package/dist/resources/testserver/commands/list/outcomes.d.ts +16 -0
  58. package/dist/resources/testserver/config.js +14 -4
  59. package/dist/resources/testserver/events.d.ts +9 -0
  60. package/dist/resources/testserver/index.js +8 -7
  61. package/dist/resources/testserver/lane.js +1 -0
  62. package/dist/resources/testserver/live/gateways.d.ts +2 -0
  63. package/dist/resources/testserver/live/gateways.js +16 -3
  64. package/dist/resources/testserver/pool/channels.d.ts +5 -2
  65. package/dist/resources/testserver/pool/channels.js +1 -1
  66. package/dist/resources/testserver/pool/create.d.ts +6 -2
  67. package/dist/resources/testserver/pool/create.js +6 -5
  68. package/dist/resources/testserver/pool/index.d.ts +1 -0
  69. package/dist/resources/testserver/pool/index.js +1 -0
  70. package/dist/resources/testserver/pool/pull-request.d.ts +89 -0
  71. package/dist/resources/testserver/pool/pull-request.js +85 -0
  72. package/dist/resources/testserver/pr-build-facts.d.ts +130 -0
  73. package/dist/resources/testserver/pr-build-facts.js +261 -0
  74. package/dist/resources/testserver/presentation.d.ts +2 -2
  75. package/dist/resources/testserver/presentation.js +12 -3
  76. package/dist/resources/testserver/provider.d.ts +16 -6
  77. package/dist/resources/testserver/provider.js +46 -15
  78. package/dist/resources/testserver/pull-request/gateways.d.ts +117 -0
  79. package/dist/resources/testserver/pull-request/gateways.js +5 -0
  80. package/dist/resources/testserver/pull-request/index.d.ts +5 -0
  81. package/dist/resources/testserver/pull-request/index.js +6 -0
  82. package/dist/resources/testserver/pull-request/live/github-rest.d.ts +28 -0
  83. package/dist/resources/testserver/pull-request/live/github-rest.js +114 -0
  84. package/dist/resources/testserver/pull-request/live/github-token-gateway.d.ts +12 -0
  85. package/dist/resources/testserver/pull-request/live/github-token-gateway.js +39 -0
  86. package/dist/resources/testserver/pull-request/live/pull-request-gateway.d.ts +10 -0
  87. package/dist/resources/testserver/pull-request/live/pull-request-gateway.js +27 -0
  88. package/dist/resources/testserver/pull-request/live/testserver-workflow-gateway.d.ts +21 -0
  89. package/dist/resources/testserver/pull-request/live/testserver-workflow-gateway.js +64 -0
  90. package/dist/resources/testserver/ran.d.ts +5 -0
  91. package/dist/resources/testserver/ran.js +19 -3
  92. package/dist/resources/testserver/resource.d.ts +5 -1
  93. package/dist/resources/testserver/resource.js +2 -2
  94. package/dist/resources/testserver/sandbox/build-reference.d.ts +38 -0
  95. package/dist/resources/testserver/sandbox/build-reference.js +79 -0
  96. package/dist/resources/testserver/sandbox/index.d.ts +1 -0
  97. package/dist/resources/testserver/sandbox/index.js +1 -0
  98. package/dist/resources.d.ts +265 -15
  99. package/dist/sdk/index.js +2 -2
  100. package/dist/story/command-line.d.ts +34 -0
  101. package/dist/story/command-line.js +100 -0
  102. package/dist/story/index.d.ts +1 -0
  103. package/dist/story/index.js +3 -2
  104. package/dist/vercel-cli/context.js +1 -1
  105. package/dist/vercel-cli/index.js +1 -1
  106. package/dist/vitest/config.js +2 -2
  107. package/dist/vitest/fixtures.d.ts +1 -1
  108. package/dist/vitest/fixtures.js +1 -1
  109. package/dist/vitest/index.js +3 -3
  110. package/dist/vitest/plan.js +1 -1
  111. package/package.json +3 -3
  112. package/skills/overview.md +2 -0
  113. package/skills/testserver-builds.md +80 -0
@@ -0,0 +1,117 @@
1
+ /** What GitHub says of one pull request: its head commit and branch, whether it is open, and whether it merged. */
2
+ export interface PullRequestHead {
3
+ /** The head commit, 40 lowercase hex characters. */
4
+ readonly headSha: string;
5
+ /** The pull request's branch, such as `schrockn/testserver-registrar-docs`. */
6
+ readonly headRef: string;
7
+ readonly state: "open" | "closed";
8
+ readonly merged: boolean;
9
+ }
10
+ /**
11
+ * Why a GitHub REST call failed. No message holds the token.
12
+ *
13
+ * - `not-found`: GitHub answered 404: there is no such pull request or run, or the token can't read the repository.
14
+ * - `rejected`: GitHub answered 401 or 403; `message` is what GitHub said.
15
+ * - `http-error`: any other status that isn't 2xx; `message` is what GitHub said, or the status.
16
+ * - `connect-failed`: no answer came: `code` is the network error's code, such as `ENOTFOUND`, or its message.
17
+ * - `timeout`: no answer within `seconds`.
18
+ * - `malformed`: the answer wasn't what was asked for.
19
+ */
20
+ export type GitHubFailure = {
21
+ readonly kind: "not-found";
22
+ readonly status: 404;
23
+ } | {
24
+ readonly kind: "rejected";
25
+ readonly status: 401 | 403;
26
+ readonly message: string;
27
+ } | {
28
+ readonly kind: "http-error";
29
+ readonly status: number;
30
+ readonly message: string;
31
+ } | {
32
+ readonly kind: "connect-failed";
33
+ readonly code: string;
34
+ } | {
35
+ readonly kind: "timeout";
36
+ readonly seconds: number;
37
+ } | {
38
+ readonly kind: "malformed";
39
+ readonly detail: string;
40
+ };
41
+ export type PullRequestHeadOutcome = {
42
+ readonly ok: true;
43
+ readonly head: PullRequestHead;
44
+ } | {
45
+ readonly ok: false;
46
+ readonly failure: GitHubFailure;
47
+ };
48
+ /** GitHub's pull requests, read with a GitHub token. No method rejects. */
49
+ export interface PullRequestGateway {
50
+ /** `GET repos/{repo}/pulls/{number}`: pull request `number`'s head commit, branch and state. */
51
+ head(request: {
52
+ readonly repo: string;
53
+ readonly number: number;
54
+ readonly token: string;
55
+ }): Promise<PullRequestHeadOutcome>;
56
+ }
57
+ /** One run of vercel/api's `Testserver` workflow for a pull request push. */
58
+ export interface TestserverRun {
59
+ /** The run's id on GitHub Actions. */
60
+ readonly id: number;
61
+ /** The pull request head commit the run was for, 40 lowercase hex characters. */
62
+ readonly headSha: string;
63
+ /** When the run started, as GitHub writes it. */
64
+ readonly createdAt: string;
65
+ }
66
+ /** The PR build a run's `Bundle & Publish` job published: its pull request number (`builds/pr-<pr>/`) and name. */
67
+ export interface PublishedBuild {
68
+ readonly pr: number;
69
+ readonly name: string;
70
+ }
71
+ export type TestserverRunsOutcome = {
72
+ readonly ok: true;
73
+ readonly runs: readonly TestserverRun[];
74
+ } | {
75
+ readonly ok: false;
76
+ readonly failure: GitHubFailure;
77
+ };
78
+ export type PublishedBuildOutcome = {
79
+ readonly ok: true;
80
+ readonly build: PublishedBuild | undefined;
81
+ } | {
82
+ readonly ok: false;
83
+ readonly failure: GitHubFailure;
84
+ };
85
+ /**
86
+ * vercel/api's `Testserver` workflow on GitHub Actions, read with a GitHub token: its runs for pushes to a pull request,
87
+ * and the PR build each one published. No method rejects.
88
+ */
89
+ export interface TestserverWorkflowGateway {
90
+ /** The workflow's newest `pull_request` runs on branch `branch` of `repo`, newest first. */
91
+ runs(request: {
92
+ readonly repo: string;
93
+ readonly branch: string;
94
+ readonly token: string;
95
+ }): Promise<TestserverRunsOutcome>;
96
+ /**
97
+ * The PR build run `runId`'s `Bundle & Publish` job published; undefined when the job hasn't succeeded, or its log
98
+ * names no PR build.
99
+ */
100
+ publishedBuild(request: {
101
+ readonly repo: string;
102
+ readonly runId: number;
103
+ readonly token: string;
104
+ }): Promise<PublishedBuildOutcome>;
105
+ }
106
+ /** Where a GitHub token came from. */
107
+ export type GitHubTokenOrigin = "GITHUB_TOKEN" | "GH_TOKEN" | "gh auth token";
108
+ /** A GitHub token, and where it came from. */
109
+ export interface GitHubToken {
110
+ readonly token: string;
111
+ readonly origin: GitHubTokenOrigin;
112
+ }
113
+ /** Finds a GitHub token on the host. It never rejects. */
114
+ export interface GitHubTokenGateway {
115
+ /** The first of `GITHUB_TOKEN`, `GH_TOKEN`, then `gh auth token`'s output; undefined when none gives one. */
116
+ token(): Promise<GitHubToken | undefined>;
117
+ }
@@ -0,0 +1,5 @@
1
+ // What the Testserver pool resource asks GitHub on the host about a vercel/api pull request: its head commit, the runs of
2
+ // vercel/api's `Testserver` workflow on it and the PR build each run published, and a GitHub token to ask with. The
3
+ // token never leaves the host: storm uses it only for these requests, and no run payload, runner sandbox, command
4
+ // environment, progress line, outcome or run directory holds it.
5
+ export {};
@@ -0,0 +1,5 @@
1
+ export type { GitHubFailure, GitHubToken, GitHubTokenGateway, GitHubTokenOrigin, PublishedBuild, PublishedBuildOutcome, PullRequestGateway, PullRequestHead, PullRequestHeadOutcome, TestserverRun, TestserverRunsOutcome, TestserverWorkflowGateway, } from "./gateways.ts";
2
+ export { type GitHubRestOptions } from "./live/github-rest.ts";
3
+ export { GH_TOKEN_TIMEOUT_MS, liveGitHubTokenGateway } from "./live/github-token-gateway.ts";
4
+ export { livePullRequestGateway } from "./live/pull-request-gateway.ts";
5
+ export { BUNDLE_JOB, liveTestserverWorkflowGateway, type LiveTestserverWorkflowOptions, TESTSERVER_WORKFLOW, } from "./live/testserver-workflow-gateway.ts";
@@ -0,0 +1,6 @@
1
+ // The Testserver folder's `pull-request/` barrel, for the other files in `resources/testserver/`. Code outside that
2
+ // folder imports `resources/testserver/index.ts` instead.
3
+ export {} from "./live/github-rest.js";
4
+ export { GH_TOKEN_TIMEOUT_MS, liveGitHubTokenGateway } from "./live/github-token-gateway.js";
5
+ export { livePullRequestGateway } from "./live/pull-request-gateway.js";
6
+ export { BUNDLE_JOB, liveTestserverWorkflowGateway, TESTSERVER_WORKFLOW, } from "./live/testserver-workflow-gateway.js";
@@ -0,0 +1,28 @@
1
+ import { z } from "zod";
2
+ import type { GitHubFailure } from "../gateways.ts";
3
+ export interface GitHubRestOptions {
4
+ /** GitHub's REST API origin, `https://api.github.com`, with no trailing slash. */
5
+ readonly origin: string;
6
+ /** How long one call, its body included, may take. */
7
+ readonly timeoutMs: number;
8
+ }
9
+ /** A GitHub REST answer: a 2xx's body, or the failure. */
10
+ export type GitHubAnswer<T> = {
11
+ readonly ok: true;
12
+ readonly value: T;
13
+ } | {
14
+ readonly ok: false;
15
+ readonly failure: GitHubFailure;
16
+ };
17
+ /** `text` with `token` replaced by `<token>`. */
18
+ export declare function hideToken(text: string, token: string): string;
19
+ /**
20
+ * `GET {origin}/{path}` with the token as a bearer token, no redirect followed and a timeout for the whole call: a 2xx
21
+ * answer's body parsed by `schema`, or the failure, with the token replaced by `<token>` in every message.
22
+ */
23
+ export declare function getJson<T>(options: GitHubRestOptions, token: string, path: string, schema: z.ZodType<T>): Promise<GitHubAnswer<T>>;
24
+ /**
25
+ * `GET {origin}/{path}`, an endpoint that answers with a redirect to the text it names, such as a job's log: the text,
26
+ * fetched from the redirect's location with no token, since that URL is signed. A 2xx answer's own body is the text.
27
+ */
28
+ export declare function getRedirectedText(options: GitHubRestOptions, token: string, path: string): Promise<GitHubAnswer<string>>;
@@ -0,0 +1,114 @@
1
+ import { z } from "zod";
2
+ const errorSchema = z.object({ message: z.string() });
3
+ /** GitHub's REST headers for a call with `token`. */
4
+ function headers(token) {
5
+ return {
6
+ accept: "application/vnd.github+json",
7
+ authorization: `Bearer ${token}`,
8
+ "x-github-api-version": "2022-11-28",
9
+ "user-agent": "storm",
10
+ };
11
+ }
12
+ /** `text` with `token` replaced by `<token>`. */
13
+ export function hideToken(text, token) {
14
+ return token === "" ? text : text.replaceAll(token, "<token>");
15
+ }
16
+ /**
17
+ * `GET {origin}/{path}` with the token as a bearer token, no redirect followed and a timeout for the whole call: a 2xx
18
+ * answer's body parsed by `schema`, or the failure, with the token replaced by `<token>` in every message.
19
+ */
20
+ export async function getJson(options, token, path, schema) {
21
+ const signal = AbortSignal.timeout(options.timeoutMs);
22
+ try {
23
+ const response = await fetch(`${options.origin}/${path}`, { headers: headers(token), redirect: "manual", signal });
24
+ const text = await response.text();
25
+ if (response.status < 200 || response.status >= 300)
26
+ return {
27
+ ok: false,
28
+ failure: statusFailure(response.status, hideToken(githubMessage(text, response.status), token)),
29
+ };
30
+ let json;
31
+ try {
32
+ json = JSON.parse(text);
33
+ }
34
+ catch {
35
+ return { ok: false, failure: { kind: "malformed", detail: "the answer isn't JSON" } };
36
+ }
37
+ const parsed = schema.safeParse(json);
38
+ if (!parsed.success) {
39
+ const issue = parsed.error.issues[0];
40
+ const where = issue === undefined || issue.path.length === 0 ? "the answer" : issue.path.join(".");
41
+ return {
42
+ ok: false,
43
+ failure: { kind: "malformed", detail: hideToken(`${where}: ${issue?.message ?? "unexpected"}`, token) },
44
+ };
45
+ }
46
+ return { ok: true, value: parsed.data };
47
+ }
48
+ catch (error) {
49
+ return { ok: false, failure: thrownFailure(error, signal, options, token) };
50
+ }
51
+ }
52
+ /**
53
+ * `GET {origin}/{path}`, an endpoint that answers with a redirect to the text it names, such as a job's log: the text,
54
+ * fetched from the redirect's location with no token, since that URL is signed. A 2xx answer's own body is the text.
55
+ */
56
+ export async function getRedirectedText(options, token, path) {
57
+ const signal = AbortSignal.timeout(options.timeoutMs);
58
+ try {
59
+ const response = await fetch(`${options.origin}/${path}`, { headers: headers(token), redirect: "manual", signal });
60
+ if (response.status >= 200 && response.status < 300)
61
+ return { ok: true, value: await response.text() };
62
+ const location = response.headers.get("location");
63
+ if (response.status < 300 || response.status >= 400 || location === null) {
64
+ const text = await response.text();
65
+ return {
66
+ ok: false,
67
+ failure: statusFailure(response.status, hideToken(githubMessage(text, response.status), token)),
68
+ };
69
+ }
70
+ await response.body?.cancel().catch(() => { });
71
+ const redirected = await fetch(location, { redirect: "follow", signal });
72
+ const text = await redirected.text();
73
+ if (!redirected.ok)
74
+ return { ok: false, failure: statusFailure(redirected.status, `HTTP ${redirected.status}`) };
75
+ return { ok: true, value: text };
76
+ }
77
+ catch (error) {
78
+ return { ok: false, failure: thrownFailure(error, signal, options, token) };
79
+ }
80
+ }
81
+ /** The failure a status that isn't 2xx means. */
82
+ function statusFailure(status, message) {
83
+ if (status === 404)
84
+ return { kind: "not-found", status };
85
+ if (status === 401 || status === 403)
86
+ return { kind: "rejected", status, message };
87
+ return { kind: "http-error", status, message };
88
+ }
89
+ /** What GitHub said in an error answer: its `message`, or the status when the body has none. */
90
+ function githubMessage(text, status) {
91
+ try {
92
+ const parsed = errorSchema.safeParse(JSON.parse(text));
93
+ if (parsed.success)
94
+ return parsed.data.message;
95
+ }
96
+ catch {
97
+ // Not JSON: the status says enough.
98
+ }
99
+ return `HTTP ${status}`;
100
+ }
101
+ /** What a rejected `fetch` means: `timeout` when its signal aborted it, else `connect-failed` with its code. */
102
+ function thrownFailure(error, signal, options, token) {
103
+ if (signal.aborted)
104
+ return { kind: "timeout", seconds: options.timeoutMs / 1000 };
105
+ return { kind: "connect-failed", code: hideToken(errorCode(error), token) };
106
+ }
107
+ /** A rejected `fetch`'s network error code, such as `ENOTFOUND`, or its message when it has none. */
108
+ function errorCode(error) {
109
+ if (!(error instanceof Error))
110
+ return String(error);
111
+ const cause = error.cause;
112
+ const code = typeof cause === "object" && cause !== null ? cause.code : undefined;
113
+ return typeof code === "string" ? code : error.message;
114
+ }
@@ -0,0 +1,12 @@
1
+ import type { GitHubTokenGateway } from "../gateways.ts";
2
+ /** How long `gh auth token` may run before it is killed. */
3
+ export declare const GH_TOKEN_TIMEOUT_MS = 5000;
4
+ /**
5
+ * Finds a GitHub token on the host: `GITHUB_TOKEN` in `env`, then `GH_TOKEN`, each only when it isn't blank, then
6
+ * the output of `gh auth token --hostname github.com`, run with no shell, `env` as its environment and a 5-second
7
+ * timeout, when it exits 0 with output. A missing `gh`, a timeout or another exit gives no token. It never rejects,
8
+ * and it logs nothing.
9
+ */
10
+ export declare function liveGitHubTokenGateway(options: {
11
+ readonly env: Readonly<Record<string, string | undefined>>;
12
+ }): GitHubTokenGateway;
@@ -0,0 +1,39 @@
1
+ import { execFile } from "node:child_process";
2
+ /** How long `gh auth token` may run before it is killed. */
3
+ export const GH_TOKEN_TIMEOUT_MS = 5_000;
4
+ /**
5
+ * Finds a GitHub token on the host: `GITHUB_TOKEN` in `env`, then `GH_TOKEN`, each only when it isn't blank, then
6
+ * the output of `gh auth token --hostname github.com`, run with no shell, `env` as its environment and a 5-second
7
+ * timeout, when it exits 0 with output. A missing `gh`, a timeout or another exit gives no token. It never rejects,
8
+ * and it logs nothing.
9
+ */
10
+ export function liveGitHubTokenGateway(options) {
11
+ return {
12
+ token: async () => {
13
+ for (const origin of ["GITHUB_TOKEN", "GH_TOKEN"]) {
14
+ const token = options.env[origin]?.trim();
15
+ if (token !== undefined && token !== "")
16
+ return { token, origin };
17
+ }
18
+ const token = await ghAuthToken(options.env);
19
+ return token === undefined ? undefined : { token, origin: "gh auth token" };
20
+ },
21
+ };
22
+ }
23
+ /**
24
+ * `gh auth token`'s output, trimmed, or undefined when it fails or prints nothing. It uses `execFile`'s callback
25
+ * form, as `vercel` runs do.
26
+ */
27
+ function ghAuthToken(env) {
28
+ return new Promise((resolve) => {
29
+ try {
30
+ execFile("gh", ["auth", "token", "--hostname", "github.com"], { env: { ...env }, timeout: GH_TOKEN_TIMEOUT_MS, encoding: "utf8" }, (error, stdout) => {
31
+ const token = typeof stdout === "string" ? stdout.trim() : "";
32
+ resolve(error === null && token !== "" ? token : undefined);
33
+ });
34
+ }
35
+ catch {
36
+ resolve(undefined);
37
+ }
38
+ });
39
+ }
@@ -0,0 +1,10 @@
1
+ import type { PullRequestGateway } from "../gateways.ts";
2
+ import { type GitHubRestOptions } from "./github-rest.ts";
3
+ /**
4
+ * GitHub's REST API over `fetch`: `GET repos/{repo}/pulls/{number}` with the token as a bearer token, no redirect
5
+ * followed and a timeout for the whole call. A failure says what GitHub said (its `message`) or what the connection
6
+ * did, with the token replaced by `<token>`. It never rejects.
7
+ */
8
+ export declare function livePullRequestGateway(options: GitHubRestOptions): PullRequestGateway;
9
+ /** `owner/name` with each part encoded. */
10
+ export declare function repoPath(repo: string): string;
@@ -0,0 +1,27 @@
1
+ import { z } from "zod";
2
+ import { getJson } from "./github-rest.js";
3
+ const pullSchema = z.object({
4
+ head: z.object({ sha: z.string().regex(/^[0-9a-f]{40}$/), ref: z.string().min(1) }),
5
+ state: z.enum(["open", "closed"]),
6
+ merged: z.boolean(),
7
+ });
8
+ /**
9
+ * GitHub's REST API over `fetch`: `GET repos/{repo}/pulls/{number}` with the token as a bearer token, no redirect
10
+ * followed and a timeout for the whole call. A failure says what GitHub said (its `message`) or what the connection
11
+ * did, with the token replaced by `<token>`. It never rejects.
12
+ */
13
+ export function livePullRequestGateway(options) {
14
+ return {
15
+ head: async ({ repo, number, token }) => {
16
+ const answered = await getJson(options, token, `repos/${repoPath(repo)}/pulls/${number}`, pullSchema);
17
+ if (!answered.ok)
18
+ return answered;
19
+ const { head, state, merged } = answered.value;
20
+ return { ok: true, head: { headSha: head.sha, headRef: head.ref, state, merged } };
21
+ },
22
+ };
23
+ }
24
+ /** `owner/name` with each part encoded. */
25
+ export function repoPath(repo) {
26
+ return repo.split("/").map(encodeURIComponent).join("/");
27
+ }
@@ -0,0 +1,21 @@
1
+ import type { TestserverWorkflowGateway } from "../gateways.ts";
2
+ import { type GitHubRestOptions } from "./github-rest.ts";
3
+ /** vercel/api's `Testserver` workflow file, whose `Bundle & Publish` job publishes a PR build for each push. */
4
+ export declare const TESTSERVER_WORKFLOW = "testserver-bundle-publish.yml";
5
+ /** The job of the `Testserver` workflow that bundles Testserver and publishes the build. */
6
+ export declare const BUNDLE_JOB = "Bundle & Publish";
7
+ export interface LiveTestserverWorkflowOptions extends GitHubRestOptions {
8
+ /** How long one job log's download may take. */
9
+ readonly logTimeoutMs: number;
10
+ }
11
+ /**
12
+ * vercel/api's `Testserver` workflow on GitHub's REST API over `fetch`, with the token as a bearer token:
13
+ *
14
+ * - `runs`: `GET repos/{repo}/actions/workflows/testserver-bundle-publish.yml/runs?event=pull_request&branch=…`.
15
+ * - `publishedBuild`: `GET repos/{repo}/actions/runs/{id}/jobs` for its latest attempt's `Bundle & Publish` job, and,
16
+ * when that job succeeded, `GET repos/{repo}/actions/jobs/{id}/logs`, whose redirect it follows with no token, for
17
+ * the line that names the build.
18
+ *
19
+ * A failure says what GitHub said or what the connection did, with the token replaced by `<token>`. It never rejects.
20
+ */
21
+ export declare function liveTestserverWorkflowGateway(options: LiveTestserverWorkflowOptions): TestserverWorkflowGateway;
@@ -0,0 +1,64 @@
1
+ import { z } from "zod";
2
+ import { getJson, getRedirectedText } from "./github-rest.js";
3
+ import { repoPath } from "./pull-request-gateway.js";
4
+ /** vercel/api's `Testserver` workflow file, whose `Bundle & Publish` job publishes a PR build for each push. */
5
+ export const TESTSERVER_WORKFLOW = "testserver-bundle-publish.yml";
6
+ /** The job of the `Testserver` workflow that bundles Testserver and publishes the build. */
7
+ export const BUNDLE_JOB = "Bundle & Publish";
8
+ /** How many of a branch's newest runs `runs` lists. */
9
+ const RUNS_LISTED = 30;
10
+ /**
11
+ * The line the `Bundle & Publish` job's log names its build with: `📦 Bundle will be: builds/pr-104196/<name>`. The
12
+ * job also echoes the command that prints it, whose `${FOLDER}/${NAME}` doesn't match.
13
+ */
14
+ const BUNDLE_LINE = /📦 Bundle will be: builds\/pr-([1-9][0-9]*)\/([^\s/]+)/;
15
+ const runsSchema = z.object({
16
+ workflow_runs: z.array(z.object({
17
+ id: z.number(),
18
+ head_sha: z.string().regex(/^[0-9a-f]{40}$/),
19
+ created_at: z.string().min(1),
20
+ })),
21
+ });
22
+ const jobsSchema = z.object({
23
+ jobs: z.array(z.object({ id: z.number(), name: z.string(), conclusion: z.string().nullable() })),
24
+ });
25
+ /**
26
+ * vercel/api's `Testserver` workflow on GitHub's REST API over `fetch`, with the token as a bearer token:
27
+ *
28
+ * - `runs`: `GET repos/{repo}/actions/workflows/testserver-bundle-publish.yml/runs?event=pull_request&branch=…`.
29
+ * - `publishedBuild`: `GET repos/{repo}/actions/runs/{id}/jobs` for its latest attempt's `Bundle & Publish` job, and,
30
+ * when that job succeeded, `GET repos/{repo}/actions/jobs/{id}/logs`, whose redirect it follows with no token, for
31
+ * the line that names the build.
32
+ *
33
+ * A failure says what GitHub said or what the connection did, with the token replaced by `<token>`. It never rejects.
34
+ */
35
+ export function liveTestserverWorkflowGateway(options) {
36
+ return {
37
+ runs: async ({ repo, branch, token }) => {
38
+ const query = new URLSearchParams({ event: "pull_request", branch, per_page: String(RUNS_LISTED) });
39
+ const answered = await getJson(options, token, `repos/${repoPath(repo)}/actions/workflows/${TESTSERVER_WORKFLOW}/runs?${query.toString()}`, runsSchema);
40
+ if (!answered.ok)
41
+ return answered;
42
+ const runs = answered.value.workflow_runs.map((run) => ({
43
+ id: run.id,
44
+ headSha: run.head_sha,
45
+ createdAt: run.created_at,
46
+ }));
47
+ return { ok: true, runs };
48
+ },
49
+ publishedBuild: async ({ repo, runId, token }) => {
50
+ const jobs = await getJson(options, token, `repos/${repoPath(repo)}/actions/runs/${runId}/jobs`, jobsSchema);
51
+ if (!jobs.ok)
52
+ return jobs;
53
+ const job = jobs.value.jobs.find(({ name }) => name === BUNDLE_JOB);
54
+ if (job === undefined || job.conclusion !== "success")
55
+ return { ok: true, build: undefined };
56
+ const log = await getRedirectedText({ origin: options.origin, timeoutMs: options.logTimeoutMs }, token, `repos/${repoPath(repo)}/actions/jobs/${job.id}/logs`);
57
+ if (!log.ok)
58
+ return log;
59
+ const match = BUNDLE_LINE.exec(log.value);
60
+ const [, pr, name] = match ?? [];
61
+ return { ok: true, build: pr === undefined || name === undefined ? undefined : { pr: Number(pr), name } };
62
+ },
63
+ };
64
+ }
@@ -36,6 +36,11 @@ declare const schema: z.ZodObject<{
36
36
  build: z.ZodObject<{
37
37
  requested: z.ZodString;
38
38
  resolved: z.ZodNullable<z.ZodString>;
39
+ pullRequest: z.ZodNullable<z.ZodObject<{
40
+ repo: z.ZodString;
41
+ number: z.ZodNumber;
42
+ headSha: z.ZodString;
43
+ }, z.core.$strip>>;
39
44
  }, z.core.$strip>;
40
45
  sandboxes: z.ZodArray<z.ZodObject<{
41
46
  testserverUrl: z.ZodString;
@@ -3,7 +3,7 @@
3
3
  // how outcomes report a failed create and an unpooled Testserver sandbox, which its errors (`command.ts`) report too.
4
4
  import { z } from "zod";
5
5
  import { describeCredentialFailure } from "../../credential/index.js";
6
- import { createFailureText, describeCreateFailure } from "./sandbox/index.js";
6
+ import { createFailureText, describeCreateFailure, TESTSERVER_REPO } from "./sandbox/index.js";
7
7
  const createFailureKinds = [
8
8
  "connect-failed",
9
9
  "http-error",
@@ -35,10 +35,20 @@ export function failedCreateFacts(failures) {
35
35
  const schema = z.object({
36
36
  /** What the build setting asked for and the build it resolved to. */
37
37
  build: z.object({
38
- /** The build or build channel the build setting gave. */
38
+ /** The build, build channel or PR build reference (pr:<N>) the build setting gave. */
39
39
  requested: z.string(),
40
40
  /** The build it resolved to; null when a build channel couldn't be resolved and was matched as written. */
41
41
  resolved: z.string().nullable(),
42
+ /** For a PR build reference, the pull request and the head commit the resolved PR build was made from; null otherwise. */
43
+ pullRequest: z
44
+ .object({
45
+ /** The repository, vercel/api. */
46
+ repo: z.string(),
47
+ number: z.number(),
48
+ /** The pull request's head commit when the run started, 40 hex characters. */
49
+ headSha: z.string(),
50
+ })
51
+ .nullable(),
42
52
  }),
43
53
  /** The sandbox list: reused Testserver sandboxes first, freshest first, then created ones. */
44
54
  sandboxes: z.array(z.object({
@@ -75,7 +85,13 @@ export const testserverPoolRanReport = {
75
85
  facts: (facts, request) => {
76
86
  const { resolution, sandboxes } = facts;
77
87
  return {
78
- build: { requested: request.build, resolved: resolution.kind === "as-written" ? null : resolution.build },
88
+ build: {
89
+ requested: request.build,
90
+ resolved: resolution.kind === "as-written" ? null : resolution.build,
91
+ pullRequest: resolution.kind === "pull-request"
92
+ ? { repo: TESTSERVER_REPO, number: resolution.pr, headSha: resolution.headSha }
93
+ : null,
94
+ },
79
95
  sandboxes: sandboxes.map(({ testserverUrl, logViewerUrl, origin }) => ({ testserverUrl, logViewerUrl, origin })),
80
96
  reused: sandboxes.filter((sandbox) => sandbox.origin === "reused").length,
81
97
  created: sandboxes.filter((sandbox) => sandbox.origin === "created").length,
@@ -5,7 +5,11 @@
5
5
  export interface TestserverPoolOptions {
6
6
  /** How many Testserver sandboxes a run gets: 1 to 32; 1 by default. */
7
7
  readonly sandboxes?: number;
8
- /** A build name, or the build channel `stable` or `latest_main`; `stable` by default. */
8
+ /**
9
+ * A build name, the build channel `stable` or `latest_main`, or a vercel/api pull request as `pr:<N>` or its URL
10
+ * (`https://github.com/vercel/api/pull/<N>`); `stable` by default. A pull request runs on the newest Testserver build
11
+ * made from its head commit, which storm reads from GitHub on the host, or the run refuses.
12
+ */
9
13
  readonly build?: string;
10
14
  /** The least time left a Testserver sandbox from the pool needs, such as `30m` or `2h`; `30m` by default. */
11
15
  readonly minTimeLeft?: string;
@@ -1,5 +1,5 @@
1
- // The Testserver pool resource's published surface, `@cnc-tools/storm/testserver`. A user's `storm.config.ts`
2
- // imports it, so, like `@cnc-tools/storm/config`, it imports nothing at run time: loading a config file pulls in no
1
+ // The Testserver pool resource's published surface, `@vercel/storm/testserver`. A user's `storm.config.ts`
2
+ // imports it, so, like `@vercel/storm/config`, it imports nothing at run time: loading a config file pulls in no
3
3
  // Clinkr, zod or CLI code.
4
4
  /**
5
5
  * The Testserver pool resource for `resources.testserverPool` in `storm.config.ts`. Its presence gives a run a
@@ -0,0 +1,38 @@
1
+ import { type BuildChannel } from "./channels.ts";
2
+ /** The one repository whose pull requests have Testserver builds: its PR builds. */
3
+ export declare const TESTSERVER_REPO = "vercel/api";
4
+ /**
5
+ * What a `build` value names: a build channel, a vercel/api pull request (a PR build reference, `pr:<N>` or the PR's
6
+ * URL), or a build named directly.
7
+ */
8
+ export type BuildReference = {
9
+ readonly kind: "channel";
10
+ readonly channel: BuildChannel;
11
+ } | {
12
+ readonly kind: "pull-request";
13
+ readonly number: number;
14
+ } | {
15
+ readonly kind: "named";
16
+ readonly build: string;
17
+ };
18
+ /**
19
+ * What `text` names, or the problem with it, worded to follow the option's key as an option rule's problem is:
20
+ *
21
+ * - `pr:<N>`, N a positive whole number with no sign or leading zero, is a PR build reference;
22
+ * - an `http` or `https` URL on `github.com` or `www.github.com` whose path is `/vercel/api/pull/<N>`, with any
23
+ * further segments (`/files`, `/checks`, `/commits/<sha>`), a trailing slash, a query or a fragment, is one too;
24
+ * the owner and repository match in any case;
25
+ * - another `github.com` pull request URL, another `github.com` URL, or a bad `pr:` number is a problem;
26
+ * - `stable` and `latest_main` are build channels, and anything else is a build named directly.
27
+ */
28
+ export declare function parseBuildReference(text: string): {
29
+ readonly ok: true;
30
+ readonly reference: BuildReference;
31
+ } | {
32
+ readonly ok: false;
33
+ readonly problem: string;
34
+ };
35
+ /** How a reference is written once settled: the channel, `pr:<N>` or the build's name. */
36
+ export declare function buildReferenceText(reference: BuildReference): string;
37
+ /** A commit's short form, as GitHub shows it: its first 7 characters. */
38
+ export declare function shortSha(sha: string): string;