Skip to content

Commit

Permalink
fix(prepare): ask commit range when tag is missing (#471)
Browse files Browse the repository at this point in the history
* fix(prepare): ask commit range when tag is missing

* fix: update message
  • Loading branch information
Eunjae Lee authored Nov 30, 2019
1 parent f9e8712 commit f659c34
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 39 deletions.
4 changes: 3 additions & 1 deletion packages/shipjs/src/flow/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getAppName, loadConfig, getReleaseType } from 'shipjs-lib';
import printHelp from '../step/prepare/printHelp';
import printDryRunBanner from '../step/printDryRunBanner';
import validate from '../step/prepare/validate';
import getCommitRange from '../step/prepare/getCommitRange';
import validateMergeStrategy from '../step/prepare/validateMergeStrategy';
import pull from '../step/pull';
import fetchTags from '../step/prepare/fetchTags';
Expand Down Expand Up @@ -50,7 +51,8 @@ async function prepare({
pull({ remote, currentBranch: baseBranch, dir, dryRun });
fetchTags({ dir, dryRun });
push({ remote, currentBranch: baseBranch, dir, dryRun });
let { nextVersion } = getNextVersion({ currentVersion, dir });
const { commitRange } = getCommitRange({ currentVersion, dir });
let { nextVersion } = getNextVersion({ commitRange, currentVersion, dir });
nextVersion = await confirmNextVersion({
yes,
currentVersion,
Expand Down
20 changes: 2 additions & 18 deletions packages/shipjs/src/helper/__tests__/validateBeforePrepare.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import validate from '../validateBeforePrepare';
import { isWorkingTreeClean, getCurrentBranch, hasTag } from 'shipjs-lib';
import { isWorkingTreeClean, getCurrentBranch } from 'shipjs-lib';

const defaultOpts = {
baseBranches: ['master'],
Expand All @@ -9,7 +9,6 @@ describe('Validate', () => {
beforeEach(() => {
isWorkingTreeClean.mockImplementation(() => true);
getCurrentBranch.mockImplementation(() => 'master');
hasTag.mockImplementation(() => true);
});

it('returns error if working tree is not clean', () => {
Expand Down Expand Up @@ -40,26 +39,11 @@ describe('Validate', () => {
expect(result).toBe(true);
});

it('returns error if there is no git tag for current version', () => {
hasTag.mockImplementation(() => false);
const result = validate(defaultOpts);
expect(result).not.toBe(true);
expect(result.length).toBe(1);
expect(result[0]).toMatchInlineSnapshot(`"noTagForCurrentVersion"`);
});

it('does not return error if there is git tag for current version', () => {
hasTag.mockImplementation(() => true);
const result = validate(defaultOpts);
expect(result).toBe(true);
});

it('returns more than one error', () => {
isWorkingTreeClean.mockImplementation(() => false);
getCurrentBranch.mockImplementation(() => 'aaa');
hasTag.mockImplementation(() => false);
const result = validate(defaultOpts);
expect(result).not.toBe(true);
expect(result.length).toBe(3);
expect(result.length).toBe(2);
});
});
12 changes: 2 additions & 10 deletions packages/shipjs/src/helper/validateBeforePrepare.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import { isWorkingTreeClean, getCurrentBranch, hasTag } from 'shipjs-lib';
import { isWorkingTreeClean, getCurrentBranch } from 'shipjs-lib';

const WORKING_TREE_NOT_CLEAN = 'workingTreeNotClean';
const CURRENT_BRANCH_INCORRECT = 'currentBranchIncorrect';
const NO_TAG_FOR_CURRENT_VERSION = 'noTagForCurrentVersion';

export default function validateBeforePrepare({
dir,
currentTagName,
baseBranches,
} = {}) {
export default function validateBeforePrepare({ dir, baseBranches } = {}) {
const result = [];
if (!isWorkingTreeClean(dir)) {
result.push(WORKING_TREE_NOT_CLEAN);
}
if (baseBranches.indexOf(getCurrentBranch(dir)) === -1) {
result.push(CURRENT_BRANCH_INCORRECT);
}
if (!hasTag(currentTagName, dir)) {
result.push(NO_TAG_FOR_CURRENT_VERSION);
}
return result.length === 0 ? true : result;
}
2 changes: 0 additions & 2 deletions packages/shipjs/src/step/prepare/__tests__/validate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ describe('validate', () => {
validateBeforePrepare.mockImplementation(() => [
'workingTreeNotClean',
'currentBranchIncorrect',
'noTagForCurrentVersion',
]);
const output = [];
mockPrint(print, output);
Expand All @@ -57,7 +56,6 @@ describe('validate', () => {
"Failed to prepare a release for the following reason(s).",
" - The working tree is not clean.",
" - The current branch must be one of [\\"master\\",\\"legacy\\"]",
" - There is no git tag for the current version (v1.2.3)",
]
`);
});
Expand Down
38 changes: 38 additions & 0 deletions packages/shipjs/src/step/prepare/getCommitRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import inquirer from 'inquirer';
import { hasTag, silentExec } from 'shipjs-lib';
import runStep from '../runStep';
import { print } from '../../util';
import { info, warning } from '../../color';

export default async ({ currentVersion, dir }) =>
await runStep(
{ title: 'Getting a commit range for this release.' },
async () => {
let commitRange = `v${currentVersion}..HEAD`;
if (hasTag(`v${currentVersion}`, dir)) {
print(info(`✔ ${commitRange}`));
return { commitRange };
}

print(warning(`Git tag 'v${currentVersion}' doesn't exist.`));
const commits = silentExec(`git log --pretty=format:"%h%d %s"`, {
dir,
})
.toString()
.split('\n');
const { answer } = await inquirer.prompt([
{
type: 'list',
pageSize: 10,
name: 'answer',
message: 'Since which commit do you want to release?',
choices: commits.map((commit, index) => `${index + 1}) ${commit}`),
},
]);
const [, commit] = answer.split(' ');

commitRange = `${commit}..HEAD`;
print(info(`✔ ${commitRange}`));
return { commitRange };
}
);
4 changes: 2 additions & 2 deletions packages/shipjs/src/step/prepare/getNextVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import runStep from '../runStep';
import { print, exitProcess } from '../../util';
import { info, warning } from '../../color';

export default ({ currentVersion, dir }) =>
export default ({ commitRange, currentVersion, dir }) =>
runStep({ title: 'Calculating the next version.' }, () => {
const { version: nextVersion, ignoredMessages = [] } = getNextVersion(
`v${currentVersion}..HEAD`,
commitRange,
currentVersion,
dir
);
Expand Down
8 changes: 2 additions & 6 deletions packages/shipjs/src/step/prepare/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { getBaseBranches, validateBeforePrepare } from '../../helper';
import { print, exitProcess } from '../../util';
import { info, error } from '../../color';

function printValidationError({ result, currentVersion, baseBranches }) {
function printValidationError({ result, baseBranches }) {
const messageMap = {
workingTreeNotClean: 'The working tree is not clean.',
currentBranchIncorrect: `The current branch must be one of ${JSON.stringify(
baseBranches
)}`,
noTagForCurrentVersion: `There is no git tag for the current version (v${currentVersion})`,
};

print(error('Failed to prepare a release for the following reason(s).'));
Expand All @@ -25,23 +24,20 @@ export default ({ config, dir }) =>
title: 'Checking the current status.',
},
() => {
const { mergeStrategy, monorepo, getTagName } = config;
const { mergeStrategy, monorepo } = config;
const baseBranches = getBaseBranches({ mergeStrategy });
const currentVersion =
monorepo && monorepo.mainVersionFile
? getCurrentVersion(dir, monorepo.mainVersionFile)
: getCurrentVersion(dir);
const currentTagName = getTagName({ version: currentVersion });
const result = validateBeforePrepare({
dir,
currentTagName,
baseBranches,
});
const baseBranch = getCurrentBranch(dir);
if (result !== true) {
printValidationError({
result,
currentVersion,
baseBranches,
});
exitProcess(1);
Expand Down

0 comments on commit f659c34

Please sign in to comment.