guardrail-core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/dist/__tests__/autopilot.test.d.ts +7 -0
  2. package/dist/__tests__/autopilot.test.d.ts.map +1 -0
  3. package/dist/__tests__/autopilot.test.js +156 -0
  4. package/dist/__tests__/tier-config.test.d.ts +9 -0
  5. package/dist/__tests__/tier-config.test.d.ts.map +1 -0
  6. package/dist/__tests__/tier-config.test.js +230 -0
  7. package/dist/__tests__/utils/hash-inline.test.d.ts +2 -0
  8. package/dist/__tests__/utils/hash-inline.test.d.ts.map +1 -0
  9. package/dist/__tests__/utils/hash-inline.test.js +62 -0
  10. package/dist/__tests__/utils/hash.test.d.ts +3 -0
  11. package/dist/__tests__/utils/hash.test.d.ts.map +1 -0
  12. package/dist/__tests__/utils/hash.test.js +95 -0
  13. package/dist/__tests__/utils/simple.test.d.ts +1 -0
  14. package/dist/__tests__/utils/simple.test.d.ts.map +1 -0
  15. package/dist/__tests__/utils/simple.test.js +10 -0
  16. package/dist/__tests__/utils/utils-simple.test.d.ts +1 -0
  17. package/dist/__tests__/utils/utils-simple.test.d.ts.map +1 -0
  18. package/dist/__tests__/utils/utils-simple.test.js +6 -0
  19. package/dist/__tests__/utils/utils.test.d.ts +15 -0
  20. package/dist/__tests__/utils/utils.test.d.ts.map +1 -0
  21. package/dist/__tests__/utils/utils.test.js +172 -0
  22. package/dist/autopilot/autopilot-runner.d.ts +33 -0
  23. package/dist/autopilot/autopilot-runner.d.ts.map +1 -0
  24. package/dist/autopilot/autopilot-runner.js +479 -0
  25. package/dist/autopilot/index.d.ts +6 -0
  26. package/dist/autopilot/index.d.ts.map +1 -0
  27. package/dist/autopilot/index.js +25 -0
  28. package/dist/autopilot/types.d.ts +102 -0
  29. package/dist/autopilot/types.d.ts.map +1 -0
  30. package/dist/autopilot/types.js +18 -0
  31. package/dist/cache/index.d.ts +7 -0
  32. package/dist/cache/index.d.ts.map +1 -0
  33. package/dist/cache/index.js +22 -0
  34. package/dist/cache/redis-cache.d.ts +145 -0
  35. package/dist/cache/redis-cache.d.ts.map +1 -0
  36. package/dist/cache/redis-cache.js +459 -0
  37. package/dist/ci/github-actions.d.ts +77 -0
  38. package/dist/ci/github-actions.d.ts.map +1 -0
  39. package/dist/ci/github-actions.js +277 -0
  40. package/dist/ci/index.d.ts +12 -0
  41. package/dist/ci/index.d.ts.map +1 -0
  42. package/dist/ci/index.js +27 -0
  43. package/dist/ci/pre-commit.d.ts +65 -0
  44. package/dist/ci/pre-commit.d.ts.map +1 -0
  45. package/dist/ci/pre-commit.js +286 -0
  46. package/dist/entitlements.d.ts +149 -0
  47. package/dist/entitlements.d.ts.map +1 -0
  48. package/dist/entitlements.js +464 -0
  49. package/dist/env.d.ts +113 -0
  50. package/dist/env.d.ts.map +1 -0
  51. package/dist/env.js +204 -0
  52. package/dist/fix-packs/__tests__/generate-fix-packs.test.d.ts +7 -0
  53. package/dist/fix-packs/__tests__/generate-fix-packs.test.d.ts.map +1 -0
  54. package/dist/fix-packs/__tests__/generate-fix-packs.test.js +250 -0
  55. package/dist/fix-packs/generate-fix-packs.d.ts +15 -0
  56. package/dist/fix-packs/generate-fix-packs.d.ts.map +1 -0
  57. package/dist/fix-packs/generate-fix-packs.js +505 -0
  58. package/dist/fix-packs/index.d.ts +8 -0
  59. package/dist/fix-packs/index.d.ts.map +1 -0
  60. package/dist/fix-packs/index.js +23 -0
  61. package/dist/fix-packs/types.d.ts +113 -0
  62. package/dist/fix-packs/types.d.ts.map +1 -0
  63. package/dist/fix-packs/types.js +71 -0
  64. package/dist/index.d.ts +13 -0
  65. package/dist/index.d.ts.map +1 -0
  66. package/dist/index.js +28 -0
  67. package/dist/metrics/prometheus.d.ts +99 -0
  68. package/dist/metrics/prometheus.d.ts.map +1 -0
  69. package/dist/metrics/prometheus.js +306 -0
  70. package/dist/quota-ledger.d.ts +119 -0
  71. package/dist/quota-ledger.d.ts.map +1 -0
  72. package/dist/quota-ledger.js +462 -0
  73. package/dist/rbac/__tests__/permissions.test.d.ts +8 -0
  74. package/dist/rbac/__tests__/permissions.test.d.ts.map +1 -0
  75. package/dist/rbac/__tests__/permissions.test.js +350 -0
  76. package/dist/rbac/index.d.ts +9 -0
  77. package/dist/rbac/index.d.ts.map +1 -0
  78. package/dist/rbac/index.js +32 -0
  79. package/dist/rbac/permissions.d.ts +71 -0
  80. package/dist/rbac/permissions.d.ts.map +1 -0
  81. package/dist/rbac/permissions.js +247 -0
  82. package/dist/rbac/types.d.ts +69 -0
  83. package/dist/rbac/types.d.ts.map +1 -0
  84. package/dist/rbac/types.js +213 -0
  85. package/dist/tier-config.d.ts +203 -0
  86. package/dist/tier-config.d.ts.map +1 -0
  87. package/dist/tier-config.js +675 -0
  88. package/dist/types.d.ts +365 -0
  89. package/dist/types.d.ts.map +1 -0
  90. package/dist/types.js +5 -0
  91. package/dist/utils.d.ts +36 -0
  92. package/dist/utils.d.ts.map +1 -0
  93. package/dist/utils.js +127 -0
  94. package/dist/verified-autofix/__tests__/format-validator.test.d.ts +11 -0
  95. package/dist/verified-autofix/__tests__/format-validator.test.d.ts.map +1 -0
  96. package/dist/verified-autofix/__tests__/format-validator.test.js +285 -0
  97. package/dist/verified-autofix/__tests__/pipeline.test.d.ts +11 -0
  98. package/dist/verified-autofix/__tests__/pipeline.test.d.ts.map +1 -0
  99. package/dist/verified-autofix/__tests__/pipeline.test.js +389 -0
  100. package/dist/verified-autofix/__tests__/repo-fingerprint.test.d.ts +11 -0
  101. package/dist/verified-autofix/__tests__/repo-fingerprint.test.d.ts.map +1 -0
  102. package/dist/verified-autofix/__tests__/repo-fingerprint.test.js +236 -0
  103. package/dist/verified-autofix/__tests__/workspace.test.d.ts +11 -0
  104. package/dist/verified-autofix/__tests__/workspace.test.d.ts.map +1 -0
  105. package/dist/verified-autofix/__tests__/workspace.test.js +314 -0
  106. package/dist/verified-autofix/format-validator.d.ts +101 -0
  107. package/dist/verified-autofix/format-validator.d.ts.map +1 -0
  108. package/dist/verified-autofix/format-validator.js +446 -0
  109. package/dist/verified-autofix/index.d.ts +14 -0
  110. package/dist/verified-autofix/index.d.ts.map +1 -0
  111. package/dist/verified-autofix/index.js +39 -0
  112. package/dist/verified-autofix/pipeline.d.ts +68 -0
  113. package/dist/verified-autofix/pipeline.d.ts.map +1 -0
  114. package/dist/verified-autofix/pipeline.js +330 -0
  115. package/dist/verified-autofix/repo-fingerprint.d.ts +56 -0
  116. package/dist/verified-autofix/repo-fingerprint.d.ts.map +1 -0
  117. package/dist/verified-autofix/repo-fingerprint.js +396 -0
  118. package/dist/verified-autofix/workspace.d.ts +83 -0
  119. package/dist/verified-autofix/workspace.d.ts.map +1 -0
  120. package/dist/verified-autofix/workspace.js +454 -0
  121. package/dist/verified-autofix.d.ts +182 -0
  122. package/dist/verified-autofix.d.ts.map +1 -0
  123. package/dist/verified-autofix.js +1021 -0
  124. package/dist/visualization/dependency-graph.d.ts +79 -0
  125. package/dist/visualization/dependency-graph.d.ts.map +1 -0
  126. package/dist/visualization/dependency-graph.js +399 -0
  127. package/dist/visualization/index.d.ts +5 -0
  128. package/dist/visualization/index.d.ts.map +1 -0
  129. package/dist/visualization/index.js +20 -0
  130. package/package.json +29 -0
  131. package/src/__tests__/autopilot.test.ts +196 -0
  132. package/src/__tests__/tier-config.test.ts +289 -0
  133. package/src/__tests__/utils/hash-inline.test.ts +76 -0
  134. package/src/__tests__/utils/hash.test.ts +119 -0
  135. package/src/__tests__/utils/simple.test.ts +10 -0
  136. package/src/__tests__/utils/utils-simple.test.ts +5 -0
  137. package/src/__tests__/utils/utils.test.ts +203 -0
  138. package/src/autopilot/autopilot-runner.ts +503 -0
  139. package/src/autopilot/index.ts +6 -0
  140. package/src/autopilot/types.ts +119 -0
  141. package/src/cache/index.ts +7 -0
  142. package/src/cache/redis-cache.d.ts +155 -0
  143. package/src/cache/redis-cache.d.ts.map +1 -0
  144. package/src/cache/redis-cache.ts +517 -0
  145. package/src/ci/github-actions.ts +335 -0
  146. package/src/ci/index.ts +12 -0
  147. package/src/ci/pre-commit.ts +338 -0
  148. package/src/db/usage-schema.prisma +114 -0
  149. package/src/entitlements.ts +570 -0
  150. package/src/env.d.ts +68 -0
  151. package/src/env.d.ts.map +1 -0
  152. package/src/env.ts +247 -0
  153. package/src/fix-packs/__tests__/generate-fix-packs.test.ts +317 -0
  154. package/src/fix-packs/generate-fix-packs.ts +577 -0
  155. package/src/fix-packs/index.ts +8 -0
  156. package/src/fix-packs/types.ts +206 -0
  157. package/src/index.d.ts +7 -0
  158. package/src/index.d.ts.map +1 -0
  159. package/src/index.ts +12 -0
  160. package/src/metrics/prometheus.d.ts +104 -0
  161. package/src/metrics/prometheus.d.ts.map +1 -0
  162. package/src/metrics/prometheus.ts +446 -0
  163. package/src/quota-ledger.ts +548 -0
  164. package/src/rbac/__tests__/permissions.test.ts +446 -0
  165. package/src/rbac/index.ts +46 -0
  166. package/src/rbac/permissions.ts +301 -0
  167. package/src/rbac/types.ts +298 -0
  168. package/src/tier-config.json +157 -0
  169. package/src/tier-config.ts +815 -0
  170. package/src/types.d.ts +365 -0
  171. package/src/types.d.ts.map +1 -0
  172. package/src/types.ts +441 -0
  173. package/src/utils.d.ts +36 -0
  174. package/src/utils.d.ts.map +1 -0
  175. package/src/utils.ts +140 -0
  176. package/src/verified-autofix/__tests__/format-validator.test.ts +335 -0
  177. package/src/verified-autofix/__tests__/pipeline.test.ts +419 -0
  178. package/src/verified-autofix/__tests__/repo-fingerprint.test.ts +241 -0
  179. package/src/verified-autofix/__tests__/workspace.test.ts +373 -0
  180. package/src/verified-autofix/format-validator.ts +517 -0
  181. package/src/verified-autofix/index.ts +63 -0
  182. package/src/verified-autofix/pipeline.ts +403 -0
  183. package/src/verified-autofix/repo-fingerprint.ts +459 -0
  184. package/src/verified-autofix/workspace.ts +531 -0
  185. package/src/verified-autofix.ts +1187 -0
  186. package/src/visualization/dependency-graph.d.ts +85 -0
  187. package/src/visualization/dependency-graph.d.ts.map +1 -0
  188. package/src/visualization/dependency-graph.ts +495 -0
  189. package/src/visualization/index.ts +5 -0
