Skip to content

Commit 9faa42e

Browse files
author
Luna Wei
committed
Add getNextVersionFromTags
Summary: Changelog: [Internal] - Add getNextVersionFromTags to determine next release version off a release branch In more detail - this work is part of an effort to automate the release process by using a push to a release branch as a trigger to prepare, package and deploy react-native to npm from CircleCI This function is later used in `prepare-package-for-release` script in D32556610 to bump the version Reviewed By: cortinico, ShikaSD Differential Revision: D32556609 fbshipit-source-id: 7d93ead0b34318a58ffeb876715fbd34d6041f4e
1 parent 87592fb commit 9faa42e

2 files changed

Lines changed: 74 additions & 1 deletion

File tree

‎scripts/__tests__/version-utils-test.js‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,37 @@
77
* @format
88
*/
99

10-
const {parseVersion} = require('../version-utils');
10+
const {parseVersion, getNextVersionFromTags} = require('../version-utils');
11+
12+
let execResult = null;
13+
jest.mock('shelljs', () => ({
14+
exec: () => {
15+
return {
16+
stdout: execResult,
17+
};
18+
},
19+
}));
1120

1221
describe('version-utils', () => {
22+
describe('getNextVersionFromTags', () => {
23+
it('should increment last stable tag', () => {
24+
execResult =
25+
'v0.66.3\nv0.66.2\nv0.66.1\nv0.66.0-rc.4\nv0.66.0-rc.3\nv0.66.0-rc.2\nv0.66.0-rc.1\nv0.66.0-rc.0';
26+
expect(getNextVersionFromTags('0.66-stable')).toBe('0.66.4');
27+
});
28+
29+
it('should find last prerelease tag and increment', () => {
30+
execResult =
31+
'v0.66.0-rc.4\nv0.66.0-rc.3\nv0.66.0-rc.2\nv0.66.0-rc.1\nv0.66.0-rc.0';
32+
expect(getNextVersionFromTags('0.66-stable')).toBe('0.66.0-rc.5');
33+
});
34+
35+
it('should return rc.0 version if no previous tags', () => {
36+
execResult = '\n';
37+
expect(getNextVersionFromTags('0.66-stable')).toBe('0.66.0-rc.0');
38+
});
39+
});
40+
1341
describe('parseVersion', () => {
1442
it('should throw error if invalid match', () => {
1543
function testInvalidVersion() {

‎scripts/version-utils.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* @format
88
*/
99

10+
const {exec} = require('shelljs');
11+
1012
function parseVersion(versionStr) {
1113
const match = versionStr.match(/^v?((\d+)\.(\d+)\.(\d+)(?:-(.+))?)$/);
1214
if (!match) {
@@ -24,6 +26,49 @@ function parseVersion(versionStr) {
2426
};
2527
}
2628

29+
function getLatestVersionTag(branchVersion) {
30+
// Returns list of tags like ["v0.67.2", "v0.67.1", "v0.67.0-rc.3", "v0.67.0-rc.2", ...] in reverse lexical order
31+
const tags = exec(`git tag --list "v${branchVersion}*" --sort=-refname`, {
32+
silent: true,
33+
})
34+
.stdout.trim()
35+
.split('\n')
36+
.filter(tag => tag.length > 0);
37+
38+
// If there are no tags, return null
39+
if (tags.length === 0) {
40+
return null;
41+
}
42+
43+
// Return most recent tag (with the "v" prefix)
44+
return tags[0];
45+
}
46+
47+
function getNextVersionFromTags(branch) {
48+
// Assumption that branch names will follow pattern `{major}.{minor}-stable`
49+
// Ex. "0.67-stable" -> "0.67"
50+
const branchVersion = branch.replace('-stable', '');
51+
52+
// Get the latest version tag of the release branch
53+
const versionTag = getLatestVersionTag(branchVersion);
54+
55+
// If there are no tags , we assume this is the first pre-release
56+
if (versionTag == null) {
57+
return `${branchVersion}.0-rc.0`;
58+
}
59+
60+
const {major, minor, patch, prerelease} = parseVersion(versionTag);
61+
if (prerelease != null) {
62+
// prelease is of the form "rc.X"
63+
const prereleasePatch = parseInt(prerelease.slice(3), 10);
64+
return `${major}.${minor}.${patch}-rc.${prereleasePatch + 1}`;
65+
}
66+
67+
// If not prerelease, increment the patch version
68+
return `${major}.${minor}.${parseInt(patch, 10) + 1}`;
69+
}
70+
2771
module.exports = {
2872
parseVersion,
73+
getNextVersionFromTags,
2974
};

0 commit comments

Comments
 (0)