Skip to content
Draft
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
3 changes: 2 additions & 1 deletion apps/cli/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ Plan-gated denials carry an optional `entitlement: { feature, upgrade_url }` on
`executeRaw` caller that builds its own status error attaches it with `planGateFromResponse`. The
json and stream-json error object carries it, the text layer's `fail` prints the upgrade hint from
it, and `withCommandTelemetry` fires `cli_upgrade_suggested` from it, so a handler that rebuilds a
mapped status error must forward it.
mapped status error must forward it. Where `upgrade_url` points decides the hint wording and the
event's `fix_kind`; see `fixKindFromUpgradeUrl` in `src/shared/output/normalize-error.ts`.

## Targets and hosted consent

Expand Down
21 changes: 19 additions & 2 deletions apps/cli/src/shared/output/normalize-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,26 @@ export type PlanGateEntitlement = {
readonly upgrade_url: string;
};

/** The text-mode stderr line that points the user at the billing page for a plan-gated feature. */
/** What clears a plan-gated denial, read from the dashboard page its `upgrade_url` points at. */
export type UpgradeFixKind = "plan" | "addon";

const PROJECT_ADDONS_PAGE = /^[^?#]*\/project\/([a-z]{20})\/settings\/addons\/?(?:[?#]|$)/;

/** The project whose add-ons page an `upgrade_url` points at, when it points at one. */
export const addonProjectRefFromUpgradeUrl = (upgradeUrl: string): string | undefined =>
PROJECT_ADDONS_PAGE.exec(upgradeUrl)?.[1];

export const fixKindFromUpgradeUrl = (upgradeUrl: string): UpgradeFixKind =>
addonProjectRefFromUpgradeUrl(upgradeUrl) === undefined ? "plan" : "addon";

const UPGRADE_HINT_LEAD: Record<UpgradeFixKind, string> = {
plan: "Your organization's plan does not include this feature. Plans and add-ons:",
addon: "This feature needs a project add-on. Add-ons:",
};

/** The text-mode stderr line that points the user at the page that clears a plan-gated denial. */
export const upgradeHint = (upgradeUrl: string): string =>
`Your organization's plan does not include this feature. Plans and add-ons: ${styleText("bold", upgradeUrl)}`;
`${UPGRADE_HINT_LEAD[fixKindFromUpgradeUrl(upgradeUrl)]} ${styleText("bold", upgradeUrl)}`;

type NormalizedCliError = {
readonly code: string;
Expand Down
20 changes: 19 additions & 1 deletion apps/cli/src/shared/output/normalize-error.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { styleText } from "node:util";

import { describe, expect, test } from "vitest";
import { Cause } from "effect";
import { CliError, Command } from "effect/cli";
Expand All @@ -7,12 +9,28 @@ import { branchesCommand } from "../../commands/branches/branches.command.ts";
import { networkRestrictionsCommand } from "../../commands/network-restrictions/network-restrictions.command.ts";
import { ssoCommand } from "../../commands/sso/sso.command.ts";
import { CliProjectHomeNotDirectoryError } from "../config/cli-project-home.service.ts";
import { formatCliError, normalizeCause, normalizeCliError } from "./normalize-error.ts";
import {
formatCliError,
normalizeCause,
normalizeCliError,
upgradeHint,
} from "./normalize-error.ts";

const testRoot = Command.make("supabase").pipe(
Command.withSubcommands([branchesCommand, networkRestrictionsCommand, ssoCommand]),
);

describe("upgradeHint", () => {
test("names a project add-on, not the plan, for a project add-ons URL", () => {
const url =
"https://supabase.com/dashboard/project/abcdefghijklmnopqrst/settings/addons?panel=pitr";

expect(upgradeHint(url)).toBe(
`This feature needs a project add-on. Add-ons: ${styleText("bold", url)}`,
);
});
});

describe("normalizeCliError", () => {
test("falls back to tagged error fields when no explicit mapping exists", () => {
const error = {
Expand Down
1 change: 1 addition & 0 deletions apps/cli/src/shared/telemetry/event-catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const EventUpgradeSuggested = "cli_upgrade_suggested";

export const PropFeatureKey = "feature_key";
export const PropOrgSlug = "org_slug";
export const PropFixKind = "fix_kind";

// `link` also accepts a branch name/UUID in addition to a project ref; these two properties
// distinguish a branch-name link from a plain ref link.
Expand Down
17 changes: 13 additions & 4 deletions apps/cli/src/telemetry/command-telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
getCommandRuntimeSpanName,
} from "../shared/runtime/command-runtime.service.ts";
import { failureBesideClosedStdout } from "../shared/output/errors.ts";
import { normalizeCause } from "../shared/output/normalize-error.ts";
import {
addonProjectRefFromUpgradeUrl,
fixKindFromUpgradeUrl,
normalizeCause,
} from "../shared/output/normalize-error.ts";
import { Output } from "../shared/output/output.service.ts";
import {
CreateTicketFlag,
Expand Down Expand Up @@ -39,6 +43,7 @@ import {
PropErrorCategory,
PropExitCode,
PropFeatureKey,
PropFixKind,
PropOrgSlug,
PropOutputFormat,
PropStackBackend,
Expand Down Expand Up @@ -569,8 +574,13 @@ function withCommandAnalyticsImplementation<Flags extends Record<string, unknown
const id = svc.stitchedDistinctId();
return id === undefined ? Option.none() : Option.some(id);
});
const entitlement =
failureCause !== undefined ? normalizeCause(failureCause).entitlement : undefined;
const failedProjectRef =
failureCause === undefined ? undefined : failureProjectRef(failureCause);
failureCause === undefined
? undefined
: (failureProjectRef(failureCause) ??
(entitlement && addonProjectRefFromUpgradeUrl(entitlement.upgrade_url)));
const contextWithProject: AnalyticsContext =
failedProjectRef === undefined
? analyticsContext
Expand All @@ -587,14 +597,13 @@ function withCommandAnalyticsImplementation<Flags extends Record<string, unknown
processControlledFailure ?? processControlledFailureActionability(command),
)
: {};
const entitlement =
failureCause !== undefined ? normalizeCause(failureCause).entitlement : undefined;

if (entitlement !== undefined) {
yield* analytics
.capture(EventUpgradeSuggested, {
[PropFeatureKey]: entitlement.feature,
[PropOrgSlug]: orgSlugFromUpgradeUrl(entitlement.upgrade_url),
[PropFixKind]: fixKindFromUpgradeUrl(entitlement.upgrade_url),
})
.pipe(
withAnalyticsContext(captureContext),
Expand Down
31 changes: 29 additions & 2 deletions apps/cli/src/telemetry/command-telemetry.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1499,9 +1499,9 @@ describe("withCommandTelemetry", () => {
upgrade_url: "https://supabase.com/dashboard/org/env-org/billing",
},
});
const runGated = () => {
const runGated = (failure = gatedFailure) => {
const analytics = mockContextualAnalytics();
return Effect.fail(gatedFailure).pipe(
return Effect.fail(failure).pipe(
withCommandTelemetry(),
Effect.provide(
Layer.mergeAll(
Expand All @@ -1528,6 +1528,7 @@ describe("withCommandTelemetry", () => {
expect(captured[0]?.properties).toMatchObject({
feature_key: "branching_limit",
org_slug: "env-org",
fix_kind: "plan",
});
expect(captured[1]?.properties).toMatchObject({
exit_code: 1,
Expand All @@ -1537,6 +1538,32 @@ describe("withCommandTelemetry", () => {
),
),
);

it.live("records an add-on denial as an add-on fix on the project its upgrade URL names", () =>
runGated(
new BranchesCreateUnexpectedStatusError({
status: 400,
body: "{}",
message: "unexpected create branch status 400",
entitlement: {
feature: "custom_domain",
upgrade_url:
"https://supabase.com/dashboard/project/abcdefghijklmnopqrst/settings/addons?panel=customDomain",
},
}),
).pipe(
Effect.tap((captured) =>
Effect.sync(() => {
expect(captured[0]?.properties).toMatchObject({
feature_key: "custom_domain",
org_slug: "",
fix_kind: "addon",
groups: { project: "abcdefghijklmnopqrst" },
});
}),
),
),
);
});

it.live("skips analytics capture when analytics are disabled", () => {
Expand Down