@sequenceholdings/studio-cli 0.9.23 → 0.9.26

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 (157) hide show
  1. package/README.md +166 -146
  2. package/dist/access/commands.js +3 -5
  3. package/dist/access/principal.js +2 -2
  4. package/dist/access/tier-hosts.js +2 -2
  5. package/dist/agents/commands.d.ts +1 -1
  6. package/dist/agents/commands.js +9 -9
  7. package/dist/agents/scaffold.js +3 -3
  8. package/dist/agents/voice.d.ts +1 -1
  9. package/dist/agents/voice.js +1 -1
  10. package/dist/app/commands.d.ts +2 -2
  11. package/dist/app/commands.js +31 -21
  12. package/dist/app/deploy.d.ts +1 -1
  13. package/dist/app/deploy.js +1 -1
  14. package/dist/app/manifest.d.ts +9 -9
  15. package/dist/app/manifest.js +1 -1
  16. package/dist/app/prepare.js +1 -1
  17. package/dist/app/primitive-scaffold.js +1 -1
  18. package/dist/app/readme.js +6 -6
  19. package/dist/app/scaffold.js +1 -1
  20. package/dist/app/validate.d.ts +1 -1
  21. package/dist/app/workspace-scaffold.js +1 -1
  22. package/dist/app-preview/commands.d.ts +7 -0
  23. package/dist/app-preview/commands.js +264 -0
  24. package/dist/app-preview/fingerprint.d.ts +20 -0
  25. package/dist/app-preview/fingerprint.js +32 -0
  26. package/dist/app-preview/install.d.ts +31 -0
  27. package/dist/app-preview/install.js +53 -0
  28. package/dist/app-preview/namespace-probe.d.ts +13 -0
  29. package/dist/app-preview/namespace-probe.js +50 -0
  30. package/dist/app-preview/plan.d.ts +28 -0
  31. package/dist/app-preview/plan.js +41 -0
  32. package/dist/app-preview/run.d.ts +32 -0
  33. package/dist/app-preview/run.js +48 -0
  34. package/dist/app-preview/runners.d.ts +73 -0
  35. package/dist/app-preview/runners.js +226 -0
  36. package/dist/app-preview/secrets.d.ts +48 -0
  37. package/dist/app-preview/secrets.js +104 -0
  38. package/dist/app-preview/target.d.ts +28 -0
  39. package/dist/app-preview/target.js +62 -0
  40. package/dist/artifact/delegate.d.ts +1 -12
  41. package/dist/artifact/delegate.js +43 -124
  42. package/dist/artifact/sandbox.js +1 -1
  43. package/dist/auth-cmds/commands.js +1 -2
  44. package/dist/auth.js +1 -3
  45. package/dist/bin.js +12 -2
  46. package/dist/cli-attribution.d.ts +4 -0
  47. package/dist/cli-attribution.js +25 -3
  48. package/dist/cli-errors.d.ts +9 -1
  49. package/dist/cli-errors.js +20 -9
  50. package/dist/command-contract.d.ts +7 -182
  51. package/dist/command-contract.js +9 -33
  52. package/dist/command-help.d.ts +6 -6
  53. package/dist/command-help.js +31 -27
  54. package/dist/command-telemetry.js +2 -2
  55. package/dist/config-dir.d.ts +8 -0
  56. package/dist/config-dir.js +35 -0
  57. package/dist/config.d.ts +6 -7
  58. package/dist/config.js +15 -15
  59. package/dist/docs/commands.js +9 -4
  60. package/dist/edge-auth-headers.js +1 -18
  61. package/dist/env-registry.d.ts +4 -0
  62. package/dist/env-registry.js +15 -6
  63. package/dist/envs/commands.d.ts +3 -0
  64. package/dist/envs/commands.js +11 -0
  65. package/dist/exit-codes.d.ts +16 -0
  66. package/dist/exit-codes.js +27 -0
  67. package/dist/functions/bundle.d.ts +1 -1
  68. package/dist/functions/commands.d.ts +15 -0
  69. package/dist/functions/commands.js +86 -29
  70. package/dist/functions/manifest.js +1 -1
  71. package/dist/functions/service-account-guidance.d.ts +1 -1
  72. package/dist/functions/service-account-guidance.js +2 -2
  73. package/dist/lifecycle/check.d.ts +13 -0
  74. package/dist/lifecycle/check.js +116 -0
  75. package/dist/lifecycle/commands.d.ts +14 -0
  76. package/dist/lifecycle/commands.js +121 -0
  77. package/dist/lifecycle/plan.d.ts +28 -0
  78. package/dist/lifecycle/plan.js +113 -0
  79. package/dist/lifecycle/rollback.d.ts +7 -0
  80. package/dist/lifecycle/rollback.js +89 -0
  81. package/dist/lifecycle/seq-ci-tools.d.ts +107 -0
  82. package/dist/lifecycle/seq-ci-tools.js +177 -0
  83. package/dist/lifecycle/upgrade.d.ts +3 -0
  84. package/dist/lifecycle/upgrade.js +38 -0
  85. package/dist/machine-output.d.ts +11 -2
  86. package/dist/machine-output.js +32 -10
  87. package/dist/main.d.ts +16 -6
  88. package/dist/main.js +187 -361
  89. package/dist/orgs.d.ts +45 -0
  90. package/dist/orgs.js +106 -0
  91. package/dist/orm/delegate.d.ts +1 -1
  92. package/dist/orm/delegate.js +32 -18
  93. package/dist/orm/deployment-policy.js +1 -2
  94. package/dist/orm/discover.js +6 -8
  95. package/dist/orm/execute.d.ts +1 -1
  96. package/dist/orm/execute.js +5 -6
  97. package/dist/orm/failures.js +2 -2
  98. package/dist/orm/help.d.ts +1 -1
  99. package/dist/orm/help.js +5 -5
  100. package/dist/orm/history.js +1 -2
  101. package/dist/orm/operation-show.js +1 -2
  102. package/dist/orm/version-guard.d.ts +7 -2
  103. package/dist/orm/version-guard.js +12 -7
  104. package/dist/pipeline/codegen.js +2 -2
  105. package/dist/pipeline/commands.js +9 -7
  106. package/dist/pipeline/discovery.d.ts +1 -1
  107. package/dist/pipeline/discovery.js +2 -3
  108. package/dist/pipeline/help.d.ts +1 -1
  109. package/dist/pipeline/help.js +1 -1
  110. package/dist/pipeline/lifecycle.js +1 -1
  111. package/dist/pipeline/stage-secrets.js +4 -4
  112. package/dist/pipeline/templates.js +2 -2
  113. package/dist/pnpm-auto-update.js +3 -3
  114. package/dist/principals/commands.d.ts +1 -1
  115. package/dist/principals/commands.js +13 -14
  116. package/dist/process/commands.js +4 -1
  117. package/dist/process/run-steps.d.ts +2 -2
  118. package/dist/process/runs.js +1 -2
  119. package/dist/registry/commands.d.ts +4 -0
  120. package/dist/registry/commands.js +281 -0
  121. package/dist/registry/help.d.ts +8 -0
  122. package/dist/registry/help.js +64 -0
  123. package/dist/registry/lifecycle-commands.d.ts +3 -0
  124. package/dist/registry/lifecycle-commands.js +67 -0
  125. package/dist/registry/lookup.d.ts +6 -0
  126. package/dist/registry/lookup.js +18 -0
  127. package/dist/registry/runners.d.ts +14 -0
  128. package/dist/registry/runners.js +206 -0
  129. package/dist/registry/spec.d.ts +45 -0
  130. package/dist/registry/spec.js +8 -0
  131. package/dist/repos/commands.d.ts +1 -4
  132. package/dist/repos/commands.js +3 -21
  133. package/dist/repos/github-connect-command.d.ts +10 -1
  134. package/dist/repos/github-connect-command.js +52 -11
  135. package/dist/repos/github-init.d.ts +9 -2
  136. package/dist/repos/github-init.js +18 -2
  137. package/dist/repos/init-repo.js +1 -2
  138. package/dist/run-pinned.d.ts +48 -0
  139. package/dist/run-pinned.js +127 -0
  140. package/dist/sdk-source.d.ts +64 -0
  141. package/dist/sdk-source.js +199 -0
  142. package/dist/secrets/commands.d.ts +1 -1
  143. package/dist/secrets/commands.js +29 -24
  144. package/dist/service-accounts/commands.d.ts +1 -1
  145. package/dist/service-accounts/commands.js +4 -5
  146. package/dist/staged-env.d.ts +14 -0
  147. package/dist/staged-env.js +73 -0
  148. package/dist/storage/commands.js +21 -22
  149. package/dist/templates/catalog.d.ts +5 -5
  150. package/dist/templates/catalog.js +1 -1
  151. package/dist/update-check.js +3 -3
  152. package/dist/version-support.d.ts +39 -0
  153. package/dist/version-support.js +71 -0
  154. package/package.json +11 -10
  155. package/release-summary.json +4 -1
  156. package/dist/preview.d.ts +0 -90
  157. package/dist/preview.js +0 -292
