refactor: build plugin pipeline parameters at initialization

In addition, factorize the pipeline config function to avoid code duplication.
This commit is contained in:
Pierre Vanduynslager
2018-07-10 13:18:58 -04:00
parent eb26254b00
commit 24ce560065
7 changed files with 105 additions and 112 deletions
+11 -77
View File
@@ -1,4 +1,4 @@
const {template, isPlainObject} = require('lodash');
const {template} = require('lodash');
const marked = require('marked');
const TerminalRenderer = require('marked-terminal');
const envCi = require('env-ci');
@@ -15,7 +15,7 @@ const getGitAuthUrl = require('./lib/get-git-auth-url');
const logger = require('./lib/logger');
const {fetch, verifyAuth, isBranchUpToDate, gitHead: getGitHead, tag, push} = require('./lib/git');
const getError = require('./lib/get-error');
const {COMMIT_NAME, COMMIT_EMAIL, RELEASE_NOTES_SEPARATOR} = require('./lib/definitions/constants');
const {COMMIT_NAME, COMMIT_EMAIL} = require('./lib/definitions/constants');
marked.setOptions({renderer: new TerminalRenderer()});
@@ -72,21 +72,14 @@ async function run(options, plugins) {
logger.log('Run automated release from branch %s', options.branch);
logger.log('Call plugin %s', 'verify-conditions');
await plugins.verifyConditions({options, logger}, {settleAll: true});
await plugins.verifyConditions({options, logger});
await fetch(options.repositoryUrl);
const lastRelease = await getLastRelease(options.tagFormat, logger);
const commits = await getCommits(lastRelease.gitHead, options.branch, logger);
logger.log('Call plugin %s', 'analyze-commits');
const [type] = await plugins.analyzeCommits({
options,
logger,
lastRelease,
commits: commits.filter(commit => !/\[skip\s+release\]|\[release\s+skip\]/i.test(commit.message)),
});
const type = await plugins.analyzeCommits({options, logger, lastRelease, commits});
if (!type) {
logger.log('There are no relevant changes, so no new version is released.');
return;
@@ -94,87 +87,28 @@ async function run(options, plugins) {
const version = getNextVersion(type, lastRelease, logger);
const nextRelease = {type, version, gitHead: await getGitHead(), gitTag: template(options.tagFormat)({version})};
logger.log('Call plugin %s', 'verify-release');
await plugins.verifyRelease({options, logger, lastRelease, commits, nextRelease}, {settleAll: true});
await plugins.verifyRelease({options, logger, lastRelease, commits, nextRelease});
const generateNotesParam = {options, logger, lastRelease, commits, nextRelease};
if (options.dryRun) {
logger.log('Call plugin %s', 'generate-notes');
const notes = (await plugins.generateNotes(generateNotesParam, {
getNextInput: ({nextRelease, ...generateNotesParam}, notes) => ({
...generateNotesParam,
nextRelease: {
...nextRelease,
notes: `${nextRelease.notes ? `${nextRelease.notes}${RELEASE_NOTES_SEPARATOR}` : ''}${notes}`,
},
}),
}))
.filter(Boolean)
.join(RELEASE_NOTES_SEPARATOR);
const notes = await plugins.generateNotes(generateNotesParam);
logger.log('Release note for version %s:\n', nextRelease.version);
if (notes) {
process.stdout.write(`${marked(notes)}\n`);
}
} else {
logger.log('Call plugin %s', 'generateNotes');
nextRelease.notes = (await plugins.generateNotes(generateNotesParam, {
getNextInput: ({nextRelease, ...generateNotesParam}, notes) => ({
...generateNotesParam,
nextRelease: {
...nextRelease,
notes: `${nextRelease.notes ? `${nextRelease.notes}${RELEASE_NOTES_SEPARATOR}` : ''}${notes}`,
},
}),
}))
.filter(Boolean)
.join(RELEASE_NOTES_SEPARATOR);
logger.log('Call plugin %s', 'prepare');
await plugins.prepare(
{options, logger, lastRelease, commits, nextRelease},
{
getNextInput: async ({nextRelease, ...prepareParam}) => {
const newGitHead = await getGitHead();
// If previous prepare plugin has created a commit (gitHead changed)
if (nextRelease.gitHead !== newGitHead) {
nextRelease.gitHead = newGitHead;
// Regenerate the release notes
logger.log('Call plugin %s', 'generateNotes');
nextRelease.notes = (await plugins.generateNotes(
{nextRelease, ...prepareParam},
{
getNextInput: ({nextRelease, ...generateNotesParam}, notes) => ({
...generateNotesParam,
nextRelease: {
...nextRelease,
notes: `${nextRelease.notes ? `${nextRelease.notes}${RELEASE_NOTES_SEPARATOR}` : ''}${notes}`,
},
}),
}
))
.filter(Boolean)
.join(RELEASE_NOTES_SEPARATOR);
}
// Call the next publish plugin with the updated `nextRelease`
return {...prepareParam, nextRelease};
},
}
);
nextRelease.notes = await plugins.generateNotes(generateNotesParam);
await plugins.prepare({options, logger, lastRelease, commits, nextRelease});
// Create the tag before calling the publish plugins as some require the tag to exists
logger.log('Create tag %s', nextRelease.gitTag);
await tag(nextRelease.gitTag);
await push(options.repositoryUrl, branch);
logger.log('Call plugin %s', 'publish');
const releases = await plugins.publish(
{options, logger, lastRelease, commits, nextRelease},
// Add nextRelease and plugin properties to published release
{transform: (release, step) => ({...(isPlainObject(release) ? release : {}), ...nextRelease, ...step})}
);
const releases = await plugins.publish({options, logger, lastRelease, commits, nextRelease});
await plugins.success({options, logger, lastRelease, commits, nextRelease, releases}, {settleAll: true});
await plugins.success({options, logger, lastRelease, commits, nextRelease, releases});
logger.log('Published release: %s', nextRelease.version);
}
@@ -199,7 +133,7 @@ async function callFail(plugins, options, error) {
const errors = extractErrors(error).filter(error => error.semanticRelease);
if (errors.length > 0) {
try {
await plugins.fail({options, logger, errors}, {settleAll: true});
await plugins.fail({options, logger, errors});
} catch (err) {
logErrors(err);
}