Skip to content

Commit f4314c2

Browse files
Luna Weifacebook-github-bot
authored andcommitted
Extract logic from bump-oss-version specific to prod releases
Summary: Changelog: [Internal] - Extract logic from bump-oss-version specific to prod releases 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 The following diagram describes the context (what kind of releases we do, relevant scripts and what they do), the pre-existing process for the different types of release and how I've modified the process. {F683387103} This diff creates the `prepare-package-for-release` script referenced by extracting it out of `bump-oss-version` and leveraging `set-rn-version`. It adds some helper functions to `version-utils` with tests Reviewed By: sota000 Differential Revision: D32556610 fbshipit-source-id: eb4ddc787498744156f985ab6d205c5d160e279b
1 parent ea6e34d commit f4314c2

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

scripts/__tests__/version-utils-test.js

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

10-
const {parseVersion, getNextVersionFromTags} = require('../version-utils');
10+
const {
11+
parseVersion,
12+
getNextVersionFromTags,
13+
isTaggedLatest,
14+
isReleaseBranch,
15+
} = require('../version-utils');
1116

1217
let execResult = null;
1318
jest.mock('shelljs', () => ({
@@ -19,6 +24,30 @@ jest.mock('shelljs', () => ({
1924
}));
2025

2126
describe('version-utils', () => {
27+
describe('isReleaseBranch', () => {
28+
it('should identify as release branch', () => {
29+
expect(isReleaseBranch('v0.66-stable')).toBe(true);
30+
expect(isReleaseBranch('0.66-stable')).toBe(true);
31+
expect(isReleaseBranch('made-up-stuff-stable')).toBe(true);
32+
});
33+
it('should not identify as release branch', () => {
34+
expect(isReleaseBranch('main')).toBe(false);
35+
expect(isReleaseBranch('pull/32659')).toBe(false);
36+
});
37+
});
38+
describe('isTaggedLatest', () => {
39+
it('it should identify commit as tagged `latest`', () => {
40+
execResult = '6c19dc3266b84f47a076b647a1c93b3c3b69d2c5\n';
41+
expect(isTaggedLatest('6c19dc3266b84f47a076b647a1c93b3c3b69d2c5')).toBe(
42+
true,
43+
);
44+
});
45+
it('it should not identify commit as tagged `latest`', () => {
46+
execResult = '6c19dc3266b84f47a076b647a1c93b3c3b69d2c5\n';
47+
expect(isTaggedLatest('6c19dc3266b8')).toBe(false);
48+
});
49+
});
50+
2251
describe('getNextVersionFromTags', () => {
2352
it('should increment last stable tag', () => {
2453
execResult =
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @format
8+
*/
9+
10+
'use strict';
11+
12+
/**
13+
* This script prepares a release package to be pushed to npm
14+
* It will:
15+
* * It updates the version in json/gradle files and makes sure they are consistent between each other (set-rn-version)
16+
* * Updates podfile for RNTester
17+
* * Commits changes and tags with the next version based off of last version tag.
18+
* This in turn will trigger another CircleCI job to publish to npm
19+
*/
20+
const {echo, exec, exit} = require('shelljs');
21+
const yargs = require('yargs');
22+
const {
23+
isReleaseBranch,
24+
isTaggedLatest,
25+
getNextVersionFromTags,
26+
} = require('./version-utils');
27+
28+
const branch = process.env.CIRCLE_BRANCH;
29+
const currentCommit = process.env.CIRCLE_SHA1;
30+
31+
const argv = yargs.option('r', {
32+
alias: 'remote',
33+
default: 'origin',
34+
}).argv;
35+
36+
if (!isReleaseBranch(branch)) {
37+
console.error('This needs to be on a release branch');
38+
exit(1);
39+
}
40+
41+
// Progress the version by 1 using existing git tags
42+
const {version} = getNextVersionFromTags(branch);
43+
44+
if (exec(`node scripts/set-rn-version.js --to-version ${version}`).code) {
45+
echo(`Failed to set React Native version to ${version}`);
46+
exit(1);
47+
}
48+
49+
// Release builds should commit the version bumps, and create tags.
50+
echo('Updating RNTester Podfile.lock...');
51+
if (exec('source scripts/update_podfile_lock.sh && update_pods').code) {
52+
echo('Failed to update RNTester Podfile.lock.');
53+
echo('Fix the issue, revert and try again.');
54+
exit(1);
55+
}
56+
57+
// Check if this release has been tagged as latest
58+
const isLatest = isTaggedLatest(currentCommit);
59+
60+
// Make commit [0.21.0-rc] Bump version numbers
61+
if (exec(`git commit -a -m "[${version}] Bump version numbers"`).code) {
62+
echo('failed to commit');
63+
exit(1);
64+
}
65+
66+
// Since we just committed, if `isLatest`, move the tag to commit we just made
67+
// This tag will also update npm release as `latest`
68+
if (isLatest) {
69+
exec('git tag -d latest');
70+
exec(`git push ${remote} :latest`);
71+
exec('git tag latest');
72+
exec(`git push ${remote} latest`);
73+
}
74+
75+
// Add tag v0.21.0-rc.1
76+
if (exec(`git tag v${version}`).code) {
77+
echo(
78+
`failed to tag the commit with v${version}, are you sure this release wasn't made earlier?`,
79+
);
80+
echo('You may want to rollback the last commit');
81+
echo('git reset --hard HEAD~1');
82+
exit(1);
83+
}
84+
85+
// Push newly created tag
86+
let remote = argv.remote;
87+
exec(`git push ${remote} v${version}`);
88+
89+
exec(`git push ${remote} ${branch} --follow-tags`);
90+
91+
exit(0);

scripts/version-utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,21 @@ function getNextVersionFromTags(branch) {
6868
return `${major}.${minor}.${parseInt(patch, 10) + 1}`;
6969
}
7070

71+
function isReleaseBranch(branch) {
72+
return branch.endsWith('-stable');
73+
}
74+
75+
function isTaggedLatest(commitSha) {
76+
return (
77+
exec(`git rev-list -1 latest | grep ${commitSha}`, {
78+
silent: true,
79+
}).stdout.trim() === commitSha
80+
);
81+
}
82+
7183
module.exports = {
84+
isTaggedLatest,
7285
parseVersion,
7386
getNextVersionFromTags,
87+
isReleaseBranch,
7488
};

0 commit comments

Comments
 (0)