package/dist/preview.d.ts DELETED
@@ -1,90 +0,0 @@
1
- /**
2
- * Per-PR preview-environment resolution for `seq-studio artifact`.
3
- *
4
- * Lets an engineer target a specific PR's Vercel preview deployment
5
- * (`https://studio-atlas-git-<slug>.preview.seqholdings.com`) WITHOUT
6
- * hand-editing `~/.config/lattice/config.toml`. See
7
- * `docs/preview-environments.md` for the full lifecycle.
8
- *
9
- * SLUG ALGORITHM — must stay byte-for-byte in sync with the other
10
- * places that compute the same slug from a branch name:
11
- * - `.github/workflows/preview-deploy.yml` (replace non-alnum, lowercase, trim hyphens)
12
- * - `.github/workflows/trigger-preview.yml` (same bash pipeline)
13
- * - `atlas/src/server/db.ts:sanitize` (`.replace(/[^a-zA-Z0-9-]/g, '-').toLowerCase().replace(/^-+|-+$/g, '')`)
14
- * - `databricks/lakebase/atlas-db/pr-lifecycle.ts:sanitize`
15
- * Changing it here without changing those produces a host that does NOT
16
- * match what `preview-deploy.yml` actually deployed.
17
- */
18
- /** Cloudflare zone the preview aliases live on. */
19
- export declare const PREVIEW_DOMAIN = "preview.seqholdings.com";
20
- /** Vercel project name → the `<project>-git-<slug>` alias label prefix. */
21
- export declare const PREVIEW_PROJECT = "studio-atlas";
22
- /**
23
- * Mirror of `preview-deploy.yml`'s `PROTECTED` list. A branch whose slug
24
- * matches one of these (or starts with `dev-`) never gets a preview — the
25
- * workflow's guard refuses to deploy it — so the CLI refuses to target it.
26
- */
27
- export declare const PREVIEW_PROTECTED_SLUGS: readonly ["banksouth", "staging", "dev", "production", "main", "development", "master"];
28
- /**
29
- * Vercel deploys the preview under the DNS label `studio-atlas-git-<slug>`.
30
- * A single DNS label can be at most 63 chars; Vercel hash-truncates labels
31
- * past that, so the resulting host can NOT be computed from the slug and must
32
- * be read back from the deployment. This is the SAME cutoff the workflow uses
33
- * (`[ "${#LABEL}" -le 63 ]`).
34
- */
35
- export declare const MAX_LABEL_LENGTH = 63;
36
- /** Apply the canonical branch → slug transform. */
37
- export declare function previewSlug(branchOrSlug: string): string;
38
- /** The `studio-atlas-git-<slug>` DNS label Vercel assigns the preview. */
39
- export declare function previewLabel(slug: string): string;
40
- /** True for protected slugs that never get a preview (mirror of the workflow). */
41
- export declare function isProtectedPreviewSlug(slug: string): boolean;
42
- /**
43
- * True when the slug is short enough that its preview host can be computed
44
- * without reading the real alias back from Vercel (label ≤ 63 chars).
45
- */
46
- export declare function isComputablePreviewSlug(slug: string): boolean;
47
- /** The computed preview host for a (short) slug. */
48
- export declare function previewHost(slug: string): string;
49
- export interface ResolvedPreview {
50
- /** The canonical branch slug. */
51
- slug: string;
52
- /** Fully-qualified `https://…preview.seqholdings.com` URL. */
53
- url: string;
54
- }
55
- /**
56
- * Resolve a `preview:<branch-or-slug>` target to its computed preview URL.
57
- * Throws for protected slugs and for long slugs whose alias can't be computed
58
- * (the caller should fall back to `--pr <number>` or `--env-url <url>`).
59
- */
60
- export declare function resolvePreviewFromSlug(branchOrSlug: string): ResolvedPreview;
61
- /** Injectable `gh` runner so PR resolution is unit-testable. */
62
- export type GhRunner = (args: string[]) => Promise<string>;
63
- interface GhPrComment {
64
- body?: string;
65
- author?: {
66
- login?: string;
67
- } | null;
68
- createdAt?: string;
69
- }
70
- /**
71
- * Find the real `*.preview.seqholdings.com` alias the Vercel bot posts on the
72
- * PR. This is the ONLY reliable way to get the host for a long (hash-truncated)
73
- * slug — we can't compute it. Returns the newest trusted match or null.
74
- */
75
- export declare function findPreviewUrlInComments(comments: ReadonlyArray<GhPrComment> | undefined, { expectedHost, requireTrustedAuthor, }?: {
76
- expectedHost?: string;
77
- requireTrustedAuthor?: boolean;
78
- }): string | null;
79
- /**
80
- * Resolve a preview env from a PR number. Reads the PR's head branch (and the
81
- * Vercel bot's preview-URL comment) via `gh`. Prefers the real alias from the
82
- * comment — which works even for long, hash-truncated slugs — and falls back to
83
- * computing the host from the branch slug for short slugs. Throws for protected
84
- * slugs and for long slugs with no resolvable alias.
85
- */
86
- export declare function resolvePreviewByPr({ pr, runGh, }: {
87
- pr: number;
88
- runGh?: GhRunner;
89
- }): Promise<ResolvedPreview>;
90
- export {};
package/dist/preview.js DELETED
@@ -1,292 +0,0 @@
1
- import { execFile } from 'node:child_process';
2
- import { promisify } from 'node:util';
3
- /**
4
- * Per-PR preview-environment resolution for `seq-studio artifact`.
5
- *
6
- * Lets an engineer target a specific PR's Vercel preview deployment
7
- * (`https://studio-atlas-git-<slug>.preview.seqholdings.com`) WITHOUT
8
- * hand-editing `~/.config/lattice/config.toml`. See
9
- * `docs/preview-environments.md` for the full lifecycle.
10
- *
11
- * SLUG ALGORITHM — must stay byte-for-byte in sync with the other
12
- * places that compute the same slug from a branch name:
13
- * - `.github/workflows/preview-deploy.yml` (replace non-alnum, lowercase, trim hyphens)
14
- * - `.github/workflows/trigger-preview.yml` (same bash pipeline)
15
- * - `atlas/src/server/db.ts:sanitize` (`.replace(/[^a-zA-Z0-9-]/g, '-').toLowerCase().replace(/^-+|-+$/g, '')`)
16
- * - `databricks/lakebase/atlas-db/pr-lifecycle.ts:sanitize`
17
- * Changing it here without changing those produces a host that does NOT
18
- * match what `preview-deploy.yml` actually deployed.
19
- */
20
- /** Cloudflare zone the preview aliases live on. */
21
- export const PREVIEW_DOMAIN = 'preview.seqholdings.com';
22
- /** Vercel project name → the `<project>-git-<slug>` alias label prefix. */
23
- export const PREVIEW_PROJECT = 'studio-atlas';
24
- const LABEL_PREFIX = `${PREVIEW_PROJECT}-git-`;
25
- /**
26
- * Mirror of `preview-deploy.yml`'s `PROTECTED` list. A branch whose slug
27
- * matches one of these (or starts with `dev-`) never gets a preview — the
28
- * workflow's guard refuses to deploy it — so the CLI refuses to target it.
29
- */
30
- export const PREVIEW_PROTECTED_SLUGS = [
31
- 'banksouth',
32
- 'staging',
33
- 'dev',
34
- 'production',
35
- 'main',
36
- 'development',
37
- 'master',
38
- ];
39
- /**
40
- * Vercel deploys the preview under the DNS label `studio-atlas-git-<slug>`.
41
- * A single DNS label can be at most 63 chars; Vercel hash-truncates labels
42
- * past that, so the resulting host can NOT be computed from the slug and must
43
- * be read back from the deployment. This is the SAME cutoff the workflow uses
44
- * (`[ "${#LABEL}" -le 63 ]`).
45
- */
46
- export const MAX_LABEL_LENGTH = 63;
47
- /** Apply the canonical branch → slug transform. */
48
- export function previewSlug(branchOrSlug) {
49
- return branchOrSlug.replace(/[^a-zA-Z0-9-]/g, '-').toLowerCase().replace(/^-+|-+$/g, '');
50
- }
51
- /** The `studio-atlas-git-<slug>` DNS label Vercel assigns the preview. */
52
- export function previewLabel(slug) {
53
- return `${LABEL_PREFIX}${slug}`;
54
- }
55
- /** True for protected slugs that never get a preview (mirror of the workflow). */
56
- export function isProtectedPreviewSlug(slug) {
57
- return (PREVIEW_PROTECTED_SLUGS.includes(slug) ||
58
- slug.startsWith('dev-'));
59
- }
60
- /**
61
- * True when the slug is short enough that its preview host can be computed
62
- * without reading the real alias back from Vercel (label ≤ 63 chars).
63
- */
64
- export function isComputablePreviewSlug(slug) {
65
- return previewLabel(slug).length <= MAX_LABEL_LENGTH;
66
- }
67
- /** The computed preview host for a (short) slug. */
68
- export function previewHost(slug) {
69
- return `${previewLabel(slug)}.${PREVIEW_DOMAIN}`;
70
- }
71
- const PROTECTED_HINT = PREVIEW_PROTECTED_SLUGS.join(', ');
72
- /**
73
- * Resolve a `preview:<branch-or-slug>` target to its computed preview URL.
74
- * Throws for protected slugs and for long slugs whose alias can't be computed
75
- * (the caller should fall back to `--pr <number>` or `--env-url <url>`).
76
- */
77
- export function resolvePreviewFromSlug(branchOrSlug) {
78
- const slug = previewSlug(branchOrSlug);
79
- if (!slug) {
80
- throw new Error('Preview env requires a branch or slug, e.g. --env preview:my-branch');
81
- }
82
- if (isProtectedPreviewSlug(slug)) {
83
- throw new Error(`Refusing to target a protected branch slug "${slug}". ` +
84
- `Protected slugs never get a preview (${PROTECTED_HINT}, or anything starting with "dev-").`);
85
- }
86
- if (!isComputablePreviewSlug(slug)) {
87
- throw new Error(`Branch slug "${slug}" is too long (label "${previewLabel(slug)}" is ` +
88
- `${previewLabel(slug).length} > ${MAX_LABEL_LENGTH} chars): Vercel ` +
89
- `hash-truncates the alias, so the preview host can't be computed. ` +
90
- `Resolve it with --pr <number> (reads the real alias from the PR) or ` +
91
- `pass --env-url <https://…preview.seqholdings.com>.`);
92
- }
93
- return { slug, url: `https://${previewHost(slug)}` };
94
- }
95
- const execFileAsync = promisify(execFile);
96
- const defaultGhRunner = async (args) => {
97
- try {
98
- const { stdout } = await execFileAsync('gh', args, { encoding: 'utf8' });
99
- return stdout;
100
- }
101
- catch (error) {
102
- const message = error instanceof Error ? error.message : String(error);
103
- throw new Error(`Failed to run \`gh ${args.join(' ')}\`. Install the GitHub CLI and run ` +
104
- `\`gh auth login\`, or pass --env-url <url> directly. (${message})`);
105
- }
106
- };
107
- const TRUSTED_PREVIEW_COMMENT_AUTHORS = new Set([
108
- 'vercel',
109
- 'vercel[bot]',
110
- 'github-actions',
111
- 'github-actions[bot]',
112
- ]);
113
- function compareIsoDesc(a, b) {
114
- if (a === b)
115
- return 0;
116
- return a > b ? -1 : 1;
117
- }
118
- function commentsNewestFirst(comments) {
119
- const withDates = comments
120
- .map((c) => ({ c, d: typeof c.createdAt === 'string' ? c.createdAt : null }))
121
- .filter((x) => x.d !== null);
122
- if (withDates.length === 0)
123
- return comments;
124
- return [...comments].sort((a, b) => {
125
- const da = typeof a.createdAt === 'string' ? a.createdAt : '';
126
- const db = typeof b.createdAt === 'string' ? b.createdAt : '';
127
- return compareIsoDesc(da, db);
128
- });
129
- }
130
- /**
131
- * Find the real `*.preview.seqholdings.com` alias the Vercel bot posts on the
132
- * PR. This is the ONLY reliable way to get the host for a long (hash-truncated)
133
- * slug — we can't compute it. Returns the newest trusted match or null.
134
- */
135
- export function findPreviewUrlInComments(comments, { expectedHost, requireTrustedAuthor = true, } = {}) {
136
- if (!comments)
137
- return null;
138
- const ordered = commentsNewestFirst(comments);
139
- const expected = expectedHost?.toLowerCase();
140
- const re = /https:\/\/[a-z0-9-]+\.preview\.seqholdings\.com/gi;
141
- const labelPrefix = LABEL_PREFIX.toLowerCase();
142
- const isTrusted = (comment) => TRUSTED_PREVIEW_COMMENT_AUTHORS.has(comment.author?.login ?? '');
143
- const search = (predicate) => {
144
- for (const comment of ordered) {
145
- if (!comment?.body)
146
- continue;
147
- if (!predicate(comment))
148
- continue;
149
- for (const match of comment.body.matchAll(re)) {
150
- const url = match[0];
151
- if (!url)
152
- continue;
153
- let host;
154
- try {
155
- host = new URL(url).host.toLowerCase();
156
- }
157
- catch {
158
- continue;
159
- }
160
- if (!host.endsWith(`.${PREVIEW_DOMAIN}`))
161
- continue;
162
- if (!host.startsWith(labelPrefix))
163
- continue;
164
- if (expected && host !== expected)
165
- continue;
166
- return url;
167
- }
168
- }
169
- return null;
170
- };
171
- if (requireTrustedAuthor) {
172
- return search(isTrusted);
173
- }
174
- return search(() => true);
175
- }
176
- async function tryResolvePreviewUrlFromDeployments({ runGh, headSha, }) {
177
- if (!headSha)
178
- return null;
179
- const repoStdout = await runGh(['repo', 'view', '--json', 'nameWithOwner']);
180
- let repo;
181
- try {
182
- repo = JSON.parse(repoStdout);
183
- }
184
- catch {
185
- return null;
186
- }
187
- const nameWithOwner = repo.nameWithOwner;
188
- if (!nameWithOwner)
189
- return null;
190
- const deploymentsStdout = await runGh([
191
- 'api',
192
- `repos/${nameWithOwner}/deployments?sha=${encodeURIComponent(headSha)}&per_page=100`,
193
- ]);
194
- let deployments;
195
- try {
196
- deployments = JSON.parse(deploymentsStdout);
197
- }
198
- catch {
199
- return null;
200
- }
201
- const previewDeployments = deployments
202
- .filter((d) => typeof d.id === 'number' && /preview/i.test(d.environment ?? ''))
203
- .sort((a, b) => compareIsoDesc(a.created_at ?? '', b.created_at ?? ''));
204
- const chosen = previewDeployments[0];
205
- if (!chosen?.id)
206
- return null;
207
- const statusesStdout = await runGh([
208
- 'api',
209
- `repos/${nameWithOwner}/deployments/${chosen.id}/statuses?per_page=100`,
210
- ]);
211
- let statuses;
212
- try {
213
- statuses = JSON.parse(statusesStdout);
214
- }
215
- catch {
216
- return null;
217
- }
218
- const candidates = statuses
219
- .filter((s) => typeof s.environment_url === 'string' && s.environment_url.length > 0)
220
- .sort((a, b) => compareIsoDesc(a.created_at ?? '', b.created_at ?? ''));
221
- const isPreviewHost = (url) => {
222
- try {
223
- const host = new URL(url).hostname.toLowerCase();
224
- return host.endsWith(`.${PREVIEW_DOMAIN}`) && host.startsWith(LABEL_PREFIX.toLowerCase());
225
- }
226
- catch {
227
- return false;
228
- }
229
- };
230
- const success = candidates.find((s) => s.state === 'success' && isPreviewHost(s.environment_url));
231
- if (success?.environment_url)
232
- return success.environment_url;
233
- const any = candidates.find((s) => isPreviewHost(s.environment_url));
234
- return any?.environment_url ?? null;
235
- }
236
- /**
237
- * Resolve a preview env from a PR number. Reads the PR's head branch (and the
238
- * Vercel bot's preview-URL comment) via `gh`. Prefers the real alias from the
239
- * comment — which works even for long, hash-truncated slugs — and falls back to
240
- * computing the host from the branch slug for short slugs. Throws for protected
241
- * slugs and for long slugs with no resolvable alias.
242
- */
243
- export async function resolvePreviewByPr({ pr, runGh = defaultGhRunner, }) {
244
- if (!Number.isInteger(pr) || pr <= 0) {
245
- throw new Error(`--pr must be a positive integer (got "${pr}").`);
246
- }
247
- const stdout = await runGh([
248
- 'pr',
249
- 'view',
250
- String(pr),
251
- '--json',
252
- 'headRefName,headRefOid,comments',
253
- ]);
254
- let parsed;
255
- try {
256
- parsed = JSON.parse(stdout);
257
- }
258
- catch {
259
- throw new Error(`Could not parse \`gh pr view ${pr}\` output as JSON.`);
260
- }
261
- const branch = parsed.headRefName;
262
- if (!branch) {
263
- throw new Error(`Could not read the head branch for PR #${pr} from gh.`);
264
- }
265
- const slug = previewSlug(branch);
266
- if (isProtectedPreviewSlug(slug)) {
267
- throw new Error(`PR #${pr} targets a protected branch slug "${slug}", which never gets a preview.`);
268
- }
269
- // If the slug is short, the alias is deterministic; prefer the computed host.
270
- // This avoids accidentally targeting a stale or user-pasted preview URL.
271
- if (isComputablePreviewSlug(slug)) {
272
- return { slug, url: `https://${previewHost(slug)}` };
273
- }
274
- // Long, hash-truncated slugs require reading the real alias back from Vercel.
275
- // Prefer deployment metadata over comment text (comments can be stale or
276
- // user-authored).
277
- const fromDeployment = await tryResolvePreviewUrlFromDeployments({
278
- runGh,
279
- headSha: parsed.headRefOid ?? '',
280
- }).catch(() => null);
281
- if (fromDeployment)
282
- return { slug, url: fromDeployment };
283
- // Fall back to the Vercel comment if deployments aren't available.
284
- const fromComment = findPreviewUrlInComments(parsed.comments);
285
- if (fromComment)
286
- return { slug, url: fromComment };
287
- // No trusted Vercel comment yet — error loudly and ask for an explicit URL.
288
- throw new Error(`PR #${pr} (branch "${branch}", slug "${slug}") has a long, ` +
289
- `hash-truncated alias and no preview URL was found in the PR comments ` +
290
- `yet. Wait for the Vercel "Visit Preview" comment, or pass ` +
291
- `--env-url <https://…preview.seqholdings.com> directly.`);
292
- }