Skip to content

Commit

Permalink
feat: return release info in publish and add success and fail h…
Browse files Browse the repository at this point in the history
…ooks (#11)
  • Loading branch information
pvdlg authored and gr2m committed Feb 13, 2018
1 parent 0ffe41d commit c0b9a50
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 6 deletions.
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);
});

0 comments on commit c0b9a50

Please sign in to comment.