Skip to content

Commit 39e27f3

Browse files
bnjjjjgoux
andauthored
feat(cli): expose consolidated Warehouse status (#7671)
Generate `warehouse status` from `GET /v2/projects/{ref}/warehouse/status`, preserving readiness, capabilities, zero-target enablement and unavailable observations through the shared runtime. Preserve `warehouse enable --wait` with selection-bound setup polling. Discover internal polling targets from response links without exposing a `warehouse setups get` command; status has no wait flags. Retain teardown waiting. Keep text output on terminals, raw JSON when piped or requested, and YAML through `-o yaml`. Stacked on #7640 (including merged #7660). Depends on supabase/platform#42039. Implements PIPE-1445. --------- Co-authored-by: Julien Goux <hi@jgoux.dev>
1 parent 75169df commit 39e27f3

22 files changed

Lines changed: 2516 additions & 268 deletions

‎apps/cli/scripts/api-command-derive.ts‎

Lines changed: 151 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
ApiCommandFlag,
1212
ApiCommandSchemas,
1313
ApiCommandWait,
14+
ApiPollTarget,
1415
} from "../src/commands/api/api-command.model.ts";
1516

1617
type JsonObject = Readonly<Record<string, unknown>>;
@@ -85,6 +86,8 @@ interface DeriveInput {
8586
readonly spec: JsonObject;
8687
/** Operation ids that generate commands; `x-operation` on any other operation is ignored. */
8788
readonly generated: ReadonlyArray<string>;
89+
/** Hidden polling operation id to the generated operation whose command inspects its state. */
90+
readonly pollTargets: Readonly<Record<string, string>>;
8891
readonly nouns: Readonly<Record<string, ApiNoun>>;
8992
/** Kebab-case operation id to the camelCase key of the typed client's `operationDefinitions`. */
9093
readonly operationIdMap: Readonly<Record<string, OperationId>>;
@@ -94,6 +97,7 @@ interface DeriveInput {
9497

9598
interface DeriveResult {
9699
readonly entries: ReadonlyArray<ApiCommandEntry>;
100+
readonly pollTargets: ReadonlyArray<ApiPollTarget>;
97101
readonly schemas: Readonly<Record<string, ApiCommandSchemas>>;
98102
readonly problems: ReadonlyArray<string>;
99103
}
@@ -532,6 +536,60 @@ const readMetadata = (
532536
return metadata;
533537
};
534538

539+
/** Reads each internal polling target's `poll` and checks its map entry against the generated operations. */
540+
const readPollTargets = (
541+
operations: ReadonlyMap<string, Operation>,
542+
generated: ReadonlyArray<string>,
543+
targets: Readonly<Record<string, string>>,
544+
problems: Array<string>,
545+
): ReadonlyMap<string, ApiCommandPoll> => {
546+
const polls = new Map<string, ApiCommandPoll>();
547+
for (const [targetId, inspectId] of Object.entries(targets)) {
548+
const fail = (reason: string) => problems.push(`${targetId}: ${reason}`);
549+
const target = operations.get(targetId);
550+
if (target === undefined) {
551+
fail("polling target has no operation in the spec");
552+
continue;
553+
}
554+
if (generated.includes(targetId)) {
555+
fail("polling target is also a generated operation; remove it from pollTargets");
556+
}
557+
if (!generated.includes(inspectId)) {
558+
fail(`inspect operation "${inspectId}" is not a generated operation`);
559+
}
560+
const block = target.node["x-operation"];
561+
if (block === undefined) {
562+
fail("polling target has no x-operation poll");
563+
continue;
564+
}
565+
const decoded = decodeXOperation(block);
566+
if (Exit.isFailure(decoded)) {
567+
fail(`invalid x-operation: ${Cause.pretty(decoded.cause)}`);
568+
} else if (decoded.value.poll === undefined) {
569+
fail("polling target declares no poll");
570+
} else {
571+
polls.set(targetId, decoded.value.poll);
572+
}
573+
}
574+
return polls;
575+
};
576+
577+
const requireExperimental = (operation: Operation, fail: (reason: string) => void): void => {
578+
if (asString(operation.node["x-scalar-stability"]) !== "experimental") {
579+
fail("only operations marked experimental can be generated (x-scalar-stability)");
580+
}
581+
};
582+
583+
const requireOneWait = (
584+
ownPoll: ApiCommandPoll | undefined,
585+
linked: ApiCommandWait | undefined,
586+
fail: (reason: string) => void,
587+
): void => {
588+
if (ownPoll !== undefined && linked !== undefined) {
589+
fail(`declares poll and also links to the polled operation "${linked.read}"; use one`);
590+
}
591+
};
592+
535593
const pollWait = (
536594
read: string,
537595
params: Readonly<Record<string, string>>,
@@ -550,12 +608,14 @@ const REQUEST_PATH = /^\$request\.path\.(.+)$/u;
550608

551609
/**
552610
* The wait a command's 2xx response `links` declare: the one link whose target operation has
553-
* `poll`. Links to operations without `poll` are ignored; a polled target that is not generated fails.
611+
* `poll`. The target is a generated operation or a listed polling target; links without `poll`
612+
* are ignored.
554613
*/
555614
const linkedWait = (
556615
spec: JsonObject,
557616
operations: ReadonlyMap<string, Operation>,
558-
metadata: ReadonlyMap<string, OperationMetadata>,
617+
polls: ReadonlyMap<string, ApiCommandPoll>,
618+
generated: ReadonlyArray<string>,
559619
operation: Operation,
560620
fail: (reason: string) => void,
561621
): ApiCommandWait | undefined => {
@@ -576,11 +636,14 @@ const linkedWait = (
576636
fail(`link "${linkName}" targets unknown operation "${targetId}"`);
577637
continue;
578638
}
579-
const poll = metadata.get(targetId)?.poll;
639+
const poll = polls.get(targetId);
580640
if (poll === undefined) {
581-
if (!metadata.has(targetId) && asRecord(target.node["x-operation"])?.poll !== undefined) {
641+
if (
642+
!generated.includes(targetId) &&
643+
asRecord(target.node["x-operation"])?.poll !== undefined
644+
) {
582645
fail(
583-
`link "${linkName}" targets "${targetId}", which declares poll but is not generated`,
646+
`link "${linkName}" targets polled operation "${targetId}", which is neither generated nor listed in pollTargets`,
584647
);
585648
}
586649
continue;
@@ -626,11 +689,23 @@ const linkedWait = (
626689

627690
/** Derives the generated commands from each operation's `x-operation` and collects every lint failure. */
628691
export function deriveApiCommands(input: DeriveInput): DeriveResult {
629-
const { spec, generated, nouns, operationIdMap, handWrittenTopLevel } = input;
692+
const {
693+
spec,
694+
generated,
695+
pollTargets: pollTargetMap,
696+
nouns,
697+
operationIdMap,
698+
handWrittenTopLevel,
699+
} = input;
630700
const operations = collectOperations(spec);
631701
const problems: Array<string> = [];
632702
const metadata = readMetadata(operations, generated, problems);
633-
const commandNamed = new Set(metadata.keys());
703+
const polls = new Map<string, ApiCommandPoll>();
704+
for (const [operationId, name] of metadata) {
705+
if (name.poll !== undefined) polls.set(operationId, name.poll);
706+
}
707+
const hiddenPolls = readPollTargets(operations, generated, pollTargetMap, problems);
708+
for (const [operationId, poll] of hiddenPolls) polls.set(operationId, poll);
634709
const entries: Array<ApiCommandEntry> = [];
635710
const schemas: Record<string, ApiCommandSchemas> = {};
636711
const commandOwners = new Map<string, string>();
@@ -681,10 +756,7 @@ export function deriveApiCommands(input: DeriveInput): DeriveResult {
681756
fail(`command "${commandKey}" collides with the hand-written "${command[0]}" command`);
682757
}
683758

684-
const stability = asString(operation.node["x-scalar-stability"]);
685-
if (stability !== "experimental") {
686-
fail("only operations marked experimental can be generated (x-scalar-stability)");
687-
}
759+
requireExperimental(operation, fail);
688760
const summary = asString(operation.node.summary);
689761
if (summary === undefined) fail("operation has no summary");
690762

@@ -937,14 +1009,12 @@ export function deriveApiCommands(input: DeriveInput): DeriveResult {
9371009
});
9381010
}
9391011

940-
const linked = linkedWait(spec, operations, metadata, operation, fail);
1012+
const linked = linkedWait(spec, operations, polls, generated, operation, fail);
9411013
const ownPoll = name.poll;
942-
if (ownPoll !== undefined && linked !== undefined) {
943-
fail(`declares poll and also links to the polled operation "${linked.read}"; use one`);
944-
}
1014+
requireOneWait(ownPoll, linked, fail);
9451015
const wait = linked ?? (ownPoll === undefined ? undefined : pollWait(operationId, {}, ownPoll));
9461016
if (wait !== undefined) {
947-
lintWait({ spec, operations, commandNamed, operation, wait, fail });
1017+
lintWait({ spec, operations, operation, wait, fail });
9481018
}
9491019

9501020
const response = classifySuccess(spec, operation, fail);
@@ -1003,28 +1073,87 @@ export function deriveApiCommands(input: DeriveInput): DeriveResult {
10031073
entries.sort((left, right) =>
10041074
left.command.join(" ").localeCompare(right.command.join(" "), "en"),
10051075
);
1006-
return { entries, schemas, problems };
1076+
const pollTargets: Array<ApiPollTarget> = [];
1077+
const reached = new Set(
1078+
entries.flatMap((entry) => (entry.wait === undefined ? [] : [entry.wait.read])),
1079+
);
1080+
for (const [operationId, inspectId] of Object.entries(pollTargetMap).toSorted(([a], [b]) =>
1081+
a.localeCompare(b, "en"),
1082+
)) {
1083+
const operation = operations.get(operationId);
1084+
const poll = hiddenPolls.get(operationId);
1085+
if (operation === undefined || poll === undefined) continue;
1086+
const fail = (reason: string) => problems.push(`${operationId}: ${reason}`);
1087+
if (!reached.has(operationId)) {
1088+
fail("polling target is not linked from any generated operation; remove it from pollTargets");
1089+
continue;
1090+
}
1091+
const definition = operationIdMap[operationId];
1092+
if (definition === undefined) {
1093+
fail("polling target is missing from the typed client; regenerate @supabase/api");
1094+
continue;
1095+
}
1096+
requireExperimental(operation, fail);
1097+
requireOneWait(poll, linkedWait(spec, operations, polls, generated, operation, fail), fail);
1098+
const inspect = entries.find((entry) => entry.operationId === inspectId);
1099+
if (
1100+
inspect !== undefined &&
1101+
(inspect.argument !== undefined ||
1102+
inspect.body !== undefined ||
1103+
inspect.flags.some((flag) => flag.required))
1104+
) {
1105+
fail(`inspect operation "${inspectId}" needs input beyond the project ref`);
1106+
}
1107+
if (inspect !== undefined && !inspect.readOnly) {
1108+
fail(`inspect operation "${inspectId}" must be a read-only command`);
1109+
}
1110+
const summary = asString(operation.node.summary);
1111+
if (summary === undefined) {
1112+
fail("polling target is missing a summary");
1113+
continue;
1114+
}
1115+
const response = classifySuccess(spec, operation, fail);
1116+
if (response === undefined) continue;
1117+
if (response.kind !== "json") {
1118+
fail("polling target must return JSON");
1119+
continue;
1120+
}
1121+
schemas[operationId] = { result: withDialect(toJsonSchema(spec, response.schema, [], fail)) };
1122+
pollTargets.push({
1123+
operationId,
1124+
definition,
1125+
summary,
1126+
response: { status: response.status, kind: response.kind },
1127+
inspect: inspectId,
1128+
});
1129+
}
1130+
return { entries, pollTargets, schemas, problems };
10071131
}
10081132

10091133
function lintWait(input: {
10101134
readonly spec: JsonObject;
10111135
readonly operations: ReadonlyMap<string, Operation>;
1012-
readonly commandNamed: ReadonlySet<string>;
10131136
readonly operation: Operation;
10141137
readonly wait: ApiCommandWait;
10151138
readonly fail: (reason: string) => void;
10161139
}): void {
1017-
const { spec, operations, commandNamed, operation, wait, fail } = input;
1140+
const { spec, operations, operation, wait, fail } = input;
10181141
const readOperation = operations.get(wait.read);
10191142
if (readOperation === undefined) {
10201143
fail(`wait.read "${wait.read}" has no operation in the spec`);
10211144
return;
10221145
}
1023-
if (!commandNamed.has(wait.read)) {
1024-
fail(`wait.read "${wait.read}" is not a generated command`);
1146+
if (readOperation.method !== "GET") {
1147+
fail(`wait.read "${wait.read}" must use GET`);
1148+
}
1149+
if (readOperation.node.requestBody !== undefined) {
1150+
fail(`wait.read "${wait.read}" must not require a request body`);
10251151
}
10261152

10271153
const readParameters = operationParameters(spec, readOperation);
1154+
if (!readParameters.some((parameter) => parameter.in === "path" && parameter.name === "ref")) {
1155+
fail(`wait.read "${wait.read}" must be scoped by a project ref path parameter`);
1156+
}
10281157
const suppliable = new Set(
10291158
readParameters
10301159
.filter(
@@ -1052,7 +1181,7 @@ function lintWait(input: {
10521181
wait.read !== operation.id &&
10531182
parameter.required === true &&
10541183
parameterName !== undefined &&
1055-
parameterName !== "ref" &&
1184+
!(parameterName === "ref" && parameter.in === "path") &&
10561185
wait.params[parameterName] === undefined
10571186
) {
10581187
fail(`wait.params does not supply required parameter "${parameterName}" of ${wait.read}`);

0 commit comments

Comments
 (0)