UNPKG

@condenast/commit-analyzer

Version:

semantic-release plugin to analyze commits with conventional-changelog

50 lines (45 loc) 1.86 kB
const {isMatchWith, isRegExp} = require('lodash'); const debug = require('debug')('semantic-release:commit-analyzer'); const RELEASE_TYPES = require('./default-release-types'); const compareReleaseTypes = require('./compare-release-types'); /** * Find all the rules matching and return the highest release type of the matching rules. * * @param {Array} releaseRules the rules to match the commit against. * @param {Commit} commit a parsed commit. * @return {string} the highest release type of the matching rules or `undefined` if no rule match the commit. */ module.exports = (releaseRules, commit) => { let releaseType; releaseRules .filter( ({breaking, revert, release, ...rule}) => // If the rule is not `breaking` or the commit doesn't have a breaking change note (!breaking || (commit.notes && commit.notes.length > 0)) && // If the rule is not `revert` or the commit is not a revert (!revert || commit.revert) && // Otherwise match the regular rules isMatchWith(commit, rule, (obj, src) => /^\/.*\/$/.test(src) || isRegExp(src) ? new RegExp(/^\/(.*)\/$/.exec(src)[1]).test(obj) : undefined ) ) .every(match => { if (compareReleaseTypes(releaseType, match.release)) { releaseType = match.release; debug('The rule %o match commit with release type %o', match, releaseType); if (releaseType === RELEASE_TYPES[0]) { debug('Release type %o is the highest possible. Stop analysis.', releaseType); return false; } } else { debug( 'The rule %o match commit with release type %o but the higher release type %o has already been found for this commit', match, match.release, releaseType ); } return true; }); return releaseType; };