@ucm-kit/cli 0.1.38 → 0.1.39
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 +43 -5
- package/package.json +5 -2
- package/src/guide.mjs +1 -0
- package/src/init.mjs +185 -14
- package/src/rapport-gitlab.mjs +152 -0
- package/src/ucm.mjs +27 -4
package/README.md
CHANGED
|
@@ -11,8 +11,8 @@ next to the component's code. This command reads those files and says whether
|
|
|
11
11
|
they still hold together.
|
|
12
12
|
|
|
13
13
|
```sh
|
|
14
|
-
npx --yes @ucm-kit/cli@0.1.
|
|
15
|
-
npx --yes @ucm-kit/cli@0.1.
|
|
14
|
+
npx --yes @ucm-kit/cli@0.1.39 init
|
|
15
|
+
npx --yes @ucm-kit/cli@0.1.39 check --report ci-report.md
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
Pin an exact version, without `^`. A range would let npx install a build this
|
|
@@ -39,9 +39,10 @@ check`.
|
|
|
39
39
|
| `ucm tokens css --out <file>` | Writes the CSS stylesheet of the tokens and their modes |
|
|
40
40
|
| `ucm aides [<aide>]` | Lists the implementation guides, or prints one |
|
|
41
41
|
| `ucm guide <contract>` | Prints what an agent reads before implementing that contract |
|
|
42
|
+
| `ucm rapport-gitlab` | Posts the report as a note on a GitLab merge request |
|
|
42
43
|
| `ucm --help` | Prints the above |
|
|
43
44
|
|
|
44
|
-
`ucm init` takes
|
|
45
|
+
`ucm init` takes five options:
|
|
45
46
|
|
|
46
47
|
| Option | Effect |
|
|
47
48
|
|---|---|
|
|
@@ -49,6 +50,7 @@ check`.
|
|
|
49
50
|
| `--tokens <dir>` | The folder that holds `tokens.json` |
|
|
50
51
|
| `--implementation <pattern>` | Where a contract's implementation lives |
|
|
51
52
|
| `--sans-agents` | Writes neither the agent relays, nor `.ucm/conventions.md`, nor the templates |
|
|
53
|
+
| `--forge github\|gitlab` | The forge whose CI is written; see [GitLab](#gitlab) |
|
|
52
54
|
|
|
53
55
|
The first two take a folder, because a folder is what a repository arranges.
|
|
54
56
|
`--tokens` appends the file name before writing it, so the `tokens` field of
|
|
@@ -62,7 +64,7 @@ not write React states its own extension here, rather than carrying a `.tsx`
|
|
|
62
64
|
that was wrong the day it was installed:
|
|
63
65
|
|
|
64
66
|
```sh
|
|
65
|
-
npx --yes @ucm-kit/cli@0.1.
|
|
67
|
+
npx --yes @ucm-kit/cli@0.1.39 init --components Sources/DesignSystem --implementation '{dir}/{id}.swift'
|
|
66
68
|
```
|
|
67
69
|
|
|
68
70
|
All three act only on a first install: `ucm init` never overwrites an existing
|
|
@@ -222,7 +224,8 @@ is, and the command names what it left alone.
|
|
|
222
224
|
| `.ucm/conventions.md` | The repository's stack and writings, with its instructions in a comment |
|
|
223
225
|
| `.ucm/gabarits/` | The templates of the installed stack adapter, when it publishes some |
|
|
224
226
|
|
|
225
|
-
The workflow is yours once written. It will never be overwritten.
|
|
227
|
+
The workflow is yours once written. It will never be overwritten. On GitLab,
|
|
228
|
+
`.gitlab/ucm.gitlab-ci.yml` and `.gitlab-ci.yml` replace the GitHub workflow.
|
|
226
229
|
|
|
227
230
|
An installed adapter that fails to load is reported, and the rest is
|
|
228
231
|
installed. The command then prints the lines it does not write, each with its
|
|
@@ -308,6 +311,41 @@ The report also relays two things it does not measure itself: the warnings the
|
|
|
308
311
|
export wrote into the contract, and the verdict of the repository's own tests
|
|
309
312
|
when an orchestrator passes it in through `UCM_ECHECS_DE_TESTS`.
|
|
310
313
|
|
|
314
|
+
## GitLab
|
|
315
|
+
|
|
316
|
+
`ucm init` writes the CI of one forge. `--forge` decides; without it, the host
|
|
317
|
+
of the `origin` remote does, then the presence of `.gitlab-ci.yml`, and GitHub
|
|
318
|
+
otherwise. The command names the forge and the signal it followed.
|
|
319
|
+
|
|
320
|
+
For GitLab it writes `.gitlab/ucm.gitlab-ci.yml`, a single `ucm` job with no
|
|
321
|
+
global key, so the project's other jobs keep their rules. The job runs in merge
|
|
322
|
+
request pipelines and on the default branch, in the default `test` stage. A
|
|
323
|
+
new `.gitlab-ci.yml` includes it; an existing one is left alone, and the
|
|
324
|
+
command prints the `include` line to add. It also prints three settings it
|
|
325
|
+
cannot make:
|
|
326
|
+
|
|
327
|
+
- create the CI/CD variable `UCM_GITLAB_TOKEN`, masked and not protected,
|
|
328
|
+
holding a token with the `api` scope. Export branches are not protected, so a
|
|
329
|
+
protected variable would be empty there;
|
|
330
|
+
- tick "Pipelines must succeed" in the merge request settings. Without it, a red
|
|
331
|
+
report does not block the merge it says is blocked;
|
|
332
|
+
- keep `test` in `stages:` when `.gitlab-ci.yml` declares them: GitLab refuses a
|
|
333
|
+
pipeline whose job names a missing stage.
|
|
334
|
+
|
|
335
|
+
`after_script` writes a minimal report when the check stopped before writing
|
|
336
|
+
one, then posts the report:
|
|
337
|
+
|
|
338
|
+
```sh
|
|
339
|
+
npx --yes @ucm-kit/cli@0.1.39 rapport-gitlab --projet "$CI_PROJECT_ID" --merge-request "$CI_MERGE_REQUEST_IID" --fichier ci-report.md --api "$CI_API_V4_URL"
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
The command reads the token from `UCM_GITLAB_TOKEN` and never prints it. It
|
|
343
|
+
replaces the note that the token's account wrote with the `<!-- ucm-rapport -->`
|
|
344
|
+
marker, across every page of notes, and creates one otherwise. Without the
|
|
345
|
+
variable it says so and exits with `0`: the report stays in the job artifacts.
|
|
346
|
+
A refused token exits with `1` and names the fix. `--api` defaults to
|
|
347
|
+
`https://gitlab.com/api/v4`.
|
|
348
|
+
|
|
311
349
|
## Optional stack adapters
|
|
312
350
|
|
|
313
351
|
The first five checks above read contracts and tokens only, so they work
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ucm-kit/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.39",
|
|
4
4
|
"description": "La ligne de commande UCM : initialiser un repository, lister ce qu'il doit couvrir.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -29,6 +29,9 @@
|
|
|
29
29
|
"test": "node scripts/run-tests.cjs"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@ucm-kit/core": "0.1.
|
|
32
|
+
"@ucm-kit/core": "0.1.35"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"yaml": "^2.9.1"
|
|
33
36
|
}
|
|
34
37
|
}
|
package/src/guide.mjs
CHANGED
package/src/init.mjs
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
* comment nommer ses fichiers d'implémentation, au lieu de porter un `.tsx`
|
|
24
24
|
* faux dès le jour de son installation.
|
|
25
25
|
*/
|
|
26
|
+
import { execFileSync } from "node:child_process";
|
|
26
27
|
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
27
28
|
import { dirname, join } from "node:path";
|
|
28
29
|
|
|
@@ -81,6 +82,7 @@ function cheminAcceptable(valeur) {
|
|
|
81
82
|
export function lireArgumentsInit(arguments_) {
|
|
82
83
|
const chemins = {};
|
|
83
84
|
let sansAgents = false;
|
|
85
|
+
let forge;
|
|
84
86
|
|
|
85
87
|
for (let i = 0; i < arguments_.length; i += 1) {
|
|
86
88
|
const argument = arguments_[i];
|
|
@@ -88,6 +90,15 @@ export function lireArgumentsInit(arguments_) {
|
|
|
88
90
|
sansAgents = true;
|
|
89
91
|
continue;
|
|
90
92
|
}
|
|
93
|
+
if (argument === "--forge") {
|
|
94
|
+
const valeur = arguments_[i + 1];
|
|
95
|
+
if (!FORGES.includes(valeur)) {
|
|
96
|
+
return { erreur: `--forge attend github ou gitlab${valeur === undefined ? "." : ` : ${valeur} n'en est pas une.`}` };
|
|
97
|
+
}
|
|
98
|
+
forge = valeur;
|
|
99
|
+
i += 1;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
91
102
|
const cle = OPTIONS_DE_DOSSIER[argument];
|
|
92
103
|
if (!cle && argument !== "--implementation") {
|
|
93
104
|
return { erreur: `Argument inconnu : ${argument}` };
|
|
@@ -123,7 +134,45 @@ export function lireArgumentsInit(arguments_) {
|
|
|
123
134
|
chemins[cle] = cle === "tokens" ? `${resolu}/${NOM_FICHIER_TOKENS}` : resolu;
|
|
124
135
|
}
|
|
125
136
|
|
|
126
|
-
return { chemins, sansAgents };
|
|
137
|
+
return { chemins, sansAgents, forge };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const FORGES = ["github", "gitlab"];
|
|
141
|
+
|
|
142
|
+
/** Le fichier qu'`init` écrit pour GitLab, et que `.gitlab-ci.yml` inclut. */
|
|
143
|
+
export const CHEMIN_CI_GITLAB = ".gitlab/ucm.gitlab-ci.yml";
|
|
144
|
+
|
|
145
|
+
/** L'URL du remote `origin`, ou `null` sans git ni remote. */
|
|
146
|
+
function remoteOrigin(racine) {
|
|
147
|
+
try {
|
|
148
|
+
return execFileSync("git", ["remote", "get-url", "origin"], { cwd: racine, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim() || null;
|
|
149
|
+
} catch {
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** L'hôte d'une URL de remote, en HTTPS, SSH ou forme `git@hôte:chemin`. */
|
|
155
|
+
function hoteDuRemote(url) {
|
|
156
|
+
return /^(?:[a-z][a-z+]*:\/\/)?(?:[^@/]+@)?([^/:]+)/i.exec(url)?.[1]?.toLowerCase() ?? null;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* La forge dont `init` écrit la CI, et le signal qui l'a désignée.
|
|
161
|
+
*
|
|
162
|
+
* Ordre : l'option, l'hôte du remote `origin`, la présence de `.gitlab-ci.yml`,
|
|
163
|
+
* puis GitHub. Le compte rendu nomme le signal : une forge devinée en silence
|
|
164
|
+
* écrirait une CI que la forge réelle ne lance jamais, et rien ne le dirait.
|
|
165
|
+
* Un hôte qui commence par `gitlab.` désigne une instance GitLab
|
|
166
|
+
* auto-hébergée, que le fichier écrit sait joindre par `CI_API_V4_URL`.
|
|
167
|
+
*/
|
|
168
|
+
export function forgeDuRepository(racine, { forge, git = remoteOrigin } = {}) {
|
|
169
|
+
if (forge) return { nom: forge, signal: "l'option --forge" };
|
|
170
|
+
const url = git(racine);
|
|
171
|
+
const hote = url ? hoteDuRemote(url) : null;
|
|
172
|
+
if (hote === "github.com") return { nom: "github", signal: `l'hôte du remote origin, ${hote}` };
|
|
173
|
+
if (hote === "gitlab.com" || hote?.startsWith("gitlab.")) return { nom: "gitlab", signal: `l'hôte du remote origin, ${hote}` };
|
|
174
|
+
if (existsSync(join(racine, ".gitlab-ci.yml"))) return { nom: "gitlab", signal: "la présence de .gitlab-ci.yml" };
|
|
175
|
+
return { nom: "github", signal: "aucun signal, GitHub par défaut" };
|
|
127
176
|
}
|
|
128
177
|
|
|
129
178
|
/** La version de `@ucm-kit/cli`, lue dans son propre `package.json`. */
|
|
@@ -149,7 +198,7 @@ function versionDuPaquet() {
|
|
|
149
198
|
* présents, le rappel se tait : trois lignes réclamées pour rien sont trois
|
|
150
199
|
* lignes qu'on apprend à sauter.
|
|
151
200
|
*/
|
|
152
|
-
function fichiers(version, chemins, { agents = true, gabarits = [] } = {}) {
|
|
201
|
+
function fichiers(version, chemins, { agents = true, gabarits = [], forge = "github" } = {}) {
|
|
153
202
|
return [
|
|
154
203
|
{
|
|
155
204
|
chemin: NOM_CONFIGURATION,
|
|
@@ -209,10 +258,17 @@ function fichiers(version, chemins, { agents = true, gabarits = [] } = {}) {
|
|
|
209
258
|
marqueurs: ["ci-report.md"],
|
|
210
259
|
rappel: "ajoutez-y `ci-report.md`. `ucm check --report` le réécrit à chaque exécution ; commité, il montrerait le verdict d'un contrôle passé, pas celui du code en cours.",
|
|
211
260
|
},
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
261
|
+
...(forge === "gitlab"
|
|
262
|
+
? [
|
|
263
|
+
{ chemin: CHEMIN_CI_GITLAB, contenu: workflowGitlab(version) },
|
|
264
|
+
{
|
|
265
|
+
chemin: ".gitlab-ci.yml",
|
|
266
|
+
contenu: `include:\n - local: ${CHEMIN_CI_GITLAB}\n`,
|
|
267
|
+
marqueurs: [CHEMIN_CI_GITLAB],
|
|
268
|
+
rappel: `ajoutez \`- local: ${CHEMIN_CI_GITLAB}\` à sa liste \`include:\`. Sans cette ligne, le job ucm ne tourne jamais et aucune merge request n'est contrôlée.`,
|
|
269
|
+
},
|
|
270
|
+
]
|
|
271
|
+
: [{ chemin: ".github/workflows/ucm.yml", contenu: workflow(version) }]),
|
|
216
272
|
...(agents
|
|
217
273
|
? [
|
|
218
274
|
{ chemin: ".agents/skills/ucm-implementer/SKILL.md", contenu: relais(version) },
|
|
@@ -443,6 +499,107 @@ function workflow(version) {
|
|
|
443
499
|
].join("\n");
|
|
444
500
|
}
|
|
445
501
|
|
|
502
|
+
/**
|
|
503
|
+
* Le job de contrôle pour GitLab, dans un fichier que `.gitlab-ci.yml` inclut.
|
|
504
|
+
*
|
|
505
|
+
* **Un job, aucune clé globale.** Ni `workflow`, ni `image`, ni `variables`, ni
|
|
506
|
+
* `stages` : un fichier inclus qui en déclarerait changerait les pipelines de
|
|
507
|
+
* tous les jobs du projet. Le job porte ses propres règles, pipeline de merge
|
|
508
|
+
* request ou branche par défaut, et tourne donc une fois par export.
|
|
509
|
+
*
|
|
510
|
+
* **Le filet et la note vivent dans `after_script`.** Un échec de `npm ci`
|
|
511
|
+
* saute le reste de `script`, alors que `after_script` tourne toujours. Son
|
|
512
|
+
* échec ne change pas le statut du job : la note manquante se lit dans le
|
|
513
|
+
* journal, et le rapport reste dans les artefacts.
|
|
514
|
+
*/
|
|
515
|
+
function workflowGitlab(version) {
|
|
516
|
+
const cli = `npx --yes @ucm-kit/cli@${version}`;
|
|
517
|
+
return [
|
|
518
|
+
"# Contrôle des contrats UCM, inclus par .gitlab-ci.yml.",
|
|
519
|
+
"#",
|
|
520
|
+
"# Le rapport publié en note de la merge request est le seul message que",
|
|
521
|
+
"# reçoit le designer qui valide un export : il n'ouvre pas les journaux de",
|
|
522
|
+
"# la CI. Toute étape qui refuse une fusion doit donc lui laisser un message.",
|
|
523
|
+
"#",
|
|
524
|
+
"# Ce fichier ne déclare qu'un job et aucune clé globale : les autres jobs du",
|
|
525
|
+
"# projet gardent leurs règles. Le job prend le stage `test`.",
|
|
526
|
+
"#",
|
|
527
|
+
"# Un job rouge ne bloque la fusion que si « Pipelines must succeed » est",
|
|
528
|
+
"# coché dans Settings > Merge requests. La note demande la variable",
|
|
529
|
+
"# UCM_GITLAB_TOKEN, masquée et non protégée, portant un jeton de scope api.",
|
|
530
|
+
"#",
|
|
531
|
+
"# Écrit par `ucm init`. Adaptez-le : il ne sera jamais réécrit par-dessus.",
|
|
532
|
+
"ucm:",
|
|
533
|
+
" image: node:22",
|
|
534
|
+
" variables:",
|
|
535
|
+
" # Le diff avec la base délimite les états informatifs du rapport.",
|
|
536
|
+
' GIT_DEPTH: "0"',
|
|
537
|
+
" rules:",
|
|
538
|
+
' - if: $CI_PIPELINE_SOURCE == "merge_request_event"',
|
|
539
|
+
" - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH",
|
|
540
|
+
" script:",
|
|
541
|
+
" # Un adaptateur appartient à la stack du repository. Sans lockfile, le",
|
|
542
|
+
" # noyau portable reste seul.",
|
|
543
|
+
" - if [ -f package-lock.json ]; then npm ci; fi",
|
|
544
|
+
" - |",
|
|
545
|
+
' if [ -n "$CI_MERGE_REQUEST_DIFF_BASE_SHA" ]; then',
|
|
546
|
+
` ${cli} check --report ci-report.md --base "$CI_MERGE_REQUEST_DIFF_BASE_SHA"`,
|
|
547
|
+
" else",
|
|
548
|
+
` ${cli} check --report ci-report.md`,
|
|
549
|
+
" fi",
|
|
550
|
+
" after_script:",
|
|
551
|
+
" # Filet : sans rapport, la CI s'est arrêtée avant le contrôle (clone,",
|
|
552
|
+
" # installation, plantage).",
|
|
553
|
+
" - |",
|
|
554
|
+
" if [ ! -f ci-report.md ]; then",
|
|
555
|
+
" printf '%s\\n\\n%s\\n\\n%s\\n' \\",
|
|
556
|
+
" \"## ❌ La vérification n'a pas pu rendre son diagnostic\" \\",
|
|
557
|
+
" \"Les contrôles se sont arrêtés avant d'avoir pu analyser cet export : le rapport habituel n'a pas été produit. **Votre design n'est pas en cause** et réexporter depuis Figma n'y changerait rien.\" \\",
|
|
558
|
+
" \"**Action attendue :** un développeur doit ouvrir [le job de la CI]($CI_JOB_URL) pour en connaître la raison.\" \\",
|
|
559
|
+
" > ci-report.md",
|
|
560
|
+
" fi",
|
|
561
|
+
" - |",
|
|
562
|
+
' if [ -n "$CI_MERGE_REQUEST_IID" ]; then',
|
|
563
|
+
` ${cli} rapport-gitlab --projet "$CI_PROJECT_ID" --merge-request "$CI_MERGE_REQUEST_IID" --fichier ci-report.md --api "$CI_API_V4_URL"`,
|
|
564
|
+
" fi",
|
|
565
|
+
" artifacts:",
|
|
566
|
+
" when: always",
|
|
567
|
+
" paths:",
|
|
568
|
+
" - ci-report.md",
|
|
569
|
+
"",
|
|
570
|
+
].join("\n");
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Les lignes qu'`init` ne peut pas écrire pour GitLab : des réglages du projet,
|
|
575
|
+
* et un `stages:` qui refuserait le job.
|
|
576
|
+
*/
|
|
577
|
+
function lignesGitlab(racine) {
|
|
578
|
+
const lignes = [
|
|
579
|
+
{
|
|
580
|
+
fichier: "Settings > CI/CD > Variables",
|
|
581
|
+
ligne: "créez `UCM_GITLAB_TOKEN`, masquée et non protégée, avec un jeton de scope api. Sans elle, le rapport n'est pas publié sur la merge request. Non protégée, parce que les branches d'export ne le sont pas.",
|
|
582
|
+
},
|
|
583
|
+
{
|
|
584
|
+
fichier: "Settings > Merge requests",
|
|
585
|
+
ligne: "cochez « Pipelines must succeed ». Sans cette case, un rapport rouge annonce une fusion bloquée qui ne l'est pas.",
|
|
586
|
+
},
|
|
587
|
+
];
|
|
588
|
+
try {
|
|
589
|
+
const ci = readFileSync(join(racine, ".gitlab-ci.yml"), "utf8");
|
|
590
|
+
const stages = /^stages:([^\n]*(?:\n[ \t]+[^\n]*|\n-[^\n]*)*)/m.exec(ci);
|
|
591
|
+
if (stages && !/\btest\b/.test(stages[1])) {
|
|
592
|
+
lignes.push({
|
|
593
|
+
fichier: ".gitlab-ci.yml",
|
|
594
|
+
ligne: "ajoutez `test` à `stages:`. Le job ucm s'y range, et GitLab refuse un pipeline dont un job vise un stage absent.",
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
} catch {
|
|
598
|
+
// Sans .gitlab-ci.yml, celui qu'`init` écrit ne déclare aucun stage.
|
|
599
|
+
}
|
|
600
|
+
return lignes;
|
|
601
|
+
}
|
|
602
|
+
|
|
446
603
|
/**
|
|
447
604
|
* Écrit ce qui manque, et rend le compte rendu de ce qui a été fait.
|
|
448
605
|
*
|
|
@@ -456,13 +613,18 @@ export function init(racine, {
|
|
|
456
613
|
sansAgents = false,
|
|
457
614
|
adaptateur = null,
|
|
458
615
|
erreurAdaptateur = null,
|
|
616
|
+
forge: forgeDemandee,
|
|
617
|
+
git,
|
|
459
618
|
} = {}) {
|
|
460
619
|
const version = versionDuPaquet();
|
|
461
620
|
const aEcrire = [];
|
|
462
621
|
const deja = [];
|
|
463
622
|
const { gabarits, erreur: erreurGabarits } = sansAgents ? { gabarits: [], erreur: null } : gabaritsDe(adaptateur);
|
|
623
|
+
// Lue avant toute écriture : le `.gitlab-ci.yml` qu'`init` écrirait ne doit
|
|
624
|
+
// pas devenir le signal qui désigne GitLab.
|
|
625
|
+
const forge = forgeDuRepository(racine, { forge: forgeDemandee, git });
|
|
464
626
|
|
|
465
|
-
for (const fichier of fichiers(version, chemins, { agents: !sansAgents, gabarits })) {
|
|
627
|
+
for (const fichier of fichiers(version, chemins, { agents: !sansAgents, gabarits, forge: forge.nom })) {
|
|
466
628
|
const cible = join(racine, fichier.chemin);
|
|
467
629
|
if (existsSync(cible)) deja.push({ ...fichier, cible });
|
|
468
630
|
else aEcrire.push({ ...fichier, cible });
|
|
@@ -500,19 +662,23 @@ export function init(racine, {
|
|
|
500
662
|
? { ...CONFIGURATION_PAR_DEFAUT, ...chemins }
|
|
501
663
|
: null,
|
|
502
664
|
version,
|
|
665
|
+
forge,
|
|
503
666
|
agents: !sansAgents,
|
|
504
667
|
// `absent` quand aucun adaptateur n'est installé, le message quand il n'a pas pu servir.
|
|
505
668
|
adaptateur: erreurAdaptateur
|
|
506
669
|
? { erreur: erreurAdaptateur?.message ?? String(erreurAdaptateur) }
|
|
507
670
|
: erreurGabarits ? { erreur: erreurGabarits } : adaptateur ? "trouve" : "absent",
|
|
508
671
|
nodeJs: existsSync(join(racine, "package.json")),
|
|
509
|
-
lignes:
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
672
|
+
lignes: [
|
|
673
|
+
...lignesRestantes(
|
|
674
|
+
racine,
|
|
675
|
+
version,
|
|
676
|
+
aEcrire.some((f) => f.chemin === NOM_CONFIGURATION)
|
|
677
|
+
? { ...CONFIGURATION_PAR_DEFAUT, ...chemins }
|
|
678
|
+
: lireConfiguration(racine).configuration,
|
|
679
|
+
),
|
|
680
|
+
...(forge.nom === "gitlab" ? lignesGitlab(racine) : []),
|
|
681
|
+
],
|
|
516
682
|
};
|
|
517
683
|
}
|
|
518
684
|
|
|
@@ -549,6 +715,7 @@ export function rendreInit({
|
|
|
549
715
|
optionsIgnorees = false,
|
|
550
716
|
chemins = null,
|
|
551
717
|
version,
|
|
718
|
+
forge = null,
|
|
552
719
|
agents = false,
|
|
553
720
|
adaptateur = "absent",
|
|
554
721
|
nodeJs = false,
|
|
@@ -572,6 +739,10 @@ export function rendreInit({
|
|
|
572
739
|
? "Rien à faire : ce repository est déjà installé."
|
|
573
740
|
: "Aucun fichier à écrire : ce repository est déjà installé.",
|
|
574
741
|
);
|
|
742
|
+
if (forge) {
|
|
743
|
+
lignes.push(`CI écrite pour ${forge.nom === "gitlab" ? "GitLab" : "GitHub"}, d'après ${forge.signal}. `
|
|
744
|
+
+ "L'option `--forge github` ou `--forge gitlab` en choisit une autre.");
|
|
745
|
+
}
|
|
575
746
|
|
|
576
747
|
if (optionsIgnorees) {
|
|
577
748
|
lignes.push("");
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ucm rapport-gitlab` : publie le rapport de `ucm check` en note d'une merge
|
|
3
|
+
* request GitLab, et remplace au push suivant la note qu'il a déjà écrite.
|
|
4
|
+
*
|
|
5
|
+
* La commande existe parce que l'image `node:22` d'un job GitLab n'a ni `glab`
|
|
6
|
+
* ni `jq`, et qu'un script recopié dans le YAML du repository ne se teste pas.
|
|
7
|
+
*
|
|
8
|
+
* **Le jeton se lit dans `UCM_GITLAB_TOKEN`, jamais dans un argument**, et
|
|
9
|
+
* n'apparaît dans aucune sortie : le journal d'un job se lit par tout membre
|
|
10
|
+
* du projet. Sans jeton, la commande le dit et sort en 0, parce que le rapport
|
|
11
|
+
* reste dans les artefacts du job et que le contrôle, lui, a déjà rendu son
|
|
12
|
+
* verdict.
|
|
13
|
+
*
|
|
14
|
+
* **Seule la note de ce compte portant le marqueur est remplacée.** Une note
|
|
15
|
+
* d'un autre compte qui recopie le marqueur reste intacte : la remplacer
|
|
16
|
+
* écraserait le texte d'un humain.
|
|
17
|
+
*/
|
|
18
|
+
import { readFileSync } from "node:fs";
|
|
19
|
+
import { resolve } from "node:path";
|
|
20
|
+
|
|
21
|
+
export const MARQUEUR_RAPPORT = "<!-- ucm-rapport -->";
|
|
22
|
+
|
|
23
|
+
const API_PAR_DEFAUT = "https://gitlab.com/api/v4";
|
|
24
|
+
|
|
25
|
+
const USAGE = "ucm rapport-gitlab --projet <id> --merge-request <iid> --fichier <chemin> [--api <url>]";
|
|
26
|
+
|
|
27
|
+
const OPTIONS = { "--projet": "projet", "--merge-request": "mergeRequest", "--fichier": "fichier", "--api": "api" };
|
|
28
|
+
|
|
29
|
+
/** Lit les arguments ; rend `erreur` pour une invocation fautive. */
|
|
30
|
+
export function lireArgumentsRapport(arguments_) {
|
|
31
|
+
const valeurs = { api: API_PAR_DEFAUT };
|
|
32
|
+
for (let i = 0; i < arguments_.length; i += 1) {
|
|
33
|
+
const cle = OPTIONS[arguments_[i]];
|
|
34
|
+
if (!cle) return { erreur: `Argument inconnu : ${arguments_[i]}` };
|
|
35
|
+
const valeur = arguments_[i + 1];
|
|
36
|
+
if (valeur === undefined || valeur.startsWith("--") || valeur === "") {
|
|
37
|
+
return { erreur: `${arguments_[i]} attend une valeur.` };
|
|
38
|
+
}
|
|
39
|
+
valeurs[cle] = valeur;
|
|
40
|
+
i += 1;
|
|
41
|
+
}
|
|
42
|
+
for (const [option, cle] of Object.entries(OPTIONS)) {
|
|
43
|
+
if (valeurs[cle] === undefined) return { erreur: `${option} est obligatoire.` };
|
|
44
|
+
}
|
|
45
|
+
if (!/^https:\/\//.test(valeurs.api)) return { erreur: `--api attend une adresse https : ${valeurs.api} n'en est pas une.` };
|
|
46
|
+
return { valeurs: { ...valeurs, api: valeurs.api.replace(/\/+$/, "") } };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Une réponse d'API refusée, avec son statut. */
|
|
50
|
+
class RefusGitlab extends Error {
|
|
51
|
+
constructor(statut, geste) {
|
|
52
|
+
super(geste);
|
|
53
|
+
this.statut = statut;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Le geste qu'un statut demande, écrit pour la personne qui lit le journal du job. */
|
|
58
|
+
function gesteDuStatut(statut, action) {
|
|
59
|
+
if (statut === 401) {
|
|
60
|
+
return `GitLab refuse le jeton de UCM_GITLAB_TOKEN (401) en voulant ${action}. `
|
|
61
|
+
+ "Un mainteneur du projet doit créer un nouveau jeton de scope api, puis remplacer la valeur de cette variable.";
|
|
62
|
+
}
|
|
63
|
+
if (statut === 403) {
|
|
64
|
+
return `Le jeton de UCM_GITLAB_TOKEN n'a pas le droit de ${action} (403). `
|
|
65
|
+
+ "Un mainteneur du projet doit lui donner le scope api et le rôle Developer sur ce projet.";
|
|
66
|
+
}
|
|
67
|
+
if (statut === 404) {
|
|
68
|
+
return `GitLab ne trouve pas la merge request en voulant ${action} (404). `
|
|
69
|
+
+ "Vérifiez --projet et --merge-request, et que le jeton a accès à ce projet.";
|
|
70
|
+
}
|
|
71
|
+
return `GitLab a répondu ${statut} en voulant ${action}. Relancez le job ; si la réponse ne change pas, un développeur doit la regarder.`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Publie ou remplace la note du rapport.
|
|
76
|
+
*
|
|
77
|
+
* Codes : 0 note écrite ou jeton absent, 1 refus de GitLab ou rapport
|
|
78
|
+
* illisible, 2 invocation fautive.
|
|
79
|
+
*/
|
|
80
|
+
export async function rapportGitlab(arguments_, {
|
|
81
|
+
racine = process.cwd(),
|
|
82
|
+
env = process.env,
|
|
83
|
+
fetch: requete = globalThis.fetch,
|
|
84
|
+
ecrire = console.log,
|
|
85
|
+
alerter = console.error,
|
|
86
|
+
} = {}) {
|
|
87
|
+
const { valeurs, erreur } = lireArgumentsRapport(arguments_);
|
|
88
|
+
if (erreur) {
|
|
89
|
+
alerter(`${erreur}\n\n${USAGE}`);
|
|
90
|
+
return 2;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const jeton = env.UCM_GITLAB_TOKEN?.trim();
|
|
94
|
+
if (!jeton) {
|
|
95
|
+
ecrire("UCM_GITLAB_TOKEN est absente : le rapport n'est pas publié sur la merge request. "
|
|
96
|
+
+ "Il reste dans les artefacts du job. Pour le publier, un mainteneur du projet doit créer "
|
|
97
|
+
+ "cette variable, masquée et non protégée, avec un jeton de scope api.");
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let rapport;
|
|
102
|
+
try {
|
|
103
|
+
rapport = readFileSync(resolve(racine, valeurs.fichier), "utf8");
|
|
104
|
+
} catch (erreurLecture) {
|
|
105
|
+
alerter(`Le rapport ${valeurs.fichier} ne se lit pas (${erreurLecture?.code ?? erreurLecture?.message}) : aucune note n'est publiée.`);
|
|
106
|
+
return 1;
|
|
107
|
+
}
|
|
108
|
+
const corps = `${MARQUEUR_RAPPORT}\n${rapport}`;
|
|
109
|
+
const projet = encodeURIComponent(valeurs.projet);
|
|
110
|
+
const notes = `${valeurs.api}/projects/${projet}/merge_requests/${encodeURIComponent(valeurs.mergeRequest)}/notes`;
|
|
111
|
+
|
|
112
|
+
async function appeler(url, action, init = {}) {
|
|
113
|
+
let reponse;
|
|
114
|
+
try {
|
|
115
|
+
reponse = await requete(url, {
|
|
116
|
+
...init,
|
|
117
|
+
headers: { "PRIVATE-TOKEN": jeton, "Content-Type": "application/json" },
|
|
118
|
+
});
|
|
119
|
+
} catch (panne) {
|
|
120
|
+
throw new RefusGitlab(null, `La requête vers GitLab n'a pas abouti en voulant ${action} (${panne?.message ?? panne}). Relancez le job.`);
|
|
121
|
+
}
|
|
122
|
+
if (!reponse.ok) throw new RefusGitlab(reponse.status, gesteDuStatut(reponse.status, action));
|
|
123
|
+
return reponse;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
const compte = await (await appeler(`${valeurs.api}/user`, "lire le compte du jeton")).json();
|
|
128
|
+
|
|
129
|
+
let existante = null;
|
|
130
|
+
let page = "1";
|
|
131
|
+
while (page && !existante) {
|
|
132
|
+
const reponse = await appeler(`${notes}?per_page=100&sort=asc&page=${page}`, "lire les notes de la merge request");
|
|
133
|
+
const lues = await reponse.json();
|
|
134
|
+
existante = lues.find((note) => !note.system && note.author?.id === compte.id
|
|
135
|
+
&& typeof note.body === "string" && note.body.includes(MARQUEUR_RAPPORT)) ?? null;
|
|
136
|
+
page = reponse.headers.get("x-next-page");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (existante) {
|
|
140
|
+
await appeler(`${notes}/${existante.id}`, "remplacer la note du rapport", { method: "PUT", body: JSON.stringify({ body: corps }) });
|
|
141
|
+
ecrire(`Rapport remplacé dans la note ${existante.id} de la merge request !${valeurs.mergeRequest}.`);
|
|
142
|
+
} else {
|
|
143
|
+
await appeler(notes, "écrire la note du rapport", { method: "POST", body: JSON.stringify({ body: corps }) });
|
|
144
|
+
ecrire(`Rapport publié en note de la merge request !${valeurs.mergeRequest}.`);
|
|
145
|
+
}
|
|
146
|
+
return 0;
|
|
147
|
+
} catch (refus) {
|
|
148
|
+
if (!(refus instanceof RefusGitlab)) throw refus;
|
|
149
|
+
alerter(refus.message);
|
|
150
|
+
return 1;
|
|
151
|
+
}
|
|
152
|
+
}
|
package/src/ucm.mjs
CHANGED
|
@@ -16,6 +16,7 @@ import { check } from "./check.mjs";
|
|
|
16
16
|
import { guide } from "./guide.mjs";
|
|
17
17
|
import { iconesDuRepository, rendreIcones } from "./icons.mjs";
|
|
18
18
|
import { init, lireArgumentsInit, rendreInit } from "./init.mjs";
|
|
19
|
+
import { rapportGitlab } from "./rapport-gitlab.mjs";
|
|
19
20
|
import { tokensCss } from "./tokens-css.mjs";
|
|
20
21
|
|
|
21
22
|
/** Le verdict de tests qu'un orchestrateur de stack peut transmettre au CLI. */
|
|
@@ -44,15 +45,19 @@ const AIDE = `ucm — la ligne de commande UCM
|
|
|
44
45
|
ucm tokens css écrit la feuille CSS des tokens et de leurs modes
|
|
45
46
|
ucm aides liste les aides à l'implémentation et leur origine
|
|
46
47
|
ucm guide imprime ce qu'un agent lit avant d'implémenter un contrat
|
|
48
|
+
ucm rapport-gitlab publie le rapport en note d'une merge request GitLab
|
|
47
49
|
ucm --help affiche cette aide
|
|
48
50
|
|
|
49
|
-
ucm init [--components <dossier>] [--tokens <dossier>] [--implementation <motif>] [--sans-agents]
|
|
51
|
+
ucm init [--components <dossier>] [--tokens <dossier>] [--implementation <motif>] [--sans-agents] [--forge github|gitlab]
|
|
50
52
|
--components dossier sous lequel les contrats sont rangés
|
|
51
53
|
--tokens dossier qui reçoit tokens.json
|
|
52
54
|
--implementation où vit l'implémentation d'un contrat, {dir} et {id}
|
|
53
55
|
pour son dossier et son identifiant
|
|
54
56
|
--sans-agents n'écrit ni les relais d'agent, ni .ucm/conventions.md,
|
|
55
57
|
ni les gabarits
|
|
58
|
+
--forge la forge dont la CI est écrite ; sans elle, l'hôte du
|
|
59
|
+
remote origin, puis la présence de .gitlab-ci.yml
|
|
60
|
+
décident, et GitHub sinon
|
|
56
61
|
Les trois n'agissent qu'à la première installation : ucm init n'écrase
|
|
57
62
|
jamais un ucm.config.json existant.
|
|
58
63
|
|
|
@@ -75,6 +80,14 @@ const AIDE = `ucm — la ligne de commande UCM
|
|
|
75
80
|
<contrat> le fichier .contract.json à implémenter
|
|
76
81
|
--out écrit le guide dans ce fichier plutôt que dans le terminal
|
|
77
82
|
|
|
83
|
+
ucm rapport-gitlab --projet <id> --merge-request <iid> --fichier <chemin> [--api <url>]
|
|
84
|
+
--projet l'identifiant ou le chemin du projet GitLab
|
|
85
|
+
--merge-request le numéro de la merge request dans ce projet
|
|
86
|
+
--fichier le rapport écrit par ucm check --report
|
|
87
|
+
--api l'API GitLab, https://gitlab.com/api/v4 par défaut
|
|
88
|
+
Le jeton se lit dans UCM_GITLAB_TOKEN. Sans lui, la commande le dit et
|
|
89
|
+
sort en 0.
|
|
90
|
+
|
|
78
91
|
Codes de sortie : 0 tout est passé, 1 des contrôles ont échoué, 2 l'invocation
|
|
79
92
|
ou la configuration est fautive.`;
|
|
80
93
|
|
|
@@ -108,10 +121,10 @@ function executerCommande(arguments_, {
|
|
|
108
121
|
}
|
|
109
122
|
|
|
110
123
|
if (commande === "init") {
|
|
111
|
-
const { chemins, sansAgents, erreur } = lireArgumentsInit(arguments_.slice(1));
|
|
124
|
+
const { chemins, sansAgents, forge, erreur } = lireArgumentsInit(arguments_.slice(1));
|
|
112
125
|
if (erreur) {
|
|
113
126
|
const alerter = sorties.alerter ?? console.error;
|
|
114
|
-
alerter(`${erreur}\n\nucm init [--components <dossier>] [--tokens <dossier>] [--implementation <motif>] [--sans-agents]`);
|
|
127
|
+
alerter(`${erreur}\n\nucm init [--components <dossier>] [--tokens <dossier>] [--implementation <motif>] [--sans-agents] [--forge github|gitlab]`);
|
|
115
128
|
return 2;
|
|
116
129
|
}
|
|
117
130
|
// Un adaptateur qui ne se charge pas prive le repository de ses gabarits,
|
|
@@ -119,7 +132,7 @@ function executerCommande(arguments_, {
|
|
|
119
132
|
return chargerAdaptateur(racine)
|
|
120
133
|
.then((adaptateur) => ({ adaptateur }), (erreurAdaptateur) => ({ erreurAdaptateur }))
|
|
121
134
|
.then((chargement) => {
|
|
122
|
-
ecrire(rendreInit(init(racine, { chemins, sansAgents, ...chargement })));
|
|
135
|
+
ecrire(rendreInit(init(racine, { chemins, sansAgents, forge, git: sorties.git, ...chargement })));
|
|
123
136
|
return 0;
|
|
124
137
|
});
|
|
125
138
|
}
|
|
@@ -168,6 +181,16 @@ function executerCommande(arguments_, {
|
|
|
168
181
|
return guide(arguments_.slice(1), { racine, ecrire, alerter: sorties.alerter ?? console.error });
|
|
169
182
|
}
|
|
170
183
|
|
|
184
|
+
if (commande === "rapport-gitlab") {
|
|
185
|
+
return rapportGitlab(arguments_.slice(1), {
|
|
186
|
+
racine,
|
|
187
|
+
env,
|
|
188
|
+
fetch: sorties.fetch,
|
|
189
|
+
ecrire,
|
|
190
|
+
alerter: sorties.alerter ?? console.error,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
171
194
|
if (commande === "icons") {
|
|
172
195
|
const { configuration, erreur } = lireConfiguration(racine);
|
|
173
196
|
if (erreur) {
|