Port fixes to release scripts to use GITHUB_REF and GITHUB_REF_NAME variables (#45066)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45066

This changes ports in main the fixes we had to make to the release scripts to run properly in release mode.

This includes the commits:

* 375c88478c
* 609c7c05b1
* https://github.com/facebook/react-native/pull/45062

## Changelog:
[Internal] - Fix release scripts to run properly in release mode

Reviewed By: cortinico

Differential Revision: D58782925

fbshipit-source-id: b096909d5f8281809ee3c2a01eefda1d19f32936
This commit is contained in:
Riccardo Cipolleschi 2024-06-20 03:24:29 -07:00 committed by Facebook GitHub Bot
parent 90d4d55122
commit 710b0e64ab
2 changed files with 46 additions and 4 deletions

View File

@ -115,6 +115,12 @@ describe('npm-utils', () => {
});
describe('getNpmInfo', () => {
beforeEach(() => {
process.env.CIRCLE_TAG = '';
process.env.GITHUB_REF = '';
process.env.GITHUB_REF_NAME = '';
});
it('return the expected format for prealpha', () => {
const isoStringSpy = jest.spyOn(Date.prototype, 'toISOString');
isoStringSpy.mockReturnValue('2023-10-04T15:43:55.123Z');
@ -147,7 +153,29 @@ describe('npm-utils', () => {
version: `0.74.1-rc.0`,
tag: '--no-tag',
});
process.env.CIRCLE_TAG = null;
});
it('return the expected format for patch-prereleases on GHA', () => {
const isoStringSpy = jest.spyOn(Date.prototype, 'toISOString');
isoStringSpy.mockReturnValue('2023-10-04T15:43:55.123Z');
getCurrentCommitMock.mockImplementation(() => 'abcd1234');
// exitIfNotOnGit takes a function as a param and it:
// 1. checks if we are on git => if not it exits
// 2. run the function passed as a param and return the output to the caller
// For the mock, we are assuming we are on github and we are returning `false`
// as the `getNpmInfo` function will pass a function that checks if the
// current commit is a tagged with 'latest'.
// In the Mock, we are assuming that we are on git (it does not exits) and the
// checkIfLatest function returns `false`
exitIfNotOnGitMock.mockImplementation(() => false);
process.env.GITHUB_REF = 'refs/tags/v0.74.1-rc.0';
process.env.GITHUB_REF_NAME = 'v0.74.1-rc.0';
const returnedValue = getNpmInfo('release');
expect(returnedValue).toMatchObject({
version: `0.74.1-rc.0`,
tag: '--no-tag',
});
});
});

View File

@ -89,14 +89,28 @@ function getNpmInfo(buildType /*: BuildType */) /*: NpmInfo */ {
}
if (buildType === 'release') {
if (process.env.CIRCLE_TAG == null) {
let versionTag /*: string*/ = '';
if (process.env.CIRCLE_TAG != null && process.env.CIRCLE_TAG !== '') {
versionTag = process.env.CIRCLE_TAG;
} else if (
process.env.GITHUB_REF != null &&
process.env.GITHUB_REF.includes('/tags/') &&
process.env.GITHUB_REF_NAME != null &&
process.env.GITHUB_REF_NAME !== ''
) {
// GITHUB_REF contains the fully qualified ref, for example refs/tags/v0.75.0-rc.0
// GITHUB_REF_NAME contains the short name, for example v0.75.0-rc.0
versionTag = process.env.GITHUB_REF_NAME;
}
if (versionTag === '') {
throw new Error(
'CIRCLE_TAG is not set for release. This should only be run in CircleCI. See https://circleci.com/docs/variables/ for how CIRCLE_TAG is set.',
'No version tag found in CI. It looks like this script is running in release mode, but the CIRCLE_TAG or the GITHUB_REF_NAME are missing.',
);
}
const {version, major, minor, patch, prerelease} = parseVersion(
process.env.CIRCLE_TAG,
versionTag,
buildType,
);