From 710b0e64ab7539fb4d98e8bec4b51bea798a4f3f Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Thu, 20 Jun 2024 03:24:29 -0700 Subject: [PATCH] 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: * https://github.com/facebook/react-native/commit/375c88478c985976e1b59dfa7353042a18b4d132 * https://github.com/facebook/react-native/commit/609c7c05b13214f2a834b251de095243f8dcbf68 * 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 --- scripts/__tests__/npm-utils-test.js | 30 ++++++++++++++++++++++++++++- scripts/npm-utils.js | 20 ++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/scripts/__tests__/npm-utils-test.js b/scripts/__tests__/npm-utils-test.js index 7b4345256d5..8bf753470fe 100644 --- a/scripts/__tests__/npm-utils-test.js +++ b/scripts/__tests__/npm-utils-test.js @@ -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', + }); }); }); diff --git a/scripts/npm-utils.js b/scripts/npm-utils.js index 3a973a87f66..7594283d69f 100644 --- a/scripts/npm-utils.js +++ b/scripts/npm-utils.js @@ -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, );