From bcafc2903f0f3c58c867daddc9dd3a175bfc9e31 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Wed, 12 Jun 2024 15:44:44 +0100 Subject: [PATCH] Refactor scripts into a single one --- .github/actions/create-release/action.yml | 29 ++++++++ .github/actions/prepare_release/action.yml | 45 ------------ ...prepare-release.yml => create-release.yml} | 8 +-- scripts/releases/README.md | 9 +-- scripts/releases/compute-npm-tag.js | 64 ----------------- scripts/releases/create-release-commit.js | 72 +++++++++++++++++++ scripts/releases/validate-version.js | 60 ---------------- 7 files changed, 107 insertions(+), 180 deletions(-) create mode 100644 .github/actions/create-release/action.yml delete mode 100644 .github/actions/prepare_release/action.yml rename .github/workflows/{prepare-release.yml => create-release.yml} (92%) delete mode 100644 scripts/releases/compute-npm-tag.js create mode 100644 scripts/releases/create-release-commit.js delete mode 100644 scripts/releases/validate-version.js diff --git a/.github/actions/create-release/action.yml b/.github/actions/create-release/action.yml new file mode 100644 index 00000000000000..aabbc2453983fe --- /dev/null +++ b/.github/actions/create-release/action.yml @@ -0,0 +1,29 @@ +name: prepare_release +description: Prepare React Native release +runs: + using: composite + steps: + - name: Yarn install + shell: bash + run: yarn install --non-interactive + - name: Creating release commit + shell: bash + run: | + node scripts/releases/create-release-commit.js \ + --reactNativeVersion "${{ inputs.version }}" \ + --tagAsLatestRelease "${{ inputs.is_latest_on_npm }}" \ + --dryRun "${{ inputs.dry_run }}" + GIT_PAGER=cat git show HEAD + - name: Update "latest" tag if needed + shell: bash + if: ${{ inputs.tag == 'latest' }} + run: | + git tag -d "latest" + git push origin :latest + git tag -a "latest" -m "latest" + - name: Pushing release commit + shell: bash + if: ${{ inputs.dry_run == false }} + run: | + CURR_BRANCH="$(git branch --show-current)" + git push origin "$CURR_BRANCH" --follow-tags diff --git a/.github/actions/prepare_release/action.yml b/.github/actions/prepare_release/action.yml deleted file mode 100644 index fe0eaeac27a360..00000000000000 --- a/.github/actions/prepare_release/action.yml +++ /dev/null @@ -1,45 +0,0 @@ -name: prepare_release -description: Prepare React Native release -runs: - using: composite - steps: - - name: Yarn install - shell: bash - run: yarn install --non-interactive - - name: Validate version - shell: bash - run: node ./scripts/releases/validate-version.js --build-type release --version "${{ inputs.version }}" - - name: Versioning workspace packages - shell: bash - run: | - node scripts/releases/set-version "${{ inputs.version }}" --skip-react-native-version - - name: Versioning react-native package - shell: bash - run: | - node scripts/releases/set-rn-version.js -v "<< parameters.version >>" --build-type "release" - - name: Creating release commit - shell: bash - run: | - BRANCH="$(git branch --show-current)" - NPM_TAG="$(node ./scripts/releases/compute-npm-tag.js --version ${{ inputs.version }} --branch $BRANCH)" - - if [[ "${{ inputs.is-latest-on-npm }}" ]]; then - NPM_TAG="$(node ./scripts/releases/compute-npm-tag.js --is_latest_on_npm --version ${{ inputs.version }} --branch $BRANCH)" - fi - - git commit -a -m "Release ${{ inputs.version }}" -m "#publish-packages-to-npm&$NPM_TAG" - git tag -a "v${{ inputs.version }}" -m "v${{ inputs.version }}" - GIT_PAGER=cat git show HEAD - - name: Update "latest" tag if needed - shell: bash - if: ${{ inputs.tag == 'latest' }} - run: | - git tag -d "latest" - git push origin :latest - git tag -a "latest" -m "latest" - - name: Pushing release commit - shell: bash - if: ${{ inputs.dry_run == false }} - run: | - CURR_BRANCH="$(git branch --show-current)" - git push origin "$CURR_BRANCH" --follow-tags diff --git a/.github/workflows/prepare-release.yml b/.github/workflows/create-release.yml similarity index 92% rename from .github/workflows/prepare-release.yml rename to .github/workflows/create-release.yml index b976638a681eff..85e14987843cae 100644 --- a/.github/workflows/prepare-release.yml +++ b/.github/workflows/create-release.yml @@ -1,13 +1,13 @@ -name: Prepare Release +name: Create release on: workflow_dispatch: inputs: version: - description: 'The version of React Native we want to release' + description: 'The version of React Native we want to release. For example 0.75.0-rc.0' required: true type: string - is-latest-on-npm: + is_latest_on_npm: description: 'Whether we want to tag this release as latest on NPM' required: true type: boolean @@ -45,4 +45,4 @@ jobs: fi - name: Execute Prepare Release if: ${{ steps.check_stable_branch.outputs.ON_STABLE_BRANCH && !steps.check_if_tag_exists.outputs.TAG_EXISTS }} - uses: ./.github/actions/prepare_release + uses: ./.github/actions/create-release diff --git a/scripts/releases/README.md b/scripts/releases/README.md index 98e663a665c2d5..e929d6fea716d4 100644 --- a/scripts/releases/README.md +++ b/scripts/releases/README.md @@ -22,11 +22,6 @@ Updates relevant files in the `react-native` package and template to materialize Updates workspace dependencies in the template app`package.json` -### `validate-version` +### `create-release-commit` -Takes a version and a build type and validates whether the version passed is valid for the given build type -It is intended to use in CI - -### `compute-npm-tag` - -Takes a version, a branch and whether we want to explicitly tag the release as "latest" and outputs the most appropriate NPM tag for the release. +Creates a release commit to trigger a new release diff --git a/scripts/releases/compute-npm-tag.js b/scripts/releases/compute-npm-tag.js deleted file mode 100644 index 79616005b0d760..00000000000000 --- a/scripts/releases/compute-npm-tag.js +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - * @format - * @oncall react_native - */ - -const {parseVersion} = require('./utils/version-utils'); -const {parseArgs} = require('@pkgjs/parseargs'); - -const config = { - options: { - is_latest_on_npm: { - type: 'boolean', - short: 'l', - default: false, - }, - version: { - type: 'string', - short: 'v', - }, - branch: { - type: 'string', - short: 'b', - }, - help: {type: 'boolean'}, - }, -}; - -async function main() { - const { - values: {help, is_latest_on_npm: latest, version: version, branch: branch}, - } = parseArgs(config); - - if (help) { - console.log(` - Usage: node ./scripts/releases/compute-npm-tag.js [OPTIONS] - - Validates a version string for a given build type. - - Options: - --is-latest-on-npm Whether we need to publish this as latest on NPM or not. Defaults to false. - --version The new version string. - --branch The branch name. - `); - return; - } - - const {prerelease} = parseVersion(version, 'release'); - - const npmTag = latest ? 'latest' : !prerelease ? branch : 'next'; - - console.log(npmTag); - return; -} - -if (require.main === module) { - // eslint-disable-next-line no-void - void main(); -} diff --git a/scripts/releases/create-release-commit.js b/scripts/releases/create-release-commit.js new file mode 100644 index 00000000000000..dd091ef3e96ecc --- /dev/null +++ b/scripts/releases/create-release-commit.js @@ -0,0 +1,72 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow + * @format + * @oncall react_native + */ + +const {execSync} = require('child_process'); +const {getBranchName} = require('../scm-utils'); +const yargs = require('yargs'); +const setVersion = require('../releases/set-version'); +const {parseVersion} = require('./utils/version-utils'); + +async function main() { + const argv = await yargs + .option('reactNativeVersion', { + describe: 'The new React Native version.', + type: 'string', + required: true, + }) + .option('tagAsLatestRelease', { + describe: + 'Whether the version should be published as the latest version on npm.', + type: 'boolean', + default: false, + }) + .option('dryRun', { + description: 'Whether we should push the commit to github or not', + type: 'boolean', + default: true, + }).argv; + + const buildType = 'release'; + const version = argv.reactNativeVersion; + const latest = argv.tagAsLatestRelease; + const dryRun = argv.dryRun; + const branch = getBranchName(); + console.info(`Running on branch: ${branch}`); + + console.info('Validating version', version); + const {prerelease} = parseVersion(version, buildType); + + const npmTag = latest ? 'latest' : prerelease ? 'next' : branch; + console.info('NPM tag:', npmTag); + + console.info('Setting version for monorepo packages and react-native'); + await setVersion(version, false); // version, skip-react-native + + if (dryRun) { + console.info('Running in dry-run mode, skipping git commit'); + console.info( + `git commit -a -m "Release ${version}" -m "#publish-packages-to-npm&${npmTag}"`, + ); + console.info(`git tag -a v${version} -m "v${version}"`); + return; + } + + console.info('Committing to git'); + execSync( + `git commit -a -m "Release ${version}" -m "#publish-packages-to-npm&${npmTag}"`, + ); + execSync(`git tag -a v${version} -m "v${version}"`); +} + +if (require.main === module) { + // eslint-disable-next-line no-void + void main(); +} diff --git a/scripts/releases/validate-version.js b/scripts/releases/validate-version.js deleted file mode 100644 index 5c29f5a3c4dde0..00000000000000 --- a/scripts/releases/validate-version.js +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow - * @format - * @oncall react_native - */ - -const {parseVersion, validateBuildType} = require('./utils/version-utils'); -const {parseArgs} = require('@pkgjs/parseargs'); - -const config = { - options: { - 'build-type': { - type: 'string', - short: 'b', - }, - version: { - type: 'string', - short: 'v', - }, - help: {type: 'boolean'}, - }, -}; - -async function main() { - const { - values: {help, 'build-type': buildType, version: version}, - } = parseArgs(config); - - if (help) { - console.log(` - Usage: node ./scripts/releases/validate-version.js [OPTIONS] - - Validates a version string for a given build type. - - Options: - --build-type One of ['dry-run', 'nightly', 'release', 'prealpha']. - --version The new version string. - `); - return; - } - - if (!validateBuildType(buildType)) { - throw new Error(`Unsupported build type: ${buildType}`); - } - - parseVersion(version, 'release'); - - console.log(`Version ${version} is valid for ${buildType} build.`); - return; -} - -if (require.main === module) { - // eslint-disable-next-line no-void - void main(); -}