@nasebanal/cli 0.12.0 → 0.14.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.
- package/README.md +283 -79
- package/dist/apis.js +2 -1
- package/dist/auth.js +75 -2
- package/dist/config.js +30 -0
- package/dist/index.js +12 -3
- package/dist/reports/monitors.js +57 -0
- package/dist/reports/upload.js +119 -252
- package/dist/spec/build.js +65 -12
- package/dist/specs/apiVersion.js +38 -0
- package/dist/specs/command.js +212 -13
- package/dist/specs/docs.js +73 -0
- package/dist/specs/pull.js +18 -13
- package/dist/specs/push.js +227 -16
- package/dist/specs/resolve.js +182 -0
- package/dist/specs/servers.js +88 -0
- package/dist/specs/settings.js +48 -0
- package/package.json +1 -1
- package/specs/account.yaml +90 -1
- package/specs/assurance.yaml +229 -1215
- package/specs/recorder.yaml +253 -1
- package/specs/specs.yaml +1587 -0
- package/specs/target.yaml +187 -1
package/dist/specs/pull.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* `nb specs pull` / `nb specs versions` - read a document back out of the API catalog (nb-specs-api), the other half of
|
|
3
|
-
* `nb specs push`: a pipeline asks the catalog for a given
|
|
3
|
+
* `nb specs push`: a pipeline asks the catalog for a given revision of an API's document and works from that (a contract
|
|
4
4
|
* test with Specmatic, a client generator) instead of from a copy that lives in some repository.
|
|
5
5
|
*
|
|
6
6
|
* - a spec is named by `--id` (what a rename cannot change) or by `--name` (matched ignoring case, like push)
|
|
7
|
-
* - `--spec-version` is the catalog's
|
|
8
|
-
* not the API's
|
|
7
|
+
* - `--spec-version` is the catalog's REVISION number (1, 2, 3...: one per `nb specs push` that changed the document),
|
|
8
|
+
* not the API's version (`v1`, `v2`: a property of the spec) and not the document's own `info.version`; absent or `latest` = the newest
|
|
9
9
|
* - the document comes back exactly as it was uploaded (the bytes, not a re-serialised copy), and its SHA-256 is
|
|
10
10
|
* checked against the `content_hash` the catalog keeps for that version; `--hash` pins what a pipeline expects, so
|
|
11
11
|
* a version that was swapped, or a name that now points somewhere else, fails instead of testing the wrong thing
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
import { ApiError, request } from "../http.js";
|
|
14
14
|
import { envTokenHint } from "../output.js";
|
|
15
15
|
import { findSpec, hashText, headersOf, reasonOf } from "./push.js";
|
|
16
|
+
import { locateSpec } from "./resolve.js";
|
|
16
17
|
/** `--spec-version`: a positive whole number, or `latest` (or nothing) for the newest. Returns undefined for the newest. */
|
|
17
18
|
export function parseVersion(raw) {
|
|
18
19
|
const v = raw?.trim();
|
|
@@ -20,7 +21,7 @@ export function parseVersion(raw) {
|
|
|
20
21
|
return undefined;
|
|
21
22
|
if (/^[1-9]\d{0,8}$/.test(v))
|
|
22
23
|
return Number(v);
|
|
23
|
-
throw new Error(`--spec-version '${raw}' must be a
|
|
24
|
+
throw new Error(`--spec-version '${raw}' must be a revision number (1, 2, 3...) or 'latest'. It is the catalog's revision, not the API's v1 / v2`);
|
|
24
25
|
}
|
|
25
26
|
/** `--hash`: 64 hex characters (case does not matter). Returns it lower-cased, or undefined when none was asked. */
|
|
26
27
|
export function parseHash(raw) {
|
|
@@ -44,9 +45,13 @@ async function refusal(what, run) {
|
|
|
44
45
|
throw err;
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
|
-
async function resolveSpec(conn, id, name) {
|
|
48
|
+
async function resolveSpec(conn, id, name, where) {
|
|
48
49
|
if (!id && !name?.trim())
|
|
49
50
|
throw new Error("Say which spec: --id <id> or --name <name> (the name is the spec's title in the catalog)");
|
|
51
|
+
if (!id && where && (where.group || where.repository || where.apiVersion)) {
|
|
52
|
+
const hit = await locateSpec(conn, { spec: name.trim(), ...where });
|
|
53
|
+
return { id: hit.spec.id, name: hit.spec.name, group_id: hit.spec.group_id ?? null, repository_id: hit.spec.repository_id ?? null, latest_version: 0 };
|
|
54
|
+
}
|
|
50
55
|
const spec = await findSpec(conn, name?.trim() ?? "", id);
|
|
51
56
|
if (!spec) {
|
|
52
57
|
throw new Error(`No spec named '${name}' in the catalog (names match ignoring case; check --org: the catalog is per owner, and 'nb specs versions' needs the exact name or the id).`);
|
|
@@ -57,23 +62,23 @@ async function listVersions(conn, spec) {
|
|
|
57
62
|
const res = await request({ ...headersOf(conn), method: "GET", path: `/api/v1/specs/${encodeURIComponent(spec.id)}/versions` });
|
|
58
63
|
return res.data.data.data;
|
|
59
64
|
}
|
|
60
|
-
/** The
|
|
65
|
+
/** The revisions of a spec, newest first, each with the SHA-256 of its document (what `--hash` pins). */
|
|
61
66
|
export async function specVersions(p) {
|
|
62
67
|
return refusal("the request", async () => {
|
|
63
|
-
const spec = await resolveSpec(p.conn, p.id, p.name);
|
|
68
|
+
const spec = await resolveSpec(p.conn, p.id, p.name, p.where);
|
|
64
69
|
return { id: spec.id, name: spec.name, versions: await listVersions(p.conn, spec) };
|
|
65
70
|
});
|
|
66
71
|
}
|
|
67
|
-
/** A
|
|
72
|
+
/** A revision's document exactly as uploaded, verified against the catalog's hash (and `--hash`). */
|
|
68
73
|
export async function pullSpec(p) {
|
|
69
74
|
return refusal("the request", async () => {
|
|
70
|
-
const spec = await resolveSpec(p.conn, p.id, p.name);
|
|
75
|
+
const spec = await resolveSpec(p.conn, p.id, p.name, p.where);
|
|
71
76
|
const versions = await listVersions(p.conn, spec);
|
|
72
77
|
if (versions.length === 0)
|
|
73
|
-
throw new Error(`Spec '${spec.name}' has no
|
|
78
|
+
throw new Error(`Spec '${spec.name}' has no revisions.`);
|
|
74
79
|
const picked = p.version === undefined ? versions[0] : versions.find((v) => v.version === p.version);
|
|
75
80
|
if (!picked) {
|
|
76
|
-
throw new Error(`Spec '${spec.name}' has no
|
|
81
|
+
throw new Error(`Spec '${spec.name}' has no revision ${p.version} (it has ${versions.map((v) => v.version).join(", ")}). The revision is the catalog's number: 'nb specs versions' lists them.`);
|
|
77
82
|
}
|
|
78
83
|
const res = await request({
|
|
79
84
|
...headersOf(p.conn),
|
|
@@ -85,11 +90,11 @@ export async function pullSpec(p) {
|
|
|
85
90
|
const hash = hashText(text);
|
|
86
91
|
// The catalog keeps the hash of what it stored: a document that does not match it was damaged on the way.
|
|
87
92
|
if (picked.content_hash && hash !== picked.content_hash) {
|
|
88
|
-
throw new Error(`The document of '${spec.name}'
|
|
93
|
+
throw new Error(`The document of '${spec.name}' rev ${picked.version} has SHA-256 ${hash}, not the ${picked.content_hash} the catalog lists for it: it did not arrive intact.`);
|
|
89
94
|
}
|
|
90
95
|
// And the one a pipeline pinned: the version, or what the name points at, is no longer what it expected.
|
|
91
96
|
if (p.hash && hash !== p.hash) {
|
|
92
|
-
throw new Error(`The document of '${spec.name}'
|
|
97
|
+
throw new Error(`The document of '${spec.name}' rev ${picked.version} has SHA-256 ${hash}, not the ${p.hash} you pinned with --hash: the revision was replaced, or the name now points at another spec.`);
|
|
93
98
|
}
|
|
94
99
|
return { id: spec.id, name: spec.name, version: picked.version, content_hash: hash, size_bytes: Buffer.byteLength(text, "utf8"), text };
|
|
95
100
|
});
|
package/dist/specs/push.js
CHANGED
|
@@ -3,12 +3,24 @@
|
|
|
3
3
|
* wants it: no group has to exist, nothing is uploaded when the file is already the latest version, and running it on
|
|
4
4
|
* every commit is safe.
|
|
5
5
|
*
|
|
6
|
-
* - the spec is found by
|
|
6
|
+
* - the spec is found by its key (or by `--id`): its group, its repository row, its API VERSION and its name; new -> created with
|
|
7
|
+
* its first revision (and the group, the repository and the spec are all made on the first run), known -> a new revision. The
|
|
8
|
+
* API version is what the document's `info.version` gives (1.8.0 -> v1; see apiVersion.ts), or `--api-version`: a document of
|
|
9
|
+
* another major makes (first-run semantics) the NEW spec for that API version and never touches the other one. The same name
|
|
10
|
+
* may exist in other groups (an environment each) and at other API versions; a group left out means "the specs in no group",
|
|
11
|
+
* not any group, and a name that exists only in groups is an error that lists them instead of silently making a second spec
|
|
7
12
|
* - the document's SHA-256 is compared with the latest version's `content_hash` BEFORE anything is sent: the same
|
|
8
13
|
* bytes mean "unchanged", and the 2 MiB stay where they are. (The server refuses an identical upload with
|
|
9
14
|
* 409 UNCHANGED too; that is treated as "unchanged", for two jobs racing.)
|
|
10
|
-
* - a group is optional: `--group <name>`
|
|
11
|
-
* exists is never moved by a push (moving is the catalog's job).
|
|
15
|
+
* - a group is optional: `--group <name>` is part of the key (the spec is looked up in that group) and files a NEW spec there
|
|
16
|
+
* (the group is made when missing); a spec that already exists is never moved by a push (moving is the catalog's job).
|
|
17
|
+
* - `--server-url <url>` replaces the document's top-level `servers` before it is sent (see servers.ts): one OpenAPI file in the
|
|
18
|
+
* repository, one URL per environment.
|
|
19
|
+
* - a repository is optional too: `--repository-id <id>` (or `$NB_REPOSITORY_ID`) ties the spec to that repository, and
|
|
20
|
+
* `--repository <name> [--component <name>]` (the key of a repository is provider + name + component; no component = the
|
|
21
|
+
* repository as a whole) registers the repository first (an idempotent upsert) and uses the id it returns. A NEW spec
|
|
22
|
+
* is created tied to it; an existing spec with no repository is tied to it; one tied to ANOTHER repository is left
|
|
23
|
+
* where it is and the result says so (a push never moves a spec).
|
|
12
24
|
*/
|
|
13
25
|
import { createHash } from "node:crypto";
|
|
14
26
|
import { existsSync, readFileSync, statSync } from "node:fs";
|
|
@@ -16,9 +28,12 @@ import { basename, extname } from "node:path";
|
|
|
16
28
|
import { parse } from "yaml";
|
|
17
29
|
import { ApiError, request } from "../http.js";
|
|
18
30
|
import { envTokenHint } from "../output.js";
|
|
31
|
+
import { apiVersionOf, checkApiVersion, sameApiVersion } from "./apiVersion.js";
|
|
32
|
+
import { checkServerUrl, setServers } from "./servers.js";
|
|
19
33
|
/** The catalog takes documents up to this size (2 MiB); the CLI refuses a larger one before sending anything. */
|
|
20
34
|
export const MAX_SPEC_BYTES = 2 * 1024 * 1024;
|
|
21
35
|
const MAX_NOTE = 500;
|
|
36
|
+
export const REPOSITORY_PROVIDERS = ["github", "gitlab", "bitbucket", "other"];
|
|
22
37
|
/** The catalog's host for the active environment (`NB_SPECS_BASE_URL` overrides; the CLI ships no spec for this API). */
|
|
23
38
|
export function specsBaseUrl(env) {
|
|
24
39
|
const override = process.env.NB_SPECS_BASE_URL?.replace(/\/$/, "");
|
|
@@ -82,7 +97,56 @@ export function reasonOf(err) {
|
|
|
82
97
|
return err.message;
|
|
83
98
|
}
|
|
84
99
|
const codeOf = (err) => (err instanceof ApiError ? err.data?.error?.code : undefined);
|
|
85
|
-
|
|
100
|
+
/** `--provider`: one of the four the catalog knows. */
|
|
101
|
+
export function checkProvider(raw) {
|
|
102
|
+
const v = (raw?.trim() || "github").toLowerCase();
|
|
103
|
+
if (REPOSITORY_PROVIDERS.includes(v))
|
|
104
|
+
return v;
|
|
105
|
+
throw new Error(`--provider '${raw}' must be one of: ${REPOSITORY_PROVIDERS.join(", ")}`);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Register a repository by provider and name and answer with it; the same pair always answers the same id, so it is safe
|
|
109
|
+
* on every run. `created` says whether this call made it (201) or it was already there (200). Never overwrites what is set.
|
|
110
|
+
*/
|
|
111
|
+
export async function upsertRepository(conn, provider, name, component = "", attachments) {
|
|
112
|
+
try {
|
|
113
|
+
const res = await request({ ...headersOf(conn), method: "PUT", path: "/api/v1/repositories", body: { provider, name, ...(component ? { component } : {}), ...(attachments !== undefined ? { attachments_enabled: attachments } : {}) } });
|
|
114
|
+
return { ...res.data.data, created: res.status === 201 };
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
if (err instanceof ApiError)
|
|
118
|
+
throw new Error(`The catalog refused the repository: ${reasonOf(err)}`);
|
|
119
|
+
throw err;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** A repository by provider and name without registering it (for a dry run); null when it is not registered. */
|
|
123
|
+
export async function findRepository(conn, provider, name, component = "") {
|
|
124
|
+
const res = await request({ ...headersOf(conn), method: "GET", path: "/api/v1/repositories" });
|
|
125
|
+
const list = res.data.data.data;
|
|
126
|
+
return list.find((r) => r.provider === provider && r.name.toLowerCase() === name.toLowerCase() && (r.component ?? "").toLowerCase() === component.toLowerCase()) ?? null;
|
|
127
|
+
}
|
|
128
|
+
/** The id of the repository a push should tie to: the id given, or the one `--repository` names (registered unless a dry run). */
|
|
129
|
+
async function repositoryIdFor(p) {
|
|
130
|
+
if (p.repositoryId)
|
|
131
|
+
return p.repositoryId;
|
|
132
|
+
if (!p.repository)
|
|
133
|
+
return null;
|
|
134
|
+
const provider = p.provider ?? "github";
|
|
135
|
+
if (p.dryRun)
|
|
136
|
+
return (await findRepository(p.conn, provider, p.repository, p.component))?.id ?? "(new)";
|
|
137
|
+
return (await upsertRepository(p.conn, provider, p.repository, p.component)).id;
|
|
138
|
+
}
|
|
139
|
+
/** Several specs carry this name (under different repositories) and no repository says which: nothing is picked silently. */
|
|
140
|
+
export class AmbiguousSpecError extends Error {
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* A spec by name, or by id. The same name may exist under different repositories, so a name is looked up inside the repository
|
|
144
|
+
* asked for (`scope.repositoryId`): that row's specs only (a spec with no repository is not adopted by a push that names one).
|
|
145
|
+
* With no repository asked, a spec with no repository wins; otherwise the name must match exactly one spec, or the error names
|
|
146
|
+
* the repositories that hold it and asks for --repository or --id. `scope.apiVersion` is part of the key: only the specs of that API
|
|
147
|
+
* version are looked at, so a name that exists at another API version is not a match (the push then makes the new spec).
|
|
148
|
+
*/
|
|
149
|
+
export async function findSpec(conn, name, id, scope) {
|
|
86
150
|
if (id) {
|
|
87
151
|
try {
|
|
88
152
|
const res = await request({ ...headersOf(conn), method: "GET", path: `/api/v1/specs/${encodeURIComponent(id)}` });
|
|
@@ -96,7 +160,43 @@ export async function findSpec(conn, name, id) {
|
|
|
96
160
|
}
|
|
97
161
|
const res = await request({ ...headersOf(conn), method: "GET", path: "/api/v1/specs" });
|
|
98
162
|
const list = res.data.data.data;
|
|
99
|
-
|
|
163
|
+
const named = list.filter((s) => s.name.toLowerCase() === name.toLowerCase() && (scope?.apiVersion === undefined || sameApiVersion(s.api_version ?? "v1", scope.apiVersion)));
|
|
164
|
+
const grouped = scope?.groupId !== undefined;
|
|
165
|
+
const pool = !grouped ? named : scope.groupId === "(new)" ? [] : named.filter((s) => (s.group_id ?? null) === scope.groupId);
|
|
166
|
+
let found;
|
|
167
|
+
if (scope?.repositoryId) {
|
|
168
|
+
found = scope.repositoryId === "(new)" ? null : (pool.find((s) => s.repository_id === scope.repositoryId) ?? null);
|
|
169
|
+
}
|
|
170
|
+
else if (pool.length <= 1) {
|
|
171
|
+
found = pool[0] ?? null;
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
const without = pool.filter((s) => !s.repository_id);
|
|
175
|
+
if (without.length > 0)
|
|
176
|
+
found = without[0];
|
|
177
|
+
else {
|
|
178
|
+
const rows = await repositoriesOf(conn);
|
|
179
|
+
const groups = (await request({ ...headersOf(conn), method: "GET", path: "/api/v1/groups" })).data.data.data;
|
|
180
|
+
const where = pool.map((s) => {
|
|
181
|
+
const r = rows.find((x) => x.id === s.repository_id);
|
|
182
|
+
const g = s.group_id ? `group '${groups.find((x) => x.id === s.group_id)?.name ?? s.group_id}'` : "no group";
|
|
183
|
+
return `${g} / ${r ? `${r.provider} ${r.name}${r.component ? ` / ${r.component}` : ""}` : `repository ${s.repository_id}`}`;
|
|
184
|
+
});
|
|
185
|
+
throw new AmbiguousSpecError(`More than one spec is named '${name}' (${where.join("; ")}): say which with --group, --repository [--component], or --id.`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (found || !grouped || scope.groupExplicit)
|
|
189
|
+
return found;
|
|
190
|
+
// No group was named and the name is not among the specs in no group: if it exists in groups, say so rather than make a second spec.
|
|
191
|
+
const others = scope?.repositoryId && scope.repositoryId !== "(new)" ? named.filter((s) => s.repository_id === scope.repositoryId) : scope?.repositoryId ? [] : named;
|
|
192
|
+
if (others.length === 0)
|
|
193
|
+
return null;
|
|
194
|
+
const groups = (await request({ ...headersOf(conn), method: "GET", path: "/api/v1/groups" })).data.data.data;
|
|
195
|
+
const where = others.map((s) => (s.group_id ? `group '${groups.find((g) => g.id === s.group_id)?.name ?? s.group_id}'` : "no group"));
|
|
196
|
+
throw new AmbiguousSpecError(`A spec named '${name}' exists in ${[...new Set(where)].join(", ")}, but not in the specs with no group: pass --group <name> (the group is part of the spec's key; leaving it out means no group) or --id.`);
|
|
197
|
+
}
|
|
198
|
+
async function repositoriesOf(conn) {
|
|
199
|
+
return (await request({ ...headersOf(conn), method: "GET", path: "/api/v1/repositories" })).data.data.data;
|
|
100
200
|
}
|
|
101
201
|
/** The id of the owner's group of that name, made when there is none. Refuses (with the catalog's reason) at the limit. */
|
|
102
202
|
async function groupIdFor(conn, name, create) {
|
|
@@ -126,14 +226,112 @@ export async function pushSpec(p) {
|
|
|
126
226
|
throw err;
|
|
127
227
|
}
|
|
128
228
|
}
|
|
229
|
+
/** The number in an API version ("v2" -> 2), or null when it is not of that form (a calendar version). */
|
|
230
|
+
function versionNumber(v) {
|
|
231
|
+
const m = /^[vV](\d{1,3})$/.exec(v.trim());
|
|
232
|
+
return m ? Number.parseInt(m[1], 10) : null;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* `--follow-version`: the spec of the document's API version in the family of the spec `p.id` names (its initial version, or any spec of the
|
|
236
|
+
* API: the family is found from it). Found: the push goes to that spec. Not there: it is made after the highest version the API has, with
|
|
237
|
+
* `initial_spec_id`, named as the initial spec is (the catalog keeps the group and the repository of the family); a version that is not the
|
|
238
|
+
* next one is refused here as the catalog refuses it (INITIAL_VERSION_NOT_NEXT), which is also what a dry run says.
|
|
239
|
+
*/
|
|
240
|
+
async function pushFollowingVersion(p) {
|
|
241
|
+
if (!p.id)
|
|
242
|
+
throw new Error("--follow-version needs --id: the id of the API's first spec");
|
|
243
|
+
let text = readSpecFile(p.file);
|
|
244
|
+
const explicitVersion = p.apiVersion !== undefined ? checkApiVersion(p.apiVersion) : undefined;
|
|
245
|
+
const apiVersion = explicitVersion ?? apiVersionOf(text) ?? undefined;
|
|
246
|
+
if (apiVersion === undefined) {
|
|
247
|
+
throw new Error("--follow-version needs the document's API version (a major such as 2.0.0 -> v2): say which with --api-version <v1|v2|...>");
|
|
248
|
+
}
|
|
249
|
+
const given = (await findSpec(p.conn, "", p.id));
|
|
250
|
+
const initialId = given.initial_spec_id ?? given.id;
|
|
251
|
+
const res = await request({ ...headersOf(p.conn), method: "GET", path: "/api/v1/specs" });
|
|
252
|
+
const family = res.data.data.data.filter((s) => s.id === initialId || s.initial_spec_id === initialId);
|
|
253
|
+
const initial = family.find((s) => s.id === initialId) ?? given;
|
|
254
|
+
const found = family.find((s) => sameApiVersion(s.api_version ?? "v1", apiVersion));
|
|
255
|
+
if (found)
|
|
256
|
+
return pushSpecInner({ ...p, followVersion: false, id: found.id });
|
|
257
|
+
// Not there yet: it must be the next one after the highest the API has.
|
|
258
|
+
const numbers = family.map((s) => versionNumber(s.api_version ?? "v1")).filter((n) => n !== null);
|
|
259
|
+
const wanted = versionNumber(apiVersion);
|
|
260
|
+
if (wanted !== null && numbers.length > 0 && wanted !== Math.max(...numbers) + 1) {
|
|
261
|
+
throw new Error(`The next API version of '${initial.name}' is v${Math.max(...numbers) + 1}, not ${apiVersion}: register the versions in order (a placeholder document will do for one that is not ready). Nothing was registered.`);
|
|
262
|
+
}
|
|
263
|
+
if (p.serverUrl !== undefined)
|
|
264
|
+
text = setServers(text, checkServerUrl(p.serverUrl)).text;
|
|
265
|
+
const hash = hashText(text);
|
|
266
|
+
const served = p.serverUrl ? { server_url: checkServerUrl(p.serverUrl) } : {};
|
|
267
|
+
if (p.dryRun) {
|
|
268
|
+
return { id: "(new)", name: initial.name, version: 1, api_version: apiVersion, group_id: initial.group_id, action: "created", content_hash: hash, repository_id: initial.repository_id ?? null, ...served };
|
|
269
|
+
}
|
|
270
|
+
try {
|
|
271
|
+
const created = await request({
|
|
272
|
+
...headersOf(p.conn),
|
|
273
|
+
method: "POST",
|
|
274
|
+
path: "/api/v1/specs",
|
|
275
|
+
body: { name: initial.name, content: text, initial_spec_id: initialId, ...(explicitVersion ? { api_version: explicitVersion } : {}), ...(p.description ? { description: p.description } : {}) },
|
|
276
|
+
});
|
|
277
|
+
const row = created.data.data;
|
|
278
|
+
return { id: row.id, name: row.name, version: 1, api_version: row.api_version ?? apiVersion, group_id: row.group_id ?? null, action: "created", content_hash: hash, repository_id: row.repository_id ?? null, ...served };
|
|
279
|
+
}
|
|
280
|
+
catch (err) {
|
|
281
|
+
throw new Error(`The catalog refused the spec: ${reasonOf(err)}`);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
129
284
|
async function pushSpecInner(p) {
|
|
130
|
-
|
|
285
|
+
if (p.followVersion)
|
|
286
|
+
return pushFollowingVersion(p);
|
|
287
|
+
let text = readSpecFile(p.file);
|
|
288
|
+
let serverUrl;
|
|
289
|
+
let serversEdit;
|
|
290
|
+
if (p.serverUrl !== undefined) {
|
|
291
|
+
serverUrl = checkServerUrl(p.serverUrl);
|
|
292
|
+
const edited = setServers(text, serverUrl);
|
|
293
|
+
text = edited.text;
|
|
294
|
+
serversEdit = edited.method;
|
|
295
|
+
}
|
|
131
296
|
const hash = hashText(text);
|
|
297
|
+
const served = serverUrl ? { server_url: serverUrl, servers_edit: serversEdit } : {};
|
|
132
298
|
const name = specNameFor(text, p.file, p.name);
|
|
133
299
|
const note = p.note !== undefined ? p.note.trim().slice(0, MAX_NOTE) || undefined : undefined;
|
|
300
|
+
// The API version is part of the key: asked for, else the document's own (info.version). A push by id needs neither: the spec has one.
|
|
301
|
+
const explicitVersion = p.apiVersion !== undefined ? checkApiVersion(p.apiVersion) : undefined;
|
|
302
|
+
const apiVersion = explicitVersion ?? apiVersionOf(text) ?? undefined;
|
|
303
|
+
if (!p.id && apiVersion === undefined) {
|
|
304
|
+
throw new Error(`The document's info.version gives no API version (a major such as 1.8.0 -> v1): say which with --api-version <v1|v2|...>, or push to a spec by --id`);
|
|
305
|
+
}
|
|
134
306
|
if (p.rename && !p.id)
|
|
135
307
|
throw new Error("--rename needs --id: without an id the spec is found by its name, so there is nothing to rename");
|
|
136
|
-
|
|
308
|
+
if (p.repositoryId && p.repository)
|
|
309
|
+
throw new Error("--repository-id and --repository name the same thing: pass one");
|
|
310
|
+
const wanted = await repositoryIdFor(p);
|
|
311
|
+
// The group is part of the key: looked up (not made) now, made only when a new spec is created below.
|
|
312
|
+
const lookedUpGroup = p.id ? undefined : p.group ? ((await groupIdFor(p.conn, p.group, false)) ?? "(new)") : null;
|
|
313
|
+
const scope = { repositoryId: wanted, ...(apiVersion !== undefined ? { apiVersion } : {}), ...(lookedUpGroup !== undefined ? { groupId: lookedUpGroup, groupExplicit: !!p.group } : {}) };
|
|
314
|
+
let existing = await findSpec(p.conn, name, p.id, scope);
|
|
315
|
+
let repositoryId = existing?.repository_id ?? null;
|
|
316
|
+
let repositoryKept;
|
|
317
|
+
if (existing && wanted && wanted !== "(new)") {
|
|
318
|
+
if (repositoryId === null) {
|
|
319
|
+
// A spec with no repository is tied to it; one tied to another is not moved.
|
|
320
|
+
if (!p.dryRun) {
|
|
321
|
+
try {
|
|
322
|
+
await request({ ...headersOf(p.conn), method: "PATCH", path: `/api/v1/specs/${encodeURIComponent(existing.id)}`, body: { repository_id: wanted } });
|
|
323
|
+
}
|
|
324
|
+
catch (err) {
|
|
325
|
+
throw new Error(`The catalog refused the repository link: ${reasonOf(err)}`);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
repositoryId = wanted;
|
|
329
|
+
}
|
|
330
|
+
else if (repositoryId !== wanted) {
|
|
331
|
+
repositoryKept = repositoryId;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
const linked = { repository_id: repositoryId, ...(repositoryKept ? { repository_kept: repositoryKept } : {}), ...served };
|
|
137
335
|
// A rename only on request: a spec named by hand in the catalog is not renamed back to the title on every push.
|
|
138
336
|
let renamedFrom;
|
|
139
337
|
if (existing && p.rename && existing.name !== name) {
|
|
@@ -154,10 +352,10 @@ async function pushSpecInner(p) {
|
|
|
154
352
|
const versions = await request({ ...headersOf(p.conn), method: "GET", path: `/api/v1/specs/${encodeURIComponent(existing.id)}/versions` });
|
|
155
353
|
const latest = versions.data.data.data[0];
|
|
156
354
|
if (latest?.content_hash === hash) {
|
|
157
|
-
return { id: existing.id, name: existing.name, version: latest.version, group_id: existing.group_id, action: "unchanged", content_hash: hash, ...renamed };
|
|
355
|
+
return { id: existing.id, name: existing.name, version: latest.version, api_version: existing.api_version ?? apiVersion ?? "v1", group_id: existing.group_id, action: "unchanged", content_hash: hash, ...linked, ...renamed };
|
|
158
356
|
}
|
|
159
357
|
if (p.dryRun)
|
|
160
|
-
return { id: existing.id, name: existing.name, version: existing.latest_version + 1, group_id: existing.group_id, action: "new_version", content_hash: hash, ...renamed };
|
|
358
|
+
return { id: existing.id, name: existing.name, version: existing.latest_version + 1, api_version: existing.api_version ?? apiVersion ?? "v1", group_id: existing.group_id, action: "new_version", content_hash: hash, ...linked, ...renamed };
|
|
161
359
|
try {
|
|
162
360
|
const res = await request({
|
|
163
361
|
...headersOf(p.conn),
|
|
@@ -166,19 +364,22 @@ async function pushSpecInner(p) {
|
|
|
166
364
|
body: { content: text, ...(note ? { note } : {}) },
|
|
167
365
|
});
|
|
168
366
|
const version = res.data.data.version;
|
|
169
|
-
return { id: existing.id, name: existing.name, version, group_id: existing.group_id, action: "new_version", content_hash: hash, ...renamed };
|
|
367
|
+
return { id: existing.id, name: existing.name, version, api_version: existing.api_version ?? apiVersion ?? "v1", group_id: existing.group_id, action: "new_version", content_hash: hash, ...linked, ...renamed };
|
|
170
368
|
}
|
|
171
369
|
catch (err) {
|
|
172
370
|
// Another job pushed the same document between our comparison and our upload: the catalog says so, and that is the goal.
|
|
173
371
|
if (codeOf(err) === "UNCHANGED")
|
|
174
|
-
return { id: existing.id, name: existing.name, version: existing.latest_version, group_id: existing.group_id, action: "unchanged", content_hash: hash, ...renamed };
|
|
175
|
-
|
|
372
|
+
return { id: existing.id, name: existing.name, version: existing.latest_version, api_version: existing.api_version ?? apiVersion ?? "v1", group_id: existing.group_id, action: "unchanged", content_hash: hash, ...linked, ...renamed };
|
|
373
|
+
if (codeOf(err) === "API_VERSION_MISMATCH") {
|
|
374
|
+
throw new Error(`This document belongs to another API version than '${existing.name}' (${existing.api_version ?? "?"}): the catalog keeps one spec per API version. Push it without --id, so the spec for its own API version is found or made. (${reasonOf(err)})`);
|
|
375
|
+
}
|
|
376
|
+
throw new Error(`The catalog refused the new revision: ${reasonOf(err)}`);
|
|
176
377
|
}
|
|
177
378
|
}
|
|
178
379
|
// A new spec. The group is by name and made on the way; none asked = in no group.
|
|
179
380
|
if (p.dryRun) {
|
|
180
381
|
const gid = p.group ? await groupIdFor(p.conn, p.group, false) : null;
|
|
181
|
-
return { id: "(new)", name, version: 1, group_id: gid, action: "created", content_hash: hash };
|
|
382
|
+
return { id: "(new)", name, version: 1, api_version: apiVersion ?? "v1", group_id: gid, action: "created", content_hash: hash, repository_id: wanted, ...served };
|
|
182
383
|
}
|
|
183
384
|
let groupId = null;
|
|
184
385
|
try {
|
|
@@ -188,18 +389,28 @@ async function pushSpecInner(p) {
|
|
|
188
389
|
...headersOf(p.conn),
|
|
189
390
|
method: "POST",
|
|
190
391
|
path: "/api/v1/specs",
|
|
191
|
-
body: {
|
|
392
|
+
body: {
|
|
393
|
+
name,
|
|
394
|
+
content: text,
|
|
395
|
+
...(groupId ? { group_id: groupId } : {}),
|
|
396
|
+
...(explicitVersion ? { api_version: explicitVersion } : {}),
|
|
397
|
+
...(wanted && wanted !== "(new)" ? { repository_id: wanted } : {}),
|
|
398
|
+
...(p.description ? { description: p.description } : {}),
|
|
399
|
+
},
|
|
192
400
|
});
|
|
193
401
|
const created = res.data.data;
|
|
194
|
-
return { id: created.id, name: created.name, version: 1, group_id: created.group_id ?? null, action: "created", content_hash: hash };
|
|
402
|
+
return { id: created.id, name: created.name, version: 1, api_version: created.api_version ?? apiVersion ?? "v1", group_id: created.group_id ?? null, action: "created", content_hash: hash, repository_id: created.repository_id ?? null, ...served };
|
|
195
403
|
}
|
|
196
404
|
catch (err) {
|
|
197
405
|
if (codeOf(err) === "NAME_TAKEN") {
|
|
198
406
|
// Two jobs creating the same spec: the loser pushes to the winner's.
|
|
199
|
-
const raced = await findSpec(p.conn, name);
|
|
407
|
+
const raced = await findSpec(p.conn, name, undefined, { ...scope, groupId: groupId ?? scope.groupId, groupExplicit: true });
|
|
200
408
|
if (raced)
|
|
201
409
|
return pushSpecInner({ ...p, id: raced.id });
|
|
202
410
|
}
|
|
411
|
+
if (codeOf(err) === "API_VERSION_REQUIRED") {
|
|
412
|
+
throw new Error(`The catalog could not tell this document's API version (its info.version has no major): pass --api-version <v1|v2|...>. (${reasonOf(err)})`);
|
|
413
|
+
}
|
|
203
414
|
throw new Error(`The catalog refused the spec: ${reasonOf(err)}`);
|
|
204
415
|
}
|
|
205
416
|
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which spec (and so which repository row) a CI step is about - the one name resolution every assurance command shares
|
|
3
|
+
* (results and external monitoring work on a SPEC that has a repository; the model is in
|
|
4
|
+
* docs/decisions/2026-11-09-repository-as-parent-of-specs.md of nb-apps, "Amendment 2").
|
|
5
|
+
*
|
|
6
|
+
* Two ways to say it, never mixed:
|
|
7
|
+
* - by id: `--spec-id <id>`
|
|
8
|
+
* - by names: `--repository <name> [--component <name>] [--group <name>] [--api-version <v>] --spec <name>` (provider defaults to
|
|
9
|
+
* github). The key of a spec is its group, its repository row (provider, name, component), its API VERSION (v1, v2) and its
|
|
10
|
+
* name: the same name may exist in other groups (an environment each), under other repositories and at other API versions.
|
|
11
|
+
* The component is optional: none = the repository as a whole. The group is optional too, and NOT a wildcard: none = the specs
|
|
12
|
+
* that are in no group. The API version is the one deliberate exception to "left out means none": left out, it is the API
|
|
13
|
+
* version of the one spec the rest of the key matches (and when the rest matches specs of several API versions the error
|
|
14
|
+
* lists them and asks for --api-version). On a miss the error lists where the name does exist and asks for --group /
|
|
15
|
+
* --repository / --component / --api-version / --spec-id.
|
|
16
|
+
*
|
|
17
|
+
* Nothing is ever created here (a result does not register a spec or a repository: that is `nb specs ...`). A spec with no
|
|
18
|
+
* repository is refused, because a result or a monitor is reported against the repository the spec is tied to.
|
|
19
|
+
*/
|
|
20
|
+
import { ApiError, request } from "../http.js";
|
|
21
|
+
import { checkApiVersion, sameApiVersion } from "./apiVersion.js";
|
|
22
|
+
import { checkProvider, headersOf, reasonOf } from "./push.js";
|
|
23
|
+
const label = (r) => `${r.provider} ${r.name}${r.component ? ` / ${r.component}` : ""}`;
|
|
24
|
+
async function get(conn, path) {
|
|
25
|
+
try {
|
|
26
|
+
return (await request({ ...headersOf(conn), method: "GET", path })).data.data;
|
|
27
|
+
}
|
|
28
|
+
catch (err) {
|
|
29
|
+
if (err instanceof ApiError && err.status === 404)
|
|
30
|
+
throw err;
|
|
31
|
+
if (err instanceof ApiError)
|
|
32
|
+
throw new Error(`The catalog refused the request: ${reasonOf(err)}`);
|
|
33
|
+
throw err;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const noRepository = (s) => new Error(`The spec '${s.name}' is tied to no repository: a result or a monitor is reported against the repository a spec is tied to. Tie it first (Spec Management on the screen, or 'nb specs specs update ${s.id} --data '{"repository_id":"<id>"}'').`);
|
|
37
|
+
async function groupsOf(conn) {
|
|
38
|
+
return (await get(conn, "/api/v1/groups")).data;
|
|
39
|
+
}
|
|
40
|
+
/** Where a spec reads in an error: `group 'prod' / github acme/api / web / v2`. */
|
|
41
|
+
function whereOf(s, groups, repositories) {
|
|
42
|
+
const g = s.group_id ? `group '${groups.find((x) => x.id === s.group_id)?.name ?? s.group_id}'` : "no group";
|
|
43
|
+
const r = s.repository_id ? (repositories.find((x) => x.id === s.repository_id) ? label(repositories.find((x) => x.id === s.repository_id)) : `repository ${s.repository_id}`) : "no repository";
|
|
44
|
+
return `${g} / ${r} / ${s.api_version ?? "v1"}`;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A spec by its key, without requiring a repository (a spec description document or a pull works on a spec with none): `--spec-id`,
|
|
48
|
+
* or `--spec <name>` with the optional `--repository` [`--component`] `--provider` and `--group`. Whatever is not given is "none":
|
|
49
|
+
* no repository = a spec tied to no repository, no group = a spec in no group. Nothing is created.
|
|
50
|
+
*/
|
|
51
|
+
export async function locateSpec(conn, ref) {
|
|
52
|
+
const byId = ref.specId?.trim() || undefined;
|
|
53
|
+
const specName = ref.spec?.trim() || undefined;
|
|
54
|
+
const repositoryName = ref.repository?.trim() || undefined;
|
|
55
|
+
const groupName = ref.group?.trim() || undefined;
|
|
56
|
+
const component = ref.component?.trim() ?? "";
|
|
57
|
+
const apiVersion = ref.apiVersion?.trim() ? checkApiVersion(ref.apiVersion) : undefined;
|
|
58
|
+
if (byId && (repositoryName || specName || component || groupName || apiVersion)) {
|
|
59
|
+
throw new Error("--spec-id names the spec by itself: do not combine it with --repository, --component, --group, --api-version or --spec");
|
|
60
|
+
}
|
|
61
|
+
if (!byId && !specName)
|
|
62
|
+
throw new Error("Say which spec: --spec-id <id>, or --spec <name> [--repository <name> [--component <name>]] [--group <name>] [--api-version <v>]");
|
|
63
|
+
if (!byId && !repositoryName && (component !== "" || ref.provider))
|
|
64
|
+
throw new Error("--component and --provider need --repository");
|
|
65
|
+
if (byId) {
|
|
66
|
+
let spec;
|
|
67
|
+
try {
|
|
68
|
+
spec = await get(conn, `/api/v1/specs/${encodeURIComponent(byId)}`);
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
if (err instanceof ApiError)
|
|
72
|
+
throw new Error(`No spec with id '${byId}' in the catalog (check --spec-id, and --org: the catalog is per owner).`);
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
return { spec, repository: await repositoryOfSpec(conn, spec) };
|
|
76
|
+
}
|
|
77
|
+
if (repositoryName)
|
|
78
|
+
checkProvider(ref.provider); // refused before anything is sent
|
|
79
|
+
// The groups are read only when one is named (to turn its name into an id); an error reads them best-effort, to name them.
|
|
80
|
+
const groups = groupName ? await groupsOf(conn) : [];
|
|
81
|
+
let groupId = null;
|
|
82
|
+
if (groupName) {
|
|
83
|
+
const g = groups.find((x) => x.name.toLowerCase() === groupName.toLowerCase());
|
|
84
|
+
if (!g)
|
|
85
|
+
throw new Error(`No group named '${groupName}' in the catalog${groups.length > 0 ? ` (groups: ${groups.map((x) => x.name).join(", ")})` : ""}.`);
|
|
86
|
+
groupId = g.id;
|
|
87
|
+
}
|
|
88
|
+
const repositories = (await get(conn, "/api/v1/repositories")).data;
|
|
89
|
+
let repository = null;
|
|
90
|
+
if (repositoryName) {
|
|
91
|
+
const provider = checkProvider(ref.provider);
|
|
92
|
+
repository = repositories.find((r) => r.provider === provider && r.name.toLowerCase() === repositoryName.toLowerCase() && (r.component ?? "").toLowerCase() === component.toLowerCase()) ?? null;
|
|
93
|
+
if (!repository) {
|
|
94
|
+
throw new Error(`No repository ${label({ provider, name: repositoryName, component })} in the catalog (nothing is created here: 'nb specs repositories upsert'; check --provider and --component, which are part of the key).`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const specs = (await get(conn, "/api/v1/specs")).data;
|
|
98
|
+
const named = specs.filter((s) => s.name.toLowerCase() === specName.toLowerCase());
|
|
99
|
+
if (named.length === 0)
|
|
100
|
+
throw new Error(`No spec named '${specName}' in the catalog (nothing is created here: 'nb specs specs push').`);
|
|
101
|
+
const here = named.filter((s) => (s.group_id ?? null) === groupId && (s.repository_id ?? null) === (repository?.id ?? null));
|
|
102
|
+
// The API version is part of the key; left out, it is the one the rest of the key leaves (several: the error lists them).
|
|
103
|
+
const hits = apiVersion === undefined ? here : here.filter((s) => sameApiVersion(s.api_version ?? "v1", apiVersion));
|
|
104
|
+
if (hits.length > 1) {
|
|
105
|
+
throw new Error(`'${specName}' exists at more than one API version in ${groupName ? `group '${groupName}'` : "no group"} / ${repository ? label(repository) : "no repository"}: ${hits.map((s) => s.api_version ?? "v1").join(", ")}. ` +
|
|
106
|
+
"Say which with --api-version (the API version is part of the spec's key; unlike the group and the component, leaving it out is not 'none').");
|
|
107
|
+
}
|
|
108
|
+
const hit = hits[0];
|
|
109
|
+
if (!hit) {
|
|
110
|
+
const asked = `${groupName ? `group '${groupName}'` : "no group"} / ${repository ? label(repository) : "no repository"}${apiVersion ? ` / ${apiVersion}` : ""}`;
|
|
111
|
+
const known = groups.length > 0 ? groups : await groupsOf(conn).catch(() => []);
|
|
112
|
+
throw new Error(`No spec named '${specName}' in ${asked}. The name exists in: ${named.map((s) => whereOf(s, known, repositories)).join("; ")}. ` +
|
|
113
|
+
"Say which with --group, --repository (and --component), --api-version, or use --spec-id (a group or repository you leave out means none, not any).");
|
|
114
|
+
}
|
|
115
|
+
return { spec: hit, repository };
|
|
116
|
+
}
|
|
117
|
+
async function repositoryOfSpec(conn, spec) {
|
|
118
|
+
if (!spec.repository_id)
|
|
119
|
+
return null;
|
|
120
|
+
try {
|
|
121
|
+
return await get(conn, `/api/v1/repositories/${encodeURIComponent(spec.repository_id)}`);
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
if (err instanceof ApiError)
|
|
125
|
+
throw new Error(`The spec '${spec.name}' points at repository '${spec.repository_id}', which the catalog does not have.`);
|
|
126
|
+
throw err;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export async function resolveSpec(conn, ref) {
|
|
130
|
+
const byId = ref.specId?.trim() || undefined;
|
|
131
|
+
const repositoryName = ref.repository?.trim() || undefined;
|
|
132
|
+
const specName = ref.spec?.trim() || undefined;
|
|
133
|
+
const groupName = ref.group?.trim() || undefined;
|
|
134
|
+
const component = ref.component?.trim() ?? "";
|
|
135
|
+
const apiVersionAsked = !!ref.apiVersion?.trim();
|
|
136
|
+
if (byId && (repositoryName || specName || component || groupName || apiVersionAsked)) {
|
|
137
|
+
throw new Error("--spec-id names the spec by itself: do not combine it with --repository, --component, --group, --api-version or --spec");
|
|
138
|
+
}
|
|
139
|
+
if (!byId && !repositoryName && !specName && component === "" && !groupName && !apiVersionAsked) {
|
|
140
|
+
throw new Error("Say which spec: --spec-id <id>, or --repository <name> [--component <name>] [--group <name>] [--api-version <v>] --spec <name>");
|
|
141
|
+
}
|
|
142
|
+
if (!byId && !repositoryName)
|
|
143
|
+
throw new Error("--repository is required with --spec (--component is optional: none = the repository as a whole; --group too: none = no group)");
|
|
144
|
+
if (!byId && !specName)
|
|
145
|
+
throw new Error("--spec is required with --repository");
|
|
146
|
+
const { spec, repository } = await locateSpec(conn, ref);
|
|
147
|
+
if (!repository)
|
|
148
|
+
throw noRepository(spec);
|
|
149
|
+
return { spec_id: spec.id, spec_name: spec.name, repository_id: repository.id, repository };
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Every spec tied to one repository row AND one API version, whatever its group (`--all-specs`: a result that belongs to the source,
|
|
153
|
+
* such as a unit test run, is recorded for each environment's spec of that repository at that API version: a `release/v1` branch
|
|
154
|
+
* does not feed the v2 specs). Names only: `--repository` [`--component`] [`--provider`] [`--api-version`]. Without `--api-version`
|
|
155
|
+
* it is the API version the repository row has, when it has one; with specs of several, the error lists them and asks.
|
|
156
|
+
*/
|
|
157
|
+
export async function resolveRepositorySpecs(conn, ref) {
|
|
158
|
+
const repositoryName = ref.repository?.trim();
|
|
159
|
+
if (!repositoryName)
|
|
160
|
+
throw new Error("--all-specs needs --repository <name> [--component <name>]");
|
|
161
|
+
const component = ref.component?.trim() ?? "";
|
|
162
|
+
const provider = checkProvider(ref.provider);
|
|
163
|
+
const repositories = (await get(conn, "/api/v1/repositories")).data;
|
|
164
|
+
const repository = repositories.find((r) => r.provider === provider && r.name.toLowerCase() === repositoryName.toLowerCase() && (r.component ?? "").toLowerCase() === component.toLowerCase());
|
|
165
|
+
if (!repository)
|
|
166
|
+
throw new Error(`No repository ${label({ provider, name: repositoryName, component })} in the catalog (nothing is created here: 'nb specs repositories upsert').`);
|
|
167
|
+
const all = (await get(conn, "/api/v1/specs")).data.filter((s) => s.repository_id === repository.id);
|
|
168
|
+
if (all.length === 0)
|
|
169
|
+
throw new Error(`No spec is tied to ${label(repository)}: register one ('nb specs specs push --repository ...') before reporting results.`);
|
|
170
|
+
const versions = [...new Set(all.map((s) => (s.api_version ?? "v1").toLowerCase()))].sort();
|
|
171
|
+
const asked = ref.apiVersion?.trim() ? checkApiVersion(ref.apiVersion) : undefined;
|
|
172
|
+
if (asked === undefined && versions.length > 1) {
|
|
173
|
+
throw new Error(`${label(repository)} has specs at several API versions (${versions.join(", ")}): say which one gets the result with --api-version (a release branch feeds its own API version only).`);
|
|
174
|
+
}
|
|
175
|
+
const specs = asked === undefined ? all : all.filter((s) => sameApiVersion(s.api_version ?? "v1", asked));
|
|
176
|
+
if (specs.length === 0)
|
|
177
|
+
throw new Error(`No spec of ${label(repository)} is at API version '${asked}' (it has: ${versions.join(", ")}).`);
|
|
178
|
+
const groups = await groupsOf(conn).catch(() => []);
|
|
179
|
+
return [...specs]
|
|
180
|
+
.sort((a, b) => (groups.find((g) => g.id === a.group_id)?.name ?? "").localeCompare(groups.find((g) => g.id === b.group_id)?.name ?? "") || a.name.localeCompare(b.name))
|
|
181
|
+
.map((s) => ({ spec_id: s.id, spec_name: s.name, repository_id: repository.id, repository }));
|
|
182
|
+
}
|