Skip to content

Commit 4de1335

Browse files
committed
Add warning message for non-stable releases
Automatically add a message to warn people not to put non-stable releases e.g. 6.2.0-beta.0 into production.
1 parent bf874a7 commit 4de1335

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

shared/github-scripts/changelog-release-helper.mjs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const processingErrorMessage =
1717
*/
1818
export function updateChangelog(newVersion, previousVersion) {
1919
// Skip the entire function if the release version is internal eg: 5.1.0-internal.0
20-
if (versionIsAPrerelease(newVersion)) {
20+
const newVersionIsAPrelease = versionIsAPrerelease(newVersion)
21+
if (newVersionIsAPrelease) {
2122
const identifier = getPrereleaseIdentifier(newVersion)
2223

2324
if (identifier === 'internal') {
@@ -42,7 +43,18 @@ export function updateChangelog(newVersion, previousVersion) {
4243
}
4344
const newVersionTitle = `## v${newVersion} (${convertIncTypeWord(versionDiff, newVersion, true, changelogLines[previousReleaseLineIndex])} release)`
4445

45-
changelogLines.splice(startIndex + 1, 0, '', newVersionTitle)
46+
const newLines = [newVersionTitle]
47+
if (newVersionIsAPrelease) {
48+
newLines.push(
49+
`> [!WARNING]`,
50+
`> Do not use in production.`,
51+
`> Use this release to prepare for the changes coming in version \`${removePrereleaseFlag(newVersion)}\`.`
52+
)
53+
}
54+
55+
// Inject the new lines while replacing the unreleased heading.
56+
changelogLines.splice(startIndex + 1, 0, '', ...newLines)
57+
4658
writeFileSync('./CHANGELOG.md', changelogLines.join('\n'))
4759
}
4860

@@ -262,3 +274,15 @@ function convertIncTypeWord(
262274
? `${rewordedIncType.charAt(0).toUpperCase()}${rewordedIncType.slice(1)}`
263275
: rewordedIncType
264276
}
277+
278+
/**
279+
* Remove any pre-release flag from a version e.g. 1.0.0-alpha -> 1.0.0
280+
*
281+
* @param {string} version - version number
282+
* @returns {string} - version number without any pre-release flag
283+
*/
284+
function removePrereleaseFlag(version) {
285+
const parsedVersion = semver.parse(version)
286+
parsedVersion.prerelease = []
287+
return parsedVersion.format()
288+
}

shared/github-scripts/changelog-release-helper.unit.test.mjs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,31 @@ describe('Changelog release helper', () => {
5555
)
5656
})
5757

58+
it('displays a warning to not use non-stable releases in production', () => {
59+
jest.mocked(fs.readFileSync).mockReturnValue(`
60+
## Unreleased
61+
62+
### Fixes
63+
64+
Bing bong
65+
66+
## v3.1.0-beta.0 (Beta feature release)
67+
`)
68+
69+
updateChangelog('3.1.0-beta.1', '3.1.0-beta.0')
70+
expect(fs.writeFileSync).toHaveBeenCalledWith(
71+
'./CHANGELOG.md',
72+
expect.stringContaining(
73+
[
74+
'## v3.1.0-beta.1 (Beta feature release)',
75+
'> [!WARNING]',
76+
'> Do not use in production.',
77+
'> Use this release to prepare for the changes coming in version `3.1.0`.'
78+
].join('\n')
79+
)
80+
)
81+
})
82+
5883
it('does not change the changelog if the provided version is an internal pre-release', () => {
5984
const consoleLogSpy = jest.spyOn(console, 'log')
6085

0 commit comments

Comments
 (0)