Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions infra/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "@cap/infra",
"type": "module",
"devDependencies": {
"sst": "3.17.14"
"sst": "3.17.14",
"vitest": "~2.1.9"
},
"scripts": {
"sst": "sst"
"sst": "sst",
"test": "vitest run"
},
"dependencies": {
"@pulumi/github": "^6.7.0",
Expand Down
13 changes: 3 additions & 10 deletions infra/sst.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// <reference path="./.sst/platform/config.d.ts" />

import { parseStageName } from "./stage";

const GITHUB_ORG = "CapSoftware";
const _GITHUB_REPO = "Cap";
const _GITHUB_APP_ID = "1196731";
Expand All @@ -12,16 +14,7 @@ const _CLOUDFLARE_ACCOUNT_ID = "3de2dd633194481d80f68f55257bdbaa";
const AXIOM_API_TOKEN = "xaat-c0704be6-e942-4935-b068-3b491d7cc00f";
const AXIOM_DATASET = "cap-otel";

const parsedStage = () => {
if ($app.stage === "staging") return { variant: "staging" } as const;
if ($app.stage === "production") return { variant: "production" } as const;
if ($app.stage.startsWith("git-branch-"))
return {
variant: "git-branch",
branch: $app.stage.slice("git-branch-".length),
} as const;
throw new Error("Unsupported stage");
};
const parsedStage = () => parseStageName($app.stage);

export default $config({
app(input) {
Expand Down
21 changes: 21 additions & 0 deletions infra/stage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { describe, expect, it } from "vitest";
import { parseStageName } from "./stage";

describe("parseStageName", () => {
it("recognizes fixed deployment stages", () => {
expect(parseStageName("staging")).toEqual({ variant: "staging" });
expect(parseStageName("production")).toEqual({ variant: "production" });
});

it("extracts branch names from git branch preview stages", () => {
expect(parseStageName("git-branch-add-infra-tests")).toEqual({
variant: "git-branch",
branch: "add-infra-tests",
});
});
Comment on lines +10 to +15
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Missing edge-case: empty branch after the prefix

parseStageName("git-branch-") currently returns { variant: "git-branch", branch: "" } — an empty string branch. Downstream in sst.config.ts, that value is interpolated directly into a Vercel preview URL, yielding a double-dash segment (…-git--mc-ilroy.vercel.app) which Vercel won't resolve. A test asserting that "git-branch-" either throws or is treated as unsupported would make the acceptable input contract explicit.

Prompt To Fix With AI
This is a comment left during a code review.
Path: infra/stage.test.ts
Line: 10-15

Comment:
**Missing edge-case: empty branch after the prefix**

`parseStageName("git-branch-")` currently returns `{ variant: "git-branch", branch: "" }` — an empty string branch. Downstream in `sst.config.ts`, that value is interpolated directly into a Vercel preview URL, yielding a double-dash segment (`…-git--mc-ilroy.vercel.app`) which Vercel won't resolve. A test asserting that `"git-branch-"` either throws or is treated as unsupported would make the acceptable input contract explicit.

How can I resolve this? If you propose a fix, please make it concise.


it("rejects unsupported stages", () => {
expect(() => parseStageName("preview")).toThrow("Unsupported stage");
expect(() => parseStageName("git-branch-")).toThrow("Unsupported stage");
});
});
15 changes: 15 additions & 0 deletions infra/stage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type ParsedStage =
| { variant: "staging" }
| { variant: "production" }
| { variant: "git-branch"; branch: string };

export function parseStageName(stage: string): ParsedStage {
if (stage === "staging") return { variant: "staging" };
if (stage === "production") return { variant: "production" };
if (stage.startsWith("git-branch-")) {
const branch = stage.slice("git-branch-".length);
if (!branch) throw new Error("Unsupported stage");
return { variant: "git-branch", branch };
}
throw new Error("Unsupported stage");
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.