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
50 changes: 25 additions & 25 deletions lib/commands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,38 +37,38 @@ module.exports = {
* @param {CommandData} data
* @returns {Promise}
*/
execute( data ) {
async execute( data ) {
const execCommand = require( './exec' );

const promises = [
execCommand.execute( getExecData( 'git rev-parse HEAD' ) ),
execCommand.execute( getExecData( 'git status --branch --porcelain' ) ),
execCommand.execute( getExecData( 'git describe --abbrev=0 --tags' ) ),
execCommand.execute( getExecData( 'git log --tags --simplify-by-decoration --pretty="%S"' ) )
];
let latestTag = null;
let currentTag = null;
let packageName = data.packageName;

return Promise.all( promises )
.then( ( [ hashResponse, currentBranchStatusResponse, currentTagStatusResponse, latestTagStatusResponse ] ) => {
let packageName = data.packageName;
const hashResponse = await execCommand.execute( getExecData( 'git rev-parse HEAD' ) );
const currentBranchStatusResponse = await execCommand.execute( getExecData( 'git status --branch --porcelain' ) );
const latestTagStatusResponse = await execCommand.execute( getExecData( 'git log --tags --simplify-by-decoration --pretty="%S"' ) );

const currentTag = currentTagStatusResponse.logs.info[ 0 ];
const latestTag = latestTagStatusResponse.logs.info[ 0 ].trim().split( '\n' ).shift();
if ( latestTagStatusResponse.logs.info.length ) {
const currentTagStatusResponse = await execCommand.execute( getExecData( 'git describe --abbrev=0 --tags' ) );

for ( const packagePrefix of data.toolOptions.packagesPrefix ) {
packageName = packageName.replace( new RegExp( '^' + packagePrefix ), '' );
}
latestTag = latestTagStatusResponse.logs.info[ 0 ].trim().split( '\n' ).shift();
currentTag = currentTagStatusResponse.logs.info[ 0 ];
}

const commandResponse = {
packageName,
status: gitStatusParser( currentBranchStatusResponse.logs.info[ 0 ], currentTag ),
commit: hashResponse.logs.info[ 0 ].slice( 0, 7 ), // Short version of the commit hash.
mrgitBranch: data.repository.branch,
mrgitTag: data.repository.tag,
latestTag
};
for ( const packagePrefix of data.toolOptions.packagesPrefix ) {
packageName = packageName.replace( new RegExp( '^' + packagePrefix ), '' );
}

return { response: commandResponse };
} );
const commandResponse = {
packageName,
status: gitStatusParser( currentBranchStatusResponse.logs.info[ 0 ], currentTag ),
commit: hashResponse.logs.info[ 0 ].slice( 0, 7 ), // Short version of the commit hash.
mrgitBranch: data.repository.branch,
mrgitTag: data.repository.tag,
latestTag
};

return { response: commandResponse };

function getExecData( command ) {
return Object.assign( {}, data, {
Expand Down
5 changes: 5 additions & 0 deletions lib/commands/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ module.exports = {
const commandOutput = await execCommand.execute(
getExecData( 'git log --tags --simplify-by-decoration --pretty="%S"' )
);

if ( !commandOutput.logs.info.length ) {
throw new Error( `Can't check out the latest tag as package "${ data.packageName }" has no tags. Aborted.` );
}

const latestTag = commandOutput.logs.info[ 0 ].trim().split( '\n' ).shift();

checkoutValue = 'tags/' + latestTag.trim();
Expand Down
1 change: 0 additions & 1 deletion lib/utils/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module.exports = function log() {

msg = msg.trim();

/* istanbul ignore if */
if ( !msg ) {
return;
}
Expand Down
65 changes: 53 additions & 12 deletions tests/commands/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ describe( 'commands/status', () => {
logs: { info: [ 'Response returned by "git status" command.' ] }
} );
stubs.execCommand.execute.onCall( 2 ).resolves( {
logs: { info: [ 'Response returned by "git describe" command.' ] }
logs: { info: [ '\nv35.3.2\nv35.3.1\nv35.3.0\nv35.2.1\nv35.2.0' ] }
} );
stubs.execCommand.execute.onCall( 3 ).resolves( {
logs: { info: [ '\nv35.3.2\nv35.3.1\nv35.3.0\nv35.2.1\nv35.2.0' ] }
logs: { info: [ 'Response returned by "git describe" command.' ] }
} );

stubs.gitStatusParser.returns( { response: 'Parsed response.' } );
Expand All @@ -160,10 +160,10 @@ describe( 'commands/status', () => {
getCommandArguments( 'git status --branch --porcelain' )
);
expect( stubs.execCommand.execute.getCall( 2 ).args[ 0 ] ).to.deep.equal(
getCommandArguments( 'git describe --abbrev=0 --tags' )
getCommandArguments( 'git log --tags --simplify-by-decoration --pretty="%S"' )
);
expect( stubs.execCommand.execute.getCall( 3 ).args[ 0 ] ).to.deep.equal(
getCommandArguments( 'git log --tags --simplify-by-decoration --pretty="%S"' )
getCommandArguments( 'git describe --abbrev=0 --tags' )
);

expect( stubs.gitStatusParser.calledOnce ).to.equal( true );
Expand All @@ -181,6 +181,47 @@ describe( 'commands/status', () => {
} );
} );

it( 'works properly for repositories without tags', () => {
stubs.execCommand.execute.onCall( 0 ).resolves( {
logs: { info: [ '6bfd379a56a32c9f8b6e58bf08e39c124cdbae10' ] }
} );
stubs.execCommand.execute.onCall( 1 ).resolves( {
logs: { info: [ 'Response returned by "git status" command.' ] }
} );
stubs.execCommand.execute.onCall( 2 ).resolves( {
logs: { info: [] }
} );

stubs.gitStatusParser.returns( { response: 'Parsed response.' } );

return statusCommand.execute( commandData )
.then( statusResponse => {
expect( stubs.execCommand.execute.callCount ).to.equal( 3 );
expect( stubs.execCommand.execute.getCall( 0 ).args[ 0 ] ).to.deep.equal(
getCommandArguments( 'git rev-parse HEAD' )
);
expect( stubs.execCommand.execute.getCall( 1 ).args[ 0 ] ).to.deep.equal(
getCommandArguments( 'git status --branch --porcelain' )
);
expect( stubs.execCommand.execute.getCall( 2 ).args[ 0 ] ).to.deep.equal(
getCommandArguments( 'git log --tags --simplify-by-decoration --pretty="%S"' )
);

expect( stubs.gitStatusParser.calledOnce ).to.equal( true );
expect( stubs.gitStatusParser.firstCall.args[ 0 ] ).to.equal( 'Response returned by "git status" command.' );
expect( stubs.gitStatusParser.firstCall.args[ 1 ] ).to.equal( null );

expect( statusResponse.response ).to.deep.equal( {
packageName: 'test-package',
status: { response: 'Parsed response.' },
commit: '6bfd379',
mrgitBranch: 'master',
mrgitTag: undefined,
latestTag: null
} );
} );
} );

it( 'modifies the package name if "packagesPrefix" is an array', () => {
commandData.toolOptions.packagesPrefix = [
'@ckeditor/ckeditor-',
Expand All @@ -194,10 +235,10 @@ describe( 'commands/status', () => {
logs: { info: [ 'Response returned by "git status" command.' ] }
} );
stubs.execCommand.execute.onCall( 2 ).resolves( {
logs: { info: [ 'Response returned by "git describe" command.' ] }
logs: { info: [ 'v35.3.2\nv35.3.1\nv35.3.0\nv35.2.1\nv35.2.0\n' ] }
} );
stubs.execCommand.execute.onCall( 3 ).resolves( {
logs: { info: [ 'v35.3.2\nv35.3.1\nv35.3.0\nv35.2.1\nv35.2.0\n' ] }
logs: { info: [ 'Response returned by "git describe" command.' ] }
} );

stubs.gitStatusParser.returns( { response: 'Parsed response.' } );
Expand All @@ -212,10 +253,10 @@ describe( 'commands/status', () => {
getCommandArguments( 'git status --branch --porcelain' )
);
expect( stubs.execCommand.execute.getCall( 2 ).args[ 0 ] ).to.deep.equal(
getCommandArguments( 'git describe --abbrev=0 --tags' )
getCommandArguments( 'git log --tags --simplify-by-decoration --pretty="%S"' )
);
expect( stubs.execCommand.execute.getCall( 3 ).args[ 0 ] ).to.deep.equal(
getCommandArguments( 'git log --tags --simplify-by-decoration --pretty="%S"' )
getCommandArguments( 'git describe --abbrev=0 --tags' )
);

expect( stubs.gitStatusParser.calledOnce ).to.equal( true );
Expand Down Expand Up @@ -244,10 +285,10 @@ describe( 'commands/status', () => {
logs: { info: [ 'Response returned by "git status" command.' ] }
} );
stubs.execCommand.execute.onCall( 2 ).resolves( {
logs: { info: [ 'Response returned by "git describe" command.' ] }
logs: { info: [ '\nv35.3.2\nv35.3.1\nv35.3.0\nv35.2.1\nv35.2.0' ] }
} );
stubs.execCommand.execute.onCall( 3 ).resolves( {
logs: { info: [ '\nv35.3.2\nv35.3.1\nv35.3.0\nv35.2.1\nv35.2.0' ] }
logs: { info: [ 'Response returned by "git describe" command.' ] }
} );

stubs.gitStatusParser.returns( { response: 'Parsed response.' } );
Expand All @@ -262,10 +303,10 @@ describe( 'commands/status', () => {
getCommandArguments( 'git status --branch --porcelain' )
);
expect( stubs.execCommand.execute.getCall( 2 ).args[ 0 ] ).to.deep.equal(
getCommandArguments( 'git describe --abbrev=0 --tags' )
getCommandArguments( 'git log --tags --simplify-by-decoration --pretty="%S"' )
);
expect( stubs.execCommand.execute.getCall( 3 ).args[ 0 ] ).to.deep.equal(
getCommandArguments( 'git log --tags --simplify-by-decoration --pretty="%S"' )
getCommandArguments( 'git describe --abbrev=0 --tags' )
);

expect( stubs.gitStatusParser.calledOnce ).to.equal( true );
Expand Down
40 changes: 39 additions & 1 deletion tests/commands/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,44 @@ describe( 'commands/sync', () => {
} );
} );

it( 'throws an error when trying to check out the latest tag in repository without tags', () => {
commandData.repository.tag = 'latest';

stubs.fs.existsSync.returns( true );

const exec = stubs.execCommand.execute;

exec.onCall( 0 ).returns( Promise.resolve( {
logs: getCommandLogs( '' )
} ) );

exec.onCall( 1 ).returns( Promise.resolve( {
logs: getCommandLogs( '' )
} ) );

exec.onCall( 2 ).returns( Promise.resolve( {
logs: getCommandLogs()
} ) );

return syncCommand.execute( commandData )
.then( () => {
throw new Error( 'Expected to throw' );
} )
.catch( response => {
expect( exec.getCall( 0 ).args[ 0 ].arguments[ 0 ] ).to.equal( 'git status -s' );
expect( exec.getCall( 1 ).args[ 0 ].arguments[ 0 ] ).to.equal( 'git fetch' );
expect( exec.getCall( 2 ).args[ 0 ].arguments[ 0 ] ).to.equal(
'git log --tags --simplify-by-decoration --pretty="%S"'
);

expect( exec.callCount ).to.equal( 3 );

expect( response.logs.error[ 0 ] ).to.equal(
'Can\'t check out the latest tag as package "test-package" has no tags. Aborted.'
);
} );
} );

it( 'aborts if package has uncommitted changes', () => {
stubs.fs.existsSync.returns( true );

Expand Down Expand Up @@ -758,7 +796,7 @@ describe( 'commands/sync', () => {

if ( isError ) {
logs.error.push( msg );
} else {
} else if ( msg ) {
logs.info.push( msg );
}

Expand Down
Loading