Skip to content

Commit 07bccb6

Browse files
authored
bail if new tag does not directly follow previous version (video-dev#4855)
* bail if new tag does not directly follow previous version This is to try and prevent typos that could trigger an incorrect version publish into the future that cannot be undone. refs video-dev#4841 * do not bail if tag exists, because it would for real tags
1 parent 68503b1 commit 07bccb6

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎scripts/set-package-version.js‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable no-console */
2+
/* eslint-env node */
13
'use strict';
24

35
const fs = require('fs');
@@ -61,6 +63,18 @@ try {
6163
`New version "${newVersion}" is not >= latest version "${latestVersion}" on this branch.`
6264
);
6365
}
66+
67+
const foundPreviousVersion = versionParser
68+
.getPotentialPreviousStableVersions(`v${newVersion}`)
69+
.every((potentialPreviousVersion) =>
70+
hasTag(`v${potentialPreviousVersion}`)
71+
);
72+
if (!foundPreviousVersion) {
73+
throw new Error(
74+
'Could not find a previous version. The tag must follow a previous stable version number.'
75+
);
76+
}
77+
6478
packageJson.version = newVersion;
6579
fs.writeFileSync('./package.json', JSON.stringify(packageJson), {
6680
encoding: 'utf8',
@@ -82,6 +96,7 @@ function getCommitHash() {
8296

8397
function getLatestVersionTag() {
8498
let commitish = '';
99+
// eslint-disable-next-line no-constant-condition
85100
while (true) {
86101
const tag = exec('git describe --tag --abbrev=0 --match="v*" ' + commitish);
87102
if (!tag) {
@@ -95,6 +110,18 @@ function getLatestVersionTag() {
95110
}
96111
}
97112

113+
function hasTag(tag) {
114+
try {
115+
exec(`git rev-parse "refs/tags/${tag}"`);
116+
return true;
117+
} catch (e) {
118+
return false;
119+
}
120+
}
121+
98122
function exec(cmd) {
99-
return require('child_process').execSync(cmd).toString().trim();
123+
return require('child_process')
124+
.execSync(cmd, { stdio: 'pipe' })
125+
.toString()
126+
.trim();
100127
}

‎scripts/version-parser.js‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const semver = require('semver');
44

5-
const VALID_VERSION_REGEX = /^v\d+\.\d+\.\d+(?:-([a-zA-Z][0-9a-zA-Z-]*))?/;
5+
const VALID_VERSION_REGEX =
6+
/^v(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z][0-9a-zA-Z-]*))?/;
67
const STABLE_VERSION_REGEX = /^v\d+\.\d+\.\d+$/;
78

89
module.exports = {
@@ -57,6 +58,22 @@ module.exports = {
5758
if (!match) {
5859
throw new Error('Invalid version.');
5960
}
60-
return match[1] || 'latest';
61+
return match[4] || 'latest';
62+
},
63+
getPotentialPreviousStableVersions: (version) => {
64+
const match = VALID_VERSION_REGEX.exec(version);
65+
if (!match) {
66+
throw new Error('Invalid version.');
67+
}
68+
69+
const major = parseInt(match[1]);
70+
const minor = parseInt(match[2]);
71+
const patch = parseInt(match[3]);
72+
73+
const versions = [];
74+
if (major > 0) versions.push(`${major - 1}.0.${major === 1 ? '1' : '0'}`);
75+
if (minor > 0) versions.push(`${major}.${minor - 1}.0`);
76+
if (patch > 0) versions.push(`${major}.${minor}.${patch - 1}`);
77+
return versions;
6178
},
6279
};

0 commit comments

Comments
 (0)