create-mercato-app 0.10.1-develop.7601.1.e057b693a8 → 0.10.1-develop.7616.1.51a1f65492

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.
Files changed (134) hide show
  1. package/agentic/shared/ai/skills/om-setup-ci/SKILL.md +42 -0
  2. package/agentic/shared/ai/skills/om-setup-ci/scripts/setup-ci.mjs +525 -0
  3. package/agentic/shared/ai/skills/om-setup-ci/scripts/verify-semgrep-rules.mjs +54 -0
  4. package/agentic/shared/ai/skills/om-setup-ci/templates/docs/README.md +463 -0
  5. package/agentic/shared/ai/skills/om-setup-ci/templates/github/actions/gitleaks/action.yml +25 -0
  6. package/agentic/shared/ai/skills/om-setup-ci/templates/github/actions/playwright/action.yml +31 -0
  7. package/agentic/shared/ai/skills/om-setup-ci/templates/github/actions/setup/action.yml +55 -0
  8. package/agentic/shared/ai/skills/om-setup-ci/templates/github/dependabot.yml +27 -0
  9. package/agentic/shared/ai/skills/om-setup-ci/templates/github/workflows/audit.yml +193 -0
  10. package/agentic/shared/ai/skills/om-setup-ci/templates/github/workflows/ci.yml +477 -0
  11. package/agentic/shared/ai/skills/om-setup-ci/templates/github/workflows/codeql.yml +63 -0
  12. package/agentic/shared/ai/skills/om-setup-ci/templates/github/workflows/security.yml +254 -0
  13. package/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/audit-ci-allowlist.json +4 -0
  14. package/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/audit-ci.mjs +283 -0
  15. package/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/semgrep/open-mercato.fixture.txt +89 -0
  16. package/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/semgrep/open-mercato.yml +146 -0
  17. package/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/time-bomb-scanner.mjs +286 -0
  18. package/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/zap-rules.tsv +11 -0
  19. package/agentic/shared/ai/skills/tiers.json +2 -1
  20. package/dist/agentic/guides/module-facts.json +643 -157
  21. package/dist/agentic/guides/module-facts.v2.json +643 -157
  22. package/dist/agentic/guides/modules/agent_orchestrator/index.md +1 -1
  23. package/dist/agentic/guides/modules/ai_assistant/index.md +1 -1
  24. package/dist/agentic/guides/modules/api_docs/index.md +1 -1
  25. package/dist/agentic/guides/modules/api_keys/index.md +1 -1
  26. package/dist/agentic/guides/modules/attachments/index.md +1 -1
  27. package/dist/agentic/guides/modules/audit_logs/index.md +1 -1
  28. package/dist/agentic/guides/modules/auth/index.md +1 -1
  29. package/dist/agentic/guides/modules/availability/index.md +1 -1
  30. package/dist/agentic/guides/modules/business_rules/index.md +1 -1
  31. package/dist/agentic/guides/modules/carrier_inpost/acl-features.md +12 -0
  32. package/dist/agentic/guides/modules/carrier_inpost/contribution-resolutions.md +13 -0
  33. package/dist/agentic/guides/modules/carrier_inpost/di-registrations-rich.md +11 -0
  34. package/dist/agentic/guides/modules/carrier_inpost/exact-override-targets.md +16 -0
  35. package/dist/agentic/guides/modules/carrier_inpost/index.md +18 -0
  36. package/dist/agentic/guides/modules/carrier_inpost/owned-contract-module-metadata.md +11 -0
  37. package/dist/agentic/guides/modules/carrier_inpost/setup.md +11 -0
  38. package/dist/agentic/guides/modules/carrier_inpost/umes-contributions.md +13 -0
  39. package/dist/agentic/guides/modules/catalog/index.md +1 -1
  40. package/dist/agentic/guides/modules/channel_apns/index.md +1 -1
  41. package/dist/agentic/guides/modules/channel_discord/index.md +1 -1
  42. package/dist/agentic/guides/modules/channel_expo/index.md +1 -1
  43. package/dist/agentic/guides/modules/channel_fcm/index.md +1 -1
  44. package/dist/agentic/guides/modules/channel_gmail/index.md +1 -1
  45. package/dist/agentic/guides/modules/channel_imap/index.md +1 -1
  46. package/dist/agentic/guides/modules/channel_resend/index.md +1 -1
  47. package/dist/agentic/guides/modules/channel_ses/index.md +1 -1
  48. package/dist/agentic/guides/modules/checkout/index.md +1 -1
  49. package/dist/agentic/guides/modules/communication_channels/index.md +1 -1
  50. package/dist/agentic/guides/modules/configs/index.md +1 -1
  51. package/dist/agentic/guides/modules/content/index.md +1 -1
  52. package/dist/agentic/guides/modules/currencies/index.md +1 -1
  53. package/dist/agentic/guides/modules/customer_accounts/index.md +1 -1
  54. package/dist/agentic/guides/modules/customer_groups/index.md +1 -1
  55. package/dist/agentic/guides/modules/customers/index.md +1 -1
  56. package/dist/agentic/guides/modules/dashboards/index.md +1 -1
  57. package/dist/agentic/guides/modules/data_sync/index.md +1 -1
  58. package/dist/agentic/guides/modules/design_system/index.md +1 -1
  59. package/dist/agentic/guides/modules/devices/index.md +1 -1
  60. package/dist/agentic/guides/modules/dictionaries/index.md +1 -1
  61. package/dist/agentic/guides/modules/directory/index.md +1 -1
  62. package/dist/agentic/guides/modules/documents/index.md +1 -1
  63. package/dist/agentic/guides/modules/ecommerce/index.md +1 -1
  64. package/dist/agentic/guides/modules/entities/index.md +1 -1
  65. package/dist/agentic/guides/modules/eudr/index.md +1 -1
  66. package/dist/agentic/guides/modules/events/index.md +1 -1
  67. package/dist/agentic/guides/modules/feature_toggles/index.md +1 -1
  68. package/dist/agentic/guides/modules/forms/index.md +1 -1
  69. package/dist/agentic/guides/modules/gateway_stripe/index.md +1 -1
  70. package/dist/agentic/guides/modules/gateway_tpay/index.md +1 -1
  71. package/dist/agentic/guides/modules/generators/index.md +1 -1
  72. package/dist/agentic/guides/modules/inbox_ops/index.md +1 -1
  73. package/dist/agentic/guides/modules/integrations/index.md +1 -1
  74. package/dist/agentic/guides/modules/messages/index.md +1 -1
  75. package/dist/agentic/guides/modules/notifications/index.md +1 -1
  76. package/dist/agentic/guides/modules/onboarding/index.md +1 -1
  77. package/dist/agentic/guides/modules/payment_gateways/index.md +1 -1
  78. package/dist/agentic/guides/modules/perspectives/index.md +1 -1
  79. package/dist/agentic/guides/modules/phone_calls/index.md +1 -1
  80. package/dist/agentic/guides/modules/planner/index.md +1 -1
  81. package/dist/agentic/guides/modules/portal/index.md +1 -1
  82. package/dist/agentic/guides/modules/progress/index.md +1 -1
  83. package/dist/agentic/guides/modules/push_notifications/index.md +1 -1
  84. package/dist/agentic/guides/modules/query_index/index.md +1 -1
  85. package/dist/agentic/guides/modules/record_locks/index.md +1 -1
  86. package/dist/agentic/guides/modules/resources/index.md +1 -1
  87. package/dist/agentic/guides/modules/sales/active-extension-bindings.md +2 -0
  88. package/dist/agentic/guides/modules/sales/incoming-installed-contributions.md +2 -0
  89. package/dist/agentic/guides/modules/sales/index.md +2 -2
  90. package/dist/agentic/guides/modules/sales/umes-hosts.md +1 -0
  91. package/dist/agentic/guides/modules/scheduler/index.md +1 -1
  92. package/dist/agentic/guides/modules/search/index.md +1 -1
  93. package/dist/agentic/guides/modules/security/index.md +1 -1
  94. package/dist/agentic/guides/modules/seeds/index.md +1 -1
  95. package/dist/agentic/guides/modules/shipping_carriers/contribution-resolutions.md +1 -1
  96. package/dist/agentic/guides/modules/shipping_carriers/index.md +1 -1
  97. package/dist/agentic/guides/modules/shipping_carriers/umes-contributions.md +1 -1
  98. package/dist/agentic/guides/modules/sso/index.md +1 -1
  99. package/dist/agentic/guides/modules/staff/index.md +1 -1
  100. package/dist/agentic/guides/modules/storage_s3/index.md +1 -1
  101. package/dist/agentic/guides/modules/sync_akeneo/index.md +1 -1
  102. package/dist/agentic/guides/modules/sync_excel/index.md +1 -1
  103. package/dist/agentic/guides/modules/system_status_overlays/index.md +1 -1
  104. package/dist/agentic/guides/modules/telemetry/index.md +1 -1
  105. package/dist/agentic/guides/modules/tillio/index.md +1 -1
  106. package/dist/agentic/guides/modules/translations/index.md +1 -1
  107. package/dist/agentic/guides/modules/warranty_claims/index.md +1 -1
  108. package/dist/agentic/guides/modules/webhooks/index.md +1 -1
  109. package/dist/agentic/guides/modules/wms/index.md +1 -1
  110. package/dist/agentic/guides/modules/workflows/index.md +1 -1
  111. package/dist/agentic/guides/reference-module-facts.json +1 -1
  112. package/dist/agentic/guides/upstream/manifest.json +1 -1
  113. package/dist/agentic/shared/ai/skills/om-setup-ci/SKILL.md +42 -0
  114. package/dist/agentic/shared/ai/skills/om-setup-ci/scripts/setup-ci.mjs +525 -0
  115. package/dist/agentic/shared/ai/skills/om-setup-ci/scripts/verify-semgrep-rules.mjs +54 -0
  116. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/docs/README.md +463 -0
  117. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/github/actions/gitleaks/action.yml +25 -0
  118. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/github/actions/playwright/action.yml +31 -0
  119. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/github/actions/setup/action.yml +55 -0
  120. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/github/dependabot.yml +27 -0
  121. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/github/workflows/audit.yml +193 -0
  122. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/github/workflows/ci.yml +477 -0
  123. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/github/workflows/codeql.yml +63 -0
  124. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/github/workflows/security.yml +254 -0
  125. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/audit-ci-allowlist.json +4 -0
  126. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/audit-ci.mjs +283 -0
  127. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/semgrep/open-mercato.fixture.txt +89 -0
  128. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/semgrep/open-mercato.yml +146 -0
  129. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/time-bomb-scanner.mjs +286 -0
  130. package/dist/agentic/shared/ai/skills/om-setup-ci/templates/scripts/ci/zap-rules.tsv +11 -0
  131. package/dist/agentic/shared/ai/skills/tiers.json +2 -1
  132. package/package.json +3 -3
  133. package/template/.env.example +22 -0
  134. package/template/src/modules.ts +8 -0
