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
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +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 { review } = require('./lib/commands/review');

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

Expand All @@ -27,6 +28,12 @@ const commands = [
fn: getAllSubmitted
},
{
trigger: 'review',
description: 'Return changes to dirty state so you can review code locally',
fn: review
},
{
trigger: 'configure',
trigger: 'configure',
description: 'Setup or update required config',
fn: configure
Expand Down
48 changes: 48 additions & 0 deletions lib/commands/review/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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');
}
};

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: 13 additions & 12 deletions lib/utils/readConfig/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
const { readFile } = require('fs');
const { promisify } = require('util');
const readFileAsync = promisify(readFile);
const { logger } = require('../logger');
const { getConfigPath } = require('../getConfigPath');

async function readConfig() {
try {
const config = await readFileAsync(getConfigPath(), '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`);

async function readConfig() {
return readFileAsync(getConfigPath(), 'utf8')
.then((config) => {
const _config = JSON.parse(config);
return _config;
})
.catch(() => {
return err;
});
}

module.exports = {
readConfig
};
};
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);
});