Skip to content

Commit

Permalink
feat: add the prepare plugin hook
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Committing or creating files in the `publish` plugin hook is not supported anymore and now must be done in the `prepare` hook

Plugins with a `publish` hook that makes a commit or create a file that can be committed must use the `prepare` hook.
  • Loading branch information
pvdlg committed Feb 19, 2018
1 parent 20246c0 commit c2beb64
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 61 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -86,6 +86,7 @@ After running the tests the command `semantic-release` will execute the followin
| Verify release | Verify the release conformity with the [verify release plugins](docs/usage/plugins.md#verifyrelease-plugin). |
| Generate notes | Generate release notes with the [generate notes plugin](docs/usage/plugins.md#generatenotes-plugin) for the commits added since the last release. |
| Create Git tag | Create a Git tag corresponding the new release version |
| Prepare | Prepare the release with the [prepare plugins](docs/usage/plugins.md#prepare-plugin). |
| Publish | Publish the release with the [publish plugins](docs/usage/plugins.md#publish-plugin). |
| Notify | Notify of new releases or errors with the [success](docs/usage/plugins.md#success-plugin) and [fail](docs/usage/plugins.md#fail-plugin) plugins. |

Expand Down
1 change: 1 addition & 0 deletions cli.js
Expand Up @@ -25,6 +25,7 @@ Usage:
.option('analyze-commits', {type: 'string', group: 'Plugins'})
.option('verify-release', {...stringList, group: 'Plugins'})
.option('generate-notes', {type: 'string', group: 'Plugins'})
.option('prepare', {...stringList, group: 'Plugins'})
.option('publish', {...stringList, group: 'Plugins'})
.option('success', {...stringList, group: 'Plugins'})
.option('fail', {...stringList, group: 'Plugins'})
Expand Down
6 changes: 4 additions & 2 deletions docs/extending/plugins-list.md
Expand Up @@ -9,6 +9,7 @@
- [fail](https://github.com/semantic-release/github#fail): Open a GitHub issue when a release fails
- [@semantic-release/npm](https://github.com/semantic-release/npm)
- [verifyConditions](https://github.com/semantic-release/npm#verifyconditions): Verify the presence and the validity of the npm authentication and release configuration
- [prepare](https://github.com/semantic-release/npm#prepare): Update the package.json version and create the npm package tarball
- [publish](https://github.com/semantic-release/npm#publish): Publish the package on the npm registry

## Official plugins
Expand All @@ -18,15 +19,16 @@
- [publish](https://github.com/semantic-release/gitlab#publish): Publish a [GitLab release](https://docs.gitlab.com/ce/workflow/releases.html)
- [@semantic-release/git](https://github.com/semantic-release/git)
- [verifyConditions](https://github.com/semantic-release/git#verifyconditions): Verify the presence and the validity of the Git authentication and release configuration
- [publish](https://github.com/semantic-release/git#publish): Push a release commit and tag, including configurable files
- [prepare](https://github.com/semantic-release/git#prepare): Push a release commit and tag, including configurable files
- [@semantic-release/changelog](https://github.com/semantic-release/changelog)
- [verifyConditions](https://github.com/semantic-release/changelog#verifyconditions): Verify the presence and the validity of the configuration
- [publish](https://github.com/semantic-release/changelog#publish): Create or update the changelog file in the local project repository
- [prepare](https://github.com/semantic-release/changelog#prepare): Create or update the changelog file in the local project repository
- [@semantic-release/exec](https://github.com/semantic-release/exec)
- [verifyConditions](https://github.com/semantic-release/exec#verifyconditions): Execute a shell command to verify if the release should happen
- [analyzeCommits](https://github.com/semantic-release/exec#analyzecommits): Execute a shell command to determine the type of release
- [verifyRelease](https://github.com/semantic-release/exec#verifyrelease): Execute a shell command to verifying a release that was determined before and is about to be published.
- [generateNotes](https://github.com/semantic-release/exec#analyzecommits): Execute a shell command to generate the release note
- [prepare](https://github.com/semantic-release/exec#prepare): Execute a shell command to prepare the release
- [publish](https://github.com/semantic-release/exec#publish): Execute a shell command to publish the release
- [success](https://github.com/semantic-release/exec#success): Execute a shell command to notify of a new release
- [fail](https://github.com/semantic-release/exec#fail): Execute a shell command to notify of a failed release
Expand Down
12 changes: 12 additions & 0 deletions docs/usage/configuration.md
Expand Up @@ -155,6 +155,18 @@ Define the [generate notes plugin](plugins.md#generatenotes-plugin).

See [Plugins configuration](plugins.md#configuration) for more details.

### prepare

Type: `Array`, `String`, `Object`

Default: `['@semantic-release/npm']`

CLI argument: `--prepare`

Define the list of [prepare plugins](plugins.md#prepare-plugin). Plugins will run in series, in the order defined in the `Array`.

See [Plugins configuration](plugins.md#configuration) for more details.

### publish

Type: `Array`, `String`, `Object`
Expand Down
8 changes: 8 additions & 0 deletions docs/usage/plugins.md
Expand Up @@ -28,6 +28,14 @@ Plugin responsible for generating release notes.

Default implementation: [@semantic-release/release-notes-generator](https://github.com/semantic-release/release-notes-generator).

### prepare plugin

Plugin responsible for preparing the release, including:
- Creating or updating files such as `package.json`, `CHANGELOG.md`, documentation or compiled assets.
- Create and push commits

Default implementation: [npm](https://github.com/semantic-release/npm#prepare).

### publish plugin

Plugin responsible for publishing the release.
Expand Down
36 changes: 17 additions & 19 deletions index.js
Expand Up @@ -12,7 +12,7 @@ const getCommits = require('./lib/get-commits');
const getLastRelease = require('./lib/get-last-release');
const {extractErrors} = require('./lib/utils');
const logger = require('./lib/logger');
const {unshallow, gitHead: getGitHead, tag, push, deleteTag} = require('./lib/git');
const {unshallow, gitHead: getGitHead, tag, push} = require('./lib/git');

marked.setOptions({renderer: new TerminalRenderer()});

Expand Down Expand Up @@ -41,7 +41,7 @@ async function run(options, plugins) {
await verify(options);

logger.log('Run automated release from branch %s', options.branch);

console.log(options);
logger.log('Call plugin %s', 'verify-conditions');
await plugins.verifyConditions({options, logger}, {settleAll: true});

Expand Down Expand Up @@ -79,26 +79,14 @@ async function run(options, plugins) {
logger.log('Call plugin %s', 'generateNotes');
nextRelease.notes = await plugins.generateNotes(generateNotesParam);

// 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(
logger.log('Call plugin %s', 'prepare');
await plugins.prepare(
{options, logger, lastRelease, commits, nextRelease},
{
getNextInput: async lastResult => {
const newGitHead = await getGitHead();
// If previous publish plugin has created a commit (gitHead changed)
// If previous prepare plugin has created a commit (gitHead changed)
if (lastResult.nextRelease.gitHead !== newGitHead) {
// Delete the previously created tag
await deleteTag(options.repositoryUrl, nextRelease.gitTag);
// Recreate the tag, referencing the new gitHead
logger.log('Create tag %s', nextRelease.gitTag);
await tag(nextRelease.gitTag);
await push(options.repositoryUrl, branch);

nextRelease.gitHead = newGitHead;
// Regenerate the release notes
logger.log('Call plugin %s', 'generateNotes');
Expand All @@ -107,11 +95,21 @@ async function run(options, plugins) {
// Call the next publish plugin with the updated `nextRelease`
return {options, logger, lastRelease, commits, nextRelease};
},
// Add nextRelease and plugin properties to published release
transform: (release, step) => ({...(isPlainObject(release) ? release : {}), ...nextRelease, ...step}),
}
);

// 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})}
);

await plugins.success(
{options, logger, lastRelease, commits, nextRelease, releases: castArray(releases)},
{settleAll: true}
Expand Down
6 changes: 6 additions & 0 deletions lib/definitions/plugins.js
Expand Up @@ -36,6 +36,12 @@ module.exports = {
error: 'ERELEASENOTESOUTPUT',
},
},
prepare: {
default: ['@semantic-release/npm'],
config: {
validator: conf => !conf || (isArray(conf) ? conf : [conf]).every(conf => validatePluginConfig(conf)),
},
},
publish: {
default: ['@semantic-release/npm', '@semantic-release/github'],
config: {
Expand Down
17 changes: 0 additions & 17 deletions lib/git.js
Expand Up @@ -115,22 +115,6 @@ async function push(origin, branch) {
await execa('git', ['push', '--tags', origin, `HEAD:${branch}`]);
}

/**
* Delete a tag locally and remotely.
*
* @param {String} origin The remote repository URL.
* @param {String} tagName The tag name to delete.
*/
async function deleteTag(origin, tagName) {
// Delete the local tag
let shell = await execa('git', ['tag', '-d', tagName], {reject: false});
debug('delete local tag', shell);

// Delete the tag remotely
shell = await execa('git', ['push', '--delete', origin, tagName], {reject: false});
debug('delete remote tag', shell);
}

/**
* Verify a tag name is a valid Git reference.
*
Expand All @@ -157,6 +141,5 @@ module.exports = {
verifyAuth,
tag,
push,
deleteTag,
verifyTagName,
};
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,7 +22,7 @@
"@semantic-release/commit-analyzer": "^5.0.0",
"@semantic-release/error": "^2.2.0",
"@semantic-release/github": "^4.1.0",
"@semantic-release/npm": "^3.1.0",
"@semantic-release/npm": "^3.2.0",
"@semantic-release/release-notes-generator": "^6.0.0",
"aggregate-error": "^1.0.0",
"chalk": "^2.3.0",
Expand Down
4 changes: 4 additions & 0 deletions test/cli.test.js
Expand Up @@ -53,6 +53,9 @@ test.serial('Pass options to semantic-release API', async t => {
'verify2',
'--generate-notes',
'notes',
'--prepare',
'prepare1',
'prepare2',
'--publish',
'publish1',
'publish2',
Expand All @@ -76,6 +79,7 @@ test.serial('Pass options to semantic-release API', async t => {
t.is(run.args[0][0].analyzeCommits, 'analyze');
t.deepEqual(run.args[0][0].verifyRelease, ['verify1', 'verify2']);
t.is(run.args[0][0].generateNotes, 'notes');
t.deepEqual(run.args[0][0].prepare, ['prepare1', 'prepare2']);
t.deepEqual(run.args[0][0].publish, ['publish1', 'publish2']);
t.deepEqual(run.args[0][0].success, ['success1', 'success2']);
t.deepEqual(run.args[0][0].fail, ['fail1', 'fail2']);
Expand Down
11 changes: 11 additions & 0 deletions test/definitions/plugins.test.js
Expand Up @@ -46,6 +46,17 @@ test('The "generateNotes" plugin, if defined, must be a single plugin definition
t.true(plugins.generateNotes.config.validator(() => {}));
});

test('The "prepare" plugin, if defined, must be a single or an array of plugins definition', t => {
t.false(plugins.verifyRelease.config.validator({}));
t.false(plugins.verifyRelease.config.validator({path: null}));

t.true(plugins.verifyRelease.config.validator({path: 'plugin-path.js'}));
t.true(plugins.verifyRelease.config.validator());
t.true(plugins.verifyRelease.config.validator('plugin-path.js'));
t.true(plugins.verifyRelease.config.validator(() => {}));
t.true(plugins.verifyRelease.config.validator([{path: 'plugin-path.js'}, 'plugin-path.js', () => {}]));
});

test('The "publish" plugin is mandatory, and must be a single or an array of plugins definition', t => {
t.false(plugins.publish.config.validator({}));
t.false(plugins.publish.config.validator({path: null}));
Expand Down
13 changes: 0 additions & 13 deletions test/git.test.js
Expand Up @@ -10,7 +10,6 @@ import {
push,
gitTags,
isGitRepo,
deleteTag,
verifyTagName,
} from '../lib/git';
import {
Expand Down Expand Up @@ -139,18 +138,6 @@ test.serial('Add tag on head commit', async t => {
await t.is(await gitCommitTag(commits[0].hash), 'tag_name');
});

test.serial('Delete a tag', async t => {
// Create a git repository with a remote, set the current working directory at the root of the repo
const repo = await gitRepo(true);
await gitCommits(['Test commit']);
await tag('tag_name');
await push(repo, 'master');

await deleteTag(repo, 'tag_name');
t.falsy(await gitTagHead('tag_name'));
t.falsy(await gitRemoteTagHead(repo, 'tag_name'));
});

test.serial('Push tag and commit to remote repository', async t => {
// Create a git repository with a remote, set the current working directory at the root of the repo
const repo = await gitRepo(true);
Expand Down

0 comments on commit c2beb64

Please sign in to comment.