UNPKG

git-release-manager

Version:

A tool to generate release notes from git commit history

136 lines 7.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.enrichCommit = enrichCommit; exports.getTypeTitle = getTypeTitle; const regexHandlers_1 = require("./regexHandlers"); const profileHandler_1 = require("./profileHandler"); async function enrichCommit(commit, config) { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3; const match = RegExp(/^(?<type>\w+)(\((?<scope>[^)]+)\))?(?<modifier>[!+~?*])?:\s(?<summary>.+)$/).exec(commit.message); const type = (_b = (_a = match === null || match === void 0 ? void 0 : match.groups) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : null; const scope = (_d = (_c = match === null || match === void 0 ? void 0 : match.groups) === null || _c === void 0 ? void 0 : _c.scope) !== null && _d !== void 0 ? _d : null; const modifier = (_f = (_e = match === null || match === void 0 ? void 0 : match.groups) === null || _e === void 0 ? void 0 : _e.modifier) !== null && _f !== void 0 ? _f : null; const summary = (_h = (_g = match === null || match === void 0 ? void 0 : match.groups) === null || _g === void 0 ? void 0 : _g.summary) !== null && _h !== void 0 ? _h : commit.message; let description = (_k = (_j = commit.body) === null || _j === void 0 ? void 0 : _j.trim()) !== null && _k !== void 0 ? _k : null; const tagDescription = (_l = description === null || description === void 0 ? void 0 : description.trim()) !== null && _l !== void 0 ? _l : ''; const tagRegex = /Tags:\s(.+)/i; const labels = []; const labelsMatch = RegExp(tagRegex).exec(tagDescription); if (labelsMatch) { labels.push(...labelsMatch[1].split(',').map(label => (label === null || label === void 0 ? void 0 : label.trim()) || '')); description = (_o = (_m = description === null || description === void 0 ? void 0 : description.replace(labelsMatch[0], '')) === null || _m === void 0 ? void 0 : _m.trim()) !== null && _o !== void 0 ? _o : null; } const mentionDescription = (_p = description === null || description === void 0 ? void 0 : description.trim()) !== null && _p !== void 0 ? _p : ''; const mentionRegex = (0, regexHandlers_1.createMentionRegex)(config.mentionTypes); const mentions = []; let mentionMatch; if (mentionRegex) { while ((mentionMatch = mentionRegex.exec(mentionDescription)) !== null) { const matchedTerm = (_q = mentionMatch[0].split(':')[0]) === null || _q === void 0 ? void 0 : _q.trim(); const matchedType = config.mentionTypes.find(mentionType => mentionType.terms.includes(matchedTerm)); const profileUrl = await (0, profileHandler_1.prepareProfileUrlsByEMail)(((_r = mentionMatch[2]) === null || _r === void 0 ? void 0 : _r.trim()) || ''); if (matchedType) { mentions.push({ type: matchedType.type, title: matchedType.title, name: ((_s = mentionMatch[1]) === null || _s === void 0 ? void 0 : _s.trim()) || '', email: ((_t = mentionMatch[2]) === null || _t === void 0 ? void 0 : _t.trim()) || '', profileUrl, }); description = (_v = (_u = description === null || description === void 0 ? void 0 : description.replace(mentionMatch[0], '')) === null || _u === void 0 ? void 0 : _u.trim()) !== null && _v !== void 0 ? _v : null; } } } const linkDescription = (_w = description === null || description === void 0 ? void 0 : description.trim()) !== null && _w !== void 0 ? _w : ''; const linkRegex = (0, regexHandlers_1.createLinkRegex)(config.linkTypes); const links = []; let linkMatch; if (linkRegex) { while ((linkMatch = linkRegex.exec(linkDescription)) !== null) { const matchedTerm = (_x = linkMatch[1]) === null || _x === void 0 ? void 0 : _x.trim(); const matchedType = config.linkTypes.filter(linkType => linkType.terms).find(linkType => { var _a; return (_a = linkType.terms) === null || _a === void 0 ? void 0 : _a.includes(matchedTerm); }); if (matchedType) { links.push({ type: matchedType.type, title: matchedType.title, id: parseInt(linkMatch[2], 10), }); description = (_z = (_y = description === null || description === void 0 ? void 0 : description.replace(linkMatch[0], '')) === null || _y === void 0 ? void 0 : _y.trim()) !== null && _z !== void 0 ? _z : null; } } } const commitMessage = ((_0 = commit.message) === null || _0 === void 0 ? void 0 : _0.trim()) || ''; const processedIds = new Set(links.map(link => link.id)); config.linkTypes.forEach(linkType => { var _a, _b; if (linkType.sign) { const signRegex = new RegExp(`\\${linkType.sign[0]}(\\d+)`); let signMatch; while ((signMatch = signRegex.exec(linkDescription)) !== null) { const matchedId = parseInt(signMatch[1], 10); if (processedIds.has(matchedId)) { continue; } if (!links.some(link => link.type === linkType.type && link.id === matchedId)) { links.push({ type: linkType.type, title: linkType.title, id: matchedId, }); } description = (_b = (_a = description === null || description === void 0 ? void 0 : description.replace(signMatch[0], '')) === null || _a === void 0 ? void 0 : _a.trim()) !== null && _b !== void 0 ? _b : null; } while ((signMatch = signRegex.exec(commitMessage)) !== null) { const matchedId = parseInt(signMatch[1], 10); if (processedIds.has(matchedId)) { continue; } if (!links.some(link => link.type === linkType.type && link.id === matchedId)) { links.push({ type: linkType.type, title: linkType.title, id: matchedId, }); } } } }); const noteDescription = (_1 = description === null || description === void 0 ? void 0 : description.trim()) !== null && _1 !== void 0 ? _1 : ''; const notes = []; const noteRegex = /(.*?):\s(.+)/g; let noteMatch; while ((noteMatch = noteRegex.exec(noteDescription)) !== null) { notes.push({ type: noteMatch[1].toLowerCase(), content: noteMatch[2], }); description = (_3 = (_2 = description === null || description === void 0 ? void 0 : description.replace(noteMatch[0], '')) === null || _2 === void 0 ? void 0 : _2.trim()) !== null && _3 !== void 0 ? _3 : null; } const typeTitle = getTypeTitle(config.commitTypes, type); const profileUrl = await (0, profileHandler_1.prepareProfileUrlsByEMail)(commit.authorEmail); const commitContent = { type, typeTitle, scope, modifier, summary, description, author: { name: commit.authorName, email: commit.authorEmail, profileUrl, }, mentions, links, notes, labels, raw: commit, files: [], }; return commitContent; } function getTypeTitle(commitTypes, type) { const matchingType = commitTypes.find(ct => ct.terms.includes(type !== null && type !== void 0 ? type : '')); return matchingType ? matchingType.title : 'Other Changes'; } //# sourceMappingURL=index.js.map