UNPKG

2.83 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.parseSquashPR = exports.parsePR = void 0;
4const make_hooks_1 = require("./utils/make-hooks");
5/** Parse the PR information for the merge commit message */
6function parsePR(commit) {
7 const merge = /Merge pull request #(\d+) from (.+)\n([\S\s]+)/;
8 const prMatch = commit.subject.match(merge);
9 if (!prMatch) {
10 return commit;
11 }
12 return Object.assign(Object.assign({}, commit), { pullRequest: {
13 number: Number(prMatch[1]),
14 base: prMatch[2],
15 }, subject: prMatch[3].trim() });
16}
17exports.parsePR = parsePR;
18/** Parse the PR information for the squashed commit message */
19function parseSquashPR(commit) {
20 const firstLine = commit.subject.split("\n")[0];
21 const squashMerge = /\(#(\d+)\)$/;
22 const squashMergeMatch = firstLine.match(squashMerge);
23 if (!squashMergeMatch) {
24 return commit;
25 }
26 return Object.assign(Object.assign({}, commit), { pullRequest: {
27 number: Number(squashMergeMatch[1]),
28 }, subject: firstLine
29 .substr(0, firstLine.length - squashMergeMatch[0].length)
30 .trim() });
31}
32exports.parseSquashPR = parseSquashPR;
33/**
34 * Parse the gitlog for commits that are PRs and attach their labels.
35 * This class can also be tapped into via plugin to parse commits
36 * in other ways (ex: conventional-commits)
37 */
38class LogParse {
39 /** Initialize the log parser and tap the default functionality */
40 constructor() {
41 this.hooks = make_hooks_1.makeLogParseHooks();
42 this.hooks.parseCommit.tap("Merge Commit", parsePR);
43 this.hooks.parseCommit.tap("Squash Merge Commit", parseSquashPR);
44 this.hooks.parseCommit.tap("Strip consecutive white-space in Titles", (commit) => {
45 const [firstLine, ...lines] = commit.subject.split("\n");
46 commit.subject = [
47 firstLine.replace(/[^\S\r\n]{2,}/g, " "),
48 ...lines,
49 ].join("\n");
50 return commit;
51 });
52 }
53 /** Run the log parser over a set of commits */
54 async normalizeCommits(commits) {
55 const eCommits = await Promise.all(commits.map(async (commit) => this.normalizeCommit(commit)));
56 return eCommits.filter(Boolean);
57 }
58 /** Process a commit to find it's labels and PR information */
59 async normalizeCommit(commit) {
60 const extended = await this.hooks.parseCommit.promise(Object.assign(Object.assign({ labels: [] }, commit), { authors: [{ name: commit.authorName, email: commit.authorEmail }] }));
61 const shouldOmit = await this.hooks.omitCommit.promise(extended);
62 if (shouldOmit) {
63 return;
64 }
65 return extended;
66 }
67}
68exports.default = LogParse;
69//# sourceMappingURL=log-parse.js.map
\No newline at end of file