-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathprepare-commit-msg.js
More file actions
executable file
·62 lines (56 loc) · 2.12 KB
/
Copy pathprepare-commit-msg.js
File metadata and controls
executable file
·62 lines (56 loc) · 2.12 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
#!/usr/bin/env node
const { promisify } = require('util');
const childProcess = require('child_process');
const fs = require('fs');
const exec = promisify(childProcess.exec);
// Build regex from commitlint's known issuePrefixes so arbitrary branch segments
// (e.g. "release-2-hotfix") don't produce bogus TICKET footers.
let issuePrefixes;
try {
issuePrefixes = require('../commitlint.config.js').parserPreset.parserOpts.issuePrefixes;
} catch (_) {
issuePrefixes = [];
}
const escapedPrefixes = issuePrefixes
.filter((p) => p !== '#')
.map((p) => p.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
// Anchored at branch start; case-insensitive so "web-000-desc" matches "WEB-"
// null when no prefixes are configured — avoids ^()(\d+) matching bare-digit branches.
const branchRegex =
escapedPrefixes.length > 0 ? new RegExp(`^(${escapedPrefixes.join('|')})(\\d+)`, 'i') : null;
// ex TICKET: WP-1234
const commitRegex = /(ticket|issue):\s(\S+)/gim;
function extractTicket(branch) {
if (!branchRegex) return null;
const found = branch.match(branchRegex);
return found ? found[0].toUpperCase() : null;
}
// Given the existing commit message and a ticket, return the message with a
// TICKET footer appended, or null when no change is needed (no ticket, or the
// message already contains a ticket/issue footer). Pure so it is unit-testable.
function appendTicketFooter(data, ticket) {
if (!ticket) {
return null;
}
// Reset lastIndex: commitRegex is global, so .test() is stateful across calls.
commitRegex.lastIndex = 0;
if (commitRegex.test(data)) {
return null;
}
return `${data}\nTICKET: ${ticket}\n`;
}
async function main() {
const commitMsgFilepath = process.argv[2];
try {
const branch = (await exec(`git branch --show-current`)).stdout.trim();
const ticket = extractTicket(branch);
const data = fs.readFileSync(commitMsgFilepath, 'utf8');
const updated = appendTicketFooter(data, ticket);
if (updated === null) {
return;
}
fs.writeFileSync(commitMsgFilepath, updated);
} catch (e) {}
}
module.exports = { extractTicket, appendTicketFooter };
if (require.main === module) main();