Skip to content

Commit

Permalink
fix: ignore commits out of convention instead of throwing (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunjae Lee authored Aug 17, 2019
1 parent 50c4813 commit b6fc850
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 26 deletions.
64 changes: 47 additions & 17 deletions packages/shipjs-lib/src/lib/util/__tests__/getNextVersion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ describe('getNextVersionFromCommitMessages', () => {
test: abc
chore: abc`;
const bodies = '';
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
const { version: actual } = getNextVersionFromCommitMessages(
version,
titles,
bodies
);
expect(actual).toBe('1.2.4');
});

Expand All @@ -28,7 +32,11 @@ describe('getNextVersionFromCommitMessages', () => {
test(abcdef): abc
chore(abcdefg): abc`;
const bodies = '';
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
const { version: actual } = getNextVersionFromCommitMessages(
version,
titles,
bodies
);
expect(actual).toBe('1.2.4');
});

Expand All @@ -43,7 +51,11 @@ describe('getNextVersionFromCommitMessages', () => {
chore(abcdefg): abc
feat(abcdefgh): abc`;
const bodies = '';
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
const { version: actual } = getNextVersionFromCommitMessages(
version,
titles,
bodies
);
expect(actual).toBe('1.3.0');
});

Expand All @@ -58,7 +70,11 @@ describe('getNextVersionFromCommitMessages', () => {
chore: abc
feat: abc`;
const bodies = '';
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
const { version: actual } = getNextVersionFromCommitMessages(
version,
titles,
bodies
);
expect(actual).toBe('1.3.0');
});

Expand All @@ -73,25 +89,36 @@ describe('getNextVersionFromCommitMessages', () => {
chore: abc
feat: abc`;
const bodies = 'BREAKING CHANGE: this breaks the previous behavior.';
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
const { version: actual } = getNextVersionFromCommitMessages(
version,
titles,
bodies
);
expect(actual).toBe('1.0.0');
});

it('gets a null with no commit messages', () => {
const version = '0.0.1';
const titles = '';
const bodies = '';
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
const { version: actual } = getNextVersionFromCommitMessages(
version,
titles,
bodies
);
expect(actual).toBe(null);
});

it('throws when there is a commit message out of convention', () => {
const version = '0.0.1';
const titles = `hello: abc`;
const bodies = '';
expect(() => {
getNextVersionFromCommitMessages(version, titles, bodies);
}).toThrow();
const { ignoredMessages } = getNextVersionFromCommitMessages(
version,
titles,
bodies
);
expect(ignoredMessages).toEqual([titles]);
});

it('increases version with postfixes', () => {
Expand All @@ -108,7 +135,11 @@ describe('getNextVersionFromCommitMessages', () => {
chore: abc
feat: abc`;
const bodies = '';
const actual = getNextVersionFromCommitMessages(version, titles, bodies);
const { version: actual } = getNextVersionFromCommitMessages(
version,
titles,
bodies
);
expect(actual).toBe(`0.0.1-${tag}.124`);
});
});
Expand All @@ -117,32 +148,31 @@ describe('getNextVersionFromCommitMessages', () => {
describe('getNextVersion', () => {
it('gets next version with patch updated', () => {
silentExec('./tests/bootstrap-examples/patch-version-up.sh');
const actual = getNextVersion('sandbox/patch-version-up');
const { version: actual } = getNextVersion('sandbox/patch-version-up');
expect(actual).toBe('0.0.2');
});

it('gets next version with minor updated', () => {
silentExec('./tests/bootstrap-examples/minor-version-up.sh');
const actual = getNextVersion('sandbox/minor-version-up');
const { version: actual } = getNextVersion('sandbox/minor-version-up');
expect(actual).toBe('0.1.0');
});

it('gets next version with major updated', () => {
silentExec('./tests/bootstrap-examples/major-version-up.sh');
const actual = getNextVersion('sandbox/major-version-up');
const { version: actual } = getNextVersion('sandbox/major-version-up');
expect(actual).toBe('1.0.0');
});

it('gets a null with no commit messages', () => {
silentExec('./tests/bootstrap-examples/empty.sh no-commit-log');
const actual = getNextVersion('sandbox/no-commit-log');
const { version: actual } = getNextVersion('sandbox/no-commit-log');
expect(actual).toBe(null);
});

it('throws when there is a commit message out of convention', () => {
silentExec('./tests/bootstrap-examples/out-of-convention.sh');
expect(() => {
getNextVersion('sandbox/out-of-convention');
}).toThrow();
const { ignoredMessages } = getNextVersion('sandbox/out-of-convention');
expect(ignoredMessages).toEqual(['hello: add a']);
});
});
16 changes: 9 additions & 7 deletions packages/shipjs-lib/src/lib/util/getNextVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import silentExec from '../shell/silentExec';

export function getNextVersionFromCommitMessages(version, titles, bodies) {
if (prerelease(version)) {
return inc(version, 'prerelease');
return { version: inc(version, 'prerelease') };
}
if (bodies.toUpperCase().includes(GIT_COMMIT_BREAKING_CHANGE)) {
return inc(version, 'major');
return { version: inc(version, 'major') };
}
let patch = false;
let minor = false;
const ignoredMessages = [];
titles.split('\n').forEach(rawTitle => {
const title = rawTitle.trim();
if (!title) {
Expand All @@ -26,24 +27,25 @@ export function getNextVersionFromCommitMessages(version, titles, bodies) {
}
const match = title.match(/(.*?)(\(.*?\))?:.*/);
if (!match || !match[1]) {
throw new Error(`Invalid commit message format.\n > ${title}`);
ignoredMessages.push(title);
return;
}
const prefix = match[1].toLowerCase();
if (GIT_COMMIT_PREFIX_PATCH.has(prefix)) {
patch = true;
} else if (GIT_COMMIT_PREFIX_MINOR.has(prefix)) {
minor = true;
} else {
throw new Error(`Out of convention.\n > ${title}`);
ignoredMessages.push(title);
}
});

if (minor) {
return inc(version, 'minor');
return { version: inc(version, 'minor'), ignoredMessages };
} else if (patch) {
return inc(version, 'patch');
return { version: inc(version, 'patch'), ignoredMessages };
} else {
return null;
return { version: null, ignoredMessages };
}
}

Expand Down
14 changes: 12 additions & 2 deletions packages/shipjs/src/step/prepare/getNextVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ import runStep from '../runStep';
export default ({ dir }) =>
runStep(
{ title: 'Calculating the next version.' },
({ print, error, exitProcess }) => {
const nextVersion = getNextVersion(dir);
({ print, warning, error, exitProcess }) => {
const { version: nextVersion, ignoredMessages = [] } = getNextVersion(
dir
);
if (ignoredMessages.length > 0) {
print(
warning(
'The following commit messages out of convention are ignored:'
)
);
ignoredMessages.forEach(message => print(` ${message}`));
}
if (nextVersion === null) {
print(error('Nothing to release!'));
exitProcess(1);
Expand Down

0 comments on commit b6fc850

Please sign in to comment.