Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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 { review } = require('./lib/commands/review');

const { readConfig } = require('./lib/utils/readConfig');

Expand All @@ -25,6 +26,11 @@ const options = [
description: 'Get all open merge request submitted to you',
fn: getAllSubmitted
},
{
trigger: '-r --review',
description: 'Return changes to dirty state so you can review code locally',
fn: review
},
{
trigger: '-c --configure',
description: 'Setup or update required config',
Expand Down
50 changes: 50 additions & 0 deletions lib/commands/review/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const inquirer = require('inquirer');
const { readdir } = require('fs');
const { promisify } = require('util');
const { spawn, exec } = require('child_process');
const { spinner } = require('../../utils/spinner');
const { logger } = require('../../utils/logger');
const { hasGit } = require('../../utils/hasGit');

const readDirAsync = promisify(readdir);

const getCommits = () => new Promise((resolve) => {
const gitLog = exec('git log --format=format:[%h]\\ %s -20');
gitLog.stdout.on('data', (data) => {
const commits = data.split('\n')
.filter(x => x !== '')
.map(x => x.substr(0, 40).concat('...'));

return resolve(commits);
});
});

const review = async({ userId }) => {
const currentWorkingDir = process.cwd();
const files = await readDirAsync(currentWorkingDir);
const itHasGit = hasGit(files);
if (itHasGit) {
const commits = await getCommits();
const choices = ['HEAD^', ...commits];
const { sha = 'HEAD^' } = inquirer.prompt([{
name: 'sha',
message: 'From which commit do you want to review?',
type: 'list',
default: 'HEAD^',
choices
}]);

spinner.start();
const reset = spawn('git', ['reset', sha]);
reset.on('close', (code) => spinner.stop());
} else {
spinner.stop();
logger.log('No git');
}

// return process.exit(0);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove this?

};

module.exports = {
review
};
7 changes: 7 additions & 0 deletions lib/utils/hasGit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { find, isNil } = require('ramda');

const hasGit = files => !isNil(find(f => f === '.git', files));

module.exports = {
hasGit
};
25 changes: 25 additions & 0 deletions lib/utils/hasGit/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { hasGit } = require('./index');

describe('utils/hasGit', () => {
test('it has git', async() => {
const files = [
'pizza',
'kebab',
'sushi',
'.git'
];

const itHasGit = await hasGit(files);
expect(itHasGit).toBe(true);
});
test('it doesn\'t has git', async() => {
const files = [
'pizza',
'kebab',
'sushi'
];

const itHasGit = await hasGit(files);
expect(itHasGit).toBe(false);
});
});
25 changes: 14 additions & 11 deletions lib/utils/readConfig/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ const { promisify } = require('util');
const readFileAsync = promisify(readFile);
const { logger } = require('../logger');

async function readConfig() {
const configFileName = `${__dirname}/../../../.config`;
try {
const config = await readFileAsync(configFileName, 'utf8');
return JSON.parse(config);
} catch (_) {
logger.log(`
const err = new Error(`
Oh no, \`mergify\` is not configured yet.
Let configure it
`);
return {};
}
Let configure it`);

function readConfig() {
const configFileName = `${__dirname}/../../../.config`;
return readFileAsync(configFileName, 'utf8')
.then((config) => {
const _config = JSON.parse(config);
return _config;
})
.catch(() => {
// logger.log(err.message);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Remove this as well?

return err;
});
}

module.exports = {
Expand Down
34 changes: 34 additions & 0 deletions lib/utils/readConfig/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,39 @@ describe('utils/readConfig', () => {
return expect(config).toEqual(stub);
});

test('reading a config with unreadable config', async() => {
const configPath = `${__dirname}/../../../.config`;

mock({
[configPath]: ''
});

const expectation = new Error(`
Oh no, \`mergify\` is not configured yet.
Let configure it`);

await readConfig()
.catch((err) => {
expect(err).rejects.toEqual(expectation);
});
});

test('reading a config with no config', async() => {
const configPath = `${__dirname}/../../../`;

mock({
[configPath]: {}
});

const expectation = new Error(`
Oh no, \`mergify\` is not configured yet.
Let configure it`);

await readConfig()
.catch((err) => {
expect(err).rejects.toEqual(expectation);
});
});

afterEach(mock.restore);
});