Repository navigation
Expand file tree
/
Copy pathcommitParser.js
More file actions
170 lines (149 loc) · 4.54 KB
/
Copy pathcommitParser.js
File metadata and controls
170 lines (149 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env node
/**
* ============================================================================
* Module: commitParser.js
* Location: scripts/agents/includes/commitParser.js
* Description:
* - Parses conventional commits format
* - Extracts type, scope, description, body, and footers
* - Identifies breaking changes in footers
* Standards:
* - Follows LightSpeed Coding Standards
* - Follows Conventional Commits v1.0.0
* ============================================================================
*/
/**
* Parse a conventional commit message
* @param {string} message - Full commit message
* @returns {Object} Parsed commit with type, scope, description, body, footers, isBreaking
*/
function parseConventionalCommit(message) {
const lines = message.split("\n");
const headerLine = lines[0];
// Parse header: type(scope)!?: description (! indicates breaking change)
const headerRegex = /^(\w+)(?:\(([^)]*)\))?(!)?\s*:\s*(.+)$/;
const headerMatch = headerRegex.exec(headerLine);
if (!headerMatch) {
return {
type: null,
scope: null,
description: headerLine,
body: null,
footers: {},
isBreaking: false,
valid: false,
};
}
const type = headerMatch[1];
const scope = headerMatch[2] || null;
const hasBreakingBang = !!headerMatch[3];
const description = headerMatch[4];
// Separate body and footers
let body = "";
const footers = {};
// Check if breaking change is indicated in header with !
let isBreaking = hasBreakingBang;
// Find blank line separating header from body
let bodyStartIndex = 1;
while (bodyStartIndex < lines.length && lines[bodyStartIndex].trim() === "") {
bodyStartIndex++;
}
if (bodyStartIndex < lines.length) {
let firstFooterIndex = lines.length;
for (let i = bodyStartIndex; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) continue;
// Check for either "Token: value" or "Token #number" format
if (
/^([^:]+):\s*(.+)$/.test(line) || // "Token: value"
/^(Closes|Fixes|Fixes|Resolves|Refs|Related-To|See-Also|Acked-By|Reviewed-By|Tested-By|Signed-Off-By)\s+/.test(
line,
) // "Token #123" style
) {
firstFooterIndex = i;
break;
}
}
// Extract body (everything before first footer)
body = lines.slice(bodyStartIndex, firstFooterIndex).join("\n").trim();
// Parse footers (from first footer onwards, skipping blank lines)
for (let i = firstFooterIndex; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) continue;
// Try "Token: value" format first
let footerMatch = /^([^:]+):\s*(.+)$/.exec(line);
if (footerMatch) {
const token = footerMatch[1].trim();
const value = footerMatch[2];
footers[token] = value;
if (token === "BREAKING CHANGE" || token === "BREAKING-CHANGE") {
isBreaking = true;
}
}
}
}
// Validate type is conventional commit type
const validTypes = [
"feat",
"fix",
"docs",
"style",
"refactor",
"perf",
"test",
"chore",
];
const valid = validTypes.includes(type);
return {
type,
scope,
description,
body,
footers,
isBreaking,
valid,
};
}
/**
* Batch parse multiple commit messages
* @param {string[]} messages - Array of commit messages
* @returns {Object[]} Array of parsed commits
*/
function parseCommits(messages) {
return messages.map((message) => parseConventionalCommit(message));
}
/**
* Filter commits by type
* @param {Object[]} commits - Parsed commits
* @param {string} type - Commit type to filter by
* @returns {Object[]} Filtered commits
*/
function filterCommitsByType(commits, type) {
return commits.filter((commit) => commit.type === type && commit.valid);
}
/**
* Extract breaking change descriptions
* @param {Object[]} commits - Parsed commits
* @returns {string[]} Breaking change descriptions
*/
function extractBreakingChanges(commits) {
const breakingChanges = [];
commits.forEach((commit) => {
if (commit.isBreaking) {
if (commit.footers["BREAKING CHANGE"]) {
breakingChanges.push(commit.footers["BREAKING CHANGE"]);
} else if (commit.footers["BREAKING-CHANGE"]) {
breakingChanges.push(commit.footers["BREAKING-CHANGE"]);
} else {
breakingChanges.push(`${commit.type}: ${commit.description}`);
}
}
});
return breakingChanges;
}
module.exports = {
parseConventionalCommit,
parseCommits,
filterCommitsByType,
extractBreakingChanges,
};