Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: diff when detecting major change from prerelease #566

Merged
merged 1 commit into from Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 31 additions & 20 deletions functions/diff.js
Expand Up @@ -13,6 +13,35 @@ const diff = (version1, version2) => {
const highVersion = v1Higher ? v1 : v2
const lowVersion = v1Higher ? v2 : v1
const highHasPre = !!highVersion.prerelease.length
const lowHasPre = !!lowVersion.prerelease.length

if (lowHasPre && !highHasPre) {
// Going from prerelease -> no prerelease requires some special casing

// If the low version has only a major, then it will always be a major
// Some examples:
// 1.0.0-1 -> 1.0.0
// 1.0.0-1 -> 1.1.1
// 1.0.0-1 -> 2.0.0
if (!lowVersion.patch && !lowVersion.minor) {
return 'major'
}

// Otherwise it can be determined by checking the high version

if (highVersion.patch) {
// anything higher than a patch bump would result in the wrong version
return 'patch'
}

if (highVersion.minor) {
// anything higher than a minor bump would result in the wrong version
return 'minor'
}

// bumping major/minor/patch all have same result
return 'major'
}

// add the `pre` prefix if we are going to a prerelease version
const prefix = highHasPre ? 'pre' : ''
Expand All @@ -29,26 +58,8 @@ const diff = (version1, version2) => {
return prefix + 'patch'
}

// at this point we know stable versions match but overall versions are not equal,
// so either they are both prereleases, or the lower version is a prerelease

if (highHasPre) {
// high and low are preleases
return 'prerelease'
}

if (lowVersion.patch) {
// anything higher than a patch bump would result in the wrong version
return 'patch'
}

if (lowVersion.minor) {
// anything higher than a minor bump would result in the wrong version
return 'minor'
}

// bumping major/minor/patch all have same result
return 'major'
// high and low are preleases
return 'prerelease'
}

module.exports = diff
2 changes: 2 additions & 0 deletions test/functions/diff.js
Expand Up @@ -26,6 +26,8 @@ test('diff versions test', (t) => {
['0.0.2-1', '1.0.0', 'major'],
['0.1.0-1', '0.1.0', 'minor'],
['1.0.0-1', '1.0.0', 'major'],
['1.0.0-1', '1.1.1', 'major'],
['1.0.0-1', '2.1.1', 'major'],
['1.0.1-1', '1.0.1', 'patch'],
['0.0.0-1', '0.0.0', 'major'],
['1.0.0-1', '2.0.0', 'major'],
Expand Down