@sparkleideas/shared 3.0.0-alpha.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +323 -0
- package/__tests__/hooks/bash-safety.test.ts +289 -0
- package/__tests__/hooks/file-organization.test.ts +335 -0
- package/__tests__/hooks/git-commit.test.ts +336 -0
- package/__tests__/hooks/index.ts +23 -0
- package/__tests__/hooks/session-hooks.test.ts +357 -0
- package/__tests__/hooks/task-hooks.test.ts +193 -0
- package/docs/EVENTS_IMPLEMENTATION_SUMMARY.md +388 -0
- package/docs/EVENTS_QUICK_REFERENCE.md +470 -0
- package/docs/EVENTS_README.md +352 -0
- package/package.json +39 -0
- package/src/core/config/defaults.ts +207 -0
- package/src/core/config/index.ts +15 -0
- package/src/core/config/loader.ts +271 -0
- package/src/core/config/schema.ts +188 -0
- package/src/core/config/validator.ts +209 -0
- package/src/core/event-bus.ts +236 -0
- package/src/core/index.ts +22 -0
- package/src/core/interfaces/agent.interface.ts +251 -0
- package/src/core/interfaces/coordinator.interface.ts +363 -0
- package/src/core/interfaces/event.interface.ts +267 -0
- package/src/core/interfaces/index.ts +19 -0
- package/src/core/interfaces/memory.interface.ts +332 -0
- package/src/core/interfaces/task.interface.ts +223 -0
- package/src/core/orchestrator/event-coordinator.ts +122 -0
- package/src/core/orchestrator/health-monitor.ts +214 -0
- package/src/core/orchestrator/index.ts +89 -0
- package/src/core/orchestrator/lifecycle-manager.ts +263 -0
- package/src/core/orchestrator/session-manager.ts +279 -0
- package/src/core/orchestrator/task-manager.ts +317 -0
- package/src/events/domain-events.ts +584 -0
- package/src/events/event-store.test.ts +387 -0
- package/src/events/event-store.ts +588 -0
- package/src/events/example-usage.ts +293 -0
- package/src/events/index.ts +90 -0
- package/src/events/projections.ts +561 -0
- package/src/events/state-reconstructor.ts +349 -0
- package/src/events.ts +367 -0
- package/src/hooks/INTEGRATION.md +658 -0
- package/src/hooks/README.md +532 -0
- package/src/hooks/example-usage.ts +499 -0
- package/src/hooks/executor.ts +379 -0
- package/src/hooks/hooks.test.ts +421 -0
- package/src/hooks/index.ts +131 -0
- package/src/hooks/registry.ts +333 -0
- package/src/hooks/safety/bash-safety.ts +604 -0
- package/src/hooks/safety/file-organization.ts +473 -0
- package/src/hooks/safety/git-commit.ts +623 -0
- package/src/hooks/safety/index.ts +46 -0
- package/src/hooks/session-hooks.ts +559 -0
- package/src/hooks/task-hooks.ts +513 -0
- package/src/hooks/types.ts +357 -0
- package/src/hooks/verify-exports.test.ts +125 -0
- package/src/index.ts +195 -0
- package/src/mcp/connection-pool.ts +438 -0
- package/src/mcp/index.ts +183 -0
- package/src/mcp/server.ts +774 -0
- package/src/mcp/session-manager.ts +428 -0
- package/src/mcp/tool-registry.ts +566 -0
- package/src/mcp/transport/http.ts +557 -0
- package/src/mcp/transport/index.ts +294 -0
- package/src/mcp/transport/stdio.ts +324 -0
- package/src/mcp/transport/websocket.ts +484 -0
- package/src/mcp/types.ts +565 -0
- package/src/plugin-interface.ts +663 -0
- package/src/plugin-loader.ts +638 -0
- package/src/plugin-registry.ts +604 -0
- package/src/plugins/index.ts +34 -0
- package/src/plugins/official/hive-mind-plugin.ts +330 -0
- package/src/plugins/official/index.ts +24 -0
- package/src/plugins/official/maestro-plugin.ts +508 -0
- package/src/plugins/types.ts +108 -0
- package/src/resilience/bulkhead.ts +277 -0
- package/src/resilience/circuit-breaker.ts +326 -0
- package/src/resilience/index.ts +26 -0
- package/src/resilience/rate-limiter.ts +420 -0
- package/src/resilience/retry.ts +224 -0
- package/src/security/index.ts +39 -0
- package/src/security/input-validation.ts +265 -0
- package/src/security/secure-random.ts +159 -0
- package/src/services/index.ts +16 -0
- package/src/services/v3-progress.service.ts +505 -0
- package/src/types/agent.types.ts +144 -0
- package/src/types/index.ts +22 -0
- package/src/types/mcp.types.ts +300 -0
- package/src/types/memory.types.ts +263 -0
- package/src/types/swarm.types.ts +255 -0
- package/src/types/task.types.ts +205 -0
- package/src/types.ts +367 -0
- package/src/utils/secure-logger.d.ts +69 -0
- package/src/utils/secure-logger.d.ts.map +1 -0
- package/src/utils/secure-logger.js +208 -0
- package/src/utils/secure-logger.js.map +1 -0
- package/src/utils/secure-logger.ts +257 -0
- package/tmp.json +0 -0
- package/tsconfig.json +9 -0
|
@@ -0,0 +1,623 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V3 Git Commit Hook
|
|
3
|
+
*
|
|
4
|
+
* TypeScript conversion of V2 git-commit-hook.sh.
|
|
5
|
+
* Provides conventional commit formatting, JIRA ticket extraction,
|
|
6
|
+
* co-author addition, and commit message validation.
|
|
7
|
+
*
|
|
8
|
+
* @module v3/shared/hooks/safety/git-commit
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
HookEvent,
|
|
13
|
+
HookContext,
|
|
14
|
+
HookResult,
|
|
15
|
+
HookPriority,
|
|
16
|
+
} from '../types.js';
|
|
17
|
+
import { HookRegistry } from '../registry.js';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Git commit hook result
|
|
21
|
+
*/
|
|
22
|
+
export interface GitCommitResult extends HookResult {
|
|
23
|
+
/** Original commit message */
|
|
24
|
+
originalMessage: string;
|
|
25
|
+
/** Modified commit message */
|
|
26
|
+
modifiedMessage: string;
|
|
27
|
+
/** Detected commit type */
|
|
28
|
+
commitType?: CommitType;
|
|
29
|
+
/** Extracted ticket reference */
|
|
30
|
+
ticketReference?: string;
|
|
31
|
+
/** Whether co-author was added */
|
|
32
|
+
coAuthorAdded: boolean;
|
|
33
|
+
/** Validation issues */
|
|
34
|
+
validationIssues?: CommitValidationIssue[];
|
|
35
|
+
/** Suggestions for improvement */
|
|
36
|
+
suggestions?: string[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Commit type definition
|
|
41
|
+
*/
|
|
42
|
+
export type CommitType =
|
|
43
|
+
| 'feat'
|
|
44
|
+
| 'fix'
|
|
45
|
+
| 'docs'
|
|
46
|
+
| 'style'
|
|
47
|
+
| 'refactor'
|
|
48
|
+
| 'perf'
|
|
49
|
+
| 'test'
|
|
50
|
+
| 'build'
|
|
51
|
+
| 'ci'
|
|
52
|
+
| 'chore'
|
|
53
|
+
| 'revert';
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Commit validation issue
|
|
57
|
+
*/
|
|
58
|
+
export interface CommitValidationIssue {
|
|
59
|
+
/** Issue type */
|
|
60
|
+
type: 'format' | 'length' | 'scope' | 'body' | 'breaking';
|
|
61
|
+
/** Issue severity */
|
|
62
|
+
severity: 'info' | 'warning' | 'error';
|
|
63
|
+
/** Issue description */
|
|
64
|
+
description: string;
|
|
65
|
+
/** Suggested fix */
|
|
66
|
+
suggestedFix?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Conventional commit patterns
|
|
71
|
+
*/
|
|
72
|
+
interface CommitTypePattern {
|
|
73
|
+
/** Keywords that indicate this commit type */
|
|
74
|
+
keywords: string[];
|
|
75
|
+
/** Commit type */
|
|
76
|
+
type: CommitType;
|
|
77
|
+
/** Description */
|
|
78
|
+
description: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const COMMIT_TYPE_PATTERNS: CommitTypePattern[] = [
|
|
82
|
+
{
|
|
83
|
+
keywords: ['add', 'implement', 'create', 'introduce', 'new'],
|
|
84
|
+
type: 'feat',
|
|
85
|
+
description: 'A new feature',
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
keywords: ['fix', 'resolve', 'repair', 'patch', 'correct', 'bug'],
|
|
89
|
+
type: 'fix',
|
|
90
|
+
description: 'A bug fix',
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
keywords: ['doc', 'docs', 'readme', 'comment', 'documentation'],
|
|
94
|
+
type: 'docs',
|
|
95
|
+
description: 'Documentation changes',
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
keywords: ['style', 'format', 'lint', 'whitespace', 'prettier'],
|
|
99
|
+
type: 'style',
|
|
100
|
+
description: 'Code style changes',
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
keywords: ['refactor', 'restructure', 'reorganize', 'extract', 'simplify'],
|
|
104
|
+
type: 'refactor',
|
|
105
|
+
description: 'Code refactoring',
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
keywords: ['perf', 'performance', 'optimize', 'speed', 'faster'],
|
|
109
|
+
type: 'perf',
|
|
110
|
+
description: 'Performance improvements',
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
keywords: ['test', 'tests', 'spec', 'coverage', 'unittest'],
|
|
114
|
+
type: 'test',
|
|
115
|
+
description: 'Adding or updating tests',
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
keywords: ['build', 'webpack', 'rollup', 'vite', 'esbuild', 'package'],
|
|
119
|
+
type: 'build',
|
|
120
|
+
description: 'Build system changes',
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
keywords: ['ci', 'github action', 'workflow', 'pipeline', 'travis', 'jenkins'],
|
|
124
|
+
type: 'ci',
|
|
125
|
+
description: 'CI/CD changes',
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
keywords: ['chore', 'update', 'upgrade', 'bump', 'dependency', 'deps'],
|
|
129
|
+
type: 'chore',
|
|
130
|
+
description: 'Maintenance tasks',
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
keywords: ['revert', 'rollback', 'undo'],
|
|
134
|
+
type: 'revert',
|
|
135
|
+
description: 'Reverting changes',
|
|
136
|
+
},
|
|
137
|
+
];
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Ticket patterns (JIRA, GitHub, etc.)
|
|
141
|
+
*/
|
|
142
|
+
const TICKET_PATTERNS: Array<{
|
|
143
|
+
name: string;
|
|
144
|
+
pattern: RegExp;
|
|
145
|
+
format: (match: RegExpExecArray) => string;
|
|
146
|
+
}> = [
|
|
147
|
+
{
|
|
148
|
+
name: 'JIRA',
|
|
149
|
+
pattern: /([A-Z]{2,10}-\d+)/,
|
|
150
|
+
format: (match) => match[1],
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
name: 'GitHub Issue',
|
|
154
|
+
pattern: /#(\d+)/,
|
|
155
|
+
format: (match) => `#${match[1]}`,
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
name: 'Linear',
|
|
159
|
+
pattern: /([A-Z]{2,10}-[A-Z0-9]+)/,
|
|
160
|
+
format: (match) => match[1],
|
|
161
|
+
},
|
|
162
|
+
];
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Co-author configuration
|
|
166
|
+
*/
|
|
167
|
+
interface CoAuthor {
|
|
168
|
+
name: string;
|
|
169
|
+
email: string;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const DEFAULT_CO_AUTHOR: CoAuthor = {
|
|
173
|
+
name: 'Claude Opus 4.5',
|
|
174
|
+
email: 'noreply@anthropic.com',
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Commit message configuration
|
|
179
|
+
*/
|
|
180
|
+
interface CommitConfig {
|
|
181
|
+
/** Maximum subject line length */
|
|
182
|
+
maxSubjectLength: number;
|
|
183
|
+
/** Maximum body line length */
|
|
184
|
+
maxBodyLength: number;
|
|
185
|
+
/** Require conventional commit format */
|
|
186
|
+
requireConventional: boolean;
|
|
187
|
+
/** Add co-author by default */
|
|
188
|
+
addCoAuthor: boolean;
|
|
189
|
+
/** Co-author to add */
|
|
190
|
+
coAuthor: CoAuthor;
|
|
191
|
+
/** Add Claude Code reference */
|
|
192
|
+
addClaudeReference: boolean;
|
|
193
|
+
/** Allowed scopes */
|
|
194
|
+
allowedScopes?: string[];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const DEFAULT_CONFIG: CommitConfig = {
|
|
198
|
+
maxSubjectLength: 72,
|
|
199
|
+
maxBodyLength: 100,
|
|
200
|
+
requireConventional: true,
|
|
201
|
+
addCoAuthor: true,
|
|
202
|
+
coAuthor: DEFAULT_CO_AUTHOR,
|
|
203
|
+
addClaudeReference: true,
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Git Commit Hook Manager
|
|
208
|
+
*/
|
|
209
|
+
export class GitCommitHook {
|
|
210
|
+
private registry: HookRegistry;
|
|
211
|
+
private config: CommitConfig;
|
|
212
|
+
|
|
213
|
+
constructor(registry: HookRegistry, config?: Partial<CommitConfig>) {
|
|
214
|
+
this.registry = registry;
|
|
215
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
216
|
+
this.registerHooks();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Register git commit hooks
|
|
221
|
+
*/
|
|
222
|
+
private registerHooks(): void {
|
|
223
|
+
// We use PreCommand hook since there's no specific commit hook event
|
|
224
|
+
// In practice, this would be called when detecting git commit commands
|
|
225
|
+
this.registry.register(
|
|
226
|
+
HookEvent.PreCommand,
|
|
227
|
+
this.handlePreCommit.bind(this),
|
|
228
|
+
HookPriority.Normal,
|
|
229
|
+
{ name: 'git-commit:pre-commit' }
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Handle pre-commit (when a git commit command is detected)
|
|
235
|
+
*/
|
|
236
|
+
async handlePreCommit(context: HookContext): Promise<HookResult> {
|
|
237
|
+
const command = context.command?.command || '';
|
|
238
|
+
|
|
239
|
+
// Only process git commit commands
|
|
240
|
+
if (!command.includes('git commit')) {
|
|
241
|
+
return { success: true };
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Extract message from command if present
|
|
245
|
+
const messageMatch = command.match(/-m\s+["']([^"']+)["']/);
|
|
246
|
+
if (!messageMatch) {
|
|
247
|
+
return { success: true }; // No message to process
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const message = messageMatch[1];
|
|
251
|
+
const branchName = context.metadata?.branchName as string | undefined;
|
|
252
|
+
|
|
253
|
+
const result = await this.processCommitMessage(message, branchName);
|
|
254
|
+
|
|
255
|
+
// Modify the command with the new message
|
|
256
|
+
if (result.success && result.modifiedMessage !== message) {
|
|
257
|
+
const modifiedCommand = command.replace(
|
|
258
|
+
/-m\s+["'][^"']+["']/,
|
|
259
|
+
`-m "${result.modifiedMessage.replace(/"/g, '\\"')}"`
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
return {
|
|
263
|
+
...result,
|
|
264
|
+
data: {
|
|
265
|
+
command: {
|
|
266
|
+
...context.command,
|
|
267
|
+
command: modifiedCommand,
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return result;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Process commit message
|
|
278
|
+
*/
|
|
279
|
+
async processCommitMessage(
|
|
280
|
+
message: string,
|
|
281
|
+
branchName?: string
|
|
282
|
+
): Promise<GitCommitResult> {
|
|
283
|
+
const originalMessage = message;
|
|
284
|
+
let modifiedMessage = message;
|
|
285
|
+
const validationIssues: CommitValidationIssue[] = [];
|
|
286
|
+
const suggestions: string[] = [];
|
|
287
|
+
|
|
288
|
+
// Parse existing message structure
|
|
289
|
+
const { subject, body, footer } = this.parseMessage(message);
|
|
290
|
+
|
|
291
|
+
// Detect commit type
|
|
292
|
+
const commitType = this.detectCommitType(subject);
|
|
293
|
+
|
|
294
|
+
// Add commit type prefix if not present and type was detected
|
|
295
|
+
if (commitType && !this.hasConventionalPrefix(subject)) {
|
|
296
|
+
modifiedMessage = `${commitType}: ${this.lowercaseFirstLetter(subject)}`;
|
|
297
|
+
suggestions.push(`Added conventional commit prefix: ${commitType}`);
|
|
298
|
+
} else if (!commitType && this.config.requireConventional && !this.hasConventionalPrefix(subject)) {
|
|
299
|
+
// No type detected but conventional commits are required - suggest adding a prefix
|
|
300
|
+
suggestions.push('Consider adding a conventional commit prefix (feat:, fix:, docs:, etc.)');
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Validate subject length
|
|
304
|
+
if (subject.length > this.config.maxSubjectLength) {
|
|
305
|
+
validationIssues.push({
|
|
306
|
+
type: 'length',
|
|
307
|
+
severity: 'warning',
|
|
308
|
+
description: `Subject line exceeds ${this.config.maxSubjectLength} characters`,
|
|
309
|
+
suggestedFix: 'Shorten the subject line',
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// Extract ticket reference from branch name
|
|
314
|
+
let ticketReference: string | undefined;
|
|
315
|
+
if (branchName) {
|
|
316
|
+
ticketReference = this.extractTicket(branchName);
|
|
317
|
+
if (ticketReference && !modifiedMessage.includes(ticketReference)) {
|
|
318
|
+
modifiedMessage = this.addTicketReference(modifiedMessage, ticketReference);
|
|
319
|
+
suggestions.push(`Added ticket reference: ${ticketReference}`);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Add Claude Code reference and co-author
|
|
324
|
+
let coAuthorAdded = false;
|
|
325
|
+
if (this.config.addClaudeReference || this.config.addCoAuthor) {
|
|
326
|
+
const additions: string[] = [];
|
|
327
|
+
|
|
328
|
+
if (this.config.addClaudeReference) {
|
|
329
|
+
additions.push('\n\nGenerated with [Claude Code](https://claude.com/claude-code)');
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (this.config.addCoAuthor) {
|
|
333
|
+
additions.push(`\n\nCo-Authored-By: ${this.config.coAuthor.name} <${this.config.coAuthor.email}>`);
|
|
334
|
+
coAuthorAdded = true;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Only add if not already present
|
|
338
|
+
for (const addition of additions) {
|
|
339
|
+
const searchStr = addition.trim().split('\n')[0];
|
|
340
|
+
if (!modifiedMessage.includes(searchStr)) {
|
|
341
|
+
modifiedMessage += addition;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Validate conventional commit format
|
|
347
|
+
if (this.config.requireConventional) {
|
|
348
|
+
const conventionalIssues = this.validateConventional(modifiedMessage);
|
|
349
|
+
validationIssues.push(...conventionalIssues);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return {
|
|
353
|
+
success: true,
|
|
354
|
+
originalMessage,
|
|
355
|
+
modifiedMessage,
|
|
356
|
+
commitType,
|
|
357
|
+
ticketReference,
|
|
358
|
+
coAuthorAdded,
|
|
359
|
+
validationIssues: validationIssues.length > 0 ? validationIssues : undefined,
|
|
360
|
+
suggestions: suggestions.length > 0 ? suggestions : undefined,
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Parse commit message into parts
|
|
366
|
+
*/
|
|
367
|
+
private parseMessage(message: string): {
|
|
368
|
+
subject: string;
|
|
369
|
+
body?: string;
|
|
370
|
+
footer?: string;
|
|
371
|
+
} {
|
|
372
|
+
const parts = message.split('\n\n');
|
|
373
|
+
return {
|
|
374
|
+
subject: parts[0] || '',
|
|
375
|
+
body: parts[1],
|
|
376
|
+
footer: parts.slice(2).join('\n\n'),
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Detect commit type from message
|
|
382
|
+
*/
|
|
383
|
+
private detectCommitType(message: string): CommitType | undefined {
|
|
384
|
+
const lowerMessage = message.toLowerCase();
|
|
385
|
+
|
|
386
|
+
// First check if message already has conventional prefix
|
|
387
|
+
const prefixMatch = lowerMessage.match(/^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?:/);
|
|
388
|
+
if (prefixMatch) {
|
|
389
|
+
return prefixMatch[1] as CommitType;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
// Score each commit type based on keyword matches
|
|
393
|
+
// More specific/unique keywords get higher weight
|
|
394
|
+
const scores: Map<CommitType, number> = new Map();
|
|
395
|
+
|
|
396
|
+
// High-priority patterns (check these first as they're more specific)
|
|
397
|
+
const priorityPatterns: Array<{ pattern: RegExp; type: CommitType; weight: number }> = [
|
|
398
|
+
// Test patterns - high priority because "add tests" should be 'test' not 'feat'
|
|
399
|
+
{ pattern: /\b(test|tests|spec|specs|unittest|unit test|testing)\b/i, type: 'test', weight: 3 },
|
|
400
|
+
// Docs patterns
|
|
401
|
+
{ pattern: /\b(doc|docs|documentation|readme|comment|comments)\b/i, type: 'docs', weight: 3 },
|
|
402
|
+
// Revert patterns
|
|
403
|
+
{ pattern: /\b(revert|rollback|undo)\b/i, type: 'revert', weight: 3 },
|
|
404
|
+
// Fix patterns (bug-specific)
|
|
405
|
+
{ pattern: /\b(fix|bug|bugfix|resolve|patch|hotfix)\b/i, type: 'fix', weight: 2 },
|
|
406
|
+
// CI patterns
|
|
407
|
+
{ pattern: /\b(ci|github action|workflow|pipeline|travis|jenkins|circleci)\b/i, type: 'ci', weight: 3 },
|
|
408
|
+
// Build patterns
|
|
409
|
+
{ pattern: /\b(build|webpack|rollup|vite|esbuild|bundler|package\.json)\b/i, type: 'build', weight: 2 },
|
|
410
|
+
// Perf patterns
|
|
411
|
+
{ pattern: /\b(perf|performance|optimize|speed|faster|slow)\b/i, type: 'perf', weight: 2 },
|
|
412
|
+
// Refactor patterns
|
|
413
|
+
{ pattern: /\b(refactor|restructure|reorganize|extract|simplify|clean)\b/i, type: 'refactor', weight: 2 },
|
|
414
|
+
// Style patterns
|
|
415
|
+
{ pattern: /\b(style|format|lint|whitespace|prettier|eslint)\b/i, type: 'style', weight: 2 },
|
|
416
|
+
// Chore patterns - specifically for dependencies
|
|
417
|
+
{ pattern: /\b(dependency|dependencies|deps|bump|upgrade version)\b/i, type: 'chore', weight: 2 },
|
|
418
|
+
// Generic update is lower priority (could be chore or other)
|
|
419
|
+
{ pattern: /\b(update)\b/i, type: 'chore', weight: 1 },
|
|
420
|
+
// Feat patterns (generic add/create/implement)
|
|
421
|
+
{ pattern: /\b(add|implement|create|introduce|new feature)\b/i, type: 'feat', weight: 1 },
|
|
422
|
+
];
|
|
423
|
+
|
|
424
|
+
// Calculate scores for each pattern
|
|
425
|
+
for (const { pattern, type, weight } of priorityPatterns) {
|
|
426
|
+
if (pattern.test(lowerMessage)) {
|
|
427
|
+
const currentScore = scores.get(type) || 0;
|
|
428
|
+
scores.set(type, currentScore + weight);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// Find highest scoring type
|
|
433
|
+
let maxScore = 0;
|
|
434
|
+
let detectedType: CommitType | undefined;
|
|
435
|
+
|
|
436
|
+
for (const [type, score] of scores) {
|
|
437
|
+
if (score > maxScore) {
|
|
438
|
+
maxScore = score;
|
|
439
|
+
detectedType = type;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
return detectedType;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Check if message has conventional commit prefix
|
|
448
|
+
*/
|
|
449
|
+
private hasConventionalPrefix(message: string): boolean {
|
|
450
|
+
return /^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?:/.test(message.toLowerCase());
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Lowercase first letter of a string
|
|
455
|
+
*/
|
|
456
|
+
private lowercaseFirstLetter(str: string): string {
|
|
457
|
+
// Don't lowercase if it's an acronym or proper noun
|
|
458
|
+
if (/^[A-Z]{2,}/.test(str)) {
|
|
459
|
+
return str;
|
|
460
|
+
}
|
|
461
|
+
return str.charAt(0).toLowerCase() + str.slice(1);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Extract ticket reference from branch name
|
|
466
|
+
*/
|
|
467
|
+
private extractTicket(branchName: string): string | undefined {
|
|
468
|
+
for (const { pattern, format } of TICKET_PATTERNS) {
|
|
469
|
+
const match = pattern.exec(branchName);
|
|
470
|
+
if (match) {
|
|
471
|
+
return format(match);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
return undefined;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Add ticket reference to message
|
|
479
|
+
*/
|
|
480
|
+
private addTicketReference(message: string, ticket: string): string {
|
|
481
|
+
const parts = message.split('\n\n');
|
|
482
|
+
const subject = parts[0];
|
|
483
|
+
const rest = parts.slice(1).join('\n\n');
|
|
484
|
+
|
|
485
|
+
// Add refs line
|
|
486
|
+
if (rest) {
|
|
487
|
+
return `${subject}\n\nRefs: ${ticket}\n\n${rest}`;
|
|
488
|
+
}
|
|
489
|
+
return `${subject}\n\nRefs: ${ticket}`;
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* Validate conventional commit format
|
|
494
|
+
*/
|
|
495
|
+
private validateConventional(message: string): CommitValidationIssue[] {
|
|
496
|
+
const issues: CommitValidationIssue[] = [];
|
|
497
|
+
const lines = message.split('\n');
|
|
498
|
+
const subject = lines[0] || '';
|
|
499
|
+
|
|
500
|
+
// Check for conventional prefix
|
|
501
|
+
if (!this.hasConventionalPrefix(subject)) {
|
|
502
|
+
issues.push({
|
|
503
|
+
type: 'format',
|
|
504
|
+
severity: 'warning',
|
|
505
|
+
description: 'Missing conventional commit prefix',
|
|
506
|
+
suggestedFix: 'Add a prefix like feat:, fix:, docs:, etc.',
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// Check subject line starts with lowercase (after prefix)
|
|
511
|
+
const afterPrefix = subject.replace(/^[a-z]+(\(.+\))?: /, '');
|
|
512
|
+
if (afterPrefix && /^[A-Z]/.test(afterPrefix) && !/^[A-Z]{2,}/.test(afterPrefix)) {
|
|
513
|
+
issues.push({
|
|
514
|
+
type: 'format',
|
|
515
|
+
severity: 'info',
|
|
516
|
+
description: 'Subject should start with lowercase (conventional style)',
|
|
517
|
+
suggestedFix: 'Use lowercase for the first word after the prefix',
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Check for period at end of subject
|
|
522
|
+
if (subject.endsWith('.')) {
|
|
523
|
+
issues.push({
|
|
524
|
+
type: 'format',
|
|
525
|
+
severity: 'info',
|
|
526
|
+
description: 'Subject line should not end with a period',
|
|
527
|
+
suggestedFix: 'Remove the trailing period',
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// Check body line lengths
|
|
532
|
+
for (let i = 1; i < lines.length; i++) {
|
|
533
|
+
const line = lines[i];
|
|
534
|
+
if (line.length > this.config.maxBodyLength && !line.startsWith('Co-Authored-By:')) {
|
|
535
|
+
issues.push({
|
|
536
|
+
type: 'body',
|
|
537
|
+
severity: 'info',
|
|
538
|
+
description: `Line ${i + 1} exceeds ${this.config.maxBodyLength} characters`,
|
|
539
|
+
suggestedFix: 'Wrap long lines in the commit body',
|
|
540
|
+
});
|
|
541
|
+
break; // Only report first occurrence
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
// Check for breaking change indicator
|
|
546
|
+
if (subject.includes('!:') || message.includes('BREAKING CHANGE:')) {
|
|
547
|
+
issues.push({
|
|
548
|
+
type: 'breaking',
|
|
549
|
+
severity: 'info',
|
|
550
|
+
description: 'Breaking change detected - ensure changelog is updated',
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
return issues;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
/**
|
|
558
|
+
* Process commit message manually
|
|
559
|
+
*/
|
|
560
|
+
async process(message: string, branchName?: string): Promise<GitCommitResult> {
|
|
561
|
+
return this.processCommitMessage(message, branchName);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* Format a commit message with heredoc-style for git
|
|
566
|
+
*/
|
|
567
|
+
formatForGit(message: string): string {
|
|
568
|
+
// Escape for heredoc usage
|
|
569
|
+
return `$(cat <<'EOF'
|
|
570
|
+
${message}
|
|
571
|
+
EOF
|
|
572
|
+
)`;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Generate a commit command with formatted message
|
|
577
|
+
*/
|
|
578
|
+
generateCommitCommand(message: string): string {
|
|
579
|
+
return `git commit -m "${this.formatForGit(message)}"`;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/**
|
|
583
|
+
* Get commit type description
|
|
584
|
+
*/
|
|
585
|
+
getCommitTypeDescription(type: CommitType): string {
|
|
586
|
+
const pattern = COMMIT_TYPE_PATTERNS.find(p => p.type === type);
|
|
587
|
+
return pattern?.description || 'Unknown commit type';
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Get all available commit types
|
|
592
|
+
*/
|
|
593
|
+
getAllCommitTypes(): Array<{ type: CommitType; description: string }> {
|
|
594
|
+
return COMMIT_TYPE_PATTERNS.map(p => ({
|
|
595
|
+
type: p.type,
|
|
596
|
+
description: p.description,
|
|
597
|
+
}));
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* Update configuration
|
|
602
|
+
*/
|
|
603
|
+
setConfig(config: Partial<CommitConfig>): void {
|
|
604
|
+
this.config = { ...this.config, ...config };
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* Get current configuration
|
|
609
|
+
*/
|
|
610
|
+
getConfig(): CommitConfig {
|
|
611
|
+
return { ...this.config };
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Create git commit hook
|
|
617
|
+
*/
|
|
618
|
+
export function createGitCommitHook(
|
|
619
|
+
registry: HookRegistry,
|
|
620
|
+
config?: Partial<CommitConfig>
|
|
621
|
+
): GitCommitHook {
|
|
622
|
+
return new GitCommitHook(registry, config);
|
|
623
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* V3 Safety Hooks - Index
|
|
3
|
+
*
|
|
4
|
+
* TypeScript conversions of V2 shell hooks for:
|
|
5
|
+
* - Bash command safety
|
|
6
|
+
* - File organization enforcement
|
|
7
|
+
* - Git commit formatting
|
|
8
|
+
*
|
|
9
|
+
* @module v3/shared/hooks/safety
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// Bash safety hook
|
|
13
|
+
export {
|
|
14
|
+
BashSafetyHook,
|
|
15
|
+
createBashSafetyHook,
|
|
16
|
+
} from './bash-safety.js';
|
|
17
|
+
|
|
18
|
+
export type {
|
|
19
|
+
BashSafetyResult,
|
|
20
|
+
CommandRisk,
|
|
21
|
+
} from './bash-safety.js';
|
|
22
|
+
|
|
23
|
+
// File organization hook
|
|
24
|
+
export {
|
|
25
|
+
FileOrganizationHook,
|
|
26
|
+
createFileOrganizationHook,
|
|
27
|
+
} from './file-organization.js';
|
|
28
|
+
|
|
29
|
+
export type {
|
|
30
|
+
FileOrganizationResult,
|
|
31
|
+
FormatterRecommendation,
|
|
32
|
+
LinterRecommendation,
|
|
33
|
+
OrganizationIssue,
|
|
34
|
+
} from './file-organization.js';
|
|
35
|
+
|
|
36
|
+
// Git commit hook
|
|
37
|
+
export {
|
|
38
|
+
GitCommitHook,
|
|
39
|
+
createGitCommitHook,
|
|
40
|
+
} from './git-commit.js';
|
|
41
|
+
|
|
42
|
+
export type {
|
|
43
|
+
GitCommitResult,
|
|
44
|
+
CommitType,
|
|
45
|
+
CommitValidationIssue,
|
|
46
|
+
} from './git-commit.js';
|