@@ -0,0 +1,277 @@
1
+ "use strict";
2
+ /**
3
+ * GitHub Actions Integration
4
+ *
5
+ * Provides integration with GitHub Actions for automated security scanning
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.githubActionsGenerator = exports.GitHubActionsGenerator = void 0;
9
+ class GitHubActionsGenerator {
10
+ /**
11
+ * Generate a complete GitHub Actions workflow
12
+ */
13
+ generateWorkflow(config) {
14
+ const workflow = {
15
+ name: config.workflowName,
16
+ on: this.buildTriggers(config.triggers),
17
+ permissions: {
18
+ contents: 'read',
19
+ 'security-events': 'write',
20
+ 'pull-requests': config.createPRComments ? 'write' : 'read',
21
+ },
22
+ jobs: {
23
+ 'Guardrail-scan': this.buildScanJob(config),
24
+ },
25
+ };
26
+ return this.toYAML(workflow);
27
+ }
28
+ /**
29
+ * Build workflow triggers
30
+ */
31
+ buildTriggers(triggers) {
32
+ const on = {};
33
+ if (triggers.push) {
34
+ on['push'] = { branches: triggers.push.branches };
35
+ }
36
+ if (triggers.pullRequest) {
37
+ on['pull_request'] = { branches: triggers.pullRequest.branches };
38
+ }
39
+ if (triggers.schedule) {
40
+ on['schedule'] = triggers.schedule.map(s => ({ cron: s.cron }));
41
+ }
42
+ if (triggers.workflowDispatch) {
43
+ on['workflow_dispatch'] = {};
44
+ }
45
+ return on;
46
+ }
47
+ /**
48
+ * Build the main scan job
49
+ */
50
+ buildScanJob(config) {
51
+ const steps = [
52
+ {
53
+ name: 'Checkout code',
54
+ uses: 'actions/checkout@v4',
55
+ with: { 'fetch-depth': 0 },
56
+ },
57
+ {
58
+ name: 'Setup Node.js',
59
+ uses: 'actions/setup-node@v4',
60
+ with: { 'node-version': '20' },
61
+ },
62
+ {
63
+ name: 'Install dependencies',
64
+ run: 'npm ci',
65
+ },
66
+ ];
67
+ // Add scan steps based on config
68
+ if (config.scanTypes.includes('secrets')) {
69
+ steps.push({
70
+ name: 'Scan for secrets',
71
+ run: 'npx Guardrail scan:secrets --format sarif --output secrets-results.sarif',
72
+ env: { Guardrail_CI: 'true' },
73
+ });
74
+ }
75
+ if (config.scanTypes.includes('vulnerabilities')) {
76
+ steps.push({
77
+ name: 'Scan for vulnerabilities',
78
+ run: 'npx Guardrail scan:vulnerabilities --format sarif --output vuln-results.sarif',
79
+ env: { Guardrail_CI: 'true' },
80
+ });
81
+ }
82
+ if (config.scanTypes.includes('security')) {
83
+ steps.push({
84
+ name: 'Security scan',
85
+ run: 'npx Guardrail scan:security --format sarif --output security-results.sarif',
86
+ env: { Guardrail_CI: 'true' },
87
+ });
88
+ }
89
+ if (config.scanTypes.includes('compliance')) {
90
+ steps.push({
91
+ name: 'Compliance check',
92
+ run: 'npx Guardrail scan:compliance --format json --output compliance-results.json',
93
+ env: { Guardrail_CI: 'true' },
94
+ });
95
+ }
96
+ if (config.scanTypes.includes('sbom')) {
97
+ steps.push({
98
+ name: 'Generate SBOM',
99
+ run: 'npx Guardrail sbom:generate --format cyclonedx --output sbom.json',
100
+ env: { Guardrail_CI: 'true' },
101
+ });
102
+ }
103
+ // Upload SARIF results
104
+ steps.push({
105
+ name: 'Upload SARIF results',
106
+ uses: 'github/codeql-action/upload-sarif@v3',
107
+ with: {
108
+ 'sarif_file': '.',
109
+ 'category': 'Guardrail-security',
110
+ },
111
+ if: 'always()',
112
+ });
113
+ // Upload artifacts
114
+ if (config.uploadArtifacts) {
115
+ steps.push({
116
+ name: 'Upload scan artifacts',
117
+ uses: 'actions/upload-artifact@v4',
118
+ with: {
119
+ name: 'Guardrail-results',
120
+ path: '*.sarif\n*.json',
121
+ 'retention-days': 30,
122
+ },
123
+ if: 'always()',
124
+ });
125
+ }
126
+ // Check results and fail if needed
127
+ steps.push({
128
+ name: 'Check scan results',
129
+ run: this.buildCheckScript(config),
130
+ env: {
131
+ FAIL_ON_CRITICAL: String(config.failOnCritical),
132
+ FAIL_ON_HIGH: String(config.failOnHigh),
133
+ },
134
+ });
135
+ return {
136
+ name: 'Guardrail Security Scan',
137
+ runsOn: 'ubuntu-latest',
138
+ steps,
139
+ };
140
+ }
141
+ /**
142
+ * Build result check script
143
+ */
144
+ buildCheckScript(config) {
145
+ return `
146
+ npx Guardrail results:check \\
147
+ --fail-on-critical=${config.failOnCritical} \\
148
+ --fail-on-high=${config.failOnHigh}
149
+ `.trim();
150
+ }
151
+ /**
152
+ * Convert workflow object to YAML
153
+ */
154
+ toYAML(workflow) {
155
+ const lines = [];
156
+ lines.push(`name: ${workflow.name}`);
157
+ lines.push('');
158
+ lines.push('on:');
159
+ lines.push(this.objectToYAML(workflow.on, 2));
160
+ lines.push('');
161
+ if (workflow.permissions) {
162
+ lines.push('permissions:');
163
+ for (const [key, value] of Object.entries(workflow.permissions)) {
164
+ lines.push(` ${key}: ${value}`);
165
+ }
166
+ lines.push('');
167
+ }
168
+ lines.push('jobs:');
169
+ for (const [jobId, job] of Object.entries(workflow.jobs)) {
170
+ lines.push(` ${jobId}:`);
171
+ lines.push(` name: ${job.name}`);
172
+ lines.push(` runs-on: ${job.runsOn}`);
173
+ lines.push(' steps:');
174
+ for (const step of job.steps) {
175
+ lines.push(` - name: ${step.name}`);
176
+ if (step.uses) {
177
+ lines.push(` uses: ${step.uses}`);
178
+ }
179
+ if (step.run) {
180
+ if (step.run.includes('\n')) {
181
+ lines.push(' run: |');
182
+ for (const line of step.run.split('\n')) {
183
+ lines.push(` ${line}`);
184
+ }
185
+ }
186
+ else {
187
+ lines.push(` run: ${step.run}`);
188
+ }
189
+ }
190
+ if (step.with) {
191
+ lines.push(' with:');
192
+ for (const [key, value] of Object.entries(step.with)) {
193
+ if (typeof value === 'string' && value.includes('\n')) {
194
+ lines.push(` ${key}: |`);
195
+ for (const line of value.split('\n')) {
196
+ lines.push(` ${line}`);
197
+ }
198
+ }
199
+ else {
200
+ lines.push(` ${key}: ${value}`);
201
+ }
202
+ }
203
+ }
204
+ if (step.env) {
205
+ lines.push(' env:');
206
+ for (const [key, value] of Object.entries(step.env)) {
207
+ lines.push(` ${key}: ${value}`);
208
+ }
209
+ }
210
+ if (step.if) {
211
+ lines.push(` if: ${step.if}`);
212
+ }
213
+ }
214
+ }
215
+ return lines.join('\n');
216
+ }
217
+ /**
218
+ * Convert object to YAML with indentation
219
+ */
220
+ objectToYAML(obj, indent) {
221
+ const lines = [];
222
+ const prefix = ' '.repeat(indent);
223
+ for (const [key, value] of Object.entries(obj)) {
224
+ if (typeof value === 'object' && !Array.isArray(value)) {
225
+ lines.push(`${prefix}${key}:`);
226
+ lines.push(this.objectToYAML(value, indent + 2));
227
+ }
228
+ else if (Array.isArray(value)) {
229
+ lines.push(`${prefix}${key}:`);
230
+ for (const item of value) {
231
+ if (typeof item === 'object') {
232
+ const entries = Object.entries(item);
233
+ if (entries.length > 0) {
234
+ const firstEntry = entries[0];
235
+ if (!firstEntry)
236
+ continue;
237
+ const [firstKey, firstValue] = firstEntry;
238
+ lines.push(`${prefix} - ${firstKey}: ${firstValue}`);
239
+ for (const [k, v] of entries.slice(1)) {
240
+ lines.push(`${prefix} ${k}: ${v}`);
241
+ }
242
+ }
243
+ }
244
+ else {
245
+ lines.push(`${prefix} - ${item}`);
246
+ }
247
+ }
248
+ }
249
+ else {
250
+ lines.push(`${prefix}${key}: ${value}`);
251
+ }
252
+ }
253
+ return lines.join('\n');
254
+ }
255
+ /**
256
+ * Generate default workflow for quick setup
257
+ */
258
+ generateDefaultWorkflow() {
259
+ return this.generateWorkflow({
260
+ workflowName: 'Guardrail Security Scan',
261
+ triggers: {
262
+ push: { branches: ['main', 'master'] },
263
+ pullRequest: { branches: ['main', 'master'] },
264
+ schedule: [{ cron: '0 0 * * 0' }], // Weekly on Sunday
265
+ workflowDispatch: true,
266
+ },
267
+ scanTypes: ['security', 'secrets', 'vulnerabilities', 'sbom'],
268
+ failOnCritical: true,
269
+ failOnHigh: false,
270
+ uploadArtifacts: true,
271
+ createPRComments: true,
272
+ });
273
+ }
274
+ }
275
+ exports.GitHubActionsGenerator = GitHubActionsGenerator;
276
+ // Export singleton
277
+ exports.githubActionsGenerator = new GitHubActionsGenerator();
@@ -0,0 +1,12 @@
1
+ /**
2
+ * CI/CD Integration Module
3
+ *
4
+ * Provides integrations for:
5
+ * - GitHub Actions
6
+ * - GitLab CI (future)
7
+ * - Jenkins (future)
8
+ * - Azure DevOps (future)
9
+ */
10
+ export * from './github-actions';
11
+ export * from './pre-commit';
12
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/ci/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC"}
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ /**
3
+ * CI/CD Integration Module
4
+ *
5
+ * Provides integrations for:
6
+ * - GitHub Actions
7
+ * - GitLab CI (future)
8
+ * - Jenkins (future)
9
+ * - Azure DevOps (future)
10
+ */
11
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
12
+ if (k2 === undefined) k2 = k;
13
+ var desc = Object.getOwnPropertyDescriptor(m, k);
14
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
15
+ desc = { enumerable: true, get: function() { return m[k]; } };
16
+ }
17
+ Object.defineProperty(o, k2, desc);
18
+ }) : (function(o, m, k, k2) {
19
+ if (k2 === undefined) k2 = k;
20
+ o[k2] = m[k];
21
+ }));
22
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
23
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ __exportStar(require("./github-actions"), exports);
27
+ __exportStar(require("./pre-commit"), exports);
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Pre-commit Hooks Integration
3
+ *
4
+ * Generates pre-commit hook configurations for local validation
5
+ * before commits are pushed to the repository
6
+ */
7
+ export interface PreCommitConfig {
8
+ scanSecrets: boolean;
9
+ scanVulnerabilities: boolean;
10
+ checkCompliance: boolean;
11
+ validateTypes: boolean;
12
+ runLint: boolean;
13
+ runTests: boolean;
14
+ blockOnCritical: boolean;
15
+ blockOnHigh: boolean;
16
+ maxFileSize: number;
17
+ excludePatterns: string[];
18
+ }
19
+ export interface HuskyConfig {
20
+ hooks: {
21
+ 'pre-commit'?: string;
22
+ 'pre-push'?: string;
23
+ 'commit-msg'?: string;
24
+ };
25
+ }
26
+ export declare class PreCommitGenerator {
27
+ /**
28
+ * Generate Husky pre-commit configuration
29
+ */
30
+ generateHuskyConfig(config: PreCommitConfig): HuskyConfig;
31
+ /**
32
+ * Generate pre-push commands
33
+ */
34
+ private generatePrePushCommands;
35
+ /**
36
+ * Generate .husky/pre-commit script
37
+ */
38
+ generatePreCommitScript(config: PreCommitConfig): string;
39
+ /**
40
+ * Generate .husky/pre-push script
41
+ */
42
+ generatePrePushScript(config: PreCommitConfig): string;
43
+ /**
44
+ * Generate lint-staged configuration
45
+ */
46
+ generateLintStagedConfig(config: PreCommitConfig): Record<string, string[]>;
47
+ /**
48
+ * Generate package.json scripts for hooks
49
+ */
50
+ generatePackageJsonScripts(): Record<string, string>;
51
+ /**
52
+ * Generate commitlint configuration
53
+ */
54
+ generateCommitlintConfig(): Record<string, any>;
55
+ /**
56
+ * Generate default configuration
57
+ */
58
+ generateDefaultConfig(): PreCommitConfig;
59
+ /**
60
+ * Generate setup instructions
61
+ */
62
+ generateSetupInstructions(): string;
63
+ }
64
+ export declare const preCommitGenerator: PreCommitGenerator;
65
+ //# sourceMappingURL=pre-commit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pre-commit.d.ts","sourceRoot":"","sources":["../../src/ci/pre-commit.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,OAAO,CAAC;IACrB,mBAAmB,EAAE,OAAO,CAAC;IAC7B,eAAe,EAAE,OAAO,CAAC;IACzB,aAAa,EAAE,OAAO,CAAC;IACvB,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,OAAO,CAAC;IAClB,eAAe,EAAE,OAAO,CAAC;IACzB,WAAW,EAAE,OAAO,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,EAAE,MAAM,EAAE,CAAC;CAC3B;AAED,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE;QACL,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB,CAAC;CACH;AAED,qBAAa,kBAAkB;IAC7B;;OAEG;IACH,mBAAmB,CAAC,MAAM,EAAE,eAAe,GAAG,WAAW;IAwBzD;;OAEG;IACH,OAAO,CAAC,uBAAuB;IAkB/B;;OAEG;IACH,uBAAuB,CAAC,MAAM,EAAE,eAAe,GAAG,MAAM;IAoDxD;;OAEG;IACH,qBAAqB,CAAC,MAAM,EAAE,eAAe,GAAG,MAAM;IA4CtD;;OAEG;IACH,wBAAwB,CAAC,MAAM,EAAE,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC;IAyB3E;;OAEG;IACH,0BAA0B,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAWpD;;OAEG;IACH,wBAAwB,IAAI,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IA6B/C;;OAEG;IACH,qBAAqB,IAAI,eAAe;IAqBxC;;OAEG;IACH,yBAAyB,IAAI,MAAM;CAsDpC;AAGD,eAAO,MAAM,kBAAkB,oBAA2B,CAAC"}
@@ -0,0 +1,286 @@
1
+ "use strict";
2
+ /**
3
+ * Pre-commit Hooks Integration
4
+ *
5
+ * Generates pre-commit hook configurations for local validation
6
+ * before commits are pushed to the repository
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.preCommitGenerator = exports.PreCommitGenerator = void 0;
10
+ class PreCommitGenerator {
11
+ /**
12
+ * Generate Husky pre-commit configuration
13
+ */
14
+ generateHuskyConfig(config) {
15
+ const commands = [];
16
+ if (config.runLint) {
17
+ commands.push('npx lint-staged');
18
+ }
19
+ if (config.scanSecrets) {
20
+ commands.push('npx Guardrail scan:secrets --staged --fail-on-detection');
21
+ }
22
+ if (config.validateTypes) {
23
+ commands.push('npx tsc --noEmit');
24
+ }
25
+ return {
26
+ hooks: {
27
+ 'pre-commit': commands.join(' && '),
28
+ 'pre-push': this.generatePrePushCommands(config),
29
+ 'commit-msg': 'npx commitlint --edit $1',
30
+ },
31
+ };
32
+ }
33
+ /**
34
+ * Generate pre-push commands
35
+ */
36
+ generatePrePushCommands(config) {
37
+ const commands = [];
38
+ if (config.scanVulnerabilities) {
39
+ commands.push('npx Guardrail scan:vulnerabilities');
40
+ }
41
+ if (config.checkCompliance) {
42
+ commands.push('npx Guardrail scan:compliance');
43
+ }
44
+ if (config.runTests) {
45
+ commands.push('npm test');
46
+ }
47
+ return commands.join(' && ');
48
+ }
49
+ /**
50
+ * Generate .husky/pre-commit script
51
+ */
52
+ generatePreCommitScript(config) {
53
+ const lines = [
54
+ '#!/usr/bin/env sh',
55
+ '. "$(dirname -- "$0")/_/husky.sh"',
56
+ '',
57
+ '# Guardrail Pre-commit Hook',
58
+ '# Generated by Guardrail AI',
59
+ '',
60
+ ];
61
+ if (config.runLint) {
62
+ lines.push('echo "Running lint-staged..."');
63
+ lines.push('npx lint-staged || exit 1');
64
+ lines.push('');
65
+ }
66
+ if (config.scanSecrets) {
67
+ lines.push('echo "Scanning for secrets..."');
68
+ lines.push('npx Guardrail scan:secrets --staged --fail-on-detection || {');
69
+ lines.push(' echo "ERROR: Secrets detected in staged files!"');
70
+ lines.push(' echo "Please remove secrets before committing."');
71
+ lines.push(' exit 1');
72
+ lines.push('}');
73
+ lines.push('');
74
+ }
75
+ if (config.validateTypes) {
76
+ lines.push('echo "Checking TypeScript types..."');
77
+ lines.push('npx tsc --noEmit || exit 1');
78
+ lines.push('');
79
+ }
80
+ if (config.maxFileSize > 0) {
81
+ lines.push(`echo "Checking file sizes (max ${config.maxFileSize}KB)..."`);
82
+ lines.push(`MAX_SIZE=${config.maxFileSize * 1024}`);
83
+ lines.push('for file in $(git diff --cached --name-only); do');
84
+ lines.push(' if [ -f "$file" ]; then');
85
+ lines.push(' size=$(wc -c < "$file")');
86
+ lines.push(' if [ $size -gt $MAX_SIZE ]; then');
87
+ lines.push(' echo "ERROR: $file exceeds maximum file size"');
88
+ lines.push(' exit 1');
89
+ lines.push(' fi');
90
+ lines.push(' fi');
91
+ lines.push('done');
92
+ lines.push('');
93
+ }
94
+ lines.push('echo "Pre-commit checks passed!"');
95
+ return lines.join('\n');
96
+ }
97
+ /**
98
+ * Generate .husky/pre-push script
99
+ */
100
+ generatePrePushScript(config) {
101
+ const lines = [
102
+ '#!/usr/bin/env sh',
103
+ '. "$(dirname -- "$0")/_/husky.sh"',
104
+ '',
105
+ '# Guardrail Pre-push Hook',
106
+ '# Generated by Guardrail AI',
107
+ '',
108
+ ];
109
+ if (config.scanVulnerabilities) {
110
+ lines.push('echo "Scanning for vulnerabilities..."');
111
+ const failCondition = config.blockOnCritical
112
+ ? (config.blockOnHigh ? '--fail-on-high' : '--fail-on-critical')
113
+ : '';
114
+ lines.push(`npx Guardrail scan:vulnerabilities ${failCondition} || {`);
115
+ lines.push(' echo "ERROR: Vulnerabilities detected!"');
116
+ lines.push(' exit 1');
117
+ lines.push('}');
118
+ lines.push('');
119
+ }
120
+ if (config.checkCompliance) {
121
+ lines.push('echo "Checking compliance..."');
122
+ lines.push('npx Guardrail scan:compliance || {');
123
+ lines.push(' echo "WARNING: Compliance issues detected"');
124
+ lines.push('}');
125
+ lines.push('');
126
+ }
127
+ if (config.runTests) {
128
+ lines.push('echo "Running tests..."');
129
+ lines.push('npm test || {');
130
+ lines.push(' echo "ERROR: Tests failed!"');
131
+ lines.push(' exit 1');
132
+ lines.push('}');
133
+ lines.push('');
134
+ }
135
+ lines.push('echo "Pre-push checks passed!"');
136
+ return lines.join('\n');
137
+ }
138
+ /**
139
+ * Generate lint-staged configuration
140
+ */
141
+ generateLintStagedConfig(config) {
142
+ const lintStaged = {};
143
+ // TypeScript/JavaScript files
144
+ lintStaged['*.{ts,tsx,js,jsx}'] = [
145
+ 'eslint --fix',
146
+ 'prettier --write',
147
+ ];
148
+ if (config.scanSecrets) {
149
+ lintStaged['*.{ts,tsx,js,jsx}'].push('Guardrail scan:secrets --file');
150
+ }
151
+ // JSON files
152
+ lintStaged['*.json'] = ['prettier --write'];
153
+ // Markdown files
154
+ lintStaged['*.md'] = ['prettier --write'];
155
+ // CSS/SCSS files
156
+ lintStaged['*.{css,scss}'] = ['prettier --write'];
157
+ return lintStaged;
158
+ }
159
+ /**
160
+ * Generate package.json scripts for hooks
161
+ */
162
+ generatePackageJsonScripts() {
163
+ return {
164
+ 'prepare': 'husky install',
165
+ 'pre-commit': 'lint-staged',
166
+ 'Guardrail:secrets': 'Guardrail scan:secrets',
167
+ 'Guardrail:vulnerabilities': 'Guardrail scan:vulnerabilities',
168
+ 'Guardrail:compliance': 'Guardrail scan:compliance',
169
+ 'Guardrail:full': 'Guardrail scan:all',
170
+ };
171
+ }
172
+ /**
173
+ * Generate commitlint configuration
174
+ */
175
+ generateCommitlintConfig() {
176
+ return {
177
+ extends: ['@commitlint/config-conventional'],
178
+ rules: {
179
+ 'type-enum': [
180
+ 2,
181
+ 'always',
182
+ [
183
+ 'feat',
184
+ 'fix',
185
+ 'docs',
186
+ 'style',
187
+ 'refactor',
188
+ 'perf',
189
+ 'test',
190
+ 'build',
191
+ 'ci',
192
+ 'chore',
193
+ 'revert',
194
+ 'security',
195
+ ],
196
+ ],
197
+ 'subject-case': [2, 'always', 'lower-case'],
198
+ 'subject-max-length': [2, 'always', 72],
199
+ 'body-max-line-length': [2, 'always', 100],
200
+ },
201
+ };
202
+ }
203
+ /**
204
+ * Generate default configuration
205
+ */
206
+ generateDefaultConfig() {
207
+ return {
208
+ scanSecrets: true,
209
+ scanVulnerabilities: true,
210
+ checkCompliance: false,
211
+ validateTypes: true,
212
+ runLint: true,
213
+ runTests: false,
214
+ blockOnCritical: true,
215
+ blockOnHigh: false,
216
+ maxFileSize: 500, // 500KB
217
+ excludePatterns: [
218
+ 'node_modules/**',
219
+ 'dist/**',
220
+ 'build/**',
221
+ '*.min.js',
222
+ '*.bundle.js',
223
+ ],
224
+ };
225
+ }
226
+ /**
227
+ * Generate setup instructions
228
+ */
229
+ generateSetupInstructions() {
230
+ return `
231
+ # Guardrail Pre-commit Hooks Setup
232
+
233
+ ## Installation
234
+
235
+ 1. Install dependencies:
236
+ \`\`\`bash
237
+ npm install -D husky lint-staged @commitlint/cli @commitlint/config-conventional
238
+ \`\`\`
239
+
240
+ 2. Initialize Husky:
241
+ \`\`\`bash
242
+ npx husky install
243
+ \`\`\`
244
+
245
+ 3. Add prepare script to package.json:
246
+ \`\`\`json
247
+ {
248
+ "scripts": {
249
+ "prepare": "husky install"
250
+ }
251
+ }
252
+ \`\`\`
253
+
254
+ 4. Create pre-commit hook:
255
+ \`\`\`bash
256
+ npx husky add .husky/pre-commit "npx lint-staged"
257
+ \`\`\`
258
+
259
+ 5. Create pre-push hook:
260
+ \`\`\`bash
261
+ npx husky add .husky/pre-push "npx Guardrail scan:vulnerabilities"
262
+ \`\`\`
263
+
264
+ ## Configuration
265
+
266
+ Add lint-staged config to package.json:
267
+ \`\`\`json
268
+ {
269
+ "lint-staged": {
270
+ "*.{ts,tsx,js,jsx}": ["eslint --fix", "prettier --write"]
271
+ }
272
+ }
273
+ \`\`\`
274
+
275
+ ## Usage
276
+
277
+ Hooks will run automatically on:
278
+ - **Pre-commit**: Lint, format, and secret scanning
279
+ - **Pre-push**: Vulnerability and compliance scanning
280
+ - **Commit-msg**: Conventional commit validation
281
+ `.trim();
282
+ }
283
+ }
284
+ exports.PreCommitGenerator = PreCommitGenerator;
285
+ // Export singleton
286
+ exports.preCommitGenerator = new PreCommitGenerator();