@lssm/app.cli-contractspec 0.0.0-canary-20251221164004

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/docs/ci-cd.md ADDED
@@ -0,0 +1,598 @@
1
+ # ContractSpec CI/CD Integration
2
+
3
+ This guide covers how to integrate ContractSpec validation into your CI/CD pipeline. ContractSpec provides a dedicated `ci` command designed for automated environments with support for multiple output formats.
4
+
5
+ ## Quick Start
6
+
7
+ ```bash
8
+ # Run all CI checks
9
+ contractspec ci
10
+
11
+ # Run with specific output format
12
+ contractspec ci --format json # Machine-readable JSON
13
+ contractspec ci --format sarif # GitHub Code Scanning compatible
14
+
15
+ # Write output to file
16
+ contractspec ci --format sarif --output results.sarif
17
+ ```
18
+
19
+ ## The `contractspec ci` Command
20
+
21
+ The `ci` command runs all validation checks and provides structured output suitable for CI/CD pipelines.
22
+
23
+ ### Options
24
+
25
+ | Option | Description |
26
+ |--------|-------------|
27
+ | `--pattern <glob>` | Glob pattern for spec discovery |
28
+ | `--format <format>` | Output format: `text`, `json`, `sarif` (default: `text`) |
29
+ | `--output <file>` | Write results to file |
30
+ | `--fail-on-warnings` | Exit with code 2 on warnings |
31
+ | `--skip <checks>` | Skip specific checks (comma-separated) |
32
+ | `--checks <checks>` | Only run specific checks (comma-separated) |
33
+ | `--check-handlers` | Include handler implementation checks |
34
+ | `--check-tests` | Include test coverage checks |
35
+ | `--verbose` | Verbose output |
36
+
37
+ ### Available Checks
38
+
39
+ | Check | Description | Default Severity |
40
+ |-------|-------------|-----------------|
41
+ | `structure` | Validate spec structure (meta, io, policy fields) | error |
42
+ | `integrity` | Find orphaned specs and broken references | error/warning |
43
+ | `deps` | Detect circular dependencies and missing refs | error |
44
+ | `doctor` | Check installation health (skip AI in CI) | error/warning |
45
+ | `handlers` | Verify handler implementations exist | warning |
46
+ | `tests` | Verify test files exist | warning |
47
+
48
+ ### Exit Codes
49
+
50
+ | Code | Description |
51
+ |------|-------------|
52
+ | `0` | All checks passed |
53
+ | `1` | Errors found |
54
+ | `2` | Warnings found (with `--fail-on-warnings`) |
55
+
56
+ ## GitHub Actions
57
+
58
+ ### Using the Official Action
59
+
60
+ The easiest way to integrate with GitHub Actions is using the official action:
61
+
62
+ ```yaml
63
+ name: ContractSpec CI
64
+
65
+ on: [push, pull_request]
66
+
67
+ jobs:
68
+ contractspec:
69
+ runs-on: ubuntu-latest
70
+ permissions:
71
+ contents: read
72
+ security-events: write # Required for SARIF upload
73
+ steps:
74
+ - uses: actions/checkout@v4
75
+
76
+ - name: Run ContractSpec CI
77
+ uses: lssm/contractspec-action@v1
78
+ with:
79
+ checks: 'all'
80
+ upload-sarif: true
81
+ ```
82
+
83
+ ### Manual Setup (without the action)
84
+
85
+ ```yaml
86
+ name: ContractSpec CI
87
+
88
+ on:
89
+ push:
90
+ branches: [main]
91
+ pull_request:
92
+
93
+ jobs:
94
+ validate:
95
+ runs-on: ubuntu-latest
96
+ permissions:
97
+ contents: read
98
+ security-events: write
99
+ steps:
100
+ - uses: actions/checkout@v4
101
+
102
+ - name: Setup Bun
103
+ uses: oven-sh/setup-bun@v2
104
+ with:
105
+ bun-version: latest
106
+
107
+ - name: Install dependencies
108
+ run: bun install
109
+
110
+ - name: Run ContractSpec CI
111
+ run: |
112
+ npx contractspec ci --format sarif --output results.sarif
113
+ npx contractspec ci --format text
114
+
115
+ - name: Upload SARIF to GitHub Code Scanning
116
+ uses: github/codeql-action/upload-sarif@v3
117
+ if: always()
118
+ with:
119
+ sarif_file: results.sarif
120
+ category: contractspec
121
+ ```
122
+
123
+ ### GitHub Code Scanning Integration
124
+
125
+ When you upload SARIF results, issues appear:
126
+ - Inline on pull requests
127
+ - In the Security tab under "Code scanning alerts"
128
+ - As annotations in the GitHub UI
129
+
130
+ ## GitLab CI
131
+
132
+ ```yaml
133
+ # .gitlab-ci.yml
134
+
135
+ stages:
136
+ - validate
137
+
138
+ contractspec:
139
+ stage: validate
140
+ image: oven/bun:latest
141
+ script:
142
+ - bun install
143
+ - npx contractspec ci --format json --output results.json
144
+ - npx contractspec ci --format text
145
+ artifacts:
146
+ reports:
147
+ # GitLab doesn't natively support SARIF, but you can store as artifact
148
+ dotenv: results.json
149
+ paths:
150
+ - results.json
151
+ when: always
152
+ rules:
153
+ - if: $CI_PIPELINE_SOURCE == "merge_request_event"
154
+ - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
155
+
156
+ # For GitLab Ultimate with SAST, you can use the SARIF output
157
+ contractspec-sast:
158
+ stage: validate
159
+ image: oven/bun:latest
160
+ script:
161
+ - bun install
162
+ - npx contractspec ci --format sarif --output gl-sast-report.json
163
+ artifacts:
164
+ reports:
165
+ sast: gl-sast-report.json
166
+ when: always
167
+ allow_failure: true
168
+ ```
169
+
170
+ ## Jenkins
171
+
172
+ ### Jenkinsfile (Declarative)
173
+
174
+ ```groovy
175
+ pipeline {
176
+ agent {
177
+ docker {
178
+ image 'oven/bun:latest'
179
+ }
180
+ }
181
+
182
+ stages {
183
+ stage('Install') {
184
+ steps {
185
+ sh 'bun install'
186
+ }
187
+ }
188
+
189
+ stage('Validate Contracts') {
190
+ steps {
191
+ script {
192
+ def result = sh(
193
+ script: 'npx contractspec ci --format json --output results.json',
194
+ returnStatus: true
195
+ )
196
+
197
+ // Also generate human-readable output
198
+ sh 'npx contractspec ci --format text || true'
199
+
200
+ // Archive results
201
+ archiveArtifacts artifacts: 'results.json', allowEmptyArchive: true
202
+
203
+ // Fail build on errors
204
+ if (result != 0) {
205
+ error("ContractSpec validation failed")
206
+ }
207
+ }
208
+ }
209
+ }
210
+ }
211
+
212
+ post {
213
+ always {
214
+ // Parse JSON results and add to build
215
+ script {
216
+ if (fileExists('results.json')) {
217
+ def results = readJSON file: 'results.json'
218
+ echo "Errors: ${results.summary?.totalErrors ?: results.errors ?: 0}"
219
+ echo "Warnings: ${results.summary?.totalWarnings ?: results.warnings ?: 0}"
220
+ }
221
+ }
222
+ }
223
+ }
224
+ }
225
+ ```
226
+
227
+ ### Jenkinsfile (Scripted)
228
+
229
+ ```groovy
230
+ node {
231
+ stage('Checkout') {
232
+ checkout scm
233
+ }
234
+
235
+ docker.image('oven/bun:latest').inside {
236
+ stage('Install') {
237
+ sh 'bun install'
238
+ }
239
+
240
+ stage('Validate') {
241
+ def exitCode = sh(
242
+ script: 'npx contractspec ci --format json --output results.json',
243
+ returnStatus: true
244
+ )
245
+
246
+ sh 'npx contractspec ci --format text || true'
247
+
248
+ archiveArtifacts 'results.json'
249
+
250
+ if (exitCode != 0) {
251
+ currentBuild.result = 'FAILURE'
252
+ error 'ContractSpec validation failed'
253
+ }
254
+ }
255
+ }
256
+ }
257
+ ```
258
+
259
+ ## AWS CodeBuild
260
+
261
+ ### buildspec.yml
262
+
263
+ ```yaml
264
+ version: 0.2
265
+
266
+ phases:
267
+ install:
268
+ runtime-versions:
269
+ nodejs: 20
270
+ commands:
271
+ - curl -fsSL https://bun.sh/install | bash
272
+ - export PATH="$HOME/.bun/bin:$PATH"
273
+ - bun install
274
+
275
+ build:
276
+ commands:
277
+ - npx contractspec ci --format json --output results.json
278
+ - npx contractspec ci --format text
279
+
280
+ post_build:
281
+ commands:
282
+ - |
283
+ if [ -f results.json ]; then
284
+ echo "ContractSpec Results:"
285
+ cat results.json | jq '.summary // {errors: .errors, warnings: .warnings}'
286
+ fi
287
+
288
+ reports:
289
+ contractspec-reports:
290
+ files:
291
+ - results.json
292
+ file-format: 'GENERICJSON'
293
+
294
+ artifacts:
295
+ files:
296
+ - results.json
297
+ - results.sarif
298
+ discard-paths: yes
299
+ ```
300
+
301
+ ## CircleCI
302
+
303
+ ```yaml
304
+ # .circleci/config.yml
305
+
306
+ version: 2.1
307
+
308
+ orbs:
309
+ bun: oven/bun@1
310
+
311
+ jobs:
312
+ validate-contracts:
313
+ executor: bun/default
314
+ steps:
315
+ - checkout
316
+ - bun/install
317
+ - run:
318
+ name: Run ContractSpec CI
319
+ command: |
320
+ npx contractspec ci --format json --output results.json
321
+ npx contractspec ci --format text
322
+ - store_artifacts:
323
+ path: results.json
324
+ destination: contractspec-results
325
+ - run:
326
+ name: Check results
327
+ command: |
328
+ ERRORS=$(jq -r '.summary.totalErrors // .errors // 0' results.json)
329
+ if [ "$ERRORS" -gt 0 ]; then
330
+ echo "ContractSpec found $ERRORS error(s)"
331
+ exit 1
332
+ fi
333
+
334
+ workflows:
335
+ validate:
336
+ jobs:
337
+ - validate-contracts
338
+ ```
339
+
340
+ ## Azure DevOps
341
+
342
+ ```yaml
343
+ # azure-pipelines.yml
344
+
345
+ trigger:
346
+ - main
347
+
348
+ pool:
349
+ vmImage: 'ubuntu-latest'
350
+
351
+ steps:
352
+ - task: UseNode@1
353
+ inputs:
354
+ version: '20.x'
355
+
356
+ - script: |
357
+ curl -fsSL https://bun.sh/install | bash
358
+ export PATH="$HOME/.bun/bin:$PATH"
359
+ bun install
360
+ displayName: 'Install dependencies'
361
+
362
+ - script: |
363
+ npx contractspec ci --format json --output $(Build.ArtifactStagingDirectory)/results.json
364
+ npx contractspec ci --format text
365
+ displayName: 'Run ContractSpec CI'
366
+ continueOnError: true
367
+
368
+ - task: PublishBuildArtifacts@1
369
+ inputs:
370
+ pathToPublish: '$(Build.ArtifactStagingDirectory)/results.json'
371
+ artifactName: 'contractspec-results'
372
+ condition: always()
373
+
374
+ - script: |
375
+ ERRORS=$(jq -r '.summary.totalErrors // .errors // 0' $(Build.ArtifactStagingDirectory)/results.json)
376
+ if [ "$ERRORS" -gt 0 ]; then
377
+ echo "##vso[task.logissue type=error]ContractSpec found $ERRORS error(s)"
378
+ exit 1
379
+ fi
380
+ displayName: 'Check results'
381
+ ```
382
+
383
+ ## Bitbucket Pipelines
384
+
385
+ ```yaml
386
+ # bitbucket-pipelines.yml
387
+
388
+ image: oven/bun:latest
389
+
390
+ pipelines:
391
+ default:
392
+ - step:
393
+ name: Validate Contracts
394
+ script:
395
+ - bun install
396
+ - npx contractspec ci --format json --output results.json
397
+ - npx contractspec ci --format text
398
+ artifacts:
399
+ - results.json
400
+
401
+ pull-requests:
402
+ '**':
403
+ - step:
404
+ name: Validate Contracts
405
+ script:
406
+ - bun install
407
+ - npx contractspec ci --format json --output results.json
408
+ - npx contractspec ci --format text
409
+ artifacts:
410
+ - results.json
411
+ ```
412
+
413
+ ## Output Formats
414
+
415
+ ### Text Output
416
+
417
+ Human-readable output with colors and formatting:
418
+
419
+ ```
420
+ 📋 ContractSpec CI Check Results
421
+
422
+ Git: branch: main, commit: abc1234
423
+
424
+ Check Results:
425
+ ✓ Spec Structure Validation: passed (45ms)
426
+ ✗ Contract Integrity Analysis: 2 error(s), 1 warning(s) (120ms)
427
+ ✓ Dependency Analysis: passed (30ms)
428
+ ✓ Installation Health: passed (15ms)
429
+
430
+ Issues:
431
+
432
+ Errors:
433
+ ✗ Event user.created.v1 not found (src/features/auth.feature.ts)
434
+ ✗ Circular dependency detected: a → b → c → a
435
+
436
+ Warnings:
437
+ ⚠ operation user.signup.v1 is not linked to any feature
438
+
439
+ ──────────────────────────────────────────────────
440
+ Found: 2 error(s), 1 warning(s)
441
+ Duration: 210ms
442
+
443
+ ❌ CI checks failed
444
+ ```
445
+
446
+ ### JSON Output
447
+
448
+ Machine-readable JSON for scripting:
449
+
450
+ ```json
451
+ {
452
+ "success": false,
453
+ "summary": {
454
+ "totalErrors": 2,
455
+ "totalWarnings": 1,
456
+ "totalNotes": 0,
457
+ "durationMs": 210,
458
+ "timestamp": "2024-01-15T10:30:00.000Z",
459
+ "commitSha": "abc1234",
460
+ "branch": "main"
461
+ },
462
+ "categories": [
463
+ {
464
+ "category": "structure",
465
+ "label": "Spec Structure Validation",
466
+ "passed": true,
467
+ "errors": 0,
468
+ "warnings": 0,
469
+ "notes": 0,
470
+ "durationMs": 45
471
+ }
472
+ ],
473
+ "issues": [
474
+ {
475
+ "ruleId": "integrity-unresolved-ref",
476
+ "severity": "error",
477
+ "message": "Event user.created.v1 not found",
478
+ "category": "integrity",
479
+ "file": "src/features/auth.feature.ts"
480
+ }
481
+ ]
482
+ }
483
+ ```
484
+
485
+ ### SARIF Output
486
+
487
+ Static Analysis Results Interchange Format for GitHub Code Scanning:
488
+
489
+ ```json
490
+ {
491
+ "$schema": "https://json.schemastore.org/sarif-2.1.0.json",
492
+ "version": "2.1.0",
493
+ "runs": [
494
+ {
495
+ "tool": {
496
+ "driver": {
497
+ "name": "ContractSpec",
498
+ "version": "1.0.0",
499
+ "rules": [...]
500
+ }
501
+ },
502
+ "results": [...]
503
+ }
504
+ ]
505
+ }
506
+ ```
507
+
508
+ ## Best Practices
509
+
510
+ ### 1. Run on Pull Requests
511
+
512
+ Always validate contracts on pull requests to catch issues before merge:
513
+
514
+ ```yaml
515
+ on:
516
+ pull_request:
517
+ branches: [main, develop]
518
+ ```
519
+
520
+ ### 2. Skip AI Checks in CI
521
+
522
+ The `doctor` check automatically skips AI provider validation in CI environments.
523
+
524
+ ### 3. Use SARIF for GitHub
525
+
526
+ Upload SARIF to get inline annotations on PRs:
527
+
528
+ ```yaml
529
+ - uses: github/codeql-action/upload-sarif@v3
530
+ with:
531
+ sarif_file: results.sarif
532
+ ```
533
+
534
+ ### 4. Cache Dependencies
535
+
536
+ Speed up CI by caching node_modules:
537
+
538
+ ```yaml
539
+ - uses: actions/cache@v4
540
+ with:
541
+ path: ~/.bun/install/cache
542
+ key: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lockb') }}
543
+ ```
544
+
545
+ ### 5. Parallel Checks for Monorepos
546
+
547
+ Run checks in parallel for each package:
548
+
549
+ ```yaml
550
+ strategy:
551
+ matrix:
552
+ package: [api, web, shared]
553
+ steps:
554
+ - uses: lssm/contractspec-action@v1
555
+ with:
556
+ working-directory: packages/${{ matrix.package }}
557
+ ```
558
+
559
+ ## Troubleshooting
560
+
561
+ ### Exit Code 1 but No Visible Errors
562
+
563
+ Check the JSON output for full details:
564
+
565
+ ```bash
566
+ contractspec ci --format json
567
+ ```
568
+
569
+ ### SARIF Upload Fails
570
+
571
+ Ensure you have the `security-events: write` permission:
572
+
573
+ ```yaml
574
+ permissions:
575
+ security-events: write
576
+ ```
577
+
578
+ ### Timeout in Large Monorepos
579
+
580
+ Use a specific pattern to limit scope:
581
+
582
+ ```bash
583
+ contractspec ci --pattern 'packages/my-app/**/*.contracts.ts'
584
+ ```
585
+
586
+ ### Missing Dependencies
587
+
588
+ Ensure all workspace dependencies are installed:
589
+
590
+ ```bash
591
+ bun install
592
+ # or for npm workspaces
593
+ npm ci --workspaces
594
+ ```
595
+
596
+
597
+
598
+
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@lssm/app.cli-contractspec",
3
+ "version": "0.0.0-canary-20251221164004",
4
+ "description": "CLI tool for creating, building, and validating contract specifications",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "bin": {
9
+ "contractspec": "./dist/cli.js"
10
+ },
11
+ "scripts": {
12
+ "publish:pkg": "bun publish --tolerate-republish --ignore-scripts --verbose",
13
+ "publish:pkg:canary": "bun publish:pkg --tag canary",
14
+ "build:off": "bun run build:bun && bun run build:types",
15
+ "build": "bun build ./src/cli.ts --outdir ./dist --target bun --minify --sourcemap",
16
+ "build:all": "bun run build:bun && bun run build:node && bun run build:types",
17
+ "build:types": "tsc --emitDeclarationOnly --declaration --outDir dist",
18
+ "dev": "bun build ./src/cli.ts --outfile ./dist/cli.js --target node --watch",
19
+ "dev:bun": "bun build ./src/cli.ts --outfile ./dist/cli.bun.js --target bun --watch",
20
+ "clean": "rimraf dist",
21
+ "lint": "eslint src --fix",
22
+ "test": "bun test",
23
+ "test:watch": "bun test --watch"
24
+ },
25
+ "dependencies": {
26
+ "ai": "beta",
27
+ "@ai-sdk/anthropic": "beta",
28
+ "@ai-sdk/google": "beta",
29
+ "@ai-sdk/mistral": "beta",
30
+ "@ai-sdk/openai": "beta",
31
+ "ollama-ai-provider": "^1.2.0",
32
+ "@lssm/bundle.contractspec-workspace": "0.0.0-canary-20251221164004",
33
+ "@lssm/lib.ai-providers": "0.0.0-canary-20251221164004",
34
+ "@lssm/lib.contracts": "0.0.0-canary-20251221164004",
35
+ "@lssm/lib.contracts-transformers": "0.0.0-canary-20251221164004",
36
+ "@lssm/lib.testing": "0.0.0-canary-20251221164004",
37
+ "@lssm/lib.schema": "0.0.0-canary-20251221164004",
38
+ "@lssm/module.ai-chat": "0.0.0-canary-20251221164004",
39
+ "@lssm/module.contractspec-examples": "0.0.0-canary-20251221164004",
40
+ "glob": "^13.0.0",
41
+ "commander": "^14.0.2",
42
+ "@inquirer/prompts": "^8.1.0",
43
+ "chalk": "^5.6.2",
44
+ "ora": "^9.0.0",
45
+ "typescript": "^5.9.3",
46
+ "zod": "^4.1.13"
47
+ },
48
+ "devDependencies": {
49
+ "@lssm/tool.tsdown": "0.0.0-canary-20251221164004",
50
+ "@lssm/tool.typescript": "0.0.0-canary-20251221164004",
51
+ "@types/node": "^22.10.2",
52
+ "rimraf": "^6.1.2",
53
+ "eslint": "^9.39.2"
54
+ },
55
+ "keywords": [
56
+ "cli",
57
+ "contracts",
58
+ "code-generation",
59
+ "ai",
60
+ "typescript"
61
+ ],
62
+ "publishConfig": {
63
+ "access": "public",
64
+ "registry": "https://registry.npmjs.org/"
65
+ },
66
+ "license": "MIT",
67
+ "repository": {
68
+ "type": "git",
69
+ "url": "https://github.com/lssm-tech/contractspec.git",
70
+ "directory": "packages/apps/cli-contractspec"
71
+ }
72
+ }