From b4ffada703018242a0516f44c85b01d98c58bcf3 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Fri, 27 Apr 2018 10:40:04 +0200 Subject: [PATCH 1/9] Update mergify to use commands instead of options --- index.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index fa8be81..e9e7506 100755 --- a/index.js +++ b/index.js @@ -15,24 +15,24 @@ program .name('mergify') .version(pack.version); -const options = [ +const commands = [ { - trigger: '-a --assigned', + trigger: 'assigned', description: 'Get all open merge request assigned to you', fn: getAllAssigned }, { - trigger: '-s --submitted', + trigger: 'submitted', description: 'Get all open merge request submitted to you', fn: getAllSubmitted }, { - trigger: '-c --configure', + trigger: 'configure', description: 'Setup or update required config', fn: configure }, { - trigger: '-v --verify', + trigger: 'verify', description: 'Verify your config is correct', fn: verify } @@ -52,8 +52,11 @@ const run = async() => { await verify(); } - options.forEach(({ trigger, description, fn }) => { - program.option(trigger, description, (...args) => fn(config, ...args)); + commands.forEach(({ trigger, description, fn }) => { + program + .command(trigger) + .description(description) + .action((...args) => fn(config, ...args)); }); return program; From d48649c6b5f0e06440529bab5876efe2ecb09fe8 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Fri, 27 Apr 2018 11:14:52 +0200 Subject: [PATCH 2/9] Add self-signed option to allow configure with private gitlab instances * Also add support for command options --- index.js | 23 ++++++++++++++++--- .../disableCertificateVerification/index.js | 12 ++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 lib/options/disableCertificateVerification/index.js diff --git a/index.js b/index.js index e9e7506..80bf013 100755 --- a/index.js +++ b/index.js @@ -8,7 +8,7 @@ const { configure } = require('./lib/commands/configure'); const { verify } = require('./lib/commands/verify'); const { getAllAssigned } = require('./lib/commands/getAllAssigned'); const { getAllSubmitted } = require('./lib/commands/getAllSubmitted'); - +const { disableCertificateVerification } = require('./lib/options/disableCertificateVerification'); const { readConfig } = require('./lib/utils/readConfig'); program @@ -29,7 +29,14 @@ const commands = [ { trigger: 'configure', description: 'Setup or update required config', - fn: configure + fn: configure, + options: [ + { + trigger: '-s --self-signed', + description: 'disables the verification of certificates when configuring mergify', + fn: disableCertificateVerification + } + ] }, { trigger: 'verify', @@ -52,11 +59,21 @@ const run = async() => { await verify(); } - commands.forEach(({ trigger, description, fn }) => { + commands.forEach(({ trigger, description, fn, options }) => { program .command(trigger) .description(description) .action((...args) => fn(config, ...args)); + + if(options && options.length > 0){ + options.forEach( + (option) => { + program.commands[program.commands.length - 1] + .option(option.trigger, option.description, option.fn); + } + ); + } + }); return program; diff --git a/lib/options/disableCertificateVerification/index.js b/lib/options/disableCertificateVerification/index.js new file mode 100644 index 0000000..2f3fdfe --- /dev/null +++ b/lib/options/disableCertificateVerification/index.js @@ -0,0 +1,12 @@ +const { logger } = require('../../utils/logger'); +const chalk = require('chalk'); + +const disableCertificateVerification = async() => { + logger.log(chalk.red.bold('⚠️ Disabling certificate verification. This is unsafe and should only be used as last resort.')); + process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; + return; +}; + +module.exports = { + disableCertificateVerification +}; From 2b19b2aabd63e7d71ea80c2acfd2c78355c4bdc4 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Sun, 29 Apr 2018 11:52:01 +0200 Subject: [PATCH 3/9] Add logic for mergify level options * Remove logic for option specific command --- index.js | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 80bf013..b41282b 100755 --- a/index.js +++ b/index.js @@ -15,6 +15,14 @@ program .name('mergify') .version(pack.version); +const options = [ + { + trigger: '-s --self-signed', + description: 'disables the verification of certificates for commands', + fn: disableCertificateVerification + } +]; + const commands = [ { trigger: 'assigned', @@ -29,14 +37,7 @@ const commands = [ { trigger: 'configure', description: 'Setup or update required config', - fn: configure, - options: [ - { - trigger: '-s --self-signed', - description: 'disables the verification of certificates when configuring mergify', - fn: disableCertificateVerification - } - ] + fn: configure }, { trigger: 'verify', @@ -59,21 +60,16 @@ const run = async() => { await verify(); } + options.forEach(({trigger, description, fn}) => { + program.option(trigger, description, (...args) => fn(config, ...args)); + }); + commands.forEach(({ trigger, description, fn, options }) => { program .command(trigger) .description(description) .action((...args) => fn(config, ...args)); - if(options && options.length > 0){ - options.forEach( - (option) => { - program.commands[program.commands.length - 1] - .option(option.trigger, option.description, option.fn); - } - ); - } - }); return program; From 1ac4ac3c7fd799b0a2488a1253cb32d1abb5bd33 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Sun, 29 Apr 2018 12:07:47 +0200 Subject: [PATCH 4/9] Remove unnecessary options input in forEach --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index b41282b..33845d0 100755 --- a/index.js +++ b/index.js @@ -64,7 +64,7 @@ const run = async() => { program.option(trigger, description, (...args) => fn(config, ...args)); }); - commands.forEach(({ trigger, description, fn, options }) => { + commands.forEach(({ trigger, description, fn }) => { program .command(trigger) .description(description) From aef29ed6eb484c6adb9d6af7ddc8ef7c39437a92 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Mon, 30 Apr 2018 22:45:32 +0200 Subject: [PATCH 5/9] Create more advanced logger * Avoid having to use chalk all the time --- lib/commands/configure/index.js | 2 +- lib/commands/verify/index.js | 2 +- .../disableCertificateVerification/index.js | 5 ++--- lib/utils/logger/index.js | 20 +++++++++++++++++-- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/commands/configure/index.js b/lib/commands/configure/index.js index c5ed370..3f09b17 100644 --- a/lib/commands/configure/index.js +++ b/lib/commands/configure/index.js @@ -8,7 +8,7 @@ const chalk = require('chalk'); const configure = async() => { if(await checkConfigExists()){ - logger.log(chalk.red.bold('⚠️ Mergify is already configured. Configuring again will override the existing file.')); + logger.warn('⚠️ Mergify is already configured. Configuring again will override the existing file.'); } try { diff --git a/lib/commands/verify/index.js b/lib/commands/verify/index.js index 4c26a67..d1a597a 100644 --- a/lib/commands/verify/index.js +++ b/lib/commands/verify/index.js @@ -23,7 +23,7 @@ const verify = async({ userId }) => { } catch (error) { spinner.stop(); logger.log('\n🙀 Oh no, could not complete verify. Please review your config'); - logger.log(error); + logger.err(error); process.exit(1); } }; diff --git a/lib/options/disableCertificateVerification/index.js b/lib/options/disableCertificateVerification/index.js index 2f3fdfe..d2861b4 100644 --- a/lib/options/disableCertificateVerification/index.js +++ b/lib/options/disableCertificateVerification/index.js @@ -1,8 +1,7 @@ -const { logger } = require('../../utils/logger'); -const chalk = require('chalk'); +const {logger} = require('../../utils/logger'); const disableCertificateVerification = async() => { - logger.log(chalk.red.bold('⚠️ Disabling certificate verification. This is unsafe and should only be used as last resort.')); + logger.warn('⚠️ Disabling certificate verification. This is unsafe and should only be used as last resort.'); process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; return; }; diff --git a/lib/utils/logger/index.js b/lib/utils/logger/index.js index 2f81c59..f91a12e 100644 --- a/lib/utils/logger/index.js +++ b/lib/utils/logger/index.js @@ -1,3 +1,19 @@ +const chalk = require('chalk'); + +var Logger = function(){}; + +Logger.prototype.log = function(...args) { + console.log(...args); +} + +Logger.prototype.warn = function(...args) { + console.log(chalk.yellow.bold(...args)); +} + +Logger.prototype.err = function(...args) { + console.log(chalk.red.bold(...args)); +} + module.exports = { - logger: console -}; + logger: new Logger() +} From e45afa67ecfaeefcbbb82cca5d765089b9bd1a31 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Tue, 1 May 2018 08:31:01 +0200 Subject: [PATCH 6/9] Add emoticons to logger --- lib/commands/configure/index.js | 2 +- lib/commands/verify/index.js | 2 +- lib/options/disableCertificateVerification/index.js | 2 +- lib/utils/logger/index.js | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/commands/configure/index.js b/lib/commands/configure/index.js index 3f09b17..53940ba 100644 --- a/lib/commands/configure/index.js +++ b/lib/commands/configure/index.js @@ -8,7 +8,7 @@ const chalk = require('chalk'); const configure = async() => { if(await checkConfigExists()){ - logger.warn('⚠️ Mergify is already configured. Configuring again will override the existing file.'); + logger.warn('Mergify is already configured. Configuring again will override the existing file.'); } try { diff --git a/lib/commands/verify/index.js b/lib/commands/verify/index.js index d1a597a..ca4e063 100644 --- a/lib/commands/verify/index.js +++ b/lib/commands/verify/index.js @@ -22,7 +22,7 @@ const verify = async({ userId }) => { return process.exit(0); } catch (error) { spinner.stop(); - logger.log('\n🙀 Oh no, could not complete verify. Please review your config'); + logger.err('Oh no, could not complete verify. Please review your config'); logger.err(error); process.exit(1); } diff --git a/lib/options/disableCertificateVerification/index.js b/lib/options/disableCertificateVerification/index.js index d2861b4..837ee69 100644 --- a/lib/options/disableCertificateVerification/index.js +++ b/lib/options/disableCertificateVerification/index.js @@ -1,7 +1,7 @@ const {logger} = require('../../utils/logger'); const disableCertificateVerification = async() => { - logger.warn('⚠️ Disabling certificate verification. This is unsafe and should only be used as last resort.'); + logger.warn('Disabling certificate verification. This is unsafe and should only be used as last resort.'); process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; return; }; diff --git a/lib/utils/logger/index.js b/lib/utils/logger/index.js index f91a12e..7fd79ed 100644 --- a/lib/utils/logger/index.js +++ b/lib/utils/logger/index.js @@ -7,11 +7,11 @@ Logger.prototype.log = function(...args) { } Logger.prototype.warn = function(...args) { - console.log(chalk.yellow.bold(...args)); + console.log('\n⚠️', chalk.yellow.bold(...args)); } Logger.prototype.err = function(...args) { - console.log(chalk.red.bold(...args)); + console.log('\n🙀', chalk.red.bold(...args)); } module.exports = { From 28a144040c8a8eabe5046258d77d1dc0abadf404 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Tue, 1 May 2018 08:41:57 +0200 Subject: [PATCH 7/9] Use object literal instead of constructor --- lib/utils/logger/index.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/lib/utils/logger/index.js b/lib/utils/logger/index.js index 7fd79ed..cd4c0c7 100644 --- a/lib/utils/logger/index.js +++ b/lib/utils/logger/index.js @@ -1,19 +1,20 @@ const chalk = require('chalk'); -var Logger = function(){}; +const Logger = { + log : (...args) =>{ + console.log(...args); + }, + + warn : (...args) =>{ + console.log('⚠️', chalk.yellow.bold(...args)); + }, + + err : (...args) =>{ + console.log('🙀', chalk.red.bold(...args)); + } -Logger.prototype.log = function(...args) { - console.log(...args); -} - -Logger.prototype.warn = function(...args) { - console.log('\n⚠️', chalk.yellow.bold(...args)); -} - -Logger.prototype.err = function(...args) { - console.log('\n🙀', chalk.red.bold(...args)); -} +}; module.exports = { - logger: new Logger() + logger: Logger } From 44f309eda2ec842348b2fdd02d0aaef6d536cf6a Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Tue, 1 May 2018 08:46:30 +0200 Subject: [PATCH 8/9] Remove duplication --- index.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/index.js b/index.js index fc6a4b0..2442312 100755 --- a/index.js +++ b/index.js @@ -71,14 +71,6 @@ const run = async() => { .action((...args) => fn(config, ...args)); }); - commands.forEach(({ trigger, description, fn }) => { - program - .command(trigger) - .description(description) - .action((...args) => fn(config, ...args)); - - }); - return program; }; From 89fdc0194ee61c2bec27850a8090029e8b772156 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Tue, 1 May 2018 09:28:35 +0200 Subject: [PATCH 9/9] Start adding tests --- lib/utils/logger/index.js | 30 ++++++++++++------------ lib/utils/logger/index.spec.js | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 lib/utils/logger/index.spec.js diff --git a/lib/utils/logger/index.js b/lib/utils/logger/index.js index cd4c0c7..23791d8 100644 --- a/lib/utils/logger/index.js +++ b/lib/utils/logger/index.js @@ -1,20 +1,22 @@ const chalk = require('chalk'); -const Logger = { - log : (...args) =>{ - console.log(...args); - }, - - warn : (...args) =>{ - console.log('⚠️', chalk.yellow.bold(...args)); - }, - - err : (...args) =>{ - console.log('🙀', chalk.red.bold(...args)); +const getLogger = function(transport){ + return { + log : (...args) =>{ + transport.log(...args); + }, + + warn : (...args) =>{ + transport.log('⚠️', chalk.yellow.bold(...args)); + }, + + err : (...args) =>{ + transport.log('🙀', chalk.red.bold(...args)); + } } - -}; +} module.exports = { - logger: Logger + logger: getLogger(console), + getLogger: getLogger } diff --git a/lib/utils/logger/index.spec.js b/lib/utils/logger/index.spec.js new file mode 100644 index 0000000..f33c240 --- /dev/null +++ b/lib/utils/logger/index.spec.js @@ -0,0 +1,42 @@ +const {getLogger} = require('./index'); + +describe('utils/logger', () => { + let mockConsole; + let logger; + + beforeAll(() => { + mockConsole = { + content: '', + + log: function(...args) { + this.content = [...args]; + }, + } + logger = getLogger(mockConsole); + }); + + test('Testing normal log message', () => { + + logger.log('test', 'plop'); + + expect(mockConsole.content).toHaveLength(2); + expect(mockConsole.content).toEqual(['test', 'plop']); + }); + + test('Testing warning log message', () => { + + logger.warn('test', 'plop'); + + expect(mockConsole.content).toHaveLength(2); + expect(mockConsole.content[0]).toEqual('⚠️'); + }); + + test('Testing error log message', () => { + + logger.err('test', 'plop'); + + expect(mockConsole.content).toHaveLength(2); + expect(mockConsole.content[0]).toEqual('🙀'); + }); +}); + \ No newline at end of file