@@ -0,0 +1,42 @@
1
+ ---
2
+ name: om-setup-ci
3
+ description: Set up or refresh CI for a standalone Open Mercato app in one command — GitHub Actions with dependency audit, secret scan (Gitleaks), SAST (Semgrep with Open Mercato rules), workflow security (zizmor), lint, generate + clean-tree check, typecheck, design-system check, unit tests, build, ephemeral integration tests, Docker build with Trivy, nightly audit and security scans, optional CodeQL and OWASP ZAP, Dependabot, SHA-pinned actions, and client-facing docs. Use for "set up CI", "add GitHub Actions", "add security scanning", "SAST/DAST", "refresh CI", "skonfiguruj CI".
4
+ ---
5
+
6
+ # Set Up CI
7
+
8
+ One deterministic generator, `scripts/setup-ci.mjs`, does the work. Your job is to choose its options, run it, and explain the result. Do not hand-write workflow files.
9
+
10
+ ## Workflow
11
+
12
+ 1. Confirm the provider. Only `github` is supported. For any other provider, stop and say so; do not improvise a pipeline.
13
+ 2. Preview the plan: `node .ai/skills/om-setup-ci/scripts/setup-ci.mjs --dry-run`. It reports the detected branches, Node source, and each check as enabled or disabled with the reason.
14
+ 3. Ask the user only about what detection cannot know:
15
+ - which branches to protect, when the detected list looks wrong;
16
+ - whether the repository is private and the build may need a larger runner (`--runner`);
17
+ - whether the repository is public or has GitHub Code Security: only then offer `--with-code-scanning` (CodeQL plus SARIF uploads);
18
+ - whether they want the nightly OWASP ZAP scan (`--with-dast`);
19
+ - whether to disable any check they explicitly do not want (`--no-<check>`).
20
+ 4. Run the generator with the agreed flags and no `--dry-run`. Exit code `3` means some files already exist with different content and nothing was written. Show the user the differences, then re-run with `--force` only after they agree. `scripts/ci/audit-ci-allowlist.json` and `scripts/ci/zap-rules.tsv` are never overwritten.
21
+ 5. Run the fast checks locally to verify them: `node scripts/ci/audit-ci.mjs --severity high` and, when Docker is available, the Semgrep and Gitleaks commands from `docs/ci/README.md` → *Running the checks locally*. Report existing findings; do not suppress them yourself.
22
+ 6. Commit on a branch and open a pull request so the first run executes, following `.ai/agentic.config.json` (`om-open-pr` when installed).
23
+ 7. Tell the user to mark the `CI` check as required and to enable secret scanning push protection (`docs/ci/README.md` → *Branch protection and repository settings*). Do not change repository settings yourself unless they ask; these are admin changes to shared settings.
24
+
25
+ ## Options
26
+
27
+ `--branches a,b` · `--node <v>` · `--runner <label>` · `--audit-severity low|moderate|high|critical` · `--audit-cron "<cron>"` · `--target <dir>` · `--dry-run` · `--force` · `--json`.
28
+
29
+ Checks, on by default (`--no-<check>` turns one off): `secrets`, `sast`, `workflow-lint`, `lint`, `typecheck`, `ds-check`, `time-bombs`, `test`, `build`, `integration`, `docker`, `image-scan`, `scheduled-audit`, `scheduled-security`, `dependabot`. Off by default (`--with-<check>` turns one on): `code-scanning`, `dast`.
30
+
31
+ Options are saved in `.github/om-ci.json`, so a later `--force` refresh keeps them.
32
+
33
+ ## Rules
34
+
35
+ - Never weaken a gate to get a green first run: do not add `continue-on-error`, delete steps, or add suppressions (`nosemgrep`, `.gitleaksignore`, `.trivyignore`, allowlist entries, ZAP `IGNORE`). Report the finding and fix its cause; suppress only a confirmed false positive, with the reason written next to it, and only when the user agrees.
36
+ - If the secret scan finds a credential, tell the user to revoke and rotate it at the provider first. Deleting the commit does not undo the leak.
37
+ - Never put secrets in workflow files; use `${{ secrets.NAME }}`.
38
+ - Keep deploy steps out of `ci.yml`; put them in a separate workflow triggered after `CI`.
39
+ - If the user edits generated files, warn them that `--force` overwrites those edits.
40
+ - `templates/docs/README.md` explains each check, why it exists, and what it does not cover. Read it before answering questions about the pipeline.
41
+ - Changing a pinned action, image or tool version: edit `ACTIONS` / `TOOLS` in `scripts/setup-ci.mjs`, choose a release at least two weeks old, pin the commit SHA, image digest or checksum, then regenerate.
42
+ - Changing an Open Mercato Semgrep rule: update `templates/scripts/ci/semgrep/open-mercato.yml` and its annotated fixture `open-mercato.fixture.txt` (`// ruleid:` must match, `// ok:` must not), then run `node .ai/skills/om-setup-ci/scripts/verify-semgrep-rules.mjs` (needs Docker).
@@ -0,0 +1,525 @@
1
+ #!/usr/bin/env node
2
+ // Generates a CI pipeline for a standalone Open Mercato app (create-mercato-app).
3
+ //
4
+ // Usage:
5
+ // node .ai/skills/om-setup-ci/scripts/setup-ci.mjs [options]
6
+ //
7
+ // The generator inspects the target app (package.json scripts, Node pin,
8
+ // Dockerfile, integration-test harness, git branches), renders the provider
9
+ // templates shipped next to this script, and writes them into the app. It is
10
+ // dependency-free so it runs before `yarn install` and from any checkout.
11
+ //
12
+ // It never overwrites a file it did not produce identically unless `--force`
13
+ // is passed, and it never overwrites the audit allowlist, which holds the
14
+ // app's own justified exceptions. Re-running reuses the options recorded in
15
+ // .github/om-ci.json, so `--force` alone refreshes a pipeline to the latest
16
+ // templates without re-typing flags.
17
+
18
+ import { execFileSync } from 'node:child_process'
19
+ import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
20
+ import path from 'node:path'
21
+ import { fileURLToPath, pathToFileURL } from 'node:url'
22
+
23
+ export const GENERATOR_ID = 'om-setup-ci'
24
+ export const TEMPLATE_VERSION = 2
25
+ export const MANIFEST_PATH = '.github/om-ci.json'
26
+ export const SUPPORTED_PROVIDERS = ['github']
27
+
28
+ const SKILL_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
29
+ const TEMPLATES_ROOT = path.join(SKILL_ROOT, 'templates')
30
+
31
+ // Every third-party action and tool is pinned to an immutable reference: actions
32
+ // to a commit SHA (Dependabot keeps them current through the trailing version
33
+ // comment), container images to a digest, the Gitleaks binary to a checksum.
34
+ // Each pinned release was at least two weeks old when it was chosen.
35
+ export const ACTIONS = {
36
+ CHECKOUT: 'actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1',
37
+ SETUP_NODE: 'actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0',
38
+ CACHE: 'actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0',
39
+ UPLOAD_ARTIFACT: 'actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2',
40
+ GITHUB_SCRIPT: 'actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0',
41
+ SETUP_BUILDX: 'docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1',
42
+ BUILD_PUSH: 'docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0',
43
+ ZIZMOR: 'zizmorcore/zizmor-action@cc914d7f3750a2d13d75c7f184a1060aa0e9d482 # v0.6.4',
44
+ CODEQL_INIT: 'github/codeql-action/init@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2',
45
+ CODEQL_ANALYZE: 'github/codeql-action/analyze@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2',
46
+ UPLOAD_SARIF: 'github/codeql-action/upload-sarif@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2',
47
+ }
48
+
49
+ export const TOOLS = {
50
+ GITLEAKS_VERSION: '8.30.1',
51
+ GITLEAKS_SHA256: '551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb',
52
+ SEMGREP_IMAGE: 'semgrep/semgrep:1.180.0@sha256:529ee8a277ec8adc5b534d7c74eea0a47e9de21d62852b6ba7ac6ba9566845c3',
53
+ TRIVY_IMAGE: 'aquasec/trivy:0.75.0@sha256:af6acf9a6b85dfe389a1941505c0ce9efef52a4719635e1a962f022a3d855daa',
54
+ ZAP_IMAGE: 'zaproxy/zap-stable:2.17.0@sha256:5ccfff9a411743ab2615136a2edd7416bec9ce41572fa94e935df38c14adb2b6',
55
+ ZIZMOR_VERSION: '1.30.1',
56
+ }
57
+
58
+ export const OPTIONAL_CHECKS = [
59
+ { id: 'secrets', label: 'Secret scan (Gitleaks)' },
60
+ { id: 'sast', label: 'Static analysis (Semgrep)' },
61
+ { id: 'workflowLint', label: 'Workflow security (zizmor)' },
62
+ { id: 'lint', label: 'Lint', script: 'lint' },
63
+ { id: 'typecheck', label: 'Typecheck', script: 'typecheck' },
64
+ { id: 'dsCheck', label: 'Design-system check', script: 'ds:check' },
65
+ { id: 'timeBombs', label: 'Time-bomb test literals' },
66
+ { id: 'test', label: 'Unit tests', script: 'test' },
67
+ { id: 'build', label: 'Production build', script: 'build' },
68
+ { id: 'integration', label: 'Integration tests', script: 'test:integration:ephemeral' },
69
+ { id: 'docker', label: 'Docker image build' },
70
+ { id: 'imageScan', label: 'Image scan (Trivy)' },
71
+ { id: 'codeScanning', label: 'CodeQL + code-scanning uploads', defaultOn: false },
72
+ { id: 'dast', label: 'Nightly dynamic scan (OWASP ZAP)', script: 'test:integration:ephemeral:start', defaultOn: false },
73
+ ]
74
+
75
+ export const OPTIONAL_FILES = [
76
+ { id: 'scheduledAudit', label: 'Nightly dependency audit' },
77
+ { id: 'scheduledSecurity', label: 'Nightly security scan' },
78
+ { id: 'dependabot', label: 'Dependabot for GitHub Actions' },
79
+ ]
80
+
81
+ const isDefaultOn = (entry) => entry.defaultOn !== false
82
+
83
+ const flagName = (id) => id.replace(/[A-Z]/g, (char) => `-${char.toLowerCase()}`)
84
+
85
+ const FLAG_NAMES = Object.fromEntries([...OPTIONAL_CHECKS, ...OPTIONAL_FILES].map((entry) => [flagName(entry.id), entry.id]))
86
+
87
+ const REMOVABLE_PATHS = new Set([
88
+ '.github/workflows/ci.yml',
89
+ '.github/workflows/audit.yml',
90
+ '.github/workflows/security.yml',
91
+ '.github/workflows/codeql.yml',
92
+ '.github/actions/setup/action.yml',
93
+ '.github/actions/gitleaks/action.yml',
94
+ '.github/actions/playwright/action.yml',
95
+ '.github/dependabot.yml',
96
+ 'scripts/ci/audit-ci.mjs',
97
+ 'scripts/ci/time-bomb-scanner.mjs',
98
+ 'scripts/ci/semgrep/open-mercato.yml',
99
+ 'docs/ci/README.md',
100
+ ])
101
+
102
+ const SECURITY_CRON = '37 3 * * *'
103
+ const CODEQL_CRON = '23 4 * * 1'
104
+
105
+ const SEVERITIES = ['low', 'moderate', 'high', 'critical']
106
+ const SAFE_REF = /^[A-Za-z0-9._/-]+$/
107
+ const SAFE_RUNNER = /^[A-Za-z0-9._-]+$/
108
+ const SAFE_NODE = /^[A-Za-z0-9.*/-]+$/
109
+ const SAFE_CRON = /^[0-9*/,\- ]+$/
110
+
111
+ export const HELP = `Usage: node .ai/skills/om-setup-ci/scripts/setup-ci.mjs [options]
112
+
113
+ Generates a CI pipeline for a standalone Open Mercato app.
114
+
115
+ Options:
116
+ --provider <name> CI provider (default: github; supported: ${SUPPORTED_PROVIDERS.join(', ')})
117
+ --target <dir> App root to configure (default: current directory)
118
+ --branches <a,b> Branches CI protects (default: detected default branch, plus develop when it exists)
119
+ --node <version> Node.js version when the app has no .nvmrc/.node-version (default: engines.node major, else 24)
120
+ --runner <label> runs-on label for every job (default: ubuntu-latest)
121
+ --audit-severity <level> Lowest advisory severity that fails the audit (default: high)
122
+ --audit-cron <expr> Schedule of the nightly audit (default: "17 6 * * *")
123
+ --no-<check> Disable a check: ${Object.keys(FLAG_NAMES).join(', ')}
124
+ --with-<check> Enable a check. Off by default: code-scanning (CodeQL and SARIF uploads;
125
+ needs a public repo or GitHub Code Security), dast (nightly OWASP ZAP scan)
126
+ --dry-run Print the plan without writing files
127
+ --force Overwrite generated files that differ from the templates
128
+ --json Print the result as JSON
129
+ -h, --help Show this help
130
+ `
131
+
132
+ export function parseArgs(argv) {
133
+ const options = { overrides: {}, force: false, dryRun: false, json: false, help: false }
134
+ const takeValue = (index, name) => {
135
+ const value = argv[index + 1]
136
+ if (value === undefined || value.startsWith('--')) throw new Error(`${name} requires a value`)
137
+ return value
138
+ }
139
+ for (let index = 0; index < argv.length; index++) {
140
+ const argument = argv[index]
141
+ const [name, inlineValue] = argument.includes('=') ? argument.split(/=(.*)/s, 2) : [argument, undefined]
142
+ const value = () => {
143
+ if (inlineValue !== undefined) return inlineValue
144
+ const next = takeValue(index, name)
145
+ index++
146
+ return next
147
+ }
148
+ if (name === '-h' || name === '--help') options.help = true
149
+ else if (name === '--force') options.force = true
150
+ else if (name === '--dry-run') options.dryRun = true
151
+ else if (name === '--json') options.json = true
152
+ else if (name === '--provider') options.provider = value()
153
+ else if (name === '--target') options.target = value()
154
+ else if (name === '--branches') options.branches = value().split(',').map((entry) => entry.trim()).filter(Boolean)
155
+ else if (name === '--node') options.node = value()
156
+ else if (name === '--runner') options.runner = value()
157
+ else if (name === '--audit-severity') options.auditSeverity = value()
158
+ else if (name === '--audit-cron') options.auditCron = value()
159
+ else if (name.startsWith('--no-') && FLAG_NAMES[name.slice(5)]) options.overrides[FLAG_NAMES[name.slice(5)]] = false
160
+ else if (name.startsWith('--with-') && FLAG_NAMES[name.slice(7)]) options.overrides[FLAG_NAMES[name.slice(7)]] = true
161
+ else throw new Error(`unknown option "${argument}" (see --help)`)
162
+ }
163
+ return options
164
+ }
165
+
166
+ function readJson(filePath) {
167
+ return JSON.parse(readFileSync(filePath, 'utf8'))
168
+ }
169
+
170
+ function git(targetDir, args) {
171
+ try {
172
+ return execFileSync('git', ['-C', targetDir, ...args], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim()
173
+ } catch {
174
+ return ''
175
+ }
176
+ }
177
+
178
+ export function detectBranches(targetDir) {
179
+ const remoteHead = git(targetDir, ['symbolic-ref', '--quiet', '--short', 'refs/remotes/origin/HEAD'])
180
+ const conventionalBranch = ['main', 'master'].find((name) => git(targetDir, ['show-ref', '--verify', `refs/heads/${name}`]))
181
+ const defaultBranch = remoteHead.startsWith('origin/')
182
+ ? remoteHead.slice('origin/'.length)
183
+ : conventionalBranch
184
+ || git(targetDir, ['rev-parse', '--abbrev-ref', 'HEAD'])
185
+ || 'main'
186
+ const branches = [defaultBranch === 'HEAD' ? 'main' : defaultBranch]
187
+ const hasDevelop = git(targetDir, ['show-ref', 'refs/remotes/origin/develop']) || git(targetDir, ['show-ref', 'refs/heads/develop'])
188
+ if (hasDevelop && !branches.includes('develop')) branches.push('develop')
189
+ return branches
190
+ }
191
+
192
+ export function detectNode(targetDir, packageJson) {
193
+ for (const file of ['.nvmrc', '.node-version']) {
194
+ if (existsSync(path.join(targetDir, file))) return { versionFile: file, version: null }
195
+ }
196
+ const major = String(packageJson.engines?.node ?? '').match(/\d+/)?.[0]
197
+ return { versionFile: null, version: major ?? '24' }
198
+ }
199
+
200
+ function hasIntegrationHarness(targetDir, packageJson) {
201
+ const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies }
202
+ return Boolean(dependencies['@playwright/test'])
203
+ && existsSync(path.join(targetDir, '.ai', 'qa', 'tests', 'playwright.config.ts'))
204
+ }
205
+
206
+ export function detectApp(targetDir) {
207
+ const packageJsonPath = path.join(targetDir, 'package.json')
208
+ if (!existsSync(packageJsonPath)) throw new Error(`no package.json in ${targetDir} — run from the app root or pass --target`)
209
+ const packageJson = readJson(packageJsonPath)
210
+ const dependencies = { ...packageJson.dependencies, ...packageJson.devDependencies }
211
+ if (!Object.keys(dependencies).some((name) => name.startsWith('@open-mercato/'))) {
212
+ throw new Error(`${packageJsonPath} has no @open-mercato/* dependency — this generator targets apps created with create-mercato-app`)
213
+ }
214
+ if (!existsSync(path.join(targetDir, 'yarn.lock'))) {
215
+ throw new Error('no yarn.lock found — the generated pipeline installs with `yarn install --immutable`, so commit a Yarn lockfile first')
216
+ }
217
+ const scripts = packageJson.scripts ?? {}
218
+ if (!scripts.generate) throw new Error('package.json has no "generate" script — expected an Open Mercato app (`yarn mercato generate`)')
219
+
220
+ const available = {}
221
+ const reasons = {}
222
+ for (const check of OPTIONAL_CHECKS) {
223
+ if (check.script) {
224
+ available[check.id] = Boolean(scripts[check.script])
225
+ if (!available[check.id]) reasons[check.id] = `no "${check.script}" script in package.json`
226
+ }
227
+ }
228
+ for (const id of ['secrets', 'sast', 'workflowLint', 'timeBombs', 'codeScanning', 'scheduledAudit', 'scheduledSecurity', 'dependabot']) {
229
+ available[id] = true
230
+ }
231
+ available.docker = existsSync(path.join(targetDir, 'Dockerfile'))
232
+ if (!available.docker) reasons.docker = 'no Dockerfile at the app root'
233
+ available.imageScan = available.docker
234
+ if (!available.imageScan) reasons.imageScan = 'no Dockerfile at the app root'
235
+ const harness = hasIntegrationHarness(targetDir, packageJson)
236
+ for (const id of ['integration', 'dast']) {
237
+ if (available[id] && !harness) {
238
+ available[id] = false
239
+ reasons[id] = 'missing @playwright/test or .ai/qa/tests/playwright.config.ts'
240
+ }
241
+ }
242
+
243
+ return {
244
+ packageJson,
245
+ available,
246
+ reasons,
247
+ node: detectNode(targetDir, packageJson),
248
+ branches: detectBranches(targetDir),
249
+ testCommand: /\bjest\b/.test(scripts.test ?? '') ? 'yarn test --ci' : 'yarn test',
250
+ existingWorkflows: existsSync(path.join(targetDir, '.github', 'workflows'))
251
+ ? readdirSync(path.join(targetDir, '.github', 'workflows')).filter((name) => /\.ya?ml$/.test(name))
252
+ : [],
253
+ }
254
+ }
255
+
256
+ export function resolveConfig(cliOptions, detected, manifest) {
257
+ const recorded = manifest?.options ?? {}
258
+ const provider = cliOptions.provider ?? manifest?.provider ?? 'github'
259
+ if (!SUPPORTED_PROVIDERS.includes(provider)) {
260
+ throw new Error(`provider "${provider}" is not supported yet (supported: ${SUPPORTED_PROVIDERS.join(', ')})`)
261
+ }
262
+ const config = {
263
+ provider,
264
+ branches: cliOptions.branches ?? recorded.branches ?? detected.branches,
265
+ node: cliOptions.node ?? recorded.node ?? null,
266
+ runner: cliOptions.runner ?? recorded.runner ?? 'ubuntu-latest',
267
+ auditSeverity: cliOptions.auditSeverity ?? recorded.auditSeverity ?? 'high',
268
+ auditCron: cliOptions.auditCron ?? recorded.auditCron ?? '17 6 * * *',
269
+ disabled: [],
270
+ }
271
+ if (config.branches.length === 0) throw new Error('--branches needs at least one branch')
272
+ for (const branch of config.branches) {
273
+ if (!SAFE_REF.test(branch)) throw new Error(`invalid branch name "${branch}"`)
274
+ }
275
+ if (!SAFE_RUNNER.test(config.runner)) throw new Error(`invalid runner label "${config.runner}"`)
276
+ if (config.node !== null && !SAFE_NODE.test(config.node)) throw new Error(`invalid Node version "${config.node}"`)
277
+ if (!SEVERITIES.includes(config.auditSeverity)) {
278
+ throw new Error(`invalid --audit-severity "${config.auditSeverity}" (expected one of ${SEVERITIES.join(', ')})`)
279
+ }
280
+ if (!SAFE_CRON.test(config.auditCron) || config.auditCron.trim().split(/\s+/).length !== 5) {
281
+ throw new Error(`invalid --audit-cron "${config.auditCron}" (expected a 5-field cron expression)`)
282
+ }
283
+
284
+ const recordedDisabled = new Set(recorded.disabled ?? [])
285
+ const recordedEnabled = new Set(recorded.enabled ?? [])
286
+ config.enabled = []
287
+ const features = {}
288
+ const notes = {}
289
+ for (const entry of [...OPTIONAL_CHECKS, ...OPTIONAL_FILES]) {
290
+ const recordedChoice = isDefaultOn(entry) ? !recordedDisabled.has(entry.id) : recordedEnabled.has(entry.id)
291
+ const requested = cliOptions.overrides[entry.id] ?? recordedChoice
292
+ if (requested !== isDefaultOn(entry)) (requested ? config.enabled : config.disabled).push(entry.id)
293
+ if (!requested) {
294
+ features[entry.id] = false
295
+ notes[entry.id] = isDefaultOn(entry) ? 'disabled by option' : `off by default, enable with --with-${flagName(entry.id)}`
296
+ } else if (!detected.available[entry.id]) {
297
+ features[entry.id] = false
298
+ notes[entry.id] = detected.reasons[entry.id]
299
+ } else {
300
+ features[entry.id] = true
301
+ }
302
+ }
303
+ if (features.imageScan && !features.docker) {
304
+ features.imageScan = false
305
+ notes.imageScan = 'needs the Docker image build'
306
+ }
307
+ if (features.dast && !features.scheduledSecurity) {
308
+ features.dast = false
309
+ notes.dast = 'needs the nightly security scan'
310
+ }
311
+ const nightlyScans = ['secrets', 'sast', 'imageScan', 'dast'].filter((id) => features[id])
312
+ if (features.scheduledSecurity && nightlyScans.length === 0) {
313
+ features.scheduledSecurity = false
314
+ notes.scheduledSecurity = 'no security scan is enabled'
315
+ }
316
+ return { config, features, notes }
317
+ }
318
+
319
+ export function renderTemplate(text, flags, tokens) {
320
+ const output = []
321
+ const stack = []
322
+ for (const line of text.split('\n')) {
323
+ const condition = line.match(/^\s*#@if\s+(!?)([A-Za-z]+)\s*$/)
324
+ if (condition) {
325
+ if (!(condition[2] in flags)) throw new Error(`template uses unknown flag "${condition[2]}"`)
326
+ stack.push(condition[1] ? !flags[condition[2]] : Boolean(flags[condition[2]]))
327
+ continue
328
+ }
329
+ if (/^\s*#@endif\s*$/.test(line)) {
330
+ if (stack.length === 0) throw new Error('template has an unmatched #@endif')
331
+ stack.pop()
332
+ continue
333
+ }
334
+ if (stack.every(Boolean)) output.push(line)
335
+ }
336
+ if (stack.length > 0) throw new Error('template has an unclosed #@if')
337
+ return output.join('\n').replace(/__OM_([A-Z0-9_]+)__/g, (match, name) => {
338
+ if (!(name in tokens)) throw new Error(`template uses unknown token ${match}`)
339
+ return tokens[name]
340
+ })
341
+ }
342
+
343
+ function checksTable(features, notes) {
344
+ const rows = [...OPTIONAL_CHECKS, ...OPTIONAL_FILES].map((entry) => {
345
+ const status = features[entry.id] ? 'enabled' : `disabled — ${notes[entry.id]}`
346
+ return `| ${entry.label} | ${status} |`
347
+ })
348
+ return ['| Check | Status in this repository |', '| --- | --- |', '| Dependency audit (pull requests) | enabled |', '| Generate + clean-tree check | enabled |', ...rows].join('\n')
349
+ }
350
+
351
+ export function buildPlan(config, features, notes, detected) {
352
+ const node = config.node
353
+ ? { versionFile: null, version: config.node }
354
+ : detected.node
355
+ const flags = { ...features, nodeVersionFile: Boolean(node.versionFile) }
356
+ const needs = [
357
+ 'changes',
358
+ 'audit',
359
+ features.secrets && 'secrets',
360
+ features.sast && 'sast',
361
+ features.workflowLint && 'workflow-lint',
362
+ features.lint && 'lint',
363
+ 'validate',
364
+ features.integration && 'integration',
365
+ features.docker && 'docker',
366
+ ].filter(Boolean)
367
+ const securityNeeds = [
368
+ features.secrets && 'secrets',
369
+ features.sast && 'sast',
370
+ features.imageScan && 'image',
371
+ features.dast && 'dast',
372
+ ].filter(Boolean)
373
+ const tokens = {
374
+ ...Object.fromEntries(Object.entries(ACTIONS).map(([name, reference]) => [`ACTION_${name}`, reference])),
375
+ ...TOOLS,
376
+ DEFAULT_BRANCH: config.branches[0],
377
+ SECURITY_CRON,
378
+ CODEQL_CRON,
379
+ SECURITY_NEEDS: securityNeeds.join(', '),
380
+ BRANCH_LIST: config.branches.map((branch) => ` - ${branch}`).join('\n'),
381
+ BRANCH_MATRIX: config.branches.map((branch) => `'${branch}'`).join(', '),
382
+ BRANCHES_INLINE: config.branches.map((branch) => `\`${branch}\``).join(', '),
383
+ RUNNER: config.runner,
384
+ NODE_VERSION: node.version ?? '',
385
+ NODE_VERSION_FILE: node.versionFile ?? '',
386
+ NODE_SOURCE: node.versionFile ? `\`${node.versionFile}\`` : `\`${node.version}\` (no \`.nvmrc\` found; change with \`--node\`)`,
387
+ AUDIT_SEVERITY: config.auditSeverity,
388
+ AUDIT_CRON: config.auditCron,
389
+ TEST_COMMAND: detected.testCommand,
390
+ DOCKERFILE: 'Dockerfile',
391
+ NEEDS: needs.join(', '),
392
+ INTEGRATION_NEEDS: features.lint ? 'lint, validate' : 'validate',
393
+ CHECKS_TABLE: checksTable(features, notes),
394
+ }
395
+ const render = (relativeTemplate) => renderTemplate(readFileSync(path.join(TEMPLATES_ROOT, relativeTemplate), 'utf8'), flags, tokens)
396
+ const copy = (relativeTemplate) => readFileSync(path.join(TEMPLATES_ROOT, relativeTemplate), 'utf8')
397
+
398
+ const files = [
399
+ { path: '.github/workflows/ci.yml', content: render('github/workflows/ci.yml') },
400
+ { path: '.github/actions/setup/action.yml', content: render('github/actions/setup/action.yml') },
401
+ { path: '.github/workflows/codeql.yml', content: render('github/workflows/codeql.yml'), when: features.codeScanning },
402
+ { path: '.github/workflows/security.yml', content: render('github/workflows/security.yml'), when: features.scheduledSecurity },
403
+ { path: '.github/actions/gitleaks/action.yml', content: render('github/actions/gitleaks/action.yml'), when: features.secrets },
404
+ { path: '.github/actions/playwright/action.yml', content: render('github/actions/playwright/action.yml'), when: features.integration },
405
+ { path: 'scripts/ci/semgrep/open-mercato.yml', content: copy('scripts/ci/semgrep/open-mercato.yml'), when: features.sast },
406
+ { path: 'scripts/ci/zap-rules.tsv', content: copy('scripts/ci/zap-rules.tsv'), preserve: true, when: features.dast },
407
+ { path: 'scripts/ci/audit-ci.mjs', content: copy('scripts/ci/audit-ci.mjs') },
408
+ { path: 'scripts/ci/audit-ci-allowlist.json', content: copy('scripts/ci/audit-ci-allowlist.json'), preserve: true },
409
+ { path: 'docs/ci/README.md', content: render('docs/README.md') },
410
+ ]
411
+ .filter((file) => file.when !== false)
412
+ .map((file) => ({ path: file.path, content: file.content, preserve: file.preserve }))
413
+ if (features.scheduledAudit) files.push({ path: '.github/workflows/audit.yml', content: render('github/workflows/audit.yml') })
414
+ if (features.dependabot) files.push({ path: '.github/dependabot.yml', content: render('github/dependabot.yml') })
415
+ if (features.timeBombs) files.push({ path: 'scripts/ci/time-bomb-scanner.mjs', content: copy('scripts/ci/time-bomb-scanner.mjs') })
416
+
417
+ const manifest = {
418
+ generator: GENERATOR_ID,
419
+ templateVersion: TEMPLATE_VERSION,
420
+ provider: config.provider,
421
+ options: {
422
+ branches: config.branches,
423
+ node: config.node,
424
+ runner: config.runner,
425
+ auditSeverity: config.auditSeverity,
426
+ auditCron: config.auditCron,
427
+ disabled: config.disabled,
428
+ enabled: config.enabled,
429
+ },
430
+ files: files.map((file) => file.path).sort(),
431
+ }
432
+ files.push({ path: MANIFEST_PATH, content: `${JSON.stringify(manifest, null, 2)}\n` })
433
+ return { files, needs }
434
+ }
435
+
436
+ export function applyPlan(targetDir, files, { force, dryRun }, previousFiles = []) {
437
+ const planned = new Set(files.map((file) => file.path))
438
+ const removals = previousFiles
439
+ .filter((filePath) => REMOVABLE_PATHS.has(filePath) && !planned.has(filePath))
440
+ .filter((filePath) => existsSync(path.join(targetDir, filePath)))
441
+ .map((filePath) => ({ path: filePath, status: 'remove' }))
442
+ const results = files.map((file) => {
443
+ const absolutePath = path.join(targetDir, file.path)
444
+ if (!existsSync(absolutePath)) return { ...file, status: 'create' }
445
+ const current = readFileSync(absolutePath, 'utf8')
446
+ if (current === file.content) return { ...file, status: 'unchanged' }
447
+ if (file.preserve) return { ...file, status: 'kept' }
448
+ return { ...file, status: force ? 'overwrite' : 'conflict' }
449
+ })
450
+ const conflicts = results.filter((result) => result.status === 'conflict')
451
+ if (!dryRun && conflicts.length === 0) {
452
+ for (const result of results) {
453
+ if (result.status !== 'create' && result.status !== 'overwrite') continue
454
+ const absolutePath = path.join(targetDir, result.path)
455
+ mkdirSync(path.dirname(absolutePath), { recursive: true })
456
+ writeFileSync(absolutePath, result.content)
457
+ }
458
+ for (const removal of removals) rmSync(path.join(targetDir, removal.path))
459
+ }
460
+ return { results: [...results, ...removals], conflicts }
461
+ }
462
+
463
+ function report(outcome) {
464
+ const { targetDir, config, features, notes, results, conflicts, dryRun, detected } = outcome
465
+ const lines = [`om-setup-ci → ${targetDir} (provider: ${config.provider}${dryRun ? ', dry run' : ''})`, '']
466
+ lines.push(`Branches: ${config.branches.join(', ')} Runner: ${config.runner} Audit: ${config.auditSeverity}+`)
467
+ lines.push('', 'Checks:')
468
+ for (const entry of [...OPTIONAL_CHECKS, ...OPTIONAL_FILES]) {
469
+ lines.push(` ${features[entry.id] ? '✓' : '·'} ${entry.label}${features[entry.id] ? '' : ` (${notes[entry.id]})`}`)
470
+ }
471
+ lines.push('', 'Files:')
472
+ for (const result of results) lines.push(` ${result.status.padEnd(9)} ${result.path}`)
473
+ const others = detected.existingWorkflows.filter((name) => !REMOVABLE_PATHS.has(`.github/workflows/${name}`))
474
+ if (others.length > 0) {
475
+ lines.push('', `Existing workflows left untouched (check them for duplicated jobs): ${others.join(', ')}`)
476
+ }
477
+ if (conflicts.length > 0) {
478
+ lines.push('', `Nothing was written: ${conflicts.length} file(s) already exist with different content.`)
479
+ lines.push('Review them, then re-run with --force to replace them with the generated versions.')
480
+ } else if (!dryRun) {
481
+ lines.push('', 'Next steps:')
482
+ lines.push(' 1. Review the diff and commit it on a branch, then open a pull request to see the first run.')
483
+ lines.push(' 2. Mark the "CI" check as required for the protected branches (see docs/ci/README.md → Branch protection).')
484
+ }
485
+ return lines.join('\n')
486
+ }
487
+
488
+ export function run(argv, { cwd = process.cwd(), log = console.log } = {}) {
489
+ const cliOptions = parseArgs(argv)
490
+ if (cliOptions.help) {
491
+ log(HELP)
492
+ return 0
493
+ }
494
+ const targetDir = path.resolve(cwd, cliOptions.target ?? '.')
495
+ const detected = detectApp(targetDir)
496
+ const manifestPath = path.join(targetDir, MANIFEST_PATH)
497
+ const manifest = existsSync(manifestPath) ? readJson(manifestPath) : null
498
+ const { config, features, notes } = resolveConfig(cliOptions, detected, manifest)
499
+ const { files } = buildPlan(config, features, notes, detected)
500
+ const { results, conflicts } = applyPlan(targetDir, files, cliOptions, Array.isArray(manifest?.files) ? manifest.files : [])
501
+ const outcome = { targetDir, config, features, notes, results, conflicts, dryRun: cliOptions.dryRun, detected }
502
+ if (cliOptions.json) {
503
+ log(JSON.stringify({
504
+ targetDir,
505
+ provider: config.provider,
506
+ dryRun: cliOptions.dryRun,
507
+ options: config,
508
+ checks: Object.fromEntries(Object.entries(features).map(([id, enabled]) => [id, enabled ? 'enabled' : notes[id]])),
509
+ files: results.map((result) => ({ path: result.path, status: result.status })),
510
+ }, null, 2))
511
+ } else {
512
+ log(report(outcome))
513
+ }
514
+ return conflicts.length > 0 ? 3 : 0
515
+ }
516
+
517
+ const invokedPath = process.argv[1] ? pathToFileURL(path.resolve(process.argv[1])).href : null
518
+ if (invokedPath === import.meta.url) {
519
+ try {
520
+ process.exitCode = run(process.argv.slice(2))
521
+ } catch (error) {
522
+ console.error(`om-setup-ci: ${error.message}`)
523
+ process.exitCode = 1
524
+ }
525
+ }
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env node
2
+ // Verifies the Open Mercato Semgrep rules against their annotated fixture.
3
+ //
4
+ // Usage (needs Docker):
5
+ // node .ai/skills/om-setup-ci/scripts/verify-semgrep-rules.mjs
6
+ //
7
+ // templates/scripts/ci/semgrep/open-mercato.fixture.txt is deliberately
8
+ // vulnerable TSX. It is stored with a .txt extension so the app's TypeScript
9
+ // and ESLint never pick it up; this script copies it to a temporary .tsx file,
10
+ // runs the pinned Semgrep image over it, and checks that every
11
+ // `// ruleid: <id>` line produces exactly that finding and every
12
+ // `// ok: <id>` line produces none. Run it after changing any rule.
13
+ // (`semgrep --test` would be the natural tool, but its rule-id matching is
14
+ // broken for this layout in the pinned release.)
15
+
16
+ import { execFileSync } from 'node:child_process'
17
+ import { copyFileSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
18
+ import os from 'node:os'
19
+ import path from 'node:path'
20
+ import { fileURLToPath } from 'node:url'
21
+ import { TOOLS } from './setup-ci.mjs'
22
+
23
+ const semgrepDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'templates', 'scripts', 'ci', 'semgrep')
24
+ const workDir = mkdtempSync(path.join(os.tmpdir(), 'om-semgrep-rules-'))
25
+
26
+ try {
27
+ copyFileSync(path.join(semgrepDir, 'open-mercato.yml'), path.join(workDir, 'rules.yml'))
28
+ copyFileSync(path.join(semgrepDir, 'open-mercato.fixture.txt'), path.join(workDir, 'fixture.tsx'))
29
+ const output = execFileSync('docker', [
30
+ 'run', '--rm', '-v', `${workDir}:/src`, '-w', '/src', TOOLS.SEMGREP_IMAGE,
31
+ 'semgrep', 'scan', '--metrics=off', '--quiet', '--json', '--config', 'rules.yml', 'fixture.tsx',
32
+ ], { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 })
33
+ const result = JSON.parse(output)
34
+ const found = new Set(result.results.map((finding) => `${finding.start.line} ${finding.check_id.split('.').pop()}`))
35
+ const problems = []
36
+ readFileSync(path.join(workDir, 'fixture.tsx'), 'utf8').split('\n').forEach((line, index) => {
37
+ const annotation = line.match(/\b(ruleid|ok): (om-[a-z-]+)/)
38
+ if (!annotation) return
39
+ const key = `${index + 2} ${annotation[2]}`
40
+ const expected = annotation[1] === 'ruleid'
41
+ if (expected !== found.has(key)) problems.push(`${expected ? 'missed' : 'false positive'}: ${annotation[2]} at fixture line ${index + 2}`)
42
+ found.delete(key)
43
+ })
44
+ for (const extra of found) problems.push(`unannotated finding: ${extra}`)
45
+ for (const error of result.errors) problems.push(`semgrep error: ${error.message}`)
46
+ if (problems.length > 0) {
47
+ console.error(problems.join('\n'))
48
+ process.exitCode = 1
49
+ } else {
50
+ console.log(`verify-semgrep-rules: all annotations match (${result.results.length} expected findings).`)
51
+ }
52
+ } finally {
53
+ rmSync(workDir, { recursive: true, force: true })
54
+ }