Skip to content

Commit

Permalink
Merge pull request #21 from jaebradley/refactor-code-search
Browse files Browse the repository at this point in the history
Refactor code search
  • Loading branch information
jaebradley authored Dec 16, 2017
2 parents 5dbeb29 + f305f26 commit ae9e3be
Show file tree
Hide file tree
Showing 14 changed files with 642 additions and 225 deletions.
469 changes: 469 additions & 0 deletions src/data/constants/github/languages.js

Large diffs are not rendered by default.

24 changes: 2 additions & 22 deletions src/executables/ghs-code.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
#!/usr/bin/env node

/* eslint-disable no-console */

import CodeSearchOptionsSelector from '../services/CodeSearchOptionsSelector';
import SearchService from '../services/SearchService';
import MatchResultsParser from '../services/MatchResultsParser';
import MatchingFilesSelector from '../services/MatchingFilesSelector';
import GitHubCredentialSaver from '../services/GitHubCredentialSaver';

async function executeCodeCommand() {
if (!GitHubCredentialSaver.hasCredential()) {
GitHubCredentialSaver.save();
}

const searchService = new SearchService();
const optionsService = new CodeSearchOptionsSelector();
const options = await optionsService.selectSearchOptions();
const results = await searchService.searchCode(options);
const matches = MatchResultsParser.parse(results);
const filesSelector = new MatchingFilesSelector();
await filesSelector.selectMatchingFile(matches);
}
import CodeSearchCommandService from '../services/code/CodeSearchCommandService';

try {
executeCodeCommand();
CodeSearchCommandService.execute();
} catch (e) {
console.error(`Rut ro! Unexpected error: ${e}`);
}
71 changes: 0 additions & 71 deletions src/services/CodeSearchOptionsSelector.js

This file was deleted.

36 changes: 0 additions & 36 deletions src/services/GitHubCredentialSaver.js

This file was deleted.

23 changes: 0 additions & 23 deletions src/services/MatchingFileMessageFormatter.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/services/RepositorySearcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import GitHubClient from 'github';

class RepositorySearcher {
constructor(authorizationToken) {
this.client = new GitHubClient({ headers: { 'user-agent': 'jaebradley' } });
this.client.authenticate({
type: 'token',
token: authorizationToken,
});
}

async search(organizationName, repoNameContains) {
return this.client.search.repos({ q: `org:${organizationName} ${repoNameContains}` });
}
}

export default RepositorySearcher;
68 changes: 0 additions & 68 deletions src/services/SearchService.js

This file was deleted.

43 changes: 43 additions & 0 deletions src/services/code/CodeSearchCommandService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import CodeSearchParametersPrompter from './CodeSearchParametersPrompter';
import GitHubDataStorer from '../GitHubDataStorer';
import MatchResultsParser from './MatchResultsParser';
import MatchingFilesSelector from './MatchingFilesSelector';
import RepositorySearcher from '../RepositorySearcher';
import SetupCommandService from '../../services/SetupCommandService';
import SearchOptions from '../../data/SearchOptions';
import CodeSearcher from './CodeSearcher';


class CodeSearchCommandService {
static async execute() {
const hasAuthorizationToken = await GitHubDataStorer.hasAuthorizationToken();

if (!hasAuthorizationToken) {
await SetupCommandService.execute();
}

const authorizationToken = await GitHubDataStorer.getAuthorizationToken();
const repositorySearcher = new RepositorySearcher(authorizationToken);
const prompter = new CodeSearchParametersPrompter(repositorySearcher);

const {
queryString,
organizationName,
repositoryName,
language,
} = await prompter.promptSearchParameters();

const options = new SearchOptions(queryString, organizationName, repositoryName, language);

const codeSearcher = new CodeSearcher(authorizationToken);

const results = await codeSearcher.search(options);

const matches = MatchResultsParser.parse(results);

const filesSelector = new MatchingFilesSelector();
await filesSelector.selectMatchingFile(matches);
}
}

export default CodeSearchCommandService;
60 changes: 60 additions & 0 deletions src/services/code/CodeSearchParametersPrompter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import inquirer from 'inquirer';
import InquirerAutocompletePrompt from 'inquirer-autocomplete-prompt';
import fuzzy from 'fuzzy';

import GitHubLanguages from '../../data/constants/github/languages';

inquirer.registerPrompt('autocomplete', InquirerAutocompletePrompt);

class CodeSearchParametersPrompter {
constructor(repositorySearcher) {
this.repositorySearcher = repositorySearcher;
this.repositoryNames = null;
}

async promptSearchParameters() {
return inquirer.prompt([
{
name: 'queryString',
message: 'Input your search string',
validate: str => str.length > 0,
type: 'input',
},
{
name: 'organizationName',
message: 'Input optional organization/user name',
type: 'input',
},
{
name: 'repositoryName',
message: 'Input optional repository name',
type: 'autocomplete',
source: (answersSoFar, searchTerm) => {
const searchTermValue = searchTerm || '';
return this.findMatchingRepositories(answersSoFar.organizationName, searchTermValue);
},
},
{
name: 'language',
message: 'Choose optional language',
type: 'autocomplete',
source: (answersSoFar, input) => (
Promise.resolve(fuzzy.filter(input || '', GitHubLanguages).map(match => match.original))
),
},
]);
}

async findMatchingRepositories(organizationName, repositoryName) {
return this.repositorySearcher
.search(organizationName, repositoryName)
.then(result => result.data.items.map(repository => repository.name))
.then((repositoryNames) => {
let names = ['None'];
names = names.concat(repositoryNames);
return fuzzy.filter(repositoryName, names).map(match => match.original);
});
}
}

export default CodeSearchParametersPrompter;
42 changes: 42 additions & 0 deletions src/services/code/CodeSearcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import GitHubClient from 'github';

class CodeSearcher {
constructor(authorizationToken) {
this.authorizationToken = authorizationToken;
this.client = new GitHubClient({
headers: {
accept: 'application/vnd.github.v3.text-match+json',
'user-agent': 'jaebradley',
},
});
this.client.authenticate({
type: 'token',
token: this.authorizationToken,
});
}

async search(options) {
return this.client.search.code({ q: CodeSearcher.generateQueryString(options) });
}

static generateQueryString(options) {
let queryString = '';
if (options.organizationName) {
if (options.repositoryName && options.repositoryName !== 'None') {
queryString += `repo:${options.organizationName}/${options.repositoryName}`;
} else {
queryString += `org:${options.organizationName}`;
}
}

queryString += ` ${options.queryString}`;

if (options.language && options.language !== 'None') {
queryString += ` language:${options.language}`;
}

return queryString;
}
}

export default CodeSearcher;
Loading

0 comments on commit ae9e3be

Please sign in to comment.