forked from semantic-release/release-notes-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (58 loc) · 3.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const url = require('url');
const {find} = require('lodash');
const getStream = require('get-stream');
const intoStream = require('into-stream');
const gitUrlParse = require('git-url-parse');
const conventionalCommitsParser = require('conventional-commits-parser').sync;
const conventionalChangelogWriter = require('conventional-changelog-writer');
const debug = require('debug')('semantic-release:release-notes-generator');
const loadChangelogConfig = require('./lib/load-changelog-config');
const HOSTS_CONFIG = require('./lib/hosts-config');
/**
* Generate the changelog for all the commits in `options.commits`.
*
* @param {Object} [pluginConfig={}] semantic-release configuration.
* @param {String} pluginConfig.preset conventional-changelog preset ('angular', 'atom', 'codemirror', 'ember', 'eslint', 'express', 'jquery', 'jscs', 'jshint').
* @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 {Object} options semantic-release options.
* @param {Array<Object>} options.commits array of commits, each containing `hash` and `message`.
* @param {Object} options.lastRelease 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} options.nextRelease 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} options.options.repositoryUrl git repository URL.
*
* @returns {String} the changelog for all the commits in `options.commits`.
*/
async function releaseNotesGenerator(pluginConfig, {commits, lastRelease, nextRelease, options: {repositoryUrl}}) {
const {parserOpts, writerOpts} = await loadChangelogConfig(pluginConfig);
const {resource: hostname, port, name: repository, owner, protocols} = gitUrlParse(repositoryUrl);
const protocol = protocols.includes('https') ? 'https' : protocols.includes('http') ? 'http' : 'https';
const {issue, commit, referenceActions, issuePrefixes} =
find(HOSTS_CONFIG, conf => conf.hostname === hostname) || HOSTS_CONFIG.default;
const parsedCommits = commits.map(rawCommit => ({
...rawCommit,
...conventionalCommitsParser(rawCommit.message, {referenceActions, issuePrefixes, ...parserOpts}),
}));
const previousTag = lastRelease.gitTag || lastRelease.gitHead;
const currentTag = nextRelease.gitTag || nextRelease.gitHead;
const context = {
version: nextRelease.version,
host: url.format({protocol, hostname, port}),
owner,
repository,
previousTag,
currentTag,
linkCompare: currentTag && previousTag,
issue,
commit,
};
debug('version: %o', nextRelease.version);
debug('host: %o', hostname);
debug('owner: %o', owner);
debug('repository: %o', repository);
debug('previousTag: %o', previousTag);
debug('currentTag: %o', currentTag);
return getStream(intoStream.obj(parsedCommits).pipe(conventionalChangelogWriter(context, writerOpts)));
}
module.exports = releaseNotesGenerator;