Skip to content

Commit 91c12fd

Browse files
aduthtyxla
andauthored
Packages: Avoid bumping the major version on prerelease packages (WordPress#74285)
* Packages: Avoid bumping the major version on prerelease packages * Ensure packageJSONPath assigned for reading JSON file * Clarify code comment on breaking changes Co-authored-by: aduth <aduth@git.wordpress.org> Co-authored-by: tyxla <tyxla@git.wordpress.org>
1 parent a4fa2a1 commit 91c12fd

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

bin/plugin/commands/common.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ async function findPluginReleaseBranchName( gitWorkingDirectoryPath ) {
3636
* @param {string[]} lines Changelog content split into lines.
3737
* @param {('patch'|'minor'|'major')} [minimumVersionBump] Minimum version bump for the package.
3838
* Defaults to `patch`.
39+
* @param {string} [currentVersion] Current version of the package.
3940
*
4041
* @return {string|null} Version bump when applicable, or null otherwise.
4142
*/
4243
function calculateVersionBumpFromChangelog(
4344
lines,
44-
minimumVersionBump = 'patch'
45+
minimumVersionBump = 'patch',
46+
currentVersion = '1.0.0'
4547
) {
4648
let changesDetected = false;
4749
let versionBump = null;
@@ -63,9 +65,14 @@ function calculateVersionBumpFromChangelog(
6365
break;
6466
}
6567

66-
// A major version bump required. Stop processing.
68+
// A major version bump required for stable packages. Stop processing.
6769
if ( lineNormalized.startsWith( '### breaking change' ) ) {
68-
versionBump = 'major';
70+
if ( semver.lt( currentVersion, '1.0.0' ) ) {
71+
versionBump = 'minor';
72+
} else {
73+
versionBump = 'major';
74+
}
75+
6976
break;
7077
}
7178

@@ -89,6 +96,7 @@ function calculateVersionBumpFromChangelog(
8996
versionBump = minimumVersionBump;
9097
}
9198
}
99+
92100
return versionBump;
93101
}
94102

bin/plugin/commands/packages.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/**
22
* External dependencies
33
*/
4-
const { command } = require( 'execa' );
54
const path = require( 'path' );
6-
const glob = require( 'fast-glob' );
75
const fs = require( 'fs' );
6+
const readline = require( 'readline' );
7+
const { join } = require( 'path' );
8+
const { command } = require( 'execa' );
9+
const glob = require( 'fast-glob' );
810
const { inc: semverInc } = require( 'semver' );
911
const { rimraf } = require( 'rimraf' );
10-
const readline = require( 'readline' );
1112
const SimpleGit = require( 'simple-git' );
1213

1314
/**
@@ -24,7 +25,6 @@ const {
2425
calculateVersionBumpFromChangelog,
2526
findPluginReleaseBranchName,
2627
} = require( './common' );
27-
const { join } = require( 'path' );
2828
const pluginConfig = require( '../config' );
2929

3030
/**
@@ -196,9 +196,15 @@ async function updatePackages( config ) {
196196
lines.push( line );
197197
}
198198

199+
const packageJSONPath = changelogPath.replace(
200+
'CHANGELOG.md',
201+
'package.json'
202+
);
203+
const { version } = readJSONFile( packageJSONPath );
199204
let versionBump = calculateVersionBumpFromChangelog(
200205
lines,
201-
minimumVersionBump
206+
minimumVersionBump,
207+
version
202208
);
203209
const packageName = `@wordpress/${
204210
changelogPath.split( '/' ).reverse()[ 1 ]
@@ -212,11 +218,6 @@ async function updatePackages( config ) {
212218
) {
213219
versionBump = minimumVersionBump;
214220
}
215-
const packageJSONPath = changelogPath.replace(
216-
'CHANGELOG.md',
217-
'package.json'
218-
);
219-
const { version } = readJSONFile( packageJSONPath );
220221
const nextVersion =
221222
versionBump !== null ? semverInc( version, versionBump ) : null;
222223

bin/plugin/commands/test/common.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,31 @@ describe( 'calculateVersionBumpFromChangelog', () => {
6161

6262
it( 'should return major version bump when breaking changes detected', () => {
6363
expect(
64-
calculateVersionBumpFromChangelog(
65-
[
66-
'First line',
67-
'## Unreleased',
68-
'### Breaking Changes',
69-
' - new item added',
70-
'Fifth line',
71-
],
72-
'major'
73-
)
64+
calculateVersionBumpFromChangelog( [
65+
'First line',
66+
'## Unreleased',
67+
'### Breaking Changes',
68+
' - new item added',
69+
'Fifth line',
70+
] )
7471
).toBe( 'major' );
7572
} );
73+
74+
describe( 'prerelease versions', () => {
75+
it( 'should not bump the major even if breaking changes detected', () => {
76+
expect(
77+
calculateVersionBumpFromChangelog(
78+
[
79+
'First line',
80+
'## Unreleased',
81+
'### Breaking Changes',
82+
' - new item added',
83+
'Fifth line',
84+
],
85+
'patch',
86+
'0.1.0'
87+
)
88+
).toBe( 'minor' );
89+
} );
90+
} );
7691
} );

0 commit comments

Comments
 (0)