@starci/hfs 4.7.17 → 4.7.18
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/CHANGELOG.md +6 -2
- package/coverage/cli.mjs +29 -4
- package/package.json +1 -1
- package/runtime/knowledge/hfs/canon-pins.yaml +5 -5
- package/runtime/knowledge/hfs/ci-standard.yaml +5 -3
- package/runtime/modules/kernel/failure-codes.yaml +1 -1
- package/runtime/packages/cli/src/catalog.generated.mjs +10 -6
- package/runtime/packages/jest-preset/sonar-exclusions.cjs +8 -0
- package/runtime/scripts/hfs/rules/ci-standard.mjs +5 -2
- package/runtime/scripts/hfs/rules/staged-specs.mjs +25 -5
- package/sync/ci-commands.mjs +276 -275
- package/sync/hygiene.mjs +8 -2
- package/sync/index.mjs +7 -6
- package/templates/be/skeleton/src/modules/platform/database/migrate-connections.client.spec.ts +60 -0
- package/runtime/packages/jest-preset/sonar-exclusions.json +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## 4.7.
|
|
3
|
+
## 4.7.18 - 2026-10-21
|
|
4
|
+
- Changed: supersedes 4.7.17, published from a lane whose history the owner refused (an unapproved allowlist entry) and therefore a superseded orphan that is not used; this version carries the same work plus the standalone scaffold fix.
|
|
5
|
+
- Fixed: the managed CI's coverage job runs `starci app coverage verify <lcov>`, the coverage judgement alone over the merged report, instead of `npm run codegen && starci app check --coverage-from` (the whole app check the fast and lint jobs already ran); its default timeout is 15 minutes (`knowledge/hfs/ci-standard.yaml`). NIVO v0.3.0's coverage job took 18m43s of 20 and the tag run was cancelled.
|
|
4
6
|
- Fixed: the sonar row of `starci app release gate --sonar` no longer passes on the scanner's exit code alone: it reads `.scannerwork/report-task.txt`, polls `api/ce/task` until the analysis ends and reads `api/qualitygates/project_status` with the same token; ERROR is red as `RELEASE_SONAR_GATE_FAILED` naming each failed condition, a 600-second wait is `RELEASE_SONAR_GATE_TIMEOUT` and an unreadable answer `RELEASE_SONAR_GATE_UNREADABLE`; canon pins follow the matching package cohort.
|
|
5
7
|
|
|
6
|
-
- Fixed: `npx @starci/cli app scaffold` works from the published packages alone: the Sonar exclusions come from the jest preset copy @starci/hfs ships (`runtime/packages/jest-preset/sonar-exclusions.
|
|
8
|
+
- Fixed: `npx @starci/cli app scaffold` works from the published packages alone: the Sonar exclusions come from the jest preset copy @starci/hfs ships (`runtime/packages/jest-preset/sonar-exclusions.cjs`, `bundledPresets`), not from a @starci/jest-preset installed beside the CLI (it was `HFS_SYNC_PRESET_MISSING`); `starci app upgrade` reads the same copy instead of a hand-kept list.
|
|
7
9
|
- Fixed: a new scaffold path is git-initialised with an unborn main before any file is written (`HFS_SCAFFOLD_GIT_INIT_FAILED` when git cannot), as the verb promised, so `starci npm ci` can install the hooks; an existing empty unborn main repository is still accepted.
|
|
8
10
|
|
|
11
|
+
- Fixed: `BE_UNIT_SPEC_NOT_STAGED` exempts a measured file with nothing to execute (constants and types only), which has nothing a spec could call and whose spec the spec rules refuse; every measured file with a function, method, constructor or accessor still needs its spec staged beside it. It refused the scaffold's own first commit on the constant-only `http-status.policy.ts`; the scaffold now also ships `migrate-connections.client.spec.ts`.
|
|
12
|
+
|
|
9
13
|
## 4.7.16 - 2026-10-21
|
|
10
14
|
- Fixed: `DB_MIGRATION_DESTRUCTIVE` counts a be/src source as a reference of a dropped or altered column only when the source names the column's table too, so a same-named column of another table (`public.brain_lessons.origin` beside a drop of `transactions.origin`) no longer blocks a contract migration; table and other object drops keep the leaf match; canon pins follow the matching package cohort.
|
|
11
15
|
|
package/coverage/cli.mjs
CHANGED
|
@@ -3,14 +3,18 @@
|
|
|
3
3
|
// coverage proof, the Codecov upload and the Sonar import read. The merge is the app's installed @starci/jest-preset (`@starci/jest-preset/merge`):
|
|
4
4
|
// istanbul-lib-coverage merges the shard maps by source location, and the preset's own lcov reporter writes the result exactly as an unsharded run
|
|
5
5
|
// would. A path is a coverage-final.json or a directory searched for every one below it; no report found refuses (exit 2), never an empty report.
|
|
6
|
+
// `starci app coverage verify <lcov>`: the coverage proof alone (scripts/hfs/coverage-from.mjs, the judgement `starci app check --coverage-from`
|
|
7
|
+
// adds to the whole check): every measured logic file is in the merged lcov at 100 per file. The managed CI's coverage job runs it after the merge,
|
|
8
|
+
// so the job re-runs none of the rules the fast and lint jobs already judged. Exit 0 proved, 1 refused (the report lists every finding), 2 bad usage.
|
|
6
9
|
import path from 'node:path';
|
|
7
10
|
import { createRequire } from 'node:module';
|
|
11
|
+
import { coverageFrom, formatCoverageReport } from '../runtime/scripts/hfs/coverage-from.mjs';
|
|
8
12
|
|
|
9
|
-
const USAGE = 'usage: starci app coverage merge --out <lcov> <coverage-final.json-or-dir>... [--cwd <dir>]';
|
|
13
|
+
const USAGE = 'usage: starci app coverage merge --out <lcov> <coverage-final.json-or-dir>... [--cwd <dir>] | starci app coverage verify <lcov> [--cwd <dir>]';
|
|
10
14
|
|
|
11
15
|
function parse(argv) {
|
|
12
16
|
const [action, ...rest] = argv;
|
|
13
|
-
if (action !== 'merge') throw new Error(USAGE);
|
|
17
|
+
if (action !== 'merge' && action !== 'verify') throw new Error(USAGE);
|
|
14
18
|
const opts = { inputs: [] };
|
|
15
19
|
for (let index = 0; index < rest.length; index += 1) {
|
|
16
20
|
const arg = rest[index];
|
|
@@ -22,6 +26,11 @@ function parse(argv) {
|
|
|
22
26
|
} else if (arg.startsWith('--')) throw new Error(`unknown flag ${arg}; ${USAGE}`);
|
|
23
27
|
else opts.inputs.push(arg);
|
|
24
28
|
}
|
|
29
|
+
opts.action = action;
|
|
30
|
+
if (action === 'verify') {
|
|
31
|
+
if (opts.out !== undefined || opts.inputs.length !== 1) throw new Error(USAGE);
|
|
32
|
+
return opts;
|
|
33
|
+
}
|
|
25
34
|
if (!opts.out || !opts.inputs.length) throw new Error(USAGE);
|
|
26
35
|
if (path.basename(opts.out) !== 'lcov.info') throw new Error(`--out names the lcov.info the coverage proof reads; ${USAGE}`);
|
|
27
36
|
return opts;
|
|
@@ -34,11 +43,27 @@ function installedMerge(cwd) {
|
|
|
34
43
|
}
|
|
35
44
|
}
|
|
36
45
|
|
|
37
|
-
/**
|
|
38
|
-
|
|
46
|
+
/** The coverage proof of the app at `cwd` over the lcov `report`: exit 0 proved, 1 refused, 2 when the proof cannot run. */
|
|
47
|
+
function verifyCoverage(cwd, report, { stdout, stderr, prove }) {
|
|
48
|
+
try {
|
|
49
|
+
const result = prove({ repoRoot: cwd, coverageFrom: report });
|
|
50
|
+
stdout(formatCoverageReport(result));
|
|
51
|
+
return result.ok ? 0 : 1;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
stderr(`starci app coverage verify: ${String(error?.message ?? error).split('\n')[0]}\n`);
|
|
54
|
+
return 2;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Exit 0 merged or proved, 1 a refused proof, 2 bad usage, no preset or no report found. `merge` replaces the installed preset merge and
|
|
60
|
+
* `prove` the coverage proof in a spec.
|
|
61
|
+
*/
|
|
62
|
+
export async function coverageMain(argv, { cwd: baseCwd = process.cwd(), stdout = (s) => process.stdout.write(s), stderr = (s) => process.stderr.write(s), merge, prove = coverageFrom } = {}) {
|
|
39
63
|
let opts;
|
|
40
64
|
try { opts = parse(argv); } catch (error) { stderr(`starci app coverage: ${error.message}\n`); return 2; }
|
|
41
65
|
const cwd = path.resolve(baseCwd, opts.cwd ?? '.');
|
|
66
|
+
if (opts.action === 'verify') return verifyCoverage(cwd, opts.inputs[0], { stdout, stderr, prove });
|
|
42
67
|
try {
|
|
43
68
|
const run = merge ?? installedMerge(cwd);
|
|
44
69
|
const outDir = path.dirname(path.resolve(cwd, opts.out));
|
package/package.json
CHANGED
|
@@ -29,14 +29,14 @@ pins:
|
|
|
29
29
|
source: packages/grammar/package.json
|
|
30
30
|
why: 'the grammar front ends read: the brand layer sets `--font-sans` and `--font-mono` and the grammar reads them; it carries the Input tel kind, the copyable and read-only Input, the IconButton disclosure props and count badge, ChatBubble, FillViewport, Textarea mentions and Enter-to-send, the leading PrimaryRailLayout rail, the plate-less Image, ListRow, the soft Alert, Progress steps, the floating primary IconButton, FlowGraph, the STATE-8 loading views and the bordered selectable TagGroup chips. The runtime source version wins for a @starci package.'
|
|
31
31
|
'@starci/eslint-canon-be':
|
|
32
|
-
version: 3.4.
|
|
32
|
+
version: 3.4.26
|
|
33
33
|
group: starci
|
|
34
34
|
install: registry
|
|
35
35
|
side: be
|
|
36
36
|
source: packages/eslint/be/package.json
|
|
37
37
|
why: 'the back-end ESLint canon: `loadHfs(import.meta.url)` of be/eslint.config.mjs finds the app-root hfs.json (kind app) and gives every linted file the view of the be side, and the project graph is built per side; its bundled runtime copies (slots, canon-pins, failure codes, the architecture machine) put .starcistacks and .sops.yaml at the app root; `starciBeConfig({ hfs: loadHfs(import.meta.url) })` is the typed factory and the BE-CONVENTION laws are its rules.'
|
|
38
38
|
'@starci/eslint-canon-fe':
|
|
39
|
-
version: 8.4.
|
|
39
|
+
version: 8.4.25
|
|
40
40
|
group: starci
|
|
41
41
|
install: registry
|
|
42
42
|
side: fe
|
|
@@ -62,7 +62,7 @@ pins:
|
|
|
62
62
|
side: both
|
|
63
63
|
source: packages/prettier-config/package.json
|
|
64
64
|
'@starci/jest-preset':
|
|
65
|
-
version: 2.3.
|
|
65
|
+
version: 2.3.4
|
|
66
66
|
group: starci
|
|
67
67
|
install: registry
|
|
68
68
|
side: be
|
|
@@ -76,14 +76,14 @@ pins:
|
|
|
76
76
|
source: packages/test-world/package.json
|
|
77
77
|
why: 'the shared e2e library of every back end (R47, R48), a devDependency of every back end: the warm stack behind toxiproxy, the network-edge fakes, the Nest boot, the typed useTestWorld handle, useSandbox for contract specs and the outage lock. Kafka is real own infra (the one digest-pinned apache/kafka KRaft image, a broker listener and proxy per slot, slot-prefixed topics, consumer groups and client ids) and Postgres is schema-per-context (a schema and a login role per context in a shared database). Each worker has its own data slot (<ns>_w<k>) with its own databases, Keycloak realm, leased Redis DB, bucket/collection/topic prefixes, fakes host, toxiproxy proxies and outage lock; a run provisions min(jest workers, workers cap, default 2) slots, and state file protocol 2 pairs exactly with @starci/jest-preset (TEST_WORLD_PAIR_MISMATCH otherwise). A modules world boots real peer apps beside its modules, world.apps.<name>.during(fn) is the outage of a peer app, w.keycloak.clientSecret(client) answers the run-generated secret of a confidential realm client, world.keycloak.events/sessions read the realm user events and live sessions, world.infra.postgresql.connection(name) takes one database connection down while the others serve, and world.resolve and scope.resolve take any Nest token. Every path a declaration names (`stack`, seeds, the realm, Dockerfiles) resolves from the app root (the directory of hfs.json); the globalSetup registers the path aliases as TypeScript resolves them. Its CLI main is exported at ./cli for starci app stack.'
|
|
78
78
|
'@starci/cli':
|
|
79
|
-
version: 1.7.
|
|
79
|
+
version: 1.7.18
|
|
80
80
|
group: starci
|
|
81
81
|
install: registry
|
|
82
82
|
side: both
|
|
83
83
|
source: packages/cli/package.json
|
|
84
84
|
why: 'the only StarCi command package; product repositories install it for starci app, while @starci/hfs remains its pinned transitive implementation.'
|
|
85
85
|
'@starci/hfs':
|
|
86
|
-
version: 4.7.
|
|
86
|
+
version: 4.7.18
|
|
87
87
|
group: starci
|
|
88
88
|
install: registry
|
|
89
89
|
side: both
|
|
@@ -19,15 +19,17 @@ jobs:
|
|
|
19
19
|
# The per-job settings the managed CI renders when .starcistacks/application-stacks.yaml ci.jobs leaves them out (ci.jobs.<job>.timeoutMinutes,
|
|
20
20
|
# ci.jobs.test.shards). Sized from a real run (NIVO v0.2.3 on the 2-vCPU GitHub runner, 2026-10-18): the be lint with the Sonar import took 11.5 minutes and the
|
|
21
21
|
# fe lint more than 8 in one 20-minute job, and ~900 unit suites ran at ~4 s each (670 of 904 in 45 minutes). One lint job per side, and 6 shards of ~150 suites
|
|
22
|
-
# (~10 minutes each plus the install) leave each job well inside its timeout.
|
|
22
|
+
# (~10 minutes each plus the install) leave each job well inside its timeout. The coverage job installs, downloads the shards, merges them and runs
|
|
23
|
+
# the coverage judgement alone (starci app coverage verify, seconds): ~5 minutes, so 15 leaves three times that. Its earlier whole app check took
|
|
24
|
+
# 18m43s on NIVO v0.3.0 and the 20-minute bound cancelled the tag run (39592175266).
|
|
23
25
|
defaults:
|
|
24
26
|
shards: 6
|
|
25
|
-
timeoutMinutes: {fast: 20, lint: 30, test: 25, coverage:
|
|
27
|
+
timeoutMinutes: {fast: 20, lint: 30, test: 25, coverage: 15, build: 30, sonar: 20, db-types: 30}
|
|
26
28
|
|
|
27
29
|
# The steps that need a raised Node heap on the GitHub runner, matched on their `run` text, and the least heap they must declare in NODE_OPTIONS.
|
|
28
30
|
heap:
|
|
29
31
|
minMiB: 6144
|
|
30
|
-
steps: ['npm run typecheck', 'npm run lint', 'app lint', 'app
|
|
32
|
+
steps: ['npm run typecheck', 'npm run lint', 'app lint', 'app coverage verify', 'npm run db:lint']
|
|
31
33
|
|
|
32
34
|
items:
|
|
33
35
|
- id: candidate-trigger
|
|
@@ -1608,7 +1608,7 @@ BE_TYPE_ESCAPE:
|
|
|
1608
1608
|
BE_UNIT_SPEC_NOT_STAGED:
|
|
1609
1609
|
title: "A new back-end logic file is committed without its unit spec"
|
|
1610
1610
|
title_vi: "Tệp logic back-end mới được commit mà không có unit spec"
|
|
1611
|
-
meaning_vi: "Commit thêm `<file>`, một tệp `<name>.<role>.ts` có vai trò logic mà unit coverage đo ở mức 100 mỗi tệp, nhưng index không có `<name>.<role>.spec.ts` bên cạnh nó: mã được viết trước, spec để sau."
|
|
1611
|
+
meaning_vi: "Commit thêm `<file>`, một tệp `<name>.<role>.ts` có vai trò logic mà unit coverage đo ở mức 100 mỗi tệp, nhưng index không có `<name>.<role>.spec.ts` bên cạnh nó: mã được viết trước, spec để sau. Tệp chỉ có hằng số và kiểu (không có hàm, phương thức nào để chạy) không cần spec riêng."
|
|
1612
1612
|
causes_vi:
|
|
1613
1613
|
- "Agent viết mã trước rồi định sửa lint và spec sau, thay vì chạy vòng op `starci gate read` → mã + spec → `starci gate run`"
|
|
1614
1614
|
- "Spec đã viết nhưng chưa được stage cùng tệp"
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// GENERATED by scripts/cli/gen-catalog.mjs — do not edit
|
|
2
2
|
export const CATALOG = {
|
|
3
3
|
"schema": "starci/cli-catalog@1",
|
|
4
|
-
"hash": "
|
|
4
|
+
"hash": "2ad45b29a66fee7ac961e8546613e0455d23cc47e28a5070bd15b86154755dd8",
|
|
5
5
|
"global": [
|
|
6
6
|
{
|
|
7
7
|
"name": "json",
|
|
@@ -790,13 +790,14 @@ export const CATALOG = {
|
|
|
790
790
|
"runtimeSide": "host"
|
|
791
791
|
},
|
|
792
792
|
"coverage": {
|
|
793
|
-
"summary": "merge the
|
|
793
|
+
"summary": "merge the sharded unit coverage into one lcov report, or prove that report at 100 per file",
|
|
794
794
|
"owner": "@starci/hfs",
|
|
795
795
|
"positional": [
|
|
796
796
|
{
|
|
797
797
|
"name": "action",
|
|
798
798
|
"enum": [
|
|
799
|
-
"merge"
|
|
799
|
+
"merge",
|
|
800
|
+
"verify"
|
|
800
801
|
],
|
|
801
802
|
"required": true,
|
|
802
803
|
"variadic": false
|
|
@@ -820,12 +821,14 @@ export const CATALOG = {
|
|
|
820
821
|
}
|
|
821
822
|
],
|
|
822
823
|
"exit": {
|
|
823
|
-
"0": "merged",
|
|
824
|
+
"0": "merged or proved",
|
|
825
|
+
"1": "the coverage proof refused a measured file",
|
|
824
826
|
"2": "bad usage, no installed preset or no report found"
|
|
825
827
|
},
|
|
826
828
|
"json": "none",
|
|
827
829
|
"examples": [
|
|
828
|
-
"starci app coverage merge --out be/coverage/lcov.info be/coverage/shards"
|
|
830
|
+
"starci app coverage merge --out be/coverage/lcov.info be/coverage/shards",
|
|
831
|
+
"starci app coverage verify be/coverage/lcov.info"
|
|
829
832
|
],
|
|
830
833
|
"editions": [
|
|
831
834
|
"full",
|
|
@@ -838,7 +841,8 @@ export const CATALOG = {
|
|
|
838
841
|
"merge runs the app's installed @starci/jest-preset/merge: istanbul-lib-coverage joins the shard maps by source location",
|
|
839
842
|
"the merged lcov is written by the preset's own lcov reporter, as an unsharded run writes it",
|
|
840
843
|
"a path is a coverage-final.json or a directory searched for every one below it; no report found refuses (exit 2)",
|
|
841
|
-
"the coverage proof
|
|
844
|
+
"verify <lcov> is the coverage proof alone: every measured logic file in the report at 100 per file, a missing one refused",
|
|
845
|
+
"verify runs none of the other rules of starci app check; the managed CI coverage job runs it after the merge"
|
|
842
846
|
],
|
|
843
847
|
"runtimeSide": "host"
|
|
844
848
|
},
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sonar `sonar.exclusions`: specs, e2e specs, dist and coverage output are not analysed at all. A plain list with no require, so
|
|
5
|
+
* @starci/hfs ships this file byte for byte (packages/hfs/runtime, scripts/hfs/sync-runtime.mjs) and `starci app scaffold` renders
|
|
6
|
+
* the same exclusions before any app dependency is installed.
|
|
7
|
+
*/
|
|
8
|
+
module.exports = Object.freeze(["**/*.spec.ts", "**/*.e2e-spec.ts", "**/dist/**", "**/coverage/**"])
|
|
@@ -28,6 +28,9 @@ const DIGEST_PIN = /@sha256:[0-9a-f]{64}$/;
|
|
|
28
28
|
|
|
29
29
|
let cached = null;
|
|
30
30
|
/** The standard (knowledge/hfs/ci-standard.yaml), read once. */
|
|
31
|
+
/** The coverage proof step: the coverage judgement over the merged lcov (`starci app coverage verify`), never the whole app check. */
|
|
32
|
+
const COVERAGE_PROOF = /app coverage verify\b/;
|
|
33
|
+
|
|
31
34
|
function ciStandard() {
|
|
32
35
|
if (!cached) {
|
|
33
36
|
const doc = parseYaml(fs.readFileSync(STANDARD_FILE, 'utf8'));
|
|
@@ -56,7 +59,7 @@ function jobShape(doc, edition, standard, say) {
|
|
|
56
59
|
const withStep = (match) => jobsOf(doc).filter(([, job]) => stepsOf(job).some((step) => match(step))).map(([id]) => id);
|
|
57
60
|
const unit = withStep((step) => step.name === 'unit');
|
|
58
61
|
const upload = withStep((step) => usesOf(step).startsWith('codecov/codecov-action@'));
|
|
59
|
-
const proof = withStep((step) =>
|
|
62
|
+
const proof = withStep((step) => COVERAGE_PROOF.test(runOf(step)));
|
|
60
63
|
if (unit.join() !== 'test') out.push(say('job-shape', `the unit run must be a step of the test job alone, not of ${unit.join(', ') || 'no job'}`));
|
|
61
64
|
if (proof.join() !== 'coverage') out.push(say('job-shape', `the coverage proof must be a step of the coverage job alone, not of ${proof.join(', ') || 'no job'}`));
|
|
62
65
|
if (upload.length && upload.join() !== 'coverage') out.push(say('job-shape', `the Codecov upload must be a step of the coverage job alone, not of ${upload.join(', ')}`));
|
|
@@ -79,7 +82,7 @@ function testSharding(doc, say) {
|
|
|
79
82
|
if (unit && !runOf(unit).includes('--coverageThreshold={}')) out.push(say('test-sharding', 'a shard measures without the per-file threshold (--coverageThreshold={}); the coverage job proves it on the merged report'));
|
|
80
83
|
const coverage = record(doc.jobs?.coverage) ? stepsOf(doc.jobs.coverage) : [];
|
|
81
84
|
const merge = coverage.findIndex((step) => /app coverage merge\b/.test(runOf(step)));
|
|
82
|
-
const proof = coverage.findIndex((step) =>
|
|
85
|
+
const proof = coverage.findIndex((step) => COVERAGE_PROOF.test(runOf(step)));
|
|
83
86
|
if (merge < 0 || (proof >= 0 && merge > proof)) out.push(say('test-sharding', 'the coverage job must merge the shard reports (starci app coverage merge) before the coverage proof'));
|
|
84
87
|
return out;
|
|
85
88
|
}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
// staged-specs.mjs - BE_UNIT_SPEC_NOT_STAGED (R47): the commit that adds a back-end logic file adds its unit spec beside it. A new
|
|
2
2
|
// file `be/<path>/<name>.<role>.ts` that the coverage scope measures (scripts/hfs/coverage-scope.mjs isMeasured: a role of
|
|
3
|
-
// ruleParams.be.logicRoles below a measured root, outside its none folders)
|
|
4
|
-
//
|
|
5
|
-
//
|
|
3
|
+
// ruleParams.be.logicRoles below a measured root, outside its none folders) and that holds executable logic (a function, a method, a
|
|
4
|
+
// constructor, an accessor) needs `<name>.<role>.spec.ts` in the same index. A measured file with nothing to execute (constants and
|
|
5
|
+
// types only, such as an error-kind to HTTP-status table) has nothing a spec could call, and the spec rules refuse a spec of it
|
|
6
|
+
// (spec-infra-double-from-kit); the coverage gate still reaches it through its callers. `starci app hygiene` judges the files the
|
|
7
|
+
// index adds (packages/hfs/sync/hygiene.mjs, run by the managed pre-commit hook), so a coding agent cannot commit the code first and
|
|
8
|
+
// its spec afterwards. A file that already exists is the coverage gate's.
|
|
6
9
|
import { isMeasured } from '../coverage-scope.mjs';
|
|
7
10
|
|
|
8
11
|
const SPEC_NOT_STAGED = 'BE_UNIT_SPEC_NOT_STAGED';
|
|
@@ -11,13 +14,30 @@ const BE = 'be/';
|
|
|
11
14
|
/** The unit spec beside an app-relative `<name>.<role>.ts`: `<name>.<role>.spec.ts`. */
|
|
12
15
|
const siblingSpec = (file) => file.replace(/\.ts$/u, '.spec.ts');
|
|
13
16
|
|
|
17
|
+
/** The syntax kinds that execute: a body a spec can call. */
|
|
18
|
+
const EXECUTABLE_KINDS = ['FunctionDeclaration', 'FunctionExpression', 'ArrowFunction', 'MethodDeclaration', 'Constructor', 'GetAccessor', 'SetAccessor'];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Whether TypeScript `text` holds executable logic. Without a TypeScript parser (`ts` null) or without the text, the file counts as
|
|
22
|
+
* logic: the gate never relaxes on what it cannot read.
|
|
23
|
+
*/
|
|
24
|
+
function holdsLogic(ts, text) {
|
|
25
|
+
if (!ts || typeof text !== 'string') return true;
|
|
26
|
+
const kinds = new Set(EXECUTABLE_KINDS.map((name) => ts.SyntaxKind[name]));
|
|
27
|
+
const source = ts.createSourceFile('subject.ts', text, ts.ScriptTarget.Latest, false, ts.ScriptKind.TS);
|
|
28
|
+
const visit = (node) => kinds.has(node.kind) || ts.forEachChild(node, visit) === true;
|
|
29
|
+
return ts.forEachChild(source, visit) === true;
|
|
30
|
+
}
|
|
31
|
+
|
|
14
32
|
/**
|
|
15
33
|
* The findings `{ file, code, message }` of the app-relative files the index adds (`added`) whose sibling spec is not among the
|
|
16
|
-
* paths the index holds (`indexed`, a Set). `manifest` is the slot manifest the coverage scope reads
|
|
34
|
+
* paths the index holds (`indexed`, a Set). `manifest` is the slot manifest the coverage scope reads; `staged(file)` answers the
|
|
35
|
+
* staged text of a file and `ts` the TypeScript parser, which tell a logic file from a constants-only one.
|
|
17
36
|
*/
|
|
18
|
-
export function stagedSpecFindings(manifest, added, indexed) {
|
|
37
|
+
export function stagedSpecFindings(manifest, added, indexed, { ts = null, staged = () => null } = {}) {
|
|
19
38
|
return added
|
|
20
39
|
.filter((file) => file.startsWith(BE) && !file.endsWith('.spec.ts') && isMeasured(manifest, file.slice(BE.length)) && !indexed.has(siblingSpec(file)))
|
|
40
|
+
.filter((file) => holdsLogic(ts, staged(file)))
|
|
21
41
|
.map((file) => ({
|
|
22
42
|
file,
|
|
23
43
|
code: SPEC_NOT_STAGED,
|
package/sync/ci-commands.mjs
CHANGED
|
@@ -1,275 +1,276 @@
|
|
|
1
|
-
import fs from 'node:fs';
|
|
2
|
-
import { parseYaml } from '../runtime/engine/yaml.mjs';
|
|
3
|
-
import { refusal } from '../stack/errors.mjs';
|
|
4
|
-
import { byCodeUnit } from '../report/order.mjs';
|
|
5
|
-
|
|
6
|
-
// One command vocabulary for managed CI rendering and the runtime coverage gate.
|
|
7
|
-
const exampleCommand = (source, command) => source?.runtimeExample
|
|
8
|
-
? 'node node_modules/@starci/hfs/runtime/scripts/hfs/example-secrets.mjs --run . -- ' + command
|
|
9
|
-
: command;
|
|
10
|
-
|
|
11
|
-
const UPLOAD_ARTIFACT = 'actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2';
|
|
12
|
-
const DOWNLOAD_ARTIFACT = 'actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0';
|
|
13
|
-
const SONAR_CONDITION = "${{ !cancelled() && startsWith(github.ref, 'refs/tags/v') && env.SONAR_TOKEN != '' }}";
|
|
14
|
-
|
|
15
|
-
/** Optional uploads require reachable upload and authentication endpoints without exposing identity tokens. */
|
|
16
|
-
function codecovProbe(codecov, condition) {
|
|
17
|
-
if (!codecov.optional) return [];
|
|
18
|
-
return [
|
|
19
|
-
' - name: Codecov availability',
|
|
20
|
-
' id: codecov-availability',
|
|
21
|
-
` if: ${condition}`,
|
|
22
|
-
' env:',
|
|
23
|
-
` CODECOV_AUTH: ${codecov.auth}`,
|
|
24
|
-
' run: |',
|
|
25
|
-
" node --input-type=module <<'NODE'",
|
|
26
|
-
" import fs from 'node:fs';",
|
|
27
|
-
' let available = false;',
|
|
28
|
-
' try {',
|
|
29
|
-
" const upload = await fetch('https://ingest.codecov.io/upload/v4', { signal: AbortSignal.timeout(10000) });",
|
|
30
|
-
' available = upload.status < 500;',
|
|
31
|
-
" if (available && process.env.CODECOV_AUTH === 'oidc') {",
|
|
32
|
-
" const url = new URL(process.env.ACTIONS_ID_TOKEN_REQUEST_URL);",
|
|
33
|
-
" url.searchParams.set('audience', 'https://codecov.io');",
|
|
34
|
-
" const identity = await fetch(url, { headers: { Authorization: 'Bearer ' + process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN }, signal: AbortSignal.timeout(10000) });",
|
|
35
|
-
' available = identity.ok && Boolean((await identity.json()).value);',
|
|
36
|
-
' }',
|
|
37
|
-
' } catch { available = false; }',
|
|
38
|
-
" fs.appendFileSync(process.env.GITHUB_OUTPUT, 'up=' + available + '\\n');",
|
|
39
|
-
" if (!available) console.log('::notice::codecov upload skipped: upload endpoint or OIDC token unavailable (services.codecov.optional)');",
|
|
40
|
-
' NODE',
|
|
41
|
-
];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** Optional Sonar only runs analysis when the provider reports UP within ten seconds. */
|
|
45
|
-
function sonarProbe(sonar) {
|
|
46
|
-
if (!sonar.optional) return [];
|
|
47
|
-
return [
|
|
48
|
-
' - name: Sonar availability',
|
|
49
|
-
' id: sonar-availability',
|
|
50
|
-
` if: ${SONAR_CONDITION}`,
|
|
51
|
-
' run: |',
|
|
52
|
-
` if curl -fsS --max-time 10 "$SONAR_HOST_URL/api/system/status" | node -e "let s='';process.stdin.on('data',c=>s+=c);process.stdin.on('end',()=>{try{process.exit(JSON.parse(s).status==='UP'?0:1)}catch{process.exit(1)}})"; then`,
|
|
53
|
-
' echo "up=true" >> "$GITHUB_OUTPUT"',
|
|
54
|
-
' else',
|
|
55
|
-
' echo "up=false" >> "$GITHUB_OUTPUT"',
|
|
56
|
-
' echo "::notice::sonar skipped: $SONAR_HOST_URL not reachable (services.sonar.optional)"',
|
|
57
|
-
' fi',
|
|
58
|
-
];
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** Codecov reports missing coverage without failing twice; uploads retain the release-tag condition and required authentication. */
|
|
62
|
-
function codecovSteps(lcovReport, codecov) {
|
|
63
|
-
if (codecov.enabled === false || codecov.wiring === 'not-used') return [
|
|
64
|
-
' - name: coverage upload',
|
|
65
|
-
' run: echo "codecov disabled in .starcistacks/application-stacks.yaml"',
|
|
66
|
-
].join('\n');
|
|
67
|
-
const condition = `\${{ !cancelled() && startsWith(github.ref, 'refs/tags/v') && hashFiles('${lcovReport}') != '' }}`;
|
|
68
|
-
const uploadCondition = codecov.optional ? condition.replace(' }}', " && steps.codecov-availability.outputs.up == 'true' }}") : condition;
|
|
69
|
-
return [
|
|
70
|
-
' - name: coverage upload skipped',
|
|
71
|
-
` if: \${{ !cancelled() && hashFiles('${lcovReport}') == '' }}`,
|
|
72
|
-
' run: |',
|
|
73
|
-
` echo "::notice::codecov upload skipped: no coverage file at ${lcovReport} (the test step failed or produced none)"`,
|
|
74
|
-
' exit 0',
|
|
75
|
-
...codecovProbe(codecov, condition),
|
|
76
|
-
' - name: coverage upload',
|
|
77
|
-
...(codecov.optional ? [' id: codecov-upload', ' continue-on-error: true'] : []),
|
|
78
|
-
' uses: codecov/codecov-action@0fb7174895f61a3b6b78fc075e0cd60383518dac # v5.5.5',
|
|
79
|
-
` if: ${uploadCondition}`,
|
|
80
|
-
' with:',
|
|
81
|
-
codecov.auth === 'token' ? ` token: \${{ secrets.${codecov.token} }}` : ' use_oidc: true',
|
|
82
|
-
` files: ${lcovReport}`,
|
|
83
|
-
' disable_search: true',
|
|
84
|
-
` fail_ci_if_error: ${!codecov.optional}`,
|
|
85
|
-
...(codecov.optional ? [
|
|
86
|
-
' - name: Codecov upload unavailable',
|
|
87
|
-
" if: ${{ !cancelled() && steps.codecov-upload.outcome == 'failure' }}",
|
|
88
|
-
' run: |',
|
|
89
|
-
' echo "::notice::codecov upload skipped: upload unavailable (services.codecov.optional)"',
|
|
90
|
-
] : []),
|
|
91
|
-
].join('\n');
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** Sonar consumes the test coverage and fast lint artifacts only on release tags with a configured token. */
|
|
95
|
-
function sonarJob(sonar, lcovReport, lcovArtifact, nodeMajor, timeout) {
|
|
96
|
-
if (!sonar) return '';
|
|
97
|
-
const condition = sonar.optional ? SONAR_CONDITION.replace(' }}', " && steps.sonar-availability.outputs.up == 'true' }}") : SONAR_CONDITION;
|
|
98
|
-
const host = sonar.provider === 'sonarcloud' ? 'https://sonarcloud.io' : `\${{ vars.${sonar.hostVar} }}`;
|
|
99
|
-
const header = [
|
|
100
|
-
' sonar:',
|
|
101
|
-
' needs: [lint, coverage]',
|
|
102
|
-
` timeout-minutes: ${timeout}`,
|
|
103
|
-
' runs-on: ubuntu-latest',
|
|
104
|
-
' env:',
|
|
105
|
-
` SONAR_TOKEN: \${{ secrets.${sonar.token} }}`,
|
|
106
|
-
` SONAR_HOST_URL: ${host}`,
|
|
107
|
-
' steps:',
|
|
108
|
-
].join('\n');
|
|
109
|
-
const steps = [
|
|
110
|
-
...sonarProbe(sonar),
|
|
111
|
-
` - uses: ${DOWNLOAD_ARTIFACT}`,
|
|
112
|
-
` if: ${condition}`,
|
|
113
|
-
' with:',
|
|
114
|
-
` name: ${lcovArtifact}`,
|
|
115
|
-
` path: ${lcovReport.split('/').slice(0, -1).join('/') || '.'}`,
|
|
116
|
-
` - uses: ${DOWNLOAD_ARTIFACT}`,
|
|
117
|
-
` if: ${condition}`,
|
|
118
|
-
' with:',
|
|
119
|
-
' pattern: sonar-lint-*',
|
|
120
|
-
' path: reports',
|
|
121
|
-
' merge-multiple: true',
|
|
122
|
-
...sonarMergeSteps(condition),
|
|
123
|
-
' - uses: SonarSource/sonarqube-scan-action@c7ee0f9df90b7aa20e8dcf9695dcfe2e7da5b4f2 # v7.2.1',
|
|
124
|
-
` if: ${condition}`,
|
|
125
|
-
' - uses: SonarSource/sonarqube-quality-gate-action@7a5fffe8e523c40e0c740b6bc2712ab503e52efa # v1.2.1',
|
|
126
|
-
` if: ${condition}`,
|
|
127
|
-
' timeout-minutes: 10',
|
|
128
|
-
].join('\n');
|
|
129
|
-
// Variable strings are inserted after render() expands partials, so resolve Sonar's shared install partial before insertion.
|
|
130
|
-
const read = name => fs.readFileSync(new URL('../templates/app/ci-workflows/' + name, import.meta.url), 'utf8').replaceAll('\r\n', '\n');
|
|
131
|
-
return read('sonar-steps.yml')
|
|
132
|
-
.replace('{{> app/ci-workflows/install-steps.yml}}', () => read('install-steps.yml').trimEnd())
|
|
133
|
-
.replaceAll('{{nodeMajor}}', nodeMajor)
|
|
134
|
-
.replace('{{sonarJobHeader}}', () => header)
|
|
135
|
-
.replace('{{sonarJobSteps}}', () => steps)
|
|
136
|
-
.trimEnd() + '\n';
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/** The app source reader is be-relative; the authored manifest lives one directory above that side. */
|
|
140
|
-
function buildDeclaration(source) {
|
|
141
|
-
if (!source?.read) return {};
|
|
142
|
-
try {
|
|
143
|
-
return parseYaml(source.read('../.starcistacks/application-stacks.yaml')) ?? {};
|
|
144
|
-
} catch (error) {
|
|
145
|
-
if (error.code === 'ENOENT') return {};
|
|
146
|
-
throw error;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/** Static CI values are strings with valid environment names; expressions cannot turn public config into runtime secret reads. */
|
|
151
|
-
function mergeBuildValues(values, entries) {
|
|
152
|
-
for (const [key, value] of Object.entries(entries ?? {})) {
|
|
153
|
-
if (!/^[A-Z][A-Z0-9_]*$/u.test(key) || typeof value !== 'string' || value.includes('${{')) {
|
|
154
|
-
throw refusal('STACK_BUILD_ARG_INVALID', 'CI build values must map environment names to literal strings');
|
|
155
|
-
}
|
|
156
|
-
if (values.has(key) && values.get(key) !== value) {
|
|
157
|
-
throw refusal('STACK_BUILD_ARG_INVALID', 'CI build values disagree for ' + key);
|
|
158
|
-
}
|
|
159
|
-
values.set(key, value);
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/** Image values own their keys; the build-fe gate supplies only missing keys, and sealed members stay unopened. */
|
|
164
|
-
function imageBuildEnv(image, gates) {
|
|
165
|
-
const values = new Map();
|
|
166
|
-
mergeBuildValues(values, image?.buildArgs?.values);
|
|
167
|
-
const fallback = new Map();
|
|
168
|
-
for (const gate of gates.filter(entry => entry.name === 'build-fe')) mergeBuildValues(fallback, gate.env);
|
|
169
|
-
for (const [key, value] of fallback) if (!values.has(key)) values.set(key, value);
|
|
170
|
-
if (!values.size) return '';
|
|
171
|
-
return [' env:', ...[...values.keys()].sort(byCodeUnit).map(key => ' ' + key + ': ' + JSON.stringify(values.get(key)))].join('\n');
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/** Multiple deployed front ends build separately through the installed turbo (a run step has no node_modules/.bin on PATH), so each fe/apps/<app> and its dependencies see its own public values. */
|
|
175
|
-
function frontendBuild(source, app) {
|
|
176
|
-
const declaration = buildDeclaration(source);
|
|
177
|
-
const environments = Object.keys(source?.stack?.imageEnvironments ?? {});
|
|
178
|
-
const environment = environments.length > 1 ? 'production' : environments[0];
|
|
179
|
-
const images = Object.values(declaration.environments?.[environment]?.deploy?.images ?? {});
|
|
180
|
-
const frontend = images.filter(image => app?.sides.fe.apps.some(entry => entry.name === image.app));
|
|
181
|
-
const gates = declaration.release?.gates ?? [];
|
|
182
|
-
const singleEnv = imageBuildEnv(frontend[0], gates);
|
|
183
|
-
if (frontend.length <= 1) return { env: singleEnv, steps: [' - name: build fe', ' run: npm run build:fe', singleEnv].filter(Boolean).join('\n') };
|
|
184
|
-
const steps = frontend.map(image => [
|
|
185
|
-
' - name: build fe ' + image.app,
|
|
186
|
-
' run: npm run codegen --silent && npx --no-install turbo run build --filter=./fe/apps/' + image.app,
|
|
187
|
-
imageBuildEnv(image, gates),
|
|
188
|
-
].filter(Boolean).join('\n')).join('\n');
|
|
189
|
-
return { env: singleEnv, steps };
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/** The per-side lint reports (one artifact per lint matrix side) join into the one reports/lint.sonar.json the Sonar import reads, even when one side wrote none. The local release gate writes the same file with mergeSonarReports (release/lint-report.mjs): keep both identical. */
|
|
193
|
-
function sonarMergeSteps(condition) {
|
|
194
|
-
return [
|
|
195
|
-
' - name: merge Sonar lint reports',
|
|
196
|
-
` if: ${condition}`,
|
|
197
|
-
' run: |',
|
|
198
|
-
" node --input-type=module <<'NODE'",
|
|
199
|
-
" import fs from 'node:fs';",
|
|
200
|
-
" const files = ['reports/lint.be.sonar.json', 'reports/lint.fe.sonar.json'];",
|
|
201
|
-
" const reports = files.filter(file => fs.existsSync(file)).map(file => JSON.parse(fs.readFileSync(file, 'utf8')));",
|
|
202
|
-
' const rules = [...new Map(reports.flatMap(report => report.rules).map(rule => [rule.id, rule])).values()];',
|
|
203
|
-
' const issues = reports.flatMap(report => report.issues);',
|
|
204
|
-
" fs.mkdirSync('reports', { recursive: true });",
|
|
205
|
-
" fs.writeFileSync('reports/lint.sonar.json', JSON.stringify({ rules, issues }, null, 2) + '\\n');",
|
|
206
|
-
' NODE',
|
|
207
|
-
];
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const STANDARD = new URL('../runtime/knowledge/hfs/ci-standard.yaml', import.meta.url);
|
|
211
|
-
/** The per-job settings of the managed CI: ci.jobs.<job>.timeoutMinutes and ci.jobs.test.shards of the manifest over the defaults of the CI standard. */
|
|
212
|
-
function ciJobSettings(declaration) {
|
|
213
|
-
const { defaults, heap } = parseYaml(fs.readFileSync(STANDARD, 'utf8'));
|
|
214
|
-
const jobs = declaration?.ci?.jobs ?? {};
|
|
215
|
-
const timeout = (job) => String(jobs[job]?.timeoutMinutes ?? defaults.timeoutMinutes[job]);
|
|
216
|
-
const shards = Number(jobs.test?.shards ?? defaults.shards);
|
|
217
|
-
return {
|
|
218
|
-
nodeOptions: `--max-old-space-size=${heap.minMiB}`,
|
|
219
|
-
timeoutFast: timeout('fast'), timeoutLint: timeout('lint'), timeoutTest: timeout('test'), timeoutCoverage: timeout('coverage'),
|
|
220
|
-
timeoutBuild: timeout('build'), timeoutSonar: timeout('sonar'), timeoutDbTypes: timeout('db-types'),
|
|
221
|
-
testShards: `[${Array.from({ length: shards }, (_, index) => index + 1).join(', ')}]`, testShardTotal: String(shards),
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/** An omitted quality declaration preserves hosted Sonar, required OIDC Codecov, and the high production dependency audit. */
|
|
226
|
-
export function ciWorkflowCommands(source, lcovReport = 'be/coverage/lcov.info', quality = undefined, { nodeMajor = '22', app } = {}) {
|
|
227
|
-
const configuredSonar = quality === undefined ? { token: 'SONAR_TOKEN', hostVar: 'SONAR_HOST_URL' } : quality.sonar;
|
|
228
|
-
const sonar = configuredSonar?.wiring === 'not-used' || configuredSonar?.mode === 'disabled' ? null : configuredSonar;
|
|
229
|
-
const codecov = quality?.codecov ?? { enabled: true, auth: 'oidc' };
|
|
230
|
-
const audit = quality?.audit ?? { level: 'high', omitDev: true };
|
|
231
|
-
const lcovArtifact = 'coverage-lcov';
|
|
232
|
-
const build = frontendBuild(source, app);
|
|
233
|
-
const settings = ciJobSettings(buildDeclaration(source));
|
|
234
|
-
const side = '${{ matrix.side }}';
|
|
235
|
-
return {
|
|
236
|
-
...settings,
|
|
237
|
-
lcovShardsDir: lcovReport.split('/').slice(0, -1).concat('shards').join('/'),
|
|
238
|
-
// The istanbul JSON a shard writes beside the lcov (STARCI_COVERAGE_SHARD=1): what the coverage job merges.
|
|
239
|
-
shardCoverageReport: lcovReport.split('/').slice(0, -1).concat('coverage-final.json').join('/'),
|
|
240
|
-
ciBuildEnv: build.env,
|
|
241
|
-
ciBuildSteps: build.steps,
|
|
242
|
-
ciLint: exampleCommand(source, 'npm run lint'),
|
|
243
|
-
ciLintSonar: exampleCommand(source, 'npm run lint -- --sonar reports/lint.sonar.json'),
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
`
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
`
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
lcovArtifact,
|
|
274
|
-
|
|
275
|
-
}
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { parseYaml } from '../runtime/engine/yaml.mjs';
|
|
3
|
+
import { refusal } from '../stack/errors.mjs';
|
|
4
|
+
import { byCodeUnit } from '../report/order.mjs';
|
|
5
|
+
|
|
6
|
+
// One command vocabulary for managed CI rendering and the runtime coverage gate.
|
|
7
|
+
const exampleCommand = (source, command) => source?.runtimeExample
|
|
8
|
+
? 'node node_modules/@starci/hfs/runtime/scripts/hfs/example-secrets.mjs --run . -- ' + command
|
|
9
|
+
: command;
|
|
10
|
+
|
|
11
|
+
const UPLOAD_ARTIFACT = 'actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2';
|
|
12
|
+
const DOWNLOAD_ARTIFACT = 'actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0';
|
|
13
|
+
const SONAR_CONDITION = "${{ !cancelled() && startsWith(github.ref, 'refs/tags/v') && env.SONAR_TOKEN != '' }}";
|
|
14
|
+
|
|
15
|
+
/** Optional uploads require reachable upload and authentication endpoints without exposing identity tokens. */
|
|
16
|
+
function codecovProbe(codecov, condition) {
|
|
17
|
+
if (!codecov.optional) return [];
|
|
18
|
+
return [
|
|
19
|
+
' - name: Codecov availability',
|
|
20
|
+
' id: codecov-availability',
|
|
21
|
+
` if: ${condition}`,
|
|
22
|
+
' env:',
|
|
23
|
+
` CODECOV_AUTH: ${codecov.auth}`,
|
|
24
|
+
' run: |',
|
|
25
|
+
" node --input-type=module <<'NODE'",
|
|
26
|
+
" import fs from 'node:fs';",
|
|
27
|
+
' let available = false;',
|
|
28
|
+
' try {',
|
|
29
|
+
" const upload = await fetch('https://ingest.codecov.io/upload/v4', { signal: AbortSignal.timeout(10000) });",
|
|
30
|
+
' available = upload.status < 500;',
|
|
31
|
+
" if (available && process.env.CODECOV_AUTH === 'oidc') {",
|
|
32
|
+
" const url = new URL(process.env.ACTIONS_ID_TOKEN_REQUEST_URL);",
|
|
33
|
+
" url.searchParams.set('audience', 'https://codecov.io');",
|
|
34
|
+
" const identity = await fetch(url, { headers: { Authorization: 'Bearer ' + process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN }, signal: AbortSignal.timeout(10000) });",
|
|
35
|
+
' available = identity.ok && Boolean((await identity.json()).value);',
|
|
36
|
+
' }',
|
|
37
|
+
' } catch { available = false; }',
|
|
38
|
+
" fs.appendFileSync(process.env.GITHUB_OUTPUT, 'up=' + available + '\\n');",
|
|
39
|
+
" if (!available) console.log('::notice::codecov upload skipped: upload endpoint or OIDC token unavailable (services.codecov.optional)');",
|
|
40
|
+
' NODE',
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Optional Sonar only runs analysis when the provider reports UP within ten seconds. */
|
|
45
|
+
function sonarProbe(sonar) {
|
|
46
|
+
if (!sonar.optional) return [];
|
|
47
|
+
return [
|
|
48
|
+
' - name: Sonar availability',
|
|
49
|
+
' id: sonar-availability',
|
|
50
|
+
` if: ${SONAR_CONDITION}`,
|
|
51
|
+
' run: |',
|
|
52
|
+
` if curl -fsS --max-time 10 "$SONAR_HOST_URL/api/system/status" | node -e "let s='';process.stdin.on('data',c=>s+=c);process.stdin.on('end',()=>{try{process.exit(JSON.parse(s).status==='UP'?0:1)}catch{process.exit(1)}})"; then`,
|
|
53
|
+
' echo "up=true" >> "$GITHUB_OUTPUT"',
|
|
54
|
+
' else',
|
|
55
|
+
' echo "up=false" >> "$GITHUB_OUTPUT"',
|
|
56
|
+
' echo "::notice::sonar skipped: $SONAR_HOST_URL not reachable (services.sonar.optional)"',
|
|
57
|
+
' fi',
|
|
58
|
+
];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Codecov reports missing coverage without failing twice; uploads retain the release-tag condition and required authentication. */
|
|
62
|
+
function codecovSteps(lcovReport, codecov) {
|
|
63
|
+
if (codecov.enabled === false || codecov.wiring === 'not-used') return [
|
|
64
|
+
' - name: coverage upload',
|
|
65
|
+
' run: echo "codecov disabled in .starcistacks/application-stacks.yaml"',
|
|
66
|
+
].join('\n');
|
|
67
|
+
const condition = `\${{ !cancelled() && startsWith(github.ref, 'refs/tags/v') && hashFiles('${lcovReport}') != '' }}`;
|
|
68
|
+
const uploadCondition = codecov.optional ? condition.replace(' }}', " && steps.codecov-availability.outputs.up == 'true' }}") : condition;
|
|
69
|
+
return [
|
|
70
|
+
' - name: coverage upload skipped',
|
|
71
|
+
` if: \${{ !cancelled() && hashFiles('${lcovReport}') == '' }}`,
|
|
72
|
+
' run: |',
|
|
73
|
+
` echo "::notice::codecov upload skipped: no coverage file at ${lcovReport} (the test step failed or produced none)"`,
|
|
74
|
+
' exit 0',
|
|
75
|
+
...codecovProbe(codecov, condition),
|
|
76
|
+
' - name: coverage upload',
|
|
77
|
+
...(codecov.optional ? [' id: codecov-upload', ' continue-on-error: true'] : []),
|
|
78
|
+
' uses: codecov/codecov-action@0fb7174895f61a3b6b78fc075e0cd60383518dac # v5.5.5',
|
|
79
|
+
` if: ${uploadCondition}`,
|
|
80
|
+
' with:',
|
|
81
|
+
codecov.auth === 'token' ? ` token: \${{ secrets.${codecov.token} }}` : ' use_oidc: true',
|
|
82
|
+
` files: ${lcovReport}`,
|
|
83
|
+
' disable_search: true',
|
|
84
|
+
` fail_ci_if_error: ${!codecov.optional}`,
|
|
85
|
+
...(codecov.optional ? [
|
|
86
|
+
' - name: Codecov upload unavailable',
|
|
87
|
+
" if: ${{ !cancelled() && steps.codecov-upload.outcome == 'failure' }}",
|
|
88
|
+
' run: |',
|
|
89
|
+
' echo "::notice::codecov upload skipped: upload unavailable (services.codecov.optional)"',
|
|
90
|
+
] : []),
|
|
91
|
+
].join('\n');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Sonar consumes the test coverage and fast lint artifacts only on release tags with a configured token. */
|
|
95
|
+
function sonarJob(sonar, lcovReport, lcovArtifact, nodeMajor, timeout) {
|
|
96
|
+
if (!sonar) return '';
|
|
97
|
+
const condition = sonar.optional ? SONAR_CONDITION.replace(' }}', " && steps.sonar-availability.outputs.up == 'true' }}") : SONAR_CONDITION;
|
|
98
|
+
const host = sonar.provider === 'sonarcloud' ? 'https://sonarcloud.io' : `\${{ vars.${sonar.hostVar} }}`;
|
|
99
|
+
const header = [
|
|
100
|
+
' sonar:',
|
|
101
|
+
' needs: [lint, coverage]',
|
|
102
|
+
` timeout-minutes: ${timeout}`,
|
|
103
|
+
' runs-on: ubuntu-latest',
|
|
104
|
+
' env:',
|
|
105
|
+
` SONAR_TOKEN: \${{ secrets.${sonar.token} }}`,
|
|
106
|
+
` SONAR_HOST_URL: ${host}`,
|
|
107
|
+
' steps:',
|
|
108
|
+
].join('\n');
|
|
109
|
+
const steps = [
|
|
110
|
+
...sonarProbe(sonar),
|
|
111
|
+
` - uses: ${DOWNLOAD_ARTIFACT}`,
|
|
112
|
+
` if: ${condition}`,
|
|
113
|
+
' with:',
|
|
114
|
+
` name: ${lcovArtifact}`,
|
|
115
|
+
` path: ${lcovReport.split('/').slice(0, -1).join('/') || '.'}`,
|
|
116
|
+
` - uses: ${DOWNLOAD_ARTIFACT}`,
|
|
117
|
+
` if: ${condition}`,
|
|
118
|
+
' with:',
|
|
119
|
+
' pattern: sonar-lint-*',
|
|
120
|
+
' path: reports',
|
|
121
|
+
' merge-multiple: true',
|
|
122
|
+
...sonarMergeSteps(condition),
|
|
123
|
+
' - uses: SonarSource/sonarqube-scan-action@c7ee0f9df90b7aa20e8dcf9695dcfe2e7da5b4f2 # v7.2.1',
|
|
124
|
+
` if: ${condition}`,
|
|
125
|
+
' - uses: SonarSource/sonarqube-quality-gate-action@7a5fffe8e523c40e0c740b6bc2712ab503e52efa # v1.2.1',
|
|
126
|
+
` if: ${condition}`,
|
|
127
|
+
' timeout-minutes: 10',
|
|
128
|
+
].join('\n');
|
|
129
|
+
// Variable strings are inserted after render() expands partials, so resolve Sonar's shared install partial before insertion.
|
|
130
|
+
const read = name => fs.readFileSync(new URL('../templates/app/ci-workflows/' + name, import.meta.url), 'utf8').replaceAll('\r\n', '\n');
|
|
131
|
+
return read('sonar-steps.yml')
|
|
132
|
+
.replace('{{> app/ci-workflows/install-steps.yml}}', () => read('install-steps.yml').trimEnd())
|
|
133
|
+
.replaceAll('{{nodeMajor}}', nodeMajor)
|
|
134
|
+
.replace('{{sonarJobHeader}}', () => header)
|
|
135
|
+
.replace('{{sonarJobSteps}}', () => steps)
|
|
136
|
+
.trimEnd() + '\n';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** The app source reader is be-relative; the authored manifest lives one directory above that side. */
|
|
140
|
+
function buildDeclaration(source) {
|
|
141
|
+
if (!source?.read) return {};
|
|
142
|
+
try {
|
|
143
|
+
return parseYaml(source.read('../.starcistacks/application-stacks.yaml')) ?? {};
|
|
144
|
+
} catch (error) {
|
|
145
|
+
if (error.code === 'ENOENT') return {};
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Static CI values are strings with valid environment names; expressions cannot turn public config into runtime secret reads. */
|
|
151
|
+
function mergeBuildValues(values, entries) {
|
|
152
|
+
for (const [key, value] of Object.entries(entries ?? {})) {
|
|
153
|
+
if (!/^[A-Z][A-Z0-9_]*$/u.test(key) || typeof value !== 'string' || value.includes('${{')) {
|
|
154
|
+
throw refusal('STACK_BUILD_ARG_INVALID', 'CI build values must map environment names to literal strings');
|
|
155
|
+
}
|
|
156
|
+
if (values.has(key) && values.get(key) !== value) {
|
|
157
|
+
throw refusal('STACK_BUILD_ARG_INVALID', 'CI build values disagree for ' + key);
|
|
158
|
+
}
|
|
159
|
+
values.set(key, value);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Image values own their keys; the build-fe gate supplies only missing keys, and sealed members stay unopened. */
|
|
164
|
+
function imageBuildEnv(image, gates) {
|
|
165
|
+
const values = new Map();
|
|
166
|
+
mergeBuildValues(values, image?.buildArgs?.values);
|
|
167
|
+
const fallback = new Map();
|
|
168
|
+
for (const gate of gates.filter(entry => entry.name === 'build-fe')) mergeBuildValues(fallback, gate.env);
|
|
169
|
+
for (const [key, value] of fallback) if (!values.has(key)) values.set(key, value);
|
|
170
|
+
if (!values.size) return '';
|
|
171
|
+
return [' env:', ...[...values.keys()].sort(byCodeUnit).map(key => ' ' + key + ': ' + JSON.stringify(values.get(key)))].join('\n');
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Multiple deployed front ends build separately through the installed turbo (a run step has no node_modules/.bin on PATH), so each fe/apps/<app> and its dependencies see its own public values. */
|
|
175
|
+
function frontendBuild(source, app) {
|
|
176
|
+
const declaration = buildDeclaration(source);
|
|
177
|
+
const environments = Object.keys(source?.stack?.imageEnvironments ?? {});
|
|
178
|
+
const environment = environments.length > 1 ? 'production' : environments[0];
|
|
179
|
+
const images = Object.values(declaration.environments?.[environment]?.deploy?.images ?? {});
|
|
180
|
+
const frontend = images.filter(image => app?.sides.fe.apps.some(entry => entry.name === image.app));
|
|
181
|
+
const gates = declaration.release?.gates ?? [];
|
|
182
|
+
const singleEnv = imageBuildEnv(frontend[0], gates);
|
|
183
|
+
if (frontend.length <= 1) return { env: singleEnv, steps: [' - name: build fe', ' run: npm run build:fe', singleEnv].filter(Boolean).join('\n') };
|
|
184
|
+
const steps = frontend.map(image => [
|
|
185
|
+
' - name: build fe ' + image.app,
|
|
186
|
+
' run: npm run codegen --silent && npx --no-install turbo run build --filter=./fe/apps/' + image.app,
|
|
187
|
+
imageBuildEnv(image, gates),
|
|
188
|
+
].filter(Boolean).join('\n')).join('\n');
|
|
189
|
+
return { env: singleEnv, steps };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** The per-side lint reports (one artifact per lint matrix side) join into the one reports/lint.sonar.json the Sonar import reads, even when one side wrote none. The local release gate writes the same file with mergeSonarReports (release/lint-report.mjs): keep both identical. */
|
|
193
|
+
function sonarMergeSteps(condition) {
|
|
194
|
+
return [
|
|
195
|
+
' - name: merge Sonar lint reports',
|
|
196
|
+
` if: ${condition}`,
|
|
197
|
+
' run: |',
|
|
198
|
+
" node --input-type=module <<'NODE'",
|
|
199
|
+
" import fs from 'node:fs';",
|
|
200
|
+
" const files = ['reports/lint.be.sonar.json', 'reports/lint.fe.sonar.json'];",
|
|
201
|
+
" const reports = files.filter(file => fs.existsSync(file)).map(file => JSON.parse(fs.readFileSync(file, 'utf8')));",
|
|
202
|
+
' const rules = [...new Map(reports.flatMap(report => report.rules).map(rule => [rule.id, rule])).values()];',
|
|
203
|
+
' const issues = reports.flatMap(report => report.issues);',
|
|
204
|
+
" fs.mkdirSync('reports', { recursive: true });",
|
|
205
|
+
" fs.writeFileSync('reports/lint.sonar.json', JSON.stringify({ rules, issues }, null, 2) + '\\n');",
|
|
206
|
+
' NODE',
|
|
207
|
+
];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const STANDARD = new URL('../runtime/knowledge/hfs/ci-standard.yaml', import.meta.url);
|
|
211
|
+
/** The per-job settings of the managed CI: ci.jobs.<job>.timeoutMinutes and ci.jobs.test.shards of the manifest over the defaults of the CI standard. */
|
|
212
|
+
function ciJobSettings(declaration) {
|
|
213
|
+
const { defaults, heap } = parseYaml(fs.readFileSync(STANDARD, 'utf8'));
|
|
214
|
+
const jobs = declaration?.ci?.jobs ?? {};
|
|
215
|
+
const timeout = (job) => String(jobs[job]?.timeoutMinutes ?? defaults.timeoutMinutes[job]);
|
|
216
|
+
const shards = Number(jobs.test?.shards ?? defaults.shards);
|
|
217
|
+
return {
|
|
218
|
+
nodeOptions: `--max-old-space-size=${heap.minMiB}`,
|
|
219
|
+
timeoutFast: timeout('fast'), timeoutLint: timeout('lint'), timeoutTest: timeout('test'), timeoutCoverage: timeout('coverage'),
|
|
220
|
+
timeoutBuild: timeout('build'), timeoutSonar: timeout('sonar'), timeoutDbTypes: timeout('db-types'),
|
|
221
|
+
testShards: `[${Array.from({ length: shards }, (_, index) => index + 1).join(', ')}]`, testShardTotal: String(shards),
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** An omitted quality declaration preserves hosted Sonar, required OIDC Codecov, and the high production dependency audit. */
|
|
226
|
+
export function ciWorkflowCommands(source, lcovReport = 'be/coverage/lcov.info', quality = undefined, { nodeMajor = '22', app } = {}) {
|
|
227
|
+
const configuredSonar = quality === undefined ? { token: 'SONAR_TOKEN', hostVar: 'SONAR_HOST_URL' } : quality.sonar;
|
|
228
|
+
const sonar = configuredSonar?.wiring === 'not-used' || configuredSonar?.mode === 'disabled' ? null : configuredSonar;
|
|
229
|
+
const codecov = quality?.codecov ?? { enabled: true, auth: 'oidc' };
|
|
230
|
+
const audit = quality?.audit ?? { level: 'high', omitDev: true };
|
|
231
|
+
const lcovArtifact = 'coverage-lcov';
|
|
232
|
+
const build = frontendBuild(source, app);
|
|
233
|
+
const settings = ciJobSettings(buildDeclaration(source));
|
|
234
|
+
const side = '${{ matrix.side }}';
|
|
235
|
+
return {
|
|
236
|
+
...settings,
|
|
237
|
+
lcovShardsDir: lcovReport.split('/').slice(0, -1).concat('shards').join('/'),
|
|
238
|
+
// The istanbul JSON a shard writes beside the lcov (STARCI_COVERAGE_SHARD=1): what the coverage job merges.
|
|
239
|
+
shardCoverageReport: lcovReport.split('/').slice(0, -1).concat('coverage-final.json').join('/'),
|
|
240
|
+
ciBuildEnv: build.env,
|
|
241
|
+
ciBuildSteps: build.steps,
|
|
242
|
+
ciLint: exampleCommand(source, 'npm run lint'),
|
|
243
|
+
ciLintSonar: exampleCommand(source, 'npm run lint -- --sonar reports/lint.sonar.json'),
|
|
244
|
+
// The coverage judgement alone over the merged report: the fast and lint jobs already ran every other rule of the app check.
|
|
245
|
+
ciCoverageProof: exampleCommand(source, 'npx starci app coverage verify ' + lcovReport),
|
|
246
|
+
ciDatabaseLint: exampleCommand(source, 'npm run db:lint'),
|
|
247
|
+
auditCommand: `npm audit${audit.omitDev ? ' --omit=dev' : ''} --audit-level=${audit.level}`,
|
|
248
|
+
sonarTokenSecret: sonar?.token ?? 'SONAR_TOKEN',
|
|
249
|
+
sonarHostVar: sonar?.hostVar ?? 'SONAR_HOST_URL',
|
|
250
|
+
sonarEnv: sonar ? ` env:\n SONAR_TOKEN: \${{ secrets.${sonar.token} }}` : '',
|
|
251
|
+
// One lint job per side (the lint matrix): with a Sonar token each side writes its own report, uploaded as sonar-lint-<side> and merged by the sonar job.
|
|
252
|
+
ciLintSteps: [
|
|
253
|
+
' - name: lint',
|
|
254
|
+
...(sonar ? [" if: env.SONAR_TOKEN == ''"] : []),
|
|
255
|
+
` run: ${exampleCommand(source, 'npm run lint -- --side ' + side)}`,
|
|
256
|
+
' env:',
|
|
257
|
+
` NODE_OPTIONS: ${settings.nodeOptions}`,
|
|
258
|
+
...(sonar ? [
|
|
259
|
+
' - name: lint with Sonar import',
|
|
260
|
+
" if: env.SONAR_TOKEN != ''",
|
|
261
|
+
` run: ${exampleCommand(source, 'npm run lint -- --side ' + side + ' --sonar reports/lint.' + side + '.sonar.json')}`,
|
|
262
|
+
' env:',
|
|
263
|
+
` NODE_OPTIONS: ${settings.nodeOptions}`,
|
|
264
|
+
` - uses: ${UPLOAD_ARTIFACT}`,
|
|
265
|
+
// Inside an if: expression the matrix value is an argument of format(), never a nested ${{ }} (actionlint: a nested expression makes the condition always true).
|
|
266
|
+
" if: always() && env.SONAR_TOKEN != '' && hashFiles(format('reports/lint.{0}.sonar.json', matrix.side)) != ''",
|
|
267
|
+
' with:',
|
|
268
|
+
` name: sonar-lint-${side}`,
|
|
269
|
+
` path: reports/lint.${side}.sonar.json`,
|
|
270
|
+
] : []),
|
|
271
|
+
].join('\n'),
|
|
272
|
+
codecovSteps: codecovSteps(lcovReport, codecov),
|
|
273
|
+
sonarJob: sonarJob(sonar, lcovReport, lcovArtifact, nodeMajor, settings.timeoutSonar),
|
|
274
|
+
lcovArtifact,
|
|
275
|
+
};
|
|
276
|
+
}
|
package/sync/hygiene.mjs
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
// being, an .enc that is no sops envelope, a line that matches a secret pattern), so no plaintext secret reaches the history whatever
|
|
6
6
|
// .gitignore says (`git add -f`, a path tracked before a rule tightened). There is no override. At an HFS app root (hfs.json) it also
|
|
7
7
|
// refuses a back-end logic file the index adds without its unit spec beside it (BE_UNIT_SPEC_NOT_STAGED,
|
|
8
|
-
// scripts/hfs/rules/staged-specs.mjs over the coverage scope), naming the file. The pre-commit hook judges the staged files;
|
|
8
|
+
// scripts/hfs/rules/staged-specs.mjs over the coverage scope; a constants-only file has nothing to test), naming the file. The pre-commit hook judges the staged files;
|
|
9
9
|
// scripts/gates/hfs-sync.mjs judges every tracked file.
|
|
10
10
|
//
|
|
11
11
|
// Run inside a full runtime checkout — the repository under judgment is the checkout — it also reports the
|
|
@@ -19,6 +19,7 @@ import { secretFileFindings } from '../runtime/scripts/hfs/rules/secrets.mjs';
|
|
|
19
19
|
import { stagedSpecFindings } from '../runtime/scripts/hfs/rules/staged-specs.mjs';
|
|
20
20
|
import { loadSlotManifest } from '../runtime/scripts/hfs/slots.mjs';
|
|
21
21
|
import { runGit } from '../runtime/scripts/api/git/lib.mjs';
|
|
22
|
+
import { loadTypescript } from '../runtime/scripts/lib/package-at.mjs';
|
|
22
23
|
|
|
23
24
|
// One git call through the runtime's git owner (scripts/api/git/lib.mjs): stdout, or the failure thrown the way execFileSync threw it (error.status carries the exit code). The caller's own env is passed on unchanged: a hook runs with GIT_INDEX_FILE set, which the staged reads must keep seeing.
|
|
24
25
|
const git = (cwd, args, options = {}) => {
|
|
@@ -75,7 +76,12 @@ const addedFiles = cwd => gitList(cwd, ['diff', '--cached', '--name-only', '--di
|
|
|
75
76
|
/** The new back-end logic files staged without their unit spec, at an HFS app root only (a repository without hfs.json has no slots). */
|
|
76
77
|
function missingSpecFindings(cwd, { added = addedFiles(cwd), indexed, manifest } = {}) {
|
|
77
78
|
if (!fs.existsSync(path.join(cwd, 'hfs.json')) || !added.some(file => file.startsWith('be/'))) return [];
|
|
78
|
-
|
|
79
|
+
const staged = (file) => {
|
|
80
|
+
try { return git(cwd, ['show', `:${file}`]); } catch { return null; }
|
|
81
|
+
};
|
|
82
|
+
let ts = null;
|
|
83
|
+
try { ts = loadTypescript(cwd, import.meta.dirname); } catch { ts = null; }
|
|
84
|
+
return stagedSpecFindings(manifest ?? loadSlotManifest(), added, indexed ?? new Set(trackedFiles(cwd)), { ts, staged });
|
|
79
85
|
}
|
|
80
86
|
|
|
81
87
|
const MAX_STAGED_BYTES = 1024 * 1024;
|
package/sync/index.mjs
CHANGED
|
@@ -19,6 +19,7 @@ import { createHash } from 'node:crypto';
|
|
|
19
19
|
import { createRequire } from 'node:module';
|
|
20
20
|
import fs from 'node:fs';
|
|
21
21
|
import path from 'node:path';
|
|
22
|
+
import { fileURLToPath } from 'node:url';
|
|
22
23
|
import { braceVariants } from '../runtime/scripts/lib/glob.mjs';
|
|
23
24
|
import { codecovPaths, coverageComponents, jestCoverage, sonarCoverageExclusions } from '../runtime/scripts/hfs/coverage-scope.mjs';
|
|
24
25
|
import { APP_SCOPE, SIDES, litePresenceOf, loadSlotManifest, resolveRepoDeclaration, slotInEdition } from '../runtime/scripts/hfs/slots.mjs';
|
|
@@ -159,19 +160,19 @@ export async function loadPresets(root) {
|
|
|
159
160
|
}
|
|
160
161
|
|
|
161
162
|
/**
|
|
162
|
-
* The jest preset's Sonar exclusions as @starci/hfs ships them: `@starci/jest-preset/sonar-exclusions.
|
|
163
|
-
* packages/hfs/runtime by scripts/hfs/sync-runtime.mjs, so a fresh `starci app scaffold` (nothing installed
|
|
164
|
-
* same list the app's installed preset answers. A missing copy is a refusal, never a render without it.
|
|
163
|
+
* The jest preset's Sonar exclusions as @starci/hfs ships them: `@starci/jest-preset/sonar-exclusions.cjs` (a require-free module),
|
|
164
|
+
* copied byte for byte into packages/hfs/runtime by scripts/hfs/sync-runtime.mjs, so a fresh `starci app scaffold` (nothing installed
|
|
165
|
+
* yet) and an upgrade render the same list the app's installed preset answers. A missing copy is a refusal, never a render without it.
|
|
165
166
|
*/
|
|
166
|
-
const BUNDLED_SONAR_EXCLUSIONS = new URL('../runtime/packages/jest-preset/sonar-exclusions.
|
|
167
|
+
const BUNDLED_SONAR_EXCLUSIONS = new URL('../runtime/packages/jest-preset/sonar-exclusions.cjs', import.meta.url);
|
|
167
168
|
|
|
168
169
|
/** The presets ({ sonarExclusions }) of the jest preset version this @starci/hfs pins, read from its bundled copy. */
|
|
169
170
|
export function bundledPresets() {
|
|
170
171
|
let list;
|
|
171
172
|
try {
|
|
172
|
-
list =
|
|
173
|
+
list = createRequire(import.meta.url)(fileURLToPath(BUNDLED_SONAR_EXCLUSIONS));
|
|
173
174
|
} catch {
|
|
174
|
-
throw new SyncError('HFS_SYNC_PRESET_MISSING', '@starci/hfs ships no runtime/packages/jest-preset/sonar-exclusions.
|
|
175
|
+
throw new SyncError('HFS_SYNC_PRESET_MISSING', '@starci/hfs ships no runtime/packages/jest-preset/sonar-exclusions.cjs; reinstall @starci/hfs at its published version');
|
|
175
176
|
}
|
|
176
177
|
return { sonarExclusions: list.join(',') };
|
|
177
178
|
}
|
package/templates/be/skeleton/src/modules/platform/database/migrate-connections.client.spec.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Test } from "@nestjs/testing"
|
|
2
|
+
import { mock } from "@starci/jest-preset"
|
|
3
|
+
import { Secret } from "@modules/platform/config"
|
|
4
|
+
import type { DatabaseConnectionOptions } from "./database.options"
|
|
5
|
+
import type { ConnectionOpener, ConnectionSource } from "./database.port"
|
|
6
|
+
import { migrateConnections } from "./migrate-connections.client"
|
|
7
|
+
|
|
8
|
+
const MIGRATE_CLIENT = Symbol("migrate-connections")
|
|
9
|
+
|
|
10
|
+
const setup = async () => {
|
|
11
|
+
const moduleRef = await Test.createTestingModule({
|
|
12
|
+
providers: [{ provide: MIGRATE_CLIENT, useFactory: () => ({ migrateConnections }) }],
|
|
13
|
+
}).compile()
|
|
14
|
+
return moduleRef.get<{ readonly migrateConnections: typeof migrateConnections }>(MIGRATE_CLIENT)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const connection = (name: string): DatabaseConnectionOptions => ({
|
|
18
|
+
name,
|
|
19
|
+
url: new Secret(`postgres://localhost:5432/${name}`),
|
|
20
|
+
entities: [],
|
|
21
|
+
migrations: [],
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
describe("migrateConnections", () => {
|
|
25
|
+
it("runs the pending migrations of each connection in order and answers their names by connection", async () => {
|
|
26
|
+
const client = await setup()
|
|
27
|
+
const primary = mock<ConnectionSource>()
|
|
28
|
+
primary.runMigrations.mockResolvedValue([{ id: 1, timestamp: 1, name: "Init1" }])
|
|
29
|
+
const archive = mock<ConnectionSource>()
|
|
30
|
+
archive.runMigrations.mockResolvedValue([])
|
|
31
|
+
const sources = new Map([
|
|
32
|
+
["primary", primary],
|
|
33
|
+
["archive", archive],
|
|
34
|
+
])
|
|
35
|
+
const opener = mock<ConnectionOpener>()
|
|
36
|
+
opener.open.mockImplementation(
|
|
37
|
+
(target: DatabaseConnectionOptions) => sources.get(target.name) ?? mock<ConnectionSource>(),
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
const applied = await client.migrateConnections([connection("primary"), connection("archive")], opener)
|
|
41
|
+
|
|
42
|
+
expect(applied).toEqual({ primary: ["Init1"], archive: [] })
|
|
43
|
+
expect(primary.initialize).toHaveBeenCalledTimes(1)
|
|
44
|
+
expect(primary.destroy).toHaveBeenCalledTimes(1)
|
|
45
|
+
expect(archive.destroy).toHaveBeenCalledTimes(1)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it("destroys the data source of a connection whose migration fails, and fails", async () => {
|
|
49
|
+
const client = await setup()
|
|
50
|
+
const failing = mock<ConnectionSource>()
|
|
51
|
+
failing.runMigrations.mockRejectedValue(new TypeError("relation already exists"))
|
|
52
|
+
const opener = mock<ConnectionOpener>()
|
|
53
|
+
opener.open.mockReturnValue(failing)
|
|
54
|
+
|
|
55
|
+
await expect(client.migrateConnections([connection("primary")], opener)).rejects.toThrow(
|
|
56
|
+
"relation already exists",
|
|
57
|
+
)
|
|
58
|
+
expect(failing.destroy).toHaveBeenCalledTimes(1)
|
|
59
|
+
})
|
|
60
|
+
})
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
["**/*.spec.ts", "**/*.e2e-spec.ts", "**/dist/**", "**/coverage/**"]
|