Skip to content

Commit

Permalink
#15: Added review command
Browse files Browse the repository at this point in the history
  • Loading branch information
RamonGebben committed Apr 20, 2018
1 parent cde9529 commit 1563df5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
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
46 changes: 46 additions & 0 deletions lib/commands/review/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 getShas = () => new Promise((resolve) => {
const gitLog = exec('git log --format=format:%H');
gitLog.stdout.on('data', (data) => {
resolve(data.split('\n').filter(x => x !== ''));
});
});

const review = async({ userId }) => {
const currentWorkingDir = process.cwd();
const files = await readDirAsync(currentWorkingDir);
const itHasGit = hasGit(files);
if (itHasGit) {
const shas = await getShas();
const choices = ['HEAD^', ...shas];
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);
};

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);
});
});

0 comments on commit 1563df5

Please sign in to comment.