Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added utils for fetching last nightly version and generating next nightly version #898

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion packages/ckeditor5-dev-release-tools/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const push = require( './tasks/push' );
const publishPackages = require( './tasks/publishpackages' );
const updateVersions = require( './tasks/updateversions' );
const cleanUpPackages = require( './tasks/cleanuppackages' );
const { getLastFromChangelog, getCurrent, getLastTagFromGit } = require( './utils/versions' );
const { getLastFromChangelog, getLastNightly, getNextNightly, getCurrent, getLastTagFromGit } = require( './utils/versions' );
const { getChangesForVersion, getChangelog, saveChangelog } = require( './utils/changelog' );
const executeInParallel = require( './utils/executeinparallel' );
const validateRepositoryToRelease = require( './utils/validaterepositorytorelease' );
Expand All @@ -35,6 +35,8 @@ module.exports = {
reassignNpmTags,
executeInParallel,
getLastFromChangelog,
getLastNightly,
getNextNightly,
getCurrent,
getLastTagFromGit,
getChangesForVersion,
Expand Down
43 changes: 43 additions & 0 deletions packages/ckeditor5-dev-release-tools/lib/utils/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,49 @@ const versions = {
return matches ? matches[ 1 ] : null;
},

/**
* Returns the current (latest) nightly version in the format of "0.0.0-nightly-YYYYMMDD.X", where the "YYYYMMDD" is the date of the
* last nightly release and the "X" is the sequential number starting from 0. If the package does not have any nightly releases yet,
* `null` is returned.
*
* @returns {Promise<String|null>}
pomek marked this conversation as resolved.
Show resolved Hide resolved
*/
getLastNightly( cwd = process.cwd() ) {
const packageName = getPackageJson( cwd ).name;

return tools.shExec( `npm view ${ packageName }@nightly version`, { verbosity: 'silent', async: true } )
.catch( () => null );
},

/**
* Returns the next free nightly version in the format of "0.0.0-nightly-YYYYMMDD.X", where the "YYYYMMDD" is the current date and the
* "X" is the next available sequential number starting from 0.
*
* @returns {Promise<String>}
*/
pomek marked this conversation as resolved.
Show resolved Hide resolved
async getNextNightly( cwd = process.cwd() ) {
const today = new Date();
const year = today.getFullYear().toString();
const month = ( today.getMonth() + 1 ).toString().padStart( 2, '0' );
const day = today.getDate().toString().padStart( 2, '0' );

const nextNightlyVersion = `0.0.0-nightly-${ year }${ month }${ day }`;
const currentNightlyVersion = await versions.getLastNightly( cwd );

if ( !currentNightlyVersion ) {
return `${ nextNightlyVersion }.0`;
}

if ( !currentNightlyVersion.startsWith( nextNightlyVersion ) ) {
return `${ nextNightlyVersion }.0`;
}

const currentNightlyVersionId = currentNightlyVersion.split( '.' ).pop();
const nextNightlyVersionId = Number( currentNightlyVersionId ) + 1;

return `${ nextNightlyVersion }.${ nextNightlyVersionId }`;
},

/**
* Returns a name of the last created tag.
*
Expand Down
71 changes: 71 additions & 0 deletions packages/ckeditor5-dev-release-tools/tests/utils/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,77 @@ describe( 'dev-release-tools/utils', () => {
} );
} );

describe( 'getLastNightly()', () => {
let shExecStub;

beforeEach( () => {
shExecStub = sandbox.stub( tools, 'shExec' );
getPackageJsonStub.returns( { name: 'ckeditor5' } );
} );

it( 'asks npm for the last nightly version', () => {
shExecStub.resolves( '0.0.0-nightly-20230615.0' );

return version.getLastNightly()
.then( result => {
expect( shExecStub.callCount ).to.equal( 1 );
expect( shExecStub.firstCall.args[ 0 ] ).to.equal( 'npm view ckeditor5@nightly version' );

expect( result ).to.equal( '0.0.0-nightly-20230615.0' );
} );
} );

it( 'returns null if there is no nightly version for a package', () => {
shExecStub.rejects();

return version.getLastNightly()
.then( result => {
expect( result ).to.equal( null );
} );
} );
} );

describe( 'getNextNightly()', () => {
let clock;

beforeEach( () => {
clock = sinon.useFakeTimers( {
now: new Date( '2023-06-15 12:00:00' )
} );
} );

afterEach( () => {
clock.restore();
} );

it( 'return nightly version with id = 0 if nightly version was never published for the package yet', () => {
sandbox.stub( version, 'getLastNightly' ).resolves( null );

return version.getNextNightly()
.then( result => {
expect( result ).to.equal( '0.0.0-nightly-20230615.0' );
} );
} );

it( 'return nightly version with id = 0 if today no nightly version was published', () => {
sandbox.stub( version, 'getLastNightly' ).resolves( '0.0.0-nightly-20230614.7' );

return version.getNextNightly()
.then( result => {
expect( result ).to.equal( '0.0.0-nightly-20230615.0' );
} );
} );

it( 'return incremented nightly version id if today another nightly was published', () => {
sandbox.stub( version, 'getLastNightly' ).resolves( '0.0.0-nightly-20230615.10' );

return version.getNextNightly()
.then( result => {
expect( result ).to.equal( '0.0.0-nightly-20230615.11' );
} );
} );
} );

describe( 'getLastTagFromGit()', () => {
it( 'returns last tag if exists', () => {
sandbox.stub( tools, 'shExec' ).returns( 'v1.0.0' );
Expand Down