UNPKG

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