Skip to content

Commit

Permalink
fix: harmonize parameters passed to getError
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Dec 18, 2018
1 parent 4aad9cd commit f96c660
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 70 deletions.
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -86,7 +86,7 @@ async function run(context, plugins) {
}
} catch (error) {
logger.error(`The command "${error.cmd}" failed with the error message ${error.stderr}.`);
throw getError('EGITNOPERMISSION', {options});
throw getError('EGITNOPERMISSION', context);
}

logger.success(`Allowed to push to the Git repository`);
Expand All @@ -98,8 +98,8 @@ async function run(context, plugins) {
context.releases = [];

await pEachSeries(releasesToAdd, async ({lastRelease, currentRelease, nextRelease}) => {
if (context.branch['merge-range'] && !semver.satisfies(nextRelease.version, context.branch['merge-range'])) {
errors.push(getError('EINVALIDMAINTENANCEMERGE', {nextRelease, branch: context.branch}));
if (context.branch.mergeRange && !semver.satisfies(nextRelease.version, context.branch.mergeRange)) {
errors.push(getError('EINVALIDMAINTENANCEMERGE', {...context, nextRelease}));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/branches/normalize.js
Expand Up @@ -51,7 +51,7 @@ function maintenance({maintenance, release}) {
tags,
range: getRange(min, max),
accept: diff ? RELEASE_TYPE.slice(0, RELEASE_TYPE.indexOf(diff)) : [],
'merge-range': getRange(maintenanceMin, getUpperBound(range)),
mergeRange: getRange(maintenanceMin, getUpperBound(range)),
};
});
}
Expand Down
42 changes: 13 additions & 29 deletions lib/definitions/errors.js
Expand Up @@ -29,27 +29,25 @@ Please make sure to add the \`repositoryUrl\` to the [semantic-release configura
'docs/usage/configuration.md'
)}).`,
}),
EGITNOPERMISSION: ({options}) => ({
EGITNOPERMISSION: ({options: {repositoryUrl}, branch: {name}}) => ({
message: 'The push permission to the Git repository is required.',
details: `**semantic-release** cannot push the version tag to the branch \`${
options.branch
}\` on remote Git repository with URL \`${options.repositoryUrl}\`.
details: `**semantic-release** cannot push the version tag to the branch \`${name}\` on remote Git repository with URL \`${repositoryUrl}\`.
Please refer to the [authentication configuration documentation](${linkify(
'docs/usage/ci-configuration.md#authentication'
)}) to configure the Git credentials on your CI environment and make sure the [repositoryUrl](${linkify(
'docs/usage/configuration.md#repositoryurl'
)}) is configured with a [valid Git URL](https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols).`,
}),
EINVALIDTAGFORMAT: ({tagFormat}) => ({
EINVALIDTAGFORMAT: ({options: {tagFormat}}) => ({
message: 'Invalid `tagFormat` option.',
details: `The [tagFormat](${linkify(
'docs/usage/configuration.md#tagformat'
)}) must compile to a [valid Git reference](https://git-scm.com/docs/git-check-ref-format#_description).
Your configuration for the \`tagFormat\` option is \`${stringify(tagFormat)}\`.`,
}),
ETAGNOVERSION: ({tagFormat}) => ({
ETAGNOVERSION: ({options: {tagFormat}}) => ({
message: 'Invalid `tagFormat` option.',
details: `The [tagFormat](${linkify(
'docs/usage/configuration.md#tagformat'
Expand Down Expand Up @@ -205,40 +203,26 @@ Your configuration for the problematic branch is \`${stringify(branch)}\`.`,
Your configuration for the problematic branches is \`${stringify(branches)}\`.`,
}),
EINVALIDNEXTVERSION: ({nextRelease, branch, commits, validBranches}) => ({
message: `The release \`${nextRelease.version}\` on branch \`${
branch.name
}\` cannot be published as it is out of range.`,
details: `Based on the releases published on other branches, only versions within the range \`${
branch.range
}\` can be published from branch \`${branch.name}\`.
EINVALIDNEXTVERSION: ({nextRelease: {version}, branch: {name, range}, commits, validBranches}) => ({
message: `The release \`${version}\` on branch \`${name}\` cannot be published as it is out of range.`,
details: `Based on the releases published on other branches, only versions within the range \`${range}\` can be published from branch \`${name}\`.
The following commit${commits.length > 1 ? 's are' : ' is'} responsible for the invalid release:
${commits.map(({commit: {short}, subject}) => `- ${subject} (${short})`).join('\n')}
${
commits.length > 1 ? 'Those commits' : 'This commit'
} should be moved to a valid branch with [git merge](https://git-scm.com/docs/git-merge) or [git cherry-pick](https://git-scm.com/docs/git-cherry-pick) and removed from branch \`${
branch.name
}\` with [git revert](https://git-scm.com/docs/git-revert) or [git reset](https://git-scm.com/docs/git-reset).
} should be moved to a valid branch with [git merge](https://git-scm.com/docs/git-merge) or [git cherry-pick](https://git-scm.com/docs/git-cherry-pick) and removed from branch \`${name}\` with [git revert](https://git-scm.com/docs/git-revert) or [git reset](https://git-scm.com/docs/git-reset).
A valid branch could be ${wordsList(validBranches.map(({name}) => `\`${name}\``))}.
See the [workflow configuration documentation](${linkify('docs/usage/workflow-configuration.md')}) for more details.`,
}),
EINVALIDMAINTENANCEMERGE: ({nextRelease, branch}) => ({
message: `The release \`${nextRelease.version}\` on branch \`${
branch.name
}\` cannot be published as it is out of range.`,
details: `Only releases within the range \`${branch['merge-range']}\` can be merged into the maintenance branch \`${
branch.name
}\` and published to the \`${nextRelease.channel}\` distribution channel.
The branch \`${
branch.name
}\` head should be [reset](https://git-scm.com/docs/git-reset) to a previous commit so the commit with tag \`${
nextRelease.gitTag
}\` is removed from the branch history.
EINVALIDMAINTENANCEMERGE: ({nextRelease: {channel, gitTag, version}, branch: {mergeRange, name}}) => ({
message: `The release \`${version}\` on branch \`${name}\` cannot be published as it is out of range.`,
details: `Only releases within the range \`${mergeRange}\` can be merged into the maintenance branch \`${name}\` and published to the \`${channel}\` distribution channel.
The branch \`${name}\` head should be [reset](https://git-scm.com/docs/git-reset) to a previous commit so the commit with tag \`${gitTag}\` is removed from the branch history.
See the [workflow configuration documentation](${linkify('docs/usage/workflow-configuration.md')}) for more details.`,
}),
Expand Down
2 changes: 1 addition & 1 deletion lib/get-releases-to-add.js
Expand Up @@ -34,7 +34,7 @@ module.exports = context => {
({channel, version}) =>
channel === higherBranch.channel &&
channel !== branch.channel &&
(branch.type !== 'maintenance' || semver.gte(version, getLowerBound(branch['merge-range'])))
(branch.type !== 'maintenance' || semver.gte(version, getLowerBound(branch.mergeRange)))
)
)
// Find ones that are not released on the building branch channel
Expand Down
11 changes: 8 additions & 3 deletions lib/verify.js
Expand Up @@ -3,7 +3,12 @@ const AggregateError = require('aggregate-error');
const {isGitRepo, verifyTagName} = require('./git');
const getError = require('./get-error');

module.exports = async ({cwd, env, options: {repositoryUrl, tagFormat, branches}}) => {
module.exports = async context => {
const {
cwd,
env,
options: {repositoryUrl, tagFormat, branches},
} = context;
const errors = [];

if (!(await isGitRepo({cwd, env}))) {
Expand All @@ -14,14 +19,14 @@ module.exports = async ({cwd, env, options: {repositoryUrl, tagFormat, branches}

// Verify that compiling the `tagFormat` produce a valid Git tag
if (!(await verifyTagName(template(tagFormat)({version: '0.0.0'})))) {
errors.push(getError('EINVALIDTAGFORMAT', {tagFormat}));
errors.push(getError('EINVALIDTAGFORMAT', context));
}

// Verify the `tagFormat` contains the variable `version` by compiling the `tagFormat` template
// with a space as the `version` value and verify the result contains the space.
// The space is used as it's an invalid tag character, so it's guaranteed to no be present in the `tagFormat`.
if ((template(tagFormat)({version: ' '}).match(/ /g) || []).length !== 1) {
errors.push(getError('ETAGNOVERSION', {tagFormat}));
errors.push(getError('ETAGNOVERSION', context));
}

branches.forEach(branch => {
Expand Down
58 changes: 27 additions & 31 deletions test/branches/normalize.test.js
Expand Up @@ -7,40 +7,38 @@ test('Maintenance branches - initial state', t => {
const maintenance = [{name: '1.x', channel: '1.x', tags: []}, {name: '1.1.x', tags: []}, {name: '1.2.x', tags: []}];
const release = [{name: 'master', tags: []}];
t.deepEqual(
normalize
.maintenance({maintenance, release})
.map(({type, name, range, accept, channel, 'merge-range': maintenanceRange}) => ({
type,
name,
range,
accept,
channel,
'merge-range': maintenanceRange,
})),
normalize.maintenance({maintenance, release}).map(({type, name, range, accept, channel, mergeRange}) => ({
type,
name,
range,
accept,
channel,
mergeRange,
})),
[
{
type: 'maintenance',
name: '1.1.x',
range: '>=1.1.0 <1.0.0',
accept: [],
channel: '1.1.x',
'merge-range': '>=1.1.0 <1.2.0',
mergeRange: '>=1.1.0 <1.2.0',
},
{
type: 'maintenance',
name: '1.2.x',
range: '>=1.2.0 <1.0.0',
accept: [],
channel: '1.2.x',
'merge-range': '>=1.2.0 <1.3.0',
mergeRange: '>=1.2.0 <1.3.0',
},
{
type: 'maintenance',
name: '1.x',
range: '>=1.3.0 <1.0.0',
accept: [],
channel: '1.x',
'merge-range': '>=1.3.0 <2.0.0',
mergeRange: '>=1.3.0 <2.0.0',
},
]
);
Expand All @@ -63,13 +61,13 @@ test('Maintenance branches - cap range to first release present on default branc
t.deepEqual(
normalize
.maintenance({maintenance, release})
.map(({type, name, range, accept, channel, 'merge-range': maintenanceRange}) => ({
.map(({type, name, range, accept, channel, mergeRange: maintenanceRange}) => ({
type,
name,
range,
accept,
channel,
'merge-range': maintenanceRange,
mergeRange: maintenanceRange,
})),
[
{
Expand All @@ -78,31 +76,31 @@ test('Maintenance branches - cap range to first release present on default branc
range: '>=1.1.1 <1.2.0',
accept: ['patch'],
channel: 'name',
'merge-range': '>=1.1.0 <1.2.0',
mergeRange: '>=1.1.0 <1.2.0',
},
{
type: 'maintenance',
name: '1.2.x',
range: '>=1.2.1 <1.3.0',
accept: ['patch'],
channel: '1.2.x',
'merge-range': '>=1.2.0 <1.3.0',
mergeRange: '>=1.2.0 <1.3.0',
},
{
type: 'maintenance',
name: '1.x',
range: '>=1.5.0 <1.6.0',
accept: ['patch'],
channel: '1.x',
'merge-range': '>=1.3.0 <2.0.0',
mergeRange: '>=1.3.0 <2.0.0',
},
{
type: 'maintenance',
name: '2.x.x',
range: '>=2.0.0 <1.6.0',
accept: [],
channel: '2.x.x',
'merge-range': '>=2.0.0 <3.0.0',
mergeRange: '>=2.0.0 <3.0.0',
},
]
);
Expand All @@ -116,32 +114,30 @@ test('Maintenance branches - cap range to default branch last release if all rel
const release = [{name: 'master', tags: toTags(['1.0.0', '1.2.0', '1.3.0', '2.0.0'])}];

t.deepEqual(
normalize
.maintenance({maintenance, release})
.map(({type, name, range, accept, channel, 'merge-range': maintenanceRange}) => ({
type,
name,
range,
accept,
channel,
'merge-range': maintenanceRange,
})),
normalize.maintenance({maintenance, release}).map(({type, name, range, accept, channel, mergeRange}) => ({
type,
name,
range,
accept,
channel,
mergeRange,
})),
[
{
type: 'maintenance',
name: '1.x',
range: '>=1.3.0 <2.0.0',
accept: ['patch', 'minor'],
channel: '1.x',
'merge-range': '>=1.0.0 <2.0.0',
mergeRange: '>=1.0.0 <2.0.0',
},
{
type: 'maintenance',
name: '2.x.x',
range: '>=2.0.0 <2.0.0',
accept: [],
channel: '2.x.x',
'merge-range': '>=2.0.0 <3.0.0',
mergeRange: '>=2.0.0 <3.0.0',
},
]
);
Expand Down
4 changes: 2 additions & 2 deletions test/get-releases-to-add.test.js
Expand Up @@ -7,7 +7,7 @@ test('Return versions merged from release to maintenance branch, excluding lower
name: '2.x',
channel: '2.x',
type: 'maintenance',
'merge-range': '>=2.0.0 <3.0.0',
mergeRange: '>=2.0.0 <3.0.0',
tags: [
{gitTag: 'v2.0.0@2.x', version: '2.0.0', channel: '2.x', gitHead: '111'},
{gitTag: 'v2.0.0', version: '2.0.0', gitHead: '111'},
Expand Down Expand Up @@ -255,7 +255,7 @@ test('Exclude versions merged from release to maintenance branch if they have th
name: '2.x',
channel: 'latest',
type: 'maintenance',
'merge-range': '>=2.0.0 <3.0.0',
mergeRange: '>=2.0.0 <3.0.0',
tags: [
{gitTag: 'v2.0.0', version: '2.0.0', gitHead: '111'},
{gitTag: 'v2.0.0', version: '2.0.0', gitHead: '111'},
Expand Down

0 comments on commit f96c660

Please sign in to comment.