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

Return release info in publish and add success and fail hooks #11

Merged
merged 4 commits into from
Feb 13, 2018
Merged
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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,26 @@ Execute a shell command to generate the release note.

Execute a shell command to publish the release.

| Command property | Description |
|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
| `exit code` | Any non `0` code is considered as an unexpected error and will stop the `semantic-release` execution with an error. |
| `stdout` | Only the `release` information must be written to `stdout` as parseable JSON (for example `{"name": "Release name", "url": "http://url/release/1.0.0"}`). |
| `stderr` | Can be used for logging. |

## success

Execute a shell command to notify of a successful release.

| Command property | Description |
|------------------|---------------------------------------------------------------------------------------------------------------------|
| `exit code` | Any non `0` code is considered as an unexpected error and will stop the `semantic-release` execution with an error. |
| `stdout` | Can be used for logging. |
| `stderr` | Can be used for logging. |

## fail

Execute a shell command to notify of a failed release.

| Command property | Description |
|------------------|---------------------------------------------------------------------------------------------------------------------|
| `exit code` | Any non `0` code is considered as an unexpected error and will stop the `semantic-release` execution with an error. |
Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {castArray, isPlainObject} = require('lodash');
const parseJson = require('parse-json');
const SemanticReleaseError = require('@semantic-release/error');
const execScript = require('./lib/exec-script');
const verifyConfig = require('./lib/verify-config');
Expand Down Expand Up @@ -46,7 +47,16 @@ async function generateNotes(pluginConfig, params) {
}

async function publish(pluginConfig, params) {
const stdout = await execScript(pluginConfig, params);
return stdout.trim() ? parseJson(stdout) : undefined;
}

async function success(pluginConfig, params) {
await execScript(pluginConfig, params);
}

async function fail(pluginConfig, params) {
await execScript(pluginConfig, params);
}

module.exports = {verifyConditions, analyzeCommits, verifyRelease, generateNotes, publish};
module.exports = {verifyConditions, analyzeCommits, verifyRelease, generateNotes, publish, success, fail};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"@semantic-release/error": "^2.1.0",
"debug": "^3.1.0",
"execa": "^0.9.0",
"lodash": "^4.17.4"
"lodash": "^4.17.4",
"parse-json": "^4.0.0"
},
"devDependencies": {
"ava": "^0.25.0",
Expand Down
29 changes: 29 additions & 0 deletions test/fail.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import test from 'ava';
import {stub} from 'sinon';
import {fail} from '..';

stub(process.stdout, 'write');
stub(process.stderr, 'write');

test.beforeEach(t => {
// Mock logger
t.context.log = stub();
t.context.error = stub();
t.context.logger = {log: t.context.log, error: t.context.error};
});

test.serial('Return the value fail script wrote to stdout', async t => {
const pluginConfig = {
cmd: './test/fixtures/echo-args.sh',
};
const params = {logger: t.context.logger};

await t.notThrows(fail(pluginConfig, params));
});

test.serial('Throw "Error" if the fail script does not returns 0', async t => {
const pluginConfig = {cmd: 'exit 1'};
const params = {logger: t.context.logger};

await t.throws(fail(pluginConfig, params), Error);
});
32 changes: 28 additions & 4 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,35 @@ test.beforeEach(t => {
t.context.logger = {log: t.context.log, error: t.context.error};
});

test.serial('Return if the publish script returns 0', async t => {
const pluginConfig = {cmd: 'exit 0'};
const params = {logger: t.context.logger, options: {}};
test.serial('Parse JSON returned by publish script', async t => {
const pluginConfig = {
cmd:
'./test/fixtures/echo-args.sh {\\"name\\": \\"Release name\\", \\"url\\": \\"https://host.com/release/1.0.0\\"}',
};
const params = {logger: t.context.logger};

const result = await publish(pluginConfig, params);
t.deepEqual(result, {name: 'Release name', url: 'https://host.com/release/1.0.0'});
});

test.serial('Return "undefined" if the publish script wrtite nothing to stdout', async t => {
const pluginConfig = {
cmd: './test/fixtures/echo-args.sh',
};
const params = {logger: t.context.logger};

const result = await publish(pluginConfig, params);
t.is(result, undefined);
});

test.serial('Throw JSONError if publish script write invalid JSON to stdout', async t => {
const pluginConfig = {
cmd: './test/fixtures/echo-args.sh invalid_json',
};
const params = {logger: t.context.logger};

await t.notThrows(publish(pluginConfig, params));
const error = await t.throws(publish(pluginConfig, params));
t.is(error.name, 'JSONError');
});

test.serial('Throw "Error" if the publish script does not returns 0', async t => {
Expand Down
29 changes: 29 additions & 0 deletions test/success.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import test from 'ava';
import {stub} from 'sinon';
import {success} from '..';

stub(process.stdout, 'write');
stub(process.stderr, 'write');

test.beforeEach(t => {
// Mock logger
t.context.log = stub();
t.context.error = stub();
t.context.logger = {log: t.context.log, error: t.context.error};
});

test.serial('Return the value success script wrote to stdout', async t => {
const pluginConfig = {
cmd: './test/fixtures/echo-args.sh',
};
const params = {logger: t.context.logger};

await t.notThrows(success(pluginConfig, params));
});

test.serial('Throw "Error" if the success script does not returns 0', async t => {
const pluginConfig = {cmd: 'exit 1'};
const params = {logger: t.context.logger};

await t.throws(success(pluginConfig, params), Error);
});