1 | import { isUndefined } from "lodash-es";
|
2 | import { sync as parser } from "conventional-commits-parser";
|
3 | import filter from "conventional-commits-filter";
|
4 | import debugFactory from "debug";
|
5 | import loadParserConfig from "./lib/load-parser-config.js";
|
6 | import loadReleaseRules from "./lib/load-release-rules.js";
|
7 | import analyzeCommit from "./lib/analyze-commit.js";
|
8 | import compareReleaseTypes from "./lib/compare-release-types.js";
|
9 | import RELEASE_TYPES from "./lib/default-release-types.js";
|
10 | import DEFAULT_RELEASE_RULES from "./lib/default-release-rules.js";
|
11 |
|
12 | const debug = debugFactory("semantic-release:commit-analyzer");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | export async function analyzeCommits(pluginConfig, context) {
|
29 | const { commits, logger } = context;
|
30 | const releaseRules = loadReleaseRules(pluginConfig, context);
|
31 | const config = await loadParserConfig(pluginConfig, context);
|
32 | let releaseType = null;
|
33 |
|
34 | filter(
|
35 | commits
|
36 | .filter(({ message, hash }) => {
|
37 | if (!message.trim()) {
|
38 | debug("Skip commit %s with empty message", hash);
|
39 | return false;
|
40 | }
|
41 |
|
42 | return true;
|
43 | })
|
44 | .map(({ message, ...commitProps }) => ({ rawMsg: message, message, ...commitProps, ...parser(message, config) }))
|
45 | ).every(({ rawMsg, ...commit }) => {
|
46 | logger.log(`Analyzing commit: %s`, rawMsg);
|
47 | let commitReleaseType;
|
48 |
|
49 |
|
50 | if (releaseRules) {
|
51 | debug("Analyzing with custom rules");
|
52 | commitReleaseType = analyzeCommit(releaseRules, commit);
|
53 | }
|
54 |
|
55 |
|
56 | if (isUndefined(commitReleaseType)) {
|
57 | debug("Analyzing with default rules");
|
58 | commitReleaseType = analyzeCommit(DEFAULT_RELEASE_RULES, commit);
|
59 | }
|
60 |
|
61 | if (commitReleaseType) {
|
62 | logger.log("The release type for the commit is %s", commitReleaseType);
|
63 | } else {
|
64 | logger.log("The commit should not trigger a release");
|
65 | }
|
66 |
|
67 |
|
68 | if (commitReleaseType && compareReleaseTypes(releaseType, commitReleaseType)) {
|
69 | releaseType = commitReleaseType;
|
70 | }
|
71 |
|
72 |
|
73 | if (releaseType === RELEASE_TYPES[0]) {
|
74 | return false;
|
75 | }
|
76 |
|
77 | return true;
|
78 | });
|
79 | logger.log("Analysis of %s commits complete: %s release", commits.length, releaseType || "no");
|
80 |
|
81 | return releaseType;
|
82 | }
|