UNPKG

1.9 kBJavaScriptView Raw
1const {isMatchWith, isRegExp} = require('lodash');
2const debug = require('debug')('semantic-release:commit-analyzer');
3const RELEASE_TYPES = require('./default-release-types');
4const compareReleaseTypes = require('./compare-release-types');
5
6/**
7 * Find all the rules matching and return the highest release type of the matching rules.
8 *
9 * @param {Array} releaseRules the rules to match the commit against.
10 * @param {Commit} commit a parsed commit.
11 * @return {string} the highest release type of the matching rules or `undefined` if no rule match the commit.
12 */
13module.exports = (releaseRules, commit) => {
14 let releaseType;
15
16 releaseRules
17 .filter(
18 ({breaking, revert, release, ...rule}) =>
19 // If the rule is not `breaking` or the commit doesn't have a breaking change note
20 (!breaking || (commit.notes && commit.notes.length > 0)) &&
21 // If the rule is not `revert` or the commit is not a revert
22 (!revert || commit.revert) &&
23 // Otherwise match the regular rules
24 isMatchWith(
25 commit,
26 rule,
27 (obj, src) =>
28 /^\/.*\/$/.test(src) || isRegExp(src) ? new RegExp(/^\/(.*)\/$/.exec(src)[1]).test(obj) : undefined
29 )
30 )
31 .every(match => {
32 if (compareReleaseTypes(releaseType, match.release)) {
33 releaseType = match.release;
34 debug('The rule %o match commit with release type %o', match, releaseType);
35 if (releaseType === RELEASE_TYPES[0]) {
36 debug('Release type %o is the highest possible. Stop analysis.', releaseType);
37 return false;
38 }
39 } else {
40 debug(
41 'The rule %o match commit with release type %o but the higher release type %o has already been found for this commit',
42 match,
43 match.release,
44 releaseType
45 );
46 }
47 return true;
48 });
49
50 return releaseType;
51};