Skip to content

Commit

Permalink
Merge pull request #889 from ckeditor/ck/14169
Browse files Browse the repository at this point in the history
Feature (release-tools): Introduced util called `truncateChangelog()` that allows reducing the changelog to the requested number of entries. See ckeditor/ckeditor5#14169.

Internal: Generated changelog is truncated to the latest five release entries. Closes ckeditor/ckeditor5#14169.
  • Loading branch information
pomek authored Jun 1, 2023
2 parents 06cc6b3 + ce96e86 commit fe6e2d8
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,9 @@ module.exports = async function generateChangelogForMonoRepository( options ) {
// Save the changelog.
changelogUtils.saveChangelog( newChangelog );

// Truncate the changelog to keep the latest five release entries.
changelogUtils.truncateChangelog( 5 );

logInfo( 'Saved.', { indentLevel: 1 } );
}

Expand Down
34 changes: 34 additions & 0 deletions packages/ckeditor5-dev-release-tools/lib/utils/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const fs = require( 'fs' );
const path = require( 'path' );
const { getRepositoryUrl } = require( './transformcommitutils' );

const utils = {
/**
Expand Down Expand Up @@ -61,6 +62,39 @@ const utils = {
const changelogFile = path.join( cwd, utils.changelogFile );

fs.writeFileSync( changelogFile, content, 'utf-8' );
},

/**
* @param {Number} length
* @param {String} [cwd=process.cwd()] Where to look for the changelog file.
*/
truncateChangelog( length, cwd = process.cwd() ) {
const changelog = utils.getChangelog( cwd );

if ( !changelog ) {
return;
}

const entryHeader = '## [\\s\\S]+?';
const entryHeaderRegexp = new RegExp( `\\n(${ entryHeader })(?=\\n${ entryHeader }|$)`, 'g' );

const entries = [ ...changelog.matchAll( entryHeaderRegexp ) ]
.filter( match => match && match[ 1 ] )
.map( match => match[ 1 ] );

if ( !entries.length ) {
return;
}

const truncatedEntries = entries.slice( 0, length );

const changelogFooter = entries.length > truncatedEntries.length ?
`\n\n---\n\nTo see all releases, visit the [release page](${ getRepositoryUrl( cwd ) }/releases).\n` :
'\n';

const truncatedChangelog = utils.changelogHeader + truncatedEntries.join( '\n' ).trim() + changelogFooter;

utils.saveChangelog( truncatedChangelog, cwd );
}
};

Expand Down
106 changes: 104 additions & 2 deletions packages/ckeditor5-dev-release-tools/tests/utils/changelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ describe( 'dev-release-tools/utils', () => {

describe( 'changelog', () => {
beforeEach( () => {
utils = require( '../../lib/utils/changelog' );

sandbox = sinon.createSandbox();

sandbox.stub(
require( '../../lib/utils/transformcommitutils' ),
'getRepositoryUrl'
).returns( 'https://github.com/ckeditor/ckeditor5-dev' );

utils = require( '../../lib/utils/changelog' );
} );

afterEach( () => {
Expand Down Expand Up @@ -313,5 +318,102 @@ describe( 'dev-release-tools/utils', () => {
expect( writeFileStub.firstCall.args[ 1 ] ).to.equal( 'New content.' );
} );
} );

describe( 'truncateChangelog()', () => {
it( 'does nothing if there is no changelog', () => {
const saveChangelogStub = sandbox.stub( utils, 'saveChangelog' );

sandbox.stub( utils, 'getChangelog' ).returns( null );

utils.truncateChangelog( 5 );

expect( saveChangelogStub.called ).to.equal( false );
} );

it( 'does nothing if changelog does not contain entries', () => {
const saveChangelogStub = sandbox.stub( utils, 'saveChangelog' );

sandbox.stub( utils, 'getChangelog' ).returns( utils.changelogHeader + '\n\n' );

utils.truncateChangelog( 5 );

expect( saveChangelogStub.called ).to.equal( false );
} );

it( 'truncates the changelog and adds the link to the release page', () => {
const expectedChangelogEntries = [
'## [0.3.0](https://github.com) (2017-01-13)',
'',
'3',
'',
'Some text ## [like a release header]',
'',
'## [0.2.0](https://github.com) (2017-01-13)',
'',
'2'
].join( '\n' );

const expectedChangelogFooter = [
'',
'',
'---',
'',
'To see all releases, visit the [release page](https://github.com/ckeditor/ckeditor5-dev/releases).',
''
].join( '\n' );

const changelogEntries = [
expectedChangelogEntries,
'',
'## [0.1.0](https://github.com) (2017-01-13)',
'',
'1'
].join( '\n' );

const saveChangelogStub = sandbox.stub( utils, 'saveChangelog' );

sandbox.stub( utils, 'getChangelog' ).returns( utils.changelogHeader + changelogEntries );

utils.truncateChangelog( 2 );

expect( saveChangelogStub.calledOnce ).to.equal( true );
expect( saveChangelogStub.firstCall.args[ 0 ] ).to.equal(
utils.changelogHeader +
expectedChangelogEntries +
expectedChangelogFooter
);
} );

it( 'does not add the link to the release page if changelog is not truncated', () => {
const expectedChangelogEntries = [
'## [0.3.0](https://github.com) (2017-01-13)',
'',
'3',
'',
'Some text ## [like a release header]',
'',
'## [0.2.0](https://github.com) (2017-01-13)',
'',
'2'
].join( '\n' );

const expectedChangelogFooter = '\n';

const changelogEntries = expectedChangelogEntries;

const saveChangelogStub = sandbox.stub( utils, 'saveChangelog' );

sandbox.stub( utils, 'getChangelog' ).returns( utils.changelogHeader + changelogEntries );

utils.truncateChangelog( 2 );

expect( saveChangelogStub.calledOnce ).to.equal( true );
expect( saveChangelogStub.firstCall.args[ 0 ] ).to.equal(
utils.changelogHeader +
expectedChangelogEntries +
expectedChangelogFooter
);
} );
} );
} );
} );

0 comments on commit fe6e2d8

Please sign in to comment.