Skip to content

Commit cc8cd79

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 78e1899 commit cc8cd79

2 files changed

Lines changed: 69 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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import fs from 'fs'
22

3+
import { outdent } from 'outdent'
4+
35
import {
46
updateChangelog,
57
generateReleaseNotes
@@ -55,6 +57,47 @@ describe('Changelog release helper', () => {
5557
)
5658
})
5759

60+
it('displays a warning to not use non-stable releases in production', () => {
61+
jest.mocked(fs.readFileSync).mockReturnValue(`
62+
## Unreleased
63+
64+
### Fixes
65+
66+
Bing bong
67+
68+
## v3.1.0-beta.0 (Beta feature release)
69+
`)
70+
71+
updateChangelog('3.1.0-beta.1', '3.1.0-beta.0')
72+
expect(fs.writeFileSync).toHaveBeenCalledWith(
73+
'./CHANGELOG.md',
74+
expect.stringContaining(outdent`
75+
## v3.1.0-beta.1 (Beta feature release)
76+
> [!WARNING]
77+
> Do not use in production.
78+
> Use this release to prepare for the changes coming in version \`3.1.0\`.
79+
`)
80+
)
81+
})
82+
83+
it('does not display a warning when the change is a stable release', () => {
84+
jest.mocked(fs.readFileSync).mockReturnValue(`
85+
## Unreleased
86+
87+
### Fixes
88+
89+
Bing bong
90+
91+
## v3.1.0 (Beta feature release)
92+
`)
93+
94+
updateChangelog('3.1.1', '3.1.0')
95+
expect(fs.writeFileSync).not.toHaveBeenCalledWith(
96+
'./CHANGELOG.md',
97+
expect.stringContaining('> [!WARNING]')
98+
)
99+
})
100+
58101
it('does not change the changelog if the provided version is an internal pre-release', () => {
59102
const consoleLogSpy = jest.spyOn(console, 'log')
60103

0 commit comments

Comments
 (0)