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

feat(prepare): add a new hook getNextVersion #724

Merged
merged 2 commits into from
Mar 17, 2020
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
1 change: 1 addition & 0 deletions packages/shipjs-lib/src/lib/config/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default {
installCommand: ({ isYarn }) =>
isYarn ? 'yarn install --silent' : 'npm install',
versionUpdated: undefined, // ({ version, releaseType, dir, exec }) => {},
getNextVersion: undefined, // ({ revisionRange, commitTitles, commitBodies, currentVersion, dir }) => {},
beforeCommitChanges: undefined, // ({ nextVersion, releaseType, exec, dir }) => {},
getStagingBranchName: ({ nextVersion, releaseType }) =>
`releases/v${nextVersion}`,
Expand Down
2 changes: 1 addition & 1 deletion packages/shipjs/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ beforeEach(() => {
});

afterEach(() => {
jest.clearAllMocks();
jest.resetAllMocks();
});
7 changes: 6 additions & 1 deletion packages/shipjs/src/flow/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ async function prepare({
currentVersion,
dir,
});
let { nextVersion } = getNextVersion({ revisionRange, currentVersion, dir });
let { nextVersion } = getNextVersion({
config,
revisionRange,
currentVersion,
dir,
});
nextVersion = await confirmNextVersion({
yes,
currentVersion,
Expand Down
19 changes: 16 additions & 3 deletions packages/shipjs/src/step/prepare/__tests__/getNextVersion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,28 @@ describe('getNextVersion', () => {
version: '0.1.2',
ignoredMessages: [],
}));
const { nextVersion } = getNextVersionStep({});
const { nextVersion } = getNextVersionStep({ config: {} });
expect(nextVersion).toEqual('0.1.2');
});

it('returns next version by hook from config', () => {
getNextVersion.mockImplementationOnce(() => ({
version: '0.1.2',
ignoredMessages: [],
}));
const { nextVersion } = getNextVersionStep({
config: {
getNextVersion: () => '1.2.3',
},
});
expect(nextVersion).toEqual('1.2.3');
});

it('exits with nothing to release', () => {
getNextVersion.mockImplementationOnce(() => ({
version: null,
}));
getNextVersionStep({});
getNextVersionStep({ config: {} });
expect(exitProcess).toHaveBeenCalledTimes(1);
expect(exitProcess).toHaveBeenCalledWith(0);
});
Expand All @@ -29,7 +42,7 @@ describe('getNextVersion', () => {
version: '0.1.2',
ignoredMessages: ['hello world', 'foo bar', 'out of convention'],
}));
getNextVersionStep({});
getNextVersionStep({ config: {} });
expect(output).toMatchInlineSnapshot(`
Array [
"› Calculating the next version.",
Expand Down
38 changes: 27 additions & 11 deletions packages/shipjs/src/step/prepare/getNextVersion.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import { getNextVersion } from 'shipjs-lib';
import { getNextVersion, getCommitTitles, getCommitBodies } from 'shipjs-lib';
import runStep from '../runStep';
import { print, exitProcess } from '../../util';
import { info, warning } from '../../color';

export default ({ revisionRange, currentVersion, dir }) =>
export default ({ config, revisionRange, currentVersion, dir }) =>
runStep({ title: 'Calculating the next version.' }, () => {
const { version: nextVersion, ignoredMessages = [] } = getNextVersion(
revisionRange,
currentVersion,
dir
);
if (ignoredMessages.length > 0) {
print(
warning('The following commit messages out of convention are ignored:')
let nextVersion;
if (typeof config.getNextVersion === 'function') {
const commitTitles = getCommitTitles(revisionRange, dir);
const commitBodies = getCommitBodies(revisionRange, dir);
nextVersion = config.getNextVersion({
revisionRange,
commitTitles,
commitBodies,
currentVersion,
dir,
});
} else {
const { version, ignoredMessages = [] } = getNextVersion(
revisionRange,
currentVersion,
dir
);
ignoredMessages.forEach(message => print(` ${message}`));
if (ignoredMessages.length > 0) {
print(
warning(
'The following commit messages out of convention are ignored:'
)
);
ignoredMessages.forEach(message => print(` ${message}`));
}
nextVersion = version;
}
if (nextVersion === null) {
print(info('Nothing to release!'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ const getDefaultParams = ({
dryRun: false,
});

const createRelease = jest.fn().mockImplementation(() => ({
data: {
upload_url: 'https://dummy/upload/url', // eslint-disable-line camelcase
},
}));
const createRelease = jest.fn();
const uploadReleaseAsset = jest.fn();
Octokit.mockImplementation(function() {
this.repos = { createRelease, uploadReleaseAsset };
});

describe('createGitHubRelease', () => {
beforeEach(() => {
createRelease.mockImplementation(() => ({
data: {
upload_url: 'https://dummy/upload/url', // eslint-disable-line camelcase
},
}));

Octokit.mockImplementation(function() {
this.repos = { createRelease, uploadReleaseAsset };
});

getRepoInfo.mockImplementation(() => ({
owner: 'my',
name: 'repo',
Expand Down
20 changes: 20 additions & 0 deletions website/reference/all-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ installCommand: ({ isYarn }) =>
isYarn ? 'yarn install --silent' : 'npm install'`
```

## `getNextVersion`

_default:_ `undefined`

This hook determines what the next version should be. It returns the next version as string. If not given, by default, Ship.js follows [conventional commits](https://www.conventionalcommits.org).

- revisionRange: for example, `v0.1.0..HEAD`
- commitTitles: string of commit titles. Be aware that it's not an array of strings. It comes from `git log --pretty=format:%s`.
- commitBodies: string of commit bodies. Be aware that it's not an array of strings. It comes from `git log --pretty=format:%b`.
- currentVersion: for example, `0.1.0`
- dir: current working dir

```js
// example
getNextVersion: ({ revisionRange, commitTitles, commitBodies, currentVersion, dir }) => {
// do something
return nextVersion; // for example, `"0.2.0"`
}
```

## `versionUpdated`

_default:_ `undefined`
Expand Down