From 1c73abbbb68f67361dc8969603273cef805a7f95 Mon Sep 17 00:00:00 2001 From: Luna Wei Date: Fri, 3 Dec 2021 13:15:38 -0800 Subject: [PATCH] isTaggedVersion checks if the commit has a version tag on it Summary: Changelog: [Internal] Add a `isTaggedVersion` function to filter out commits from release automation. Reviewed By: sota000 Differential Revision: D32842035 fbshipit-source-id: 14bb262a1d2a96ffda87c759a3202c4f9a356141 --- scripts/__tests__/version-utils-test.js | 28 +++++++++++++++++++++++++ scripts/prepare-package-for-release.js | 11 ++++++++++ scripts/version-utils.js | 14 ++++++++++++- 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/scripts/__tests__/version-utils-test.js b/scripts/__tests__/version-utils-test.js index c9f3a1746ff951..d9a463f91a28e5 100644 --- a/scripts/__tests__/version-utils-test.js +++ b/scripts/__tests__/version-utils-test.js @@ -11,6 +11,7 @@ const { parseVersion, getNextVersionFromTags, isTaggedLatest, + isTaggedVersion, isReleaseBranch, } = require('../version-utils'); @@ -24,6 +25,33 @@ jest.mock('shelljs', () => ({ })); describe('version-utils', () => { + describe('isTaggedVersion', () => { + it('should return true on pre-release versions', () => { + execResult = 'v0.66.0-rc.3\nlatest\n\n'; + expect(isTaggedVersion('6c19dc3266b84f47a076b647a1c93b3c3b69d2c5')).toBe( + true, + ); + }); + it('should return true on release versions', () => { + execResult = 'latest\nv0.66.2\n\n'; + expect(isTaggedVersion('6c19dc3266b84f47a076b647a1c93b3c3b69d2c5')).toBe( + true, + ); + }); + it('should return false when no tags', () => { + execResult = '\n'; + expect(isTaggedVersion('6c19dc3266b84f47a076b647a1c93b3c3b69d2c5')).toBe( + false, + ); + }); + it('should return false on tags that are not versions', () => { + execResult = 'latest\n0.someother-made-up-tag\n\n'; + expect(isTaggedVersion('6c19dc3266b84f47a076b647a1c93b3c3b69d2c5')).toBe( + false, + ); + }); + }); + describe('isReleaseBranch', () => { it('should identify as release branch', () => { expect(isReleaseBranch('v0.66-stable')).toBe(true); diff --git a/scripts/prepare-package-for-release.js b/scripts/prepare-package-for-release.js index 1999e5c7895644..75553316ea49ed 100755 --- a/scripts/prepare-package-for-release.js +++ b/scripts/prepare-package-for-release.js @@ -11,6 +11,7 @@ /** * This script prepares a release package to be pushed to npm + * It is run by CircleCI on a push to a release branch * It will: * * It updates the version in json/gradle files and makes sure they are consistent between each other (set-rn-version) * * Updates podfile for RNTester @@ -22,6 +23,7 @@ const yargs = require('yargs'); const { isReleaseBranch, isTaggedLatest, + isTaggedVersion, getNextVersionFromTags, } = require('./version-utils'); @@ -33,6 +35,15 @@ const argv = yargs.option('r', { default: 'origin', }).argv; +// We do this check to prevent a loop of commit in this script to trigger the job again. +// I haven't figured out a way for CircleCI to filter out commits from CircleCI jobs +if (isTaggedVersion(currentCommit)) { + console.log( + 'Skip running prepare-package-for-release as this job was triggered from previous run of this script.', + ); + exit(0); +} + if (!isReleaseBranch(branch)) { console.error('This needs to be on a release branch'); exit(1); diff --git a/scripts/version-utils.js b/scripts/version-utils.js index fbd1b700acf8e0..807bd9a5fd2a1b 100644 --- a/scripts/version-utils.js +++ b/scripts/version-utils.js @@ -9,8 +9,10 @@ const {exec} = require('shelljs'); +const VERSION_REGEX = /^v?((\d+)\.(\d+)\.(\d+)(?:-(.+))?)$/; + function parseVersion(versionStr) { - const match = versionStr.match(/^v?((\d+)\.(\d+)\.(\d+)(?:-(.+))?)$/); + const match = versionStr.match(VERSION_REGEX); if (!match) { throw new Error( `You must pass a correctly formatted version; couldn't parse ${versionStr}`, @@ -72,6 +74,15 @@ function isReleaseBranch(branch) { return branch.endsWith('-stable'); } +function isTaggedVersion(commitSha) { + const tags = exec(`git tag --points-at ${commitSha}`, { + silent: true, + }) + .stdout.trim() + .split('\n'); + return tags.some(tag => !!tag.match(VERSION_REGEX)); +} + function isTaggedLatest(commitSha) { return ( exec(`git rev-list -1 latest | grep ${commitSha}`, { @@ -82,6 +93,7 @@ function isTaggedLatest(commitSha) { module.exports = { isTaggedLatest, + isTaggedVersion, parseVersion, getNextVersionFromTags, isReleaseBranch,