Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option for custom release note header #349

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const fs = require('fs')
const fspath = require('path')
const {format} = require('url');
const {find, merge} = require('lodash');
const getStream = require('get-stream');
Expand All @@ -18,6 +20,7 @@ const HOSTS_CONFIG = require('./lib/hosts-config.js');
* @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 {String} pluginConfig.header Path to a text file appended to the head of the release notes.
* @param {Object} context The semantic-release context.
* @param {Array<Object>} 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`.
Expand Down Expand Up @@ -85,8 +88,36 @@ async function generateNotes(pluginConfig, context) {
debug('linkReferences: %o', changelogContext.linkReferences);
debug('issue: %o', changelogContext.issue);
debug('commit: %o', changelogContext.commit);

var res = await getStream(intoStream.object(parsedCommits).pipe(writer(changelogContext, writerOpts)));

if('header' in pluginConfig) {
var headerOpt = pluginConfig['header'];

return getStream(intoStream.object(parsedCommits).pipe(writer(changelogContext, writerOpts)));
if(headerOpt === fspath.basename(headerOpt)) {
res = headerOpt + '\n' + res;
} else {
if(fs.existsSync(pluginConfig['header'])) {
const header = fs.readFileSync(headerOpt, { encoding: 'utf8' });
res = header + '\n' + res;
}
}
}

if('footer' in pluginConfig) {
var footerOpt = pluginConfig['footer'];

if(footerOpt === fspath.basename(footerOpt)) {
res = res + '\n' + footerOpt
} else {
if(fs.existsSync(pluginConfig['footer'])) {
const footer = fs.readFileSync(footerOpt, { encoding: 'utf8' });
res = res + '\n' + footer
}
}
}

return res;
}

module.exports = {generateNotes};
40 changes: 40 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,43 @@ test('ReThrow error from "conventional-changelog"', async (t) => {
{message: 'Test error'}
);
});

test('Accept "header" file path option', async(t) => {
const commits = [
{hash: '111', message: 'fix(scope1): First fix'},
{hash: '222', message: 'feat(scope2): First feature'},
];

const releaseNotes = await generateNotes({header: './test/testHeader.md'}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits});
t.regex(releaseNotes, /### Test Header/);
});

test('Accept "header" string option', async(t) => {
const commits = [
{hash: '111', message: 'fix(scope1): First fix'},
{hash: '222', message: 'feat(scope2): First feature'},
];

const releaseNotes = await generateNotes({header: "### Test Header"}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits});
t.regex(releaseNotes, /### Test Header/);
});

test('Accept "footer" file path option', async(t) => {
const commits = [
{hash: '111', message: 'fix(scope1): First fix'},
{hash: '222', message: 'feat(scope2): First feature'},
];

const releaseNotes = await generateNotes({footer: './test/testHeader.md'}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits})
t.regex(releaseNotes, /### Test Header/);
});

test('Accept "footer" string option', async(t) => {
const commits = [
{hash: '111', message: 'fix(scope1): First fix'},
{hash: '222', message: 'feat(scope2): First feature'},
];

const releaseNotes = await generateNotes({footer: "### Test Footer"}, {cwd, options: {repositoryUrl}, lastRelease, nextRelease, commits});
t.regex(releaseNotes, /### Test Footer/);
});
1 change: 1 addition & 0 deletions test/testHeader.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### Test Header