diff --git a/README.md b/README.md index 930a7177..e5ad67d0 100644 --- a/README.md +++ b/README.md @@ -53,18 +53,19 @@ With this example: ### Options -| Option | Description | Default | -|------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------| -| `preset` | [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset (possible values: [`angular`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-angular), [`atom`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-atom), [`codemirror`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-codemirror), [`ember`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-ember), [`eslint`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-eslint), [`express`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-express), [`jquery`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-jquery), [`jshint`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-jshint), [`conventionalcommits`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-conventionalcommits)). | [`angular`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-angular) | -| `config` | NPM package name of a custom [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset. | - | -| `parserOpts` | Additional [conventional-commits-parser](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser#conventionalcommitsparseroptions) options that will extends the ones loaded by `preset` or `config`. This is convenient to use a [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset with some customizations without having to create a new module. | - | -| `writerOpts` | Additional [conventional-commits-writer](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-writer#options) options that will extends the ones loaded by `preset` or `config`. This is convenient to use a [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset with some customizations without having to create a new module. | - | -| `host` | The host used to generate links to issues and commits. See [conventional-changelog-writer#host](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-writer#host). | The host from the [`repositoryurl` option](https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#repositoryurl). | -| `linkCompare` | Whether to include a link to compare changes since previous release in the release note. | `true` | -| `linkReferences` | Whether to include a link to issues and commits in the release note. See [conventional-changelog-writer#linkreferences](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-writer#linkreferences). | `true` | -| `commit` | Keyword used to generate commit links (formatted as `////`). See [conventional-changelog-writer#commit](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-writer#commit). | `commits` for Bitbucket repositories, `commit` otherwise | -| `issue` | Keyword used to generate issue links (formatted as `////`). See [conventional-changelog-writer#issue](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-writer#issue). | `issue` for Bitbucket repositories, `issues` otherwise | -| `presetConfig` | Additional configuration passed to the [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset. Used for example with [conventional-changelog-conventionalcommits](https://github.com/conventional-changelog/conventional-changelog-config-spec/blob/master/versions/2.0.0/README.md). | - | +| Option | Description | Default | +| ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `preset` | [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset (possible values: [`angular`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-angular), [`atom`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-atom), [`codemirror`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-codemirror), [`ember`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-ember), [`eslint`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-eslint), [`express`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-express), [`jquery`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-jquery), [`jshint`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-jshint), [`conventionalcommits`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-conventionalcommits)). | [`angular`](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-angular) | +| `config` | NPM package name of a custom [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset. | - | +| `parserOpts` | Additional [conventional-commits-parser](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser#conventionalcommitsparseroptions) options that will extends the ones loaded by `preset` or `config`. This is convenient to use a [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset with some customizations without having to create a new module. | - | +| `writerOpts` | Additional [conventional-commits-writer](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-writer#options) options that will extends the ones loaded by `preset` or `config`. This is convenient to use a [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset with some customizations without having to create a new module. | - | +| `host` | The host used to generate links to issues and commits. See [conventional-changelog-writer#host](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-writer#host). | The host from the [`repositoryurl` option](https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#repositoryurl). | +| `linkCompare` | Whether to include a link to compare changes since previous release in the release note. | `true` | +| `linkReferences` | Whether to include a link to issues and commits in the release note. See [conventional-changelog-writer#linkreferences](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-writer#linkreferences). | `true` | +| `commit` | Keyword used to generate commit links (formatted as `////`). See [conventional-changelog-writer#commit](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-writer#commit). | `commits` for Bitbucket repositories, `commit` otherwise | +| `issue` | Keyword used to generate issue links (formatted as `////`). See [conventional-changelog-writer#issue](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-writer#issue). | `issue` for Bitbucket repositories, `issues` otherwise | +| `presetConfig` | Additional configuration passed to the [conventional-changelog](https://github.com/conventional-changelog/conventional-changelog) preset. Used for example with [conventional-changelog-conventionalcommits](https://github.com/conventional-changelog/conventional-changelog-config-spec/blob/master/versions/2.0.0/README.md). | - | +| `transformCommits` | Function or requirable NPM package with a function to transform commits before generating release notes. | **Notes**: in order to use a `preset` it must be installed (for example to use the [eslint preset](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-eslint) you must install it with `npm install conventional-changelog-eslint -D`) diff --git a/index.js b/index.js index 8cbd5f8f..3d065b15 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,26 @@ const debug = require('debug')('semantic-release:release-notes-generator'); const loadChangelogConfig = require('./lib/load-changelog-config'); const HOSTS_CONFIG = require('./lib/hosts-config'); +/** + * Get commit transformation function. A default passtru function is returned + * when no transform is configured. + * + * @param {Object} pluginConfig The plugin configuration. + * @param {Function|string} [pluginConfig.transformCommits] Function or requirable npm package with function to transform commits. + * + * @returns {Function} A function to transform commits with. + */ +function getTransform(pluginConfig) { + const passthru = c => c; + const {transformCommits = passthru} = pluginConfig; + + if (typeof transformCommits === 'string') { + return require(transformCommits); + } + + return transformCommits; +} + /** * Generate the changelog for all the commits in `options.commits`. * @@ -18,13 +38,14 @@ const HOSTS_CONFIG = require('./lib/hosts-config'); * @param {String} pluginConfig.config Requierable npm package with a custom conventional-changelog preset * @param {Object} pluginConfig.parserOpts Additional `conventional-changelog-parser` options that will overwrite ones loaded by `preset` or `config`. * @param {Object} pluginConfig.writerOpts Additional `conventional-changelog-writer` options that will overwrite ones loaded by `preset` or `config`. + * @param {Function|string} [pluginConfig.transformCommits] Function or requirable npm package with function to transform commits. * @param {Object} context The semantic-release context. * @param {Array} context.commits The commits to analyze. * @param {Object} context.lastRelease The last release with `gitHead` corresponding to the commit hash used to make the last release and `gitTag` corresponding to the git tag associated with `gitHead`. * @param {Object} context.nextRelease The next release with `gitHead` corresponding to the commit hash used to make the release, the release `version` and `gitTag` corresponding to the git tag associated with `gitHead`. * @param {Object} context.options.repositoryUrl The git repository URL. * - * @returns {String} The changelog for all the commits in `context.commits`. + * @returns {Promise} The changelog for all the commits in `context.commits`. */ async function generateNotes(pluginConfig, context) { const {commits, lastRelease, nextRelease, options, cwd} = context; @@ -41,6 +62,7 @@ async function generateNotes(pluginConfig, context) { const {issue, commit, referenceActions, issuePrefixes} = find(HOSTS_CONFIG, conf => conf.hostname === hostname) || HOSTS_CONFIG.default; + const transformCommits = getTransform(pluginConfig); const parsedCommits = filter( commits .filter(({message, hash}) => { @@ -51,10 +73,12 @@ async function generateNotes(pluginConfig, context) { return true; }) - .map(rawCommit => ({ - ...rawCommit, - ...parser(rawCommit.message, {referenceActions, issuePrefixes, ...parserOpts}), - })) + .map(rawCommit => + transformCommits({ + ...rawCommit, + ...parser(rawCommit.message, {referenceActions, issuePrefixes, ...parserOpts}), + }) + ) ); const previousTag = lastRelease.gitTag || lastRelease.gitHead; const currentTag = nextRelease.gitTag || nextRelease.gitHead; diff --git a/test/integration.test.js b/test/integration.test.js index c1bd39f4..1a184461 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -639,3 +639,43 @@ test('ReThrow error from "conventional-changelog"', async t => { {message: 'Test error'} ); }); + +test('Accept a "transformCommits" function as option', async t => { + const commits = [ + {hash: '111', message: 'fix: First fix'}, + {hash: '222', message: 'fix: Second fix'}, + ]; + const transformCommits = commit => { + commit.scope = commit.hash; + return commit; + }; + + const pluginConfig = {transformCommits}; + const options = {repositoryUrl}; + const changelog = await generateNotes(pluginConfig, {cwd, options, lastRelease, nextRelease, commits}); + + t.regex(changelog, new RegExp(escape('* **111:** First fix ([111](https://github.com/owner/repo/commit/111))'))); + t.regex(changelog, new RegExp(escape('* **222:** Second fix ([222](https://github.com/owner/repo/commit/222))'))); +}); + +test('Accept a "transformCommits" string as option', async t => { + const commits = [ + {hash: '111', message: 'fix: First fix'}, + {hash: '222', message: 'fix: Second fix'}, + ]; + function transformCommits(commit) { + commit.scope = commit.hash; + return commit; + } + + transformCommits['@noCallThru'] = true; + + const moduleName = 'mock-transform'; + const {generateNotes} = proxyquire('..', {[moduleName]: transformCommits}); + const pluginConfig = {transformCommits: moduleName}; + const options = {repositoryUrl}; + const changelog = await generateNotes(pluginConfig, {cwd, options, lastRelease, nextRelease, commits}); + + t.regex(changelog, new RegExp(escape('* **111:** First fix ([111](https://github.com/owner/repo/commit/111))'))); + t.regex(changelog, new RegExp(escape('* **222:** Second fix ([222](https://github.com/owner/repo/commit/222))'))); +});