Skip to content

Commit

Permalink
isTaggedVersion checks if the commit has a version tag on it
Browse files Browse the repository at this point in the history
Summary: Changelog: [Internal] Add a `isTaggedVersion` function to filter out commits from release automation.

Reviewed By: sota000

Differential Revision: D32842035

fbshipit-source-id: 14bb262a1d2a96ffda87c759a3202c4f9a356141
  • Loading branch information
Luna Wei committed Dec 6, 2021
1 parent 43eaf3c commit 1c73abb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
28 changes: 28 additions & 0 deletions scripts/__tests__/version-utils-test.js
Expand Up @@ -11,6 +11,7 @@ const {
parseVersion,
getNextVersionFromTags,
isTaggedLatest,
isTaggedVersion,
isReleaseBranch,
} = require('../version-utils');

Expand All @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions scripts/prepare-package-for-release.js
Expand Up @@ -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
Expand All @@ -22,6 +23,7 @@ const yargs = require('yargs');
const {
isReleaseBranch,
isTaggedLatest,
isTaggedVersion,
getNextVersionFromTags,
} = require('./version-utils');

Expand All @@ -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);
Expand Down
14 changes: 13 additions & 1 deletion scripts/version-utils.js
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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}`, {
Expand All @@ -82,6 +93,7 @@ function isTaggedLatest(commitSha) {

module.exports = {
isTaggedLatest,
isTaggedVersion,
parseVersion,
getNextVersionFromTags,
isReleaseBranch,
Expand Down

0 comments on commit 1c73abb

Please sign in to comment.