Skip to content
Open
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
14 changes: 13 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ 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
.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',
Expand Down Expand Up @@ -52,6 +60,10 @@ const run = async() => {
await verify();
}

options.forEach(({trigger, description, fn}) => {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put whitespace around deconstruction arguments?

({ a, b }) => //...

program.option(trigger, description, (...args) => fn(config, ...args));
});

commands.forEach(({ trigger, description, fn }) => {
program
.command(trigger)
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/configure/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/verify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ 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.log(error);
logger.err('Oh no, could not complete verify. Please review your config');
logger.err(error);
process.exit(1);
}
};
Expand Down
11 changes: 11 additions & 0 deletions lib/options/disableCertificateVerification/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const {logger} = require('../../utils/logger');

const disableCertificateVerification = async() => {
logger.warn('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
};
23 changes: 21 additions & 2 deletions lib/utils/logger/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
const chalk = require('chalk');

const getLogger = function(transport){
return {
log : (...args) =>{
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting can be a little prettier:

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: console
};
logger: getLogger(console),
getLogger: getLogger
}
42 changes: 42 additions & 0 deletions lib/utils/logger/index.spec.js
Original file line number Diff line number Diff line change
@@ -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');
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use mock functions from Jest?
https://jestjs.io/docs/en/mock-functions

Then you could getLogger with { log: jest.fn() }

const mockConsole = {
  log: jest.fn(),
};

logger = getLogger(mockConsole);

logger.log('Pizza')

expect(mockConsole.log.mock.results[0].value).toBe('Pizza')


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('🙀');
});
});