@nasebanal/cli 0.8.0 → 0.10.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 +35 -0
- package/dist/http.js +1 -1
- package/dist/reports/ci.js +3 -0
- package/dist/reports/upload.js +49 -3
- package/dist/specs/command.js +63 -1
- package/dist/specs/pull.js +96 -0
- package/dist/specs/push.js +3 -3
- package/package.json +1 -1
- package/specs/account.yaml +13 -2
- package/specs/assurance.yaml +24 -3
package/README.md
CHANGED
|
@@ -92,6 +92,10 @@ nb assurance report upload --dir locust/logs/20261001_090000
|
|
|
92
92
|
relative to the repository root, so Assurance can link to the file at that commit.
|
|
93
93
|
A report that CI has just generated is not in the commit, so no path is sent;
|
|
94
94
|
override with `--report-path`.
|
|
95
|
+
The repository's web address is sent too (`repo_url`, detected from `GITHUB_SERVER_URL` /
|
|
96
|
+
`CI_PROJECT_URL`; override with `--repo-url`), so the link also works on a self-hosted
|
|
97
|
+
GitLab or GitHub Enterprise. The CI's repository also replaces the project's `repo`
|
|
98
|
+
(the latest sender wins).
|
|
95
99
|
- **Tie the project to its API in the catalog (`--spec-id`, `--spec-environment`):**
|
|
96
100
|
`--spec-id <id>` links the project to a spec of the API catalog (its id in Assurance /
|
|
97
101
|
Specs), `--spec-environment production` says which environment of it the project covers
|
|
@@ -161,6 +165,37 @@ It is made to run on **every commit**:
|
|
|
161
165
|
happen. `NB_SPECS_BASE_URL` points it at another host (local: `http://localhost:8792`).
|
|
162
166
|
- In GitHub Actions the spec's id is written to `$GITHUB_OUTPUT` as `spec_id`.
|
|
163
167
|
|
|
168
|
+
### Reading a version back (`nb specs pull`, `nb specs versions`)
|
|
169
|
+
|
|
170
|
+
A pipeline can work from the catalog's document instead of a copy kept in a repository (a
|
|
171
|
+
contract test with Specmatic against the API's provider side, a stub for a consumer, a client
|
|
172
|
+
generator):
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
nb specs versions --name "Billing API" # versions newest first, each with its sha256
|
|
176
|
+
nb specs pull --name "Billing API" --spec-version 3 --out spec.yaml
|
|
177
|
+
nb specs pull --id kMsl4TXEMmeN --spec-version 3 --hash <sha256> --out spec.yaml # pinned: fails if it differs
|
|
178
|
+
nb specs pull --name "Billing API" > spec.yaml # latest; the document is all of stdout
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
- **`--spec-version` is the catalog's version number** (1, 2, 3...: one per `nb specs push` that
|
|
182
|
+
changed the document). It is not the API's URL version (`v1`, `v2`, the label of a test result
|
|
183
|
+
in `nb assurance report upload --api-version`), not the document's own `info.version`, and not
|
|
184
|
+
the CLI's own `nb --version`.
|
|
185
|
+
Absent or `latest` is the newest; **pin a number in a pipeline**, `latest` moves.
|
|
186
|
+
- **The document comes back exactly as it was uploaded** (the bytes: a JSON document is not
|
|
187
|
+
parsed and re-printed), and its SHA-256 is checked against the `content_hash` the catalog keeps
|
|
188
|
+
for that version, so a document that did not arrive intact fails. `--hash <sha256>` pins what
|
|
189
|
+
the pipeline expects, so a version that was replaced, or a name that now points at another
|
|
190
|
+
spec, fails instead of testing the wrong thing. `nb specs versions` prints the hashes.
|
|
191
|
+
- A spec is named by `--id` (a rename cannot change it) or by `--name` (matched ignoring case).
|
|
192
|
+
`--org <id|personal>` / `$NB_SPECS_ORG` and `NB_SPECS_BASE_URL` work as for `push`. With `--out`
|
|
193
|
+
the details are printed as JSON; without it they go to stderr. In GitHub Actions `spec_id`,
|
|
194
|
+
`spec_version` and `spec_hash` are written to `$GITHUB_OUTPUT`.
|
|
195
|
+
- The catalog has no "released" mark yet: unlike the `@nasebanal/api-specs-*` packages, which
|
|
196
|
+
only advance once the provider has deployed, a pull of `latest` can return a version that
|
|
197
|
+
nothing implements. Pinning a version (and its hash) is the safe way to use it today.
|
|
198
|
+
|
|
164
199
|
## Installation
|
|
165
200
|
|
|
166
201
|
The CLI installs the `nb` command. Node ≥ 20 is required.
|
package/dist/http.js
CHANGED
package/dist/reports/ci.js
CHANGED
|
@@ -61,6 +61,7 @@ export function detectCi(env, git = readGit, now = new Date()) {
|
|
|
61
61
|
ciUrl: repo && runId ? `${server}/${repo}/actions/runs/${runId}` : null,
|
|
62
62
|
project: repo,
|
|
63
63
|
repo,
|
|
64
|
+
repoUrl: repo ? `${server.replace(/\/+$/, "")}/${repo}` : null,
|
|
64
65
|
};
|
|
65
66
|
}
|
|
66
67
|
if (env.GITLAB_CI === "true") {
|
|
@@ -74,6 +75,7 @@ export function detectCi(env, git = readGit, now = new Date()) {
|
|
|
74
75
|
ciUrl: nonEmpty(env.CI_PIPELINE_URL),
|
|
75
76
|
project: path,
|
|
76
77
|
repo: path && host && host !== "gitlab.com" ? `${host}/${path}` : path,
|
|
78
|
+
repoUrl: nonEmpty(env.CI_PROJECT_URL) ?? (path && host ? `https://${host}/${path}` : null),
|
|
77
79
|
};
|
|
78
80
|
}
|
|
79
81
|
const { sha, branch } = git();
|
|
@@ -86,5 +88,6 @@ export function detectCi(env, git = readGit, now = new Date()) {
|
|
|
86
88
|
ciUrl: null,
|
|
87
89
|
project: null,
|
|
88
90
|
repo: null,
|
|
91
|
+
repoUrl: null,
|
|
89
92
|
};
|
|
90
93
|
}
|
package/dist/reports/upload.js
CHANGED
|
@@ -60,6 +60,20 @@ export function checkProvider(raw) {
|
|
|
60
60
|
throw new Error(`--provider must be one of ${RUN_PROVIDERS.join(" | ")}`);
|
|
61
61
|
return v;
|
|
62
62
|
}
|
|
63
|
+
/** `--repo-url`: an http(s) address without credentials, query or fragment, as the API accepts it. */
|
|
64
|
+
export function checkRepoUrl(v) {
|
|
65
|
+
let u;
|
|
66
|
+
try {
|
|
67
|
+
u = new URL(v);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
throw new Error("--repo-url must be an http(s) address, e.g. https://gitlab.example.com/group/app");
|
|
71
|
+
}
|
|
72
|
+
if ((u.protocol !== "https:" && u.protocol !== "http:") || u.username || u.password || u.search || u.hash) {
|
|
73
|
+
throw new Error("--repo-url must be an http(s) address without credentials, query or fragment");
|
|
74
|
+
}
|
|
75
|
+
return `${u.origin}${u.pathname.replace(/\/+$/, "")}`;
|
|
76
|
+
}
|
|
63
77
|
/** `--report-path`: a path relative to the repository root, as the API accepts it. */
|
|
64
78
|
export function checkReportPath(raw) {
|
|
65
79
|
const v = raw.trim();
|
|
@@ -218,7 +232,7 @@ export async function ensureProject(conn, name, repo, create, spec) {
|
|
|
218
232
|
const res = await request({ ...conn, method: "GET", path: "/api/v1/projects", query: { name } });
|
|
219
233
|
const list = res.data.data.data;
|
|
220
234
|
const hit = list[0];
|
|
221
|
-
return hit ? { id: hit.id, created: false, spec: { id: hit.spec_id ?? null, environment: hit.spec_environment ?? null } } : null;
|
|
235
|
+
return hit ? { id: hit.id, created: false, repo: hit.repo, spec: { id: hit.spec_id ?? null, environment: hit.spec_environment ?? null } } : null;
|
|
222
236
|
};
|
|
223
237
|
const existing = await find();
|
|
224
238
|
if (existing)
|
|
@@ -234,7 +248,7 @@ export async function ensureProject(conn, name, repo, create, spec) {
|
|
|
234
248
|
// A new project is born linked: nothing to compare, nothing to patch afterwards.
|
|
235
249
|
body: { name, ...(repo ? { repo } : {}), ...(spec ? { spec_id: spec.id, ...(spec.environment ? { spec_environment: spec.environment } : {}) } : {}) },
|
|
236
250
|
});
|
|
237
|
-
return { id: res.data.data.id, created: true, spec: spec ? { id: spec.id, environment: spec.environment ?? null } : undefined, specLinked: !!spec };
|
|
251
|
+
return { id: res.data.data.id, created: true, repo, spec: spec ? { id: spec.id, environment: spec.environment ?? null } : undefined, specLinked: !!spec };
|
|
238
252
|
}
|
|
239
253
|
catch (err) {
|
|
240
254
|
// Another job of the same pipeline created it between our lookup and our create.
|
|
@@ -246,6 +260,21 @@ export async function ensureProject(conn, name, repo, create, spec) {
|
|
|
246
260
|
throw err;
|
|
247
261
|
}
|
|
248
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* The repository the CI reports is the project's repository: the latest sender wins, whether the old value was typed in
|
|
265
|
+
* or came from an earlier upload. (To follow another repository, use another project.) Only a difference is written.
|
|
266
|
+
*/
|
|
267
|
+
async function syncRepo(conn, project, repo) {
|
|
268
|
+
let current = project.repo;
|
|
269
|
+
if (current === undefined) {
|
|
270
|
+
const res = await request({ ...conn, method: "GET", path: `/api/v1/projects/${encodeURIComponent(project.id)}` });
|
|
271
|
+
current = res.data.data.repo ?? null;
|
|
272
|
+
}
|
|
273
|
+
if (current === repo)
|
|
274
|
+
return { changed: false, was: current };
|
|
275
|
+
await request({ ...conn, method: "PATCH", path: `/api/v1/projects/${encodeURIComponent(project.id)}`, body: { repo } });
|
|
276
|
+
return { changed: true, was: current ?? null };
|
|
277
|
+
}
|
|
249
278
|
/**
|
|
250
279
|
* Bring the project's link to the catalog in line with what the CI says. Only what differs is written, so a pipeline that
|
|
251
280
|
* runs on every commit leaves the project untouched once it is linked. Pointing at another spec clears the environment
|
|
@@ -292,6 +321,17 @@ export async function uploadReport(p) {
|
|
|
292
321
|
}
|
|
293
322
|
const data = res.data.data;
|
|
294
323
|
const done = { project_id: project.id, project_created: project.created, ...data };
|
|
324
|
+
if (p.repo) {
|
|
325
|
+
try {
|
|
326
|
+
const synced = await syncRepo(p.conn, project, p.repo);
|
|
327
|
+
if (synced.changed)
|
|
328
|
+
done.repo_changed_from = synced.was;
|
|
329
|
+
}
|
|
330
|
+
catch (err) {
|
|
331
|
+
const detail = err instanceof ApiError ? (err.data?.error?.message ?? err.message) : err.message;
|
|
332
|
+
throw new Error(`The result was reported, but the project's repository could not be updated: ${detail}`);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
295
335
|
if (p.spec) {
|
|
296
336
|
// The result is in; a link that fails now leaves it as it is and says so.
|
|
297
337
|
try {
|
|
@@ -426,7 +466,8 @@ export function buildReportUploadCommand() {
|
|
|
426
466
|
.option("--spec-org <id>", "with --spec-file: the organization whose catalog it is (an id), or 'personal' (default; or $NB_SPECS_ORG)")
|
|
427
467
|
.option("--spec-environment <name>", "which environment of that API the project covers (production, staging, ...); needs --spec-id")
|
|
428
468
|
.option("--project-id <id>", "send to this project by id (or $NB_ASSURANCE_PROJECT_ID): no lookup by name and nothing is created, so a rename never moves the results; recommended for a pipeline once the project exists")
|
|
429
|
-
.option("--repo <repo>", "repository recorded on
|
|
469
|
+
.option("--repo <repo>", "repository recorded on the project; the latest sender wins (CI: detected)")
|
|
470
|
+
.option("--repo-url <url>", "the repository's web address on its host, e.g. https://gitlab.example.com/group/app (CI: detected); lets Evolution link to the report on a self-hosted GitLab / GitHub Enterprise")
|
|
430
471
|
.option("--provider <provider>", `the CI provider: ${RUN_PROVIDERS.join(" | ")} (CI: detected)`)
|
|
431
472
|
.option("--report-path <path>", "the report file's path in the repository, relative to its root (default: detected, and only for a file committed at the reported commit)")
|
|
432
473
|
.option("--run-key <key>", "identity of the pipeline run (CI: detected); all jobs of a run share it")
|
|
@@ -513,6 +554,7 @@ export function buildReportUploadCommand() {
|
|
|
513
554
|
const branch = opts.branch ?? ci.branch;
|
|
514
555
|
const ciUrl = opts.ciUrl ?? ci.ciUrl;
|
|
515
556
|
const provider = opts.provider !== undefined ? checkProvider(opts.provider) : ci.provider === "local" ? undefined : ci.provider;
|
|
557
|
+
const repoUrl = opts.repoUrl !== undefined ? checkRepoUrl(opts.repoUrl) : ci.repoUrl;
|
|
516
558
|
const reportPath = opts.reportPath !== undefined ? checkReportPath(opts.reportPath) : readReportPath(file, commit);
|
|
517
559
|
if (reportPath)
|
|
518
560
|
parsed.suite.report_path = reportPath;
|
|
@@ -523,6 +565,7 @@ export function buildReportUploadCommand() {
|
|
|
523
565
|
...(branch ? { branch } : {}),
|
|
524
566
|
...(ciUrl ? { ci_url: ciUrl } : {}),
|
|
525
567
|
...(provider ? { provider } : {}),
|
|
568
|
+
...(repoUrl ? { repo_url: repoUrl } : {}),
|
|
526
569
|
},
|
|
527
570
|
suite: parsed.suite,
|
|
528
571
|
};
|
|
@@ -575,6 +618,9 @@ export function buildReportUploadCommand() {
|
|
|
575
618
|
`${attachment ? `, attached ${attachment.name} (${(attachment.bytes.length / 1024).toFixed(0)} KiB)` : ""}` +
|
|
576
619
|
`${pushed ? `, spec ${pushed.name} v${pushed.version} (${pushed.action.replace("_", " ")})` : ""}` +
|
|
577
620
|
`${result.spec ? `, catalog ${result.spec.id}${result.spec.environment ? ` (${result.spec.environment})` : ""} ${result.spec.changed ? (result.spec.was ? `(changed from ${result.spec.was})` : "(linked)") : "(already linked)"}` : ""}\n`);
|
|
621
|
+
if (result.repo_changed_from !== undefined) {
|
|
622
|
+
process.stderr.write(` Repository of the project set to ${repo} (was ${result.repo_changed_from ?? "not set"}); the latest sender wins\n`);
|
|
623
|
+
}
|
|
578
624
|
if (!projectId) {
|
|
579
625
|
process.stderr.write(` Pin it so a rename or a typo cannot send results elsewhere: --project-id ${result.project_id} (or NB_ASSURANCE_PROJECT_ID=${result.project_id})\n`);
|
|
580
626
|
}
|
package/dist/specs/command.js
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
* `nb specs` - the API catalog (your own OpenAPI documents) from the command line. Hand-written: the CLI's generated
|
|
3
3
|
* command tree comes from the pinned contracts, and the catalog's is not among them.
|
|
4
4
|
*/
|
|
5
|
+
import { writeFileSync } from "node:fs";
|
|
5
6
|
import { Command } from "commander";
|
|
6
7
|
import { resolveAccessToken, resolveEnv } from "../config.js";
|
|
7
8
|
import { exportGithubOutput, printJson } from "../output.js";
|
|
8
9
|
import { detectCi } from "../reports/ci.js";
|
|
10
|
+
import { parseHash, parseVersion, pullSpec, specVersions } from "./pull.js";
|
|
9
11
|
import { checkOrg, ciNote, pushSpec, specsBaseUrl } from "./push.js";
|
|
10
12
|
export function buildPushCommand() {
|
|
11
13
|
return new Command("push")
|
|
@@ -39,6 +41,66 @@ export function buildPushCommand() {
|
|
|
39
41
|
process.stderr.write(`${opts.dryRun ? "(dry run) " : ""}✓ ${result.name} -> ${what}\n`);
|
|
40
42
|
});
|
|
41
43
|
}
|
|
44
|
+
async function connection(opts) {
|
|
45
|
+
const env = opts.env || resolveEnv();
|
|
46
|
+
const token = opts.token || (await resolveAccessToken());
|
|
47
|
+
return { baseUrl: specsBaseUrl(env), token, org: checkOrg(opts.org ?? process.env.NB_SPECS_ORG) };
|
|
48
|
+
}
|
|
49
|
+
export function buildPullCommand() {
|
|
50
|
+
return new Command("pull")
|
|
51
|
+
.description("Read a version of a spec's document out of the API catalog, exactly as it was uploaded, and check its SHA-256. For a pipeline that works from the catalog's document (a contract test, a client generator)")
|
|
52
|
+
.option("--id <id>", "the spec, by id (a rename cannot change it)")
|
|
53
|
+
.option("--name <name>", "the spec, by its name in the catalog (matched ignoring case)")
|
|
54
|
+
.option("--spec-version <n|latest>", "the catalog's version number (1, 2, 3...), not the API's v1 / v2 and not the CLI's own --version. Default: latest. Pin a number in a pipeline: 'latest' moves")
|
|
55
|
+
.option("--hash <sha256>", "the SHA-256 the document must have (as 'nb specs versions' prints it): the pull fails when it differs")
|
|
56
|
+
.option("--out <file>", "write the document here (default: to standard output, the details to standard error)")
|
|
57
|
+
.option("--org <id>", "the organization whose catalog it is (an id), or 'personal' (default; or $NB_SPECS_ORG)")
|
|
58
|
+
.action(async (_flags, command) => {
|
|
59
|
+
const opts = command.optsWithGlobals();
|
|
60
|
+
const result = await pullSpec({
|
|
61
|
+
conn: await connection(opts),
|
|
62
|
+
id: opts.id,
|
|
63
|
+
name: opts.name,
|
|
64
|
+
version: parseVersion(opts.specVersion),
|
|
65
|
+
hash: parseHash(opts.hash),
|
|
66
|
+
});
|
|
67
|
+
const { text, ...details } = result;
|
|
68
|
+
if (opts.out) {
|
|
69
|
+
try {
|
|
70
|
+
writeFileSync(opts.out, text);
|
|
71
|
+
}
|
|
72
|
+
catch (err) {
|
|
73
|
+
throw new Error(`--out: cannot write ${opts.out}: ${err.message}`);
|
|
74
|
+
}
|
|
75
|
+
printJson({ ...details, out: opts.out });
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
process.stdout.write(text);
|
|
79
|
+
}
|
|
80
|
+
exportGithubOutput("spec_id", result.id);
|
|
81
|
+
exportGithubOutput("spec_version", String(result.version));
|
|
82
|
+
exportGithubOutput("spec_hash", result.content_hash);
|
|
83
|
+
process.stderr.write(`✓ ${result.name} v${result.version} (sha256 ${result.content_hash.slice(0, 12)}..., ${result.size_bytes} bytes)${opts.out ? ` -> ${opts.out}` : ""}\n`);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
export function buildVersionsCommand() {
|
|
87
|
+
return new Command("versions")
|
|
88
|
+
.description("List the versions of a spec in the API catalog, newest first, each with the SHA-256 of its document (what 'nb specs pull --hash' pins)")
|
|
89
|
+
.option("--id <id>", "the spec, by id")
|
|
90
|
+
.option("--name <name>", "the spec, by its name in the catalog (matched ignoring case)")
|
|
91
|
+
.option("--org <id>", "the organization whose catalog it is (an id), or 'personal' (default; or $NB_SPECS_ORG)")
|
|
92
|
+
.action(async (_flags, command) => {
|
|
93
|
+
const opts = command.optsWithGlobals();
|
|
94
|
+
const result = await specVersions({ conn: await connection(opts), id: opts.id, name: opts.name });
|
|
95
|
+
printJson(result);
|
|
96
|
+
const latest = result.versions[0];
|
|
97
|
+
process.stderr.write(`✓ ${result.name}: ${result.versions.length} version${result.versions.length === 1 ? "" : "s"}${latest ? ` (latest v${latest.version})` : ""}\n`);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
42
100
|
export function buildSpecsCommand() {
|
|
43
|
-
return new Command("specs")
|
|
101
|
+
return new Command("specs")
|
|
102
|
+
.description("The API catalog: your own OpenAPI documents (NASEBANAL Evolution)")
|
|
103
|
+
.addCommand(buildPushCommand())
|
|
104
|
+
.addCommand(buildPullCommand())
|
|
105
|
+
.addCommand(buildVersionsCommand());
|
|
44
106
|
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
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 version of an API's document and works from that (a contract
|
|
4
|
+
* test with Specmatic, a client generator) instead of from a copy that lives in some repository.
|
|
5
|
+
*
|
|
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 version NUMBER (1, 2, 3...: one per `nb specs push` that changed the document),
|
|
8
|
+
* not the API's URL version (`v1`, `v2`) and not the document's own `info.version`; absent or `latest` = the newest
|
|
9
|
+
* - the document comes back exactly as it was uploaded (the bytes, not a re-serialised copy), and its SHA-256 is
|
|
10
|
+
* checked against the `content_hash` the catalog keeps for that version; `--hash` pins what a pipeline expects, so
|
|
11
|
+
* a version that was swapped, or a name that now points somewhere else, fails instead of testing the wrong thing
|
|
12
|
+
*/
|
|
13
|
+
import { ApiError, request } from "../http.js";
|
|
14
|
+
import { envTokenHint } from "../output.js";
|
|
15
|
+
import { findSpec, hashText, headersOf, reasonOf } from "./push.js";
|
|
16
|
+
/** `--spec-version`: a positive whole number, or `latest` (or nothing) for the newest. Returns undefined for the newest. */
|
|
17
|
+
export function parseVersion(raw) {
|
|
18
|
+
const v = raw?.trim();
|
|
19
|
+
if (v === undefined || v === "" || v.toLowerCase() === "latest")
|
|
20
|
+
return undefined;
|
|
21
|
+
if (/^[1-9]\d{0,8}$/.test(v))
|
|
22
|
+
return Number(v);
|
|
23
|
+
throw new Error(`--spec-version '${raw}' must be a version number (1, 2, 3...) or 'latest'. It is the catalog's version, not the API's v1 / v2`);
|
|
24
|
+
}
|
|
25
|
+
/** `--hash`: 64 hex characters (case does not matter). Returns it lower-cased, or undefined when none was asked. */
|
|
26
|
+
export function parseHash(raw) {
|
|
27
|
+
const v = raw?.trim();
|
|
28
|
+
if (v === undefined || v === "")
|
|
29
|
+
return undefined;
|
|
30
|
+
if (/^[0-9a-fA-F]{64}$/.test(v))
|
|
31
|
+
return v.toLowerCase();
|
|
32
|
+
throw new Error(`--hash '${raw}' must be a SHA-256: 64 hexadecimal characters (the content_hash that 'nb specs versions' prints)`);
|
|
33
|
+
}
|
|
34
|
+
/** Whatever the catalog refuses comes back with its own reason, and the token hint on a 401, not a bare "403 Forbidden". */
|
|
35
|
+
async function refusal(what, run) {
|
|
36
|
+
try {
|
|
37
|
+
return await run();
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
if (err instanceof ApiError) {
|
|
41
|
+
const hint = err.status === 401 ? envTokenHint() : null;
|
|
42
|
+
throw new Error(`The catalog refused ${what}: ${reasonOf(err)}${hint ? `\n${hint}` : ""}`);
|
|
43
|
+
}
|
|
44
|
+
throw err;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function resolveSpec(conn, id, name) {
|
|
48
|
+
if (!id && !name?.trim())
|
|
49
|
+
throw new Error("Say which spec: --id <id> or --name <name> (the name is the spec's title in the catalog)");
|
|
50
|
+
const spec = await findSpec(conn, name?.trim() ?? "", id);
|
|
51
|
+
if (!spec) {
|
|
52
|
+
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).`);
|
|
53
|
+
}
|
|
54
|
+
return spec;
|
|
55
|
+
}
|
|
56
|
+
async function listVersions(conn, spec) {
|
|
57
|
+
const res = await request({ ...headersOf(conn), method: "GET", path: `/api/v1/specs/${encodeURIComponent(spec.id)}/versions` });
|
|
58
|
+
return res.data.data.data;
|
|
59
|
+
}
|
|
60
|
+
/** The versions of a spec, newest first, each with the SHA-256 of its document (what `--hash` pins). */
|
|
61
|
+
export async function specVersions(p) {
|
|
62
|
+
return refusal("the request", async () => {
|
|
63
|
+
const spec = await resolveSpec(p.conn, p.id, p.name);
|
|
64
|
+
return { id: spec.id, name: spec.name, versions: await listVersions(p.conn, spec) };
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** A version's document exactly as uploaded, verified against the catalog's hash (and `--hash`). */
|
|
68
|
+
export async function pullSpec(p) {
|
|
69
|
+
return refusal("the request", async () => {
|
|
70
|
+
const spec = await resolveSpec(p.conn, p.id, p.name);
|
|
71
|
+
const versions = await listVersions(p.conn, spec);
|
|
72
|
+
if (versions.length === 0)
|
|
73
|
+
throw new Error(`Spec '${spec.name}' has no versions.`);
|
|
74
|
+
const picked = p.version === undefined ? versions[0] : versions.find((v) => v.version === p.version);
|
|
75
|
+
if (!picked) {
|
|
76
|
+
throw new Error(`Spec '${spec.name}' has no version ${p.version} (it has ${versions.map((v) => v.version).join(", ")}). The version is the catalog's number: 'nb specs versions' lists them.`);
|
|
77
|
+
}
|
|
78
|
+
const res = await request({
|
|
79
|
+
...headersOf(p.conn),
|
|
80
|
+
method: "GET",
|
|
81
|
+
path: `/api/v1/specs/${encodeURIComponent(spec.id)}/versions/${picked.version}/content`,
|
|
82
|
+
raw: true,
|
|
83
|
+
});
|
|
84
|
+
const text = res.data;
|
|
85
|
+
const hash = hashText(text);
|
|
86
|
+
// The catalog keeps the hash of what it stored: a document that does not match it was damaged on the way.
|
|
87
|
+
if (picked.content_hash && hash !== picked.content_hash) {
|
|
88
|
+
throw new Error(`The document of '${spec.name}' v${picked.version} has SHA-256 ${hash}, not the ${picked.content_hash} the catalog lists for it: it did not arrive intact.`);
|
|
89
|
+
}
|
|
90
|
+
// And the one a pipeline pinned: the version, or what the name points at, is no longer what it expected.
|
|
91
|
+
if (p.hash && hash !== p.hash) {
|
|
92
|
+
throw new Error(`The document of '${spec.name}' v${picked.version} has SHA-256 ${hash}, not the ${p.hash} you pinned with --hash: the version was replaced, or the name now points at another spec.`);
|
|
93
|
+
}
|
|
94
|
+
return { id: spec.id, name: spec.name, version: picked.version, content_hash: hash, size_bytes: Buffer.byteLength(text, "utf8"), text };
|
|
95
|
+
});
|
|
96
|
+
}
|
package/dist/specs/push.js
CHANGED
|
@@ -73,16 +73,16 @@ export function specNameFor(text, file, asked) {
|
|
|
73
73
|
throw new Error("The spec has no name: pass --name");
|
|
74
74
|
return name.slice(0, 100);
|
|
75
75
|
}
|
|
76
|
-
function headersOf(conn) {
|
|
76
|
+
export function headersOf(conn) {
|
|
77
77
|
return { baseUrl: conn.baseUrl, token: conn.token, headers: { "X-Active-Organization-Id": conn.org } };
|
|
78
78
|
}
|
|
79
|
-
function reasonOf(err) {
|
|
79
|
+
export function reasonOf(err) {
|
|
80
80
|
if (err instanceof ApiError)
|
|
81
81
|
return err.data?.error?.message ?? err.message;
|
|
82
82
|
return err.message;
|
|
83
83
|
}
|
|
84
84
|
const codeOf = (err) => (err instanceof ApiError ? err.data?.error?.code : undefined);
|
|
85
|
-
async function findSpec(conn, name, id) {
|
|
85
|
+
export async function findSpec(conn, name, id) {
|
|
86
86
|
if (id) {
|
|
87
87
|
try {
|
|
88
88
|
const res = await request({ ...headersOf(conn), method: "GET", path: `/api/v1/specs/${encodeURIComponent(id)}` });
|
package/package.json
CHANGED
package/specs/account.yaml
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
openapi: 3.0.3
|
|
2
2
|
info:
|
|
3
3
|
title: NASEBANAL Account API
|
|
4
|
-
description: Centralized user and billing management API for NASEBANAL applications. Manages user profiles, per-service subscriptions,
|
|
5
|
-
version: 1.
|
|
4
|
+
description: Centralized user and billing management API for NASEBANAL applications. Manages user profiles, per-service subscriptions, and Personal Access Tokens (PAT).
|
|
5
|
+
version: 1.7.0
|
|
6
6
|
contact:
|
|
7
7
|
name: NASEBANAL
|
|
8
8
|
url: https://www.nasebanal.com
|
|
@@ -1150,6 +1150,17 @@ components:
|
|
|
1150
1150
|
error:
|
|
1151
1151
|
code: NOT_FOUND
|
|
1152
1152
|
message: Resource not found
|
|
1153
|
+
Gone:
|
|
1154
|
+
description: Billing is not offered
|
|
1155
|
+
content:
|
|
1156
|
+
application/json:
|
|
1157
|
+
schema:
|
|
1158
|
+
$ref: "#/components/schemas/Error"
|
|
1159
|
+
example:
|
|
1160
|
+
success: false
|
|
1161
|
+
error:
|
|
1162
|
+
code: BILLING_NOT_AVAILABLE
|
|
1163
|
+
message: Billing is not offered
|
|
1153
1164
|
Forbidden:
|
|
1154
1165
|
description: Caller is authenticated but lacks the required role or plan
|
|
1155
1166
|
content:
|
package/specs/assurance.yaml
CHANGED
|
@@ -2,7 +2,7 @@ openapi: 3.0.3
|
|
|
2
2
|
info:
|
|
3
3
|
title: NASEBANAL - Assurance API
|
|
4
4
|
description: "Backend of NASEBANAL Assurance (quality assurance for APIs and web apps, under NASEBANAL Evolution). Authenticates users via Auth0 JWT and Personal Access Tokens (PAT) and integrates with the Account API. External monitoring: register URLs to be probed on an interval, then read availability reports. Test results: report the normalised result of each CI/CD job into a named project (typically one per repository), then read its runs and suites. Every response is wrapped as `{ success, data }` (or `{ success: false, error }`)."
|
|
5
|
-
version: 1.
|
|
5
|
+
version: 1.9.0
|
|
6
6
|
contact:
|
|
7
7
|
name: NASEBANAL
|
|
8
8
|
url: https://www.nasebanal.com
|
|
@@ -1859,6 +1859,7 @@ components:
|
|
|
1859
1859
|
- canceled
|
|
1860
1860
|
- past_due
|
|
1861
1861
|
- trialing
|
|
1862
|
+
- null
|
|
1862
1863
|
nullable: true
|
|
1863
1864
|
subscription_expires_at:
|
|
1864
1865
|
type: string
|
|
@@ -1959,8 +1960,14 @@ components:
|
|
|
1959
1960
|
MonitorCreate:
|
|
1960
1961
|
allOf:
|
|
1961
1962
|
- $ref: "#/components/schemas/MonitorSettings"
|
|
1962
|
-
|
|
1963
|
-
|
|
1963
|
+
- type: object
|
|
1964
|
+
required:
|
|
1965
|
+
- url
|
|
1966
|
+
properties:
|
|
1967
|
+
url:
|
|
1968
|
+
type: string
|
|
1969
|
+
format: uri
|
|
1970
|
+
maxLength: 2048
|
|
1964
1971
|
MonitorUpdate:
|
|
1965
1972
|
allOf:
|
|
1966
1973
|
- $ref: "#/components/schemas/MonitorSettings"
|
|
@@ -2662,6 +2669,11 @@ components:
|
|
|
2662
2669
|
nullable: true
|
|
2663
2670
|
maxLength: 20
|
|
2664
2671
|
description: The CI provider the run came from (`github` or `gitlab`; `other` for anything else), when the sender knew it. With the project's repository and the commit it lets a page link to a file in the repository.
|
|
2672
|
+
repo_url:
|
|
2673
|
+
type: string
|
|
2674
|
+
maxLength: 2048
|
|
2675
|
+
nullable: true
|
|
2676
|
+
description: The web address of the repository on its host (for example `https://gitlab.example.com/group/app`), http(s) only, without a trailing slash. Needed to link to a file when the host is not github.com / gitlab.com (a self-hosted GitLab or GitHub Enterprise); otherwise the host is taken from `provider`. Informational.
|
|
2665
2677
|
AttachmentStored:
|
|
2666
2678
|
type: object
|
|
2667
2679
|
description: What was stored for the result.
|
|
@@ -2821,6 +2833,11 @@ components:
|
|
|
2821
2833
|
nullable: true
|
|
2822
2834
|
maxLength: 20
|
|
2823
2835
|
description: The CI provider the run came from (`github` or `gitlab`; `other` for anything else), when the sender knew it. With the project's repository and the commit it lets a page link to a file in the repository.
|
|
2836
|
+
repo_url:
|
|
2837
|
+
type: string
|
|
2838
|
+
maxLength: 2048
|
|
2839
|
+
nullable: true
|
|
2840
|
+
description: The web address of the repository on its host (for example `https://gitlab.example.com/group/app`), http(s) only, without a trailing slash. Needed to link to a file when the host is not github.com / gitlab.com (a self-hosted GitLab or GitHub Enterprise); otherwise the host is taken from `provider`. Informational.
|
|
2824
2841
|
started_at:
|
|
2825
2842
|
type: string
|
|
2826
2843
|
format: date-time
|
|
@@ -2872,6 +2889,10 @@ components:
|
|
|
2872
2889
|
- gitlab
|
|
2873
2890
|
- other
|
|
2874
2891
|
description: The CI provider the run came from (`github` or `gitlab`; `other` for anything else), when the sender knew it. With the project's repository and the commit it lets a page link to a file in the repository.
|
|
2892
|
+
repo_url:
|
|
2893
|
+
type: string
|
|
2894
|
+
maxLength: 2048
|
|
2895
|
+
description: The web address of the repository on its host (for example `https://gitlab.example.com/group/app`), http(s) only, without a trailing slash. Needed to link to a file when the host is not github.com / gitlab.com (a self-hosted GitLab or GitHub Enterprise); otherwise the host is taken from `provider`. Informational.
|
|
2875
2896
|
started_at:
|
|
2876
2897
|
type: string
|
|
2877
2898
|
format: date-time
|