Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,15 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Save current version
run: echo "PREVIOUS_PACKAGE_VERSION=$(npm run get-version --silent --workspace govuk-frontend)" >> $GITHUB_ENV

- name: Update package version
- name: Update package version and CHANGELOG
run: |
npm version --no-git-tag-version --workspace govuk-frontend "$RELEASE_TYPE" --preid "$PREID"
Comment thread
NickColley marked this conversation as resolved.
# After `npm version` runs, the `postversion` lifecycle hook script from the govuk-frontend package
# will be run which then updates the CHANGELOG.

# Save the new version number for access in future steps
echo "PACKAGE_VERSION=$(npm run get-version --silent --workspace govuk-frontend)" >> $GITHUB_ENV

- name: Update CHANGELOG
uses: actions/github-script@v9.0.0
with:
script: |
const { updateChangelog } = await import('${{ github.workspace }}/shared/github-scripts/changelog-release-helper.mjs')

updateChangelog(process.env.PACKAGE_VERSION, process.env.PREVIOUS_PACKAGE_VERSION)

- name: Generate release notes
uses: actions/github-script@v9.0.0
with:
Expand Down
9 changes: 9 additions & 0 deletions bin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
Comment thread
NickColley marked this conversation as resolved.
"extends": "../tsconfig.base.json",
"include": ["**/*.mjs"],
"exclude": ["**/*.test.*"],
"compilerOptions": {
"strictNullChecks": false,
"types": ["node"]
}
}
16 changes: 16 additions & 0 deletions bin/update-changelog.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { updateChangelog } from '../shared/github-scripts/changelog-release-helper.mjs'
Comment thread
NickColley marked this conversation as resolved.

// npm exposes these environment variable as part of the lifecycle hooks
// (https://github.com/npm/cli/blob/c97b39b1e3436cd20a67ab5f4012a5f395c538b9/workspaces/libnpmversion/lib/version.js#L100-L103)
const { npm_old_version: previousVersion, npm_new_version: newVersion } =
process.env
Comment thread
NickColley marked this conversation as resolved.

if (!previousVersion || !newVersion) {
throw new Error('Both previous and new version must be set to continue.')
}

console.log(
`Updating changelog from version ${previousVersion} to ${newVersion}...`
)

updateChangelog(newVersion, previousVersion)
1 change: 1 addition & 0 deletions packages/govuk-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"clean:package": "del-cli *.tsbuildinfo dist govuk-prototype-kit.config.json",
"clean:release": "del-cli ../../dist --force",
"postbuild:package": "npm run build:stats && govuk-prototype-kit validate-plugin .",
"postversion": "node ../../bin/update-changelog.mjs",
"get-version": "echo $npm_package_version"
},
"devDependencies": {
Expand Down
8 changes: 6 additions & 2 deletions shared/github-scripts/changelog-release-helper.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'

import { paths } from '@govuk-frontend/config'
import semver from 'semver'

const CHANGELOG_FILE_PATH = join(paths.root, 'CHANGELOG.md')

const processingErrorMessage =
'There was a problem processing information from the changelog. This likely means that there is an issue with the changelog content itself. Please check it and try running this task again.'

Expand Down Expand Up @@ -62,7 +66,7 @@ export function updateChangelog(newVersion, previousVersion) {
// Inject the new lines into the CHANGELOG
changelogLines.splice(startIndex + 1, 0, '', ...newLines)

writeFileSync('./CHANGELOG.md', changelogLines.join('\n'))
writeFileSync(CHANGELOG_FILE_PATH, changelogLines.join('\n'))
}

/**
Expand Down Expand Up @@ -132,7 +136,7 @@ function validateVersionNumber(version) {
* @returns {Array<string>} - Changelog split into an array by lines
*/
function getChangelogLines() {
return readFileSync('./CHANGELOG.md', 'utf8').split('\n')
return readFileSync(CHANGELOG_FILE_PATH, 'utf8').split('\n')
}

/**
Expand Down
18 changes: 11 additions & 7 deletions shared/github-scripts/changelog-release-helper.unit.test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import fs from 'fs'
import { join } from 'path'

import { paths } from '@govuk-frontend/config'
import { outdent } from 'outdent'

import {
Expand All @@ -9,6 +11,8 @@ import {

jest.mock('fs')

const CHANGELOG_FILE_PATH = join(paths.root, 'CHANGELOG.md')

describe('Changelog release helper', () => {
beforeEach(() => {
jest.mocked(fs.readFileSync).mockReturnValue(`
Expand All @@ -26,15 +30,15 @@ describe('Changelog release helper', () => {
it('adds a new heading to the changelog for the new version', () => {
updateChangelog('3.1.0', '3.0.0')
expect(fs.writeFileSync).toHaveBeenCalledWith(
'./CHANGELOG.md',
CHANGELOG_FILE_PATH,
expect.stringContaining('## v3.1.0 (Feature release)')
)
})

it('prefixes a new heading with a pre-release identifier if the new version is a pre-release', () => {
updateChangelog('3.1.0-beta.0', '3.0.0')
expect(fs.writeFileSync).toHaveBeenCalledWith(
'./CHANGELOG.md',
CHANGELOG_FILE_PATH,
expect.stringContaining('## v3.1.0-beta.0 (Beta feature release)')
)
})
Expand All @@ -52,7 +56,7 @@ describe('Changelog release helper', () => {

updateChangelog('3.1.0-beta.1', '3.1.0-beta.0')
expect(fs.writeFileSync).toHaveBeenCalledWith(
'./CHANGELOG.md',
CHANGELOG_FILE_PATH,
expect.stringContaining('## v3.1.0-beta.1 (Beta feature release)')
)
})
Expand All @@ -70,7 +74,7 @@ describe('Changelog release helper', () => {

updateChangelog('3.1.0-beta.1', '3.1.0-beta.0')
expect(fs.writeFileSync).toHaveBeenCalledWith(
'./CHANGELOG.md',
CHANGELOG_FILE_PATH,
expect.stringContaining(outdent`
## v3.1.0-beta.1 (Beta feature release)
> [!WARNING]
Expand All @@ -79,7 +83,7 @@ describe('Changelog release helper', () => {
`)
)
expect(fs.writeFileSync).toHaveBeenCalledWith(
'./CHANGELOG.md',
CHANGELOG_FILE_PATH,
expect.stringContaining('To install this version with npm')
)
})
Expand All @@ -97,7 +101,7 @@ describe('Changelog release helper', () => {

updateChangelog('3.1.1', '3.1.0')
expect(fs.writeFileSync).not.toHaveBeenCalledWith(
'./CHANGELOG.md',
CHANGELOG_FILE_PATH,
expect.stringContaining('> [!WARNING]')
)
})
Expand All @@ -115,7 +119,7 @@ describe('Changelog release helper', () => {

updateChangelog('3.1.1', '3.1.0')
expect(fs.writeFileSync).toHaveBeenCalledWith(
'./CHANGELOG.md',
CHANGELOG_FILE_PATH,
expect.stringContaining(outdent`
## v3.1.1 (Fix release)
To install this version with npm, run \`npm install govuk-frontend@3.1.1\`. You can also find more information about [how to stay up to date](https://frontend.design-system.service.gov.uk/staying-up-to-date/#updating-to-the-latest-version) in our documentation.
Expand Down
1 change: 1 addition & 0 deletions shared/github-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"license": "MIT",
"devDependencies": {
"@govuk-frontend/config": "*",
"@govuk-frontend/lib": "*",
"@govuk-frontend/stats": "*",
"outdent": "^0.8.0",
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
{
"path": "./tsconfig.dev.json"
},
{
"path": "./bin/tsconfig.json"
},
{
"path": "./packages/govuk-frontend-review/tsconfig.json"
},
Expand Down
Loading