Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1: Add organization cli parameter to filter by organization name #19

Merged
merged 6 commits into from
Oct 14, 2023
Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ Options:
-f, --find <keyword>
The keyword you want to search for. Example: es6

-o, --org <org name> (optional)
GitHub organisation name. Example: GitHub

-l, --limit <number>
Limit the search results to the specified number. Default is 10

Expand All @@ -83,7 +86,6 @@ Options:
Outputs stack trace in case an exception is thrown
```


### Non-verbose output

```sh
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "src/cli.js",
"scripts": {
"test": "mocha",
"build": "ncc build src/cli.js -o bin && mv bin/index.js bin/starred_search"
"build": "ncc build src/cli.js -o bin && mv bin/index.js bin/starred_search",
"all": "npm test && npm run build"
},
"bin": {
"@link-/starred_search": "bin/starred_search",
Expand All @@ -32,7 +33,7 @@
"author": "Link-",
"license": "ISC",
"dependencies": {
"@vercel/ncc": "^0.28.4",
"@vercel/ncc": "^0.38.0",
"arg": "^4.1.3",
"axios": "^0.21.1",
"cachedir": "^2.3.0",
Expand Down
6 changes: 6 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Options:

-f, --find <keyword>
The keyword you want to search for. Example: es6

-o, --org <org name> (optional)
GitHub organisation name. Example: GitHub

-l, --limit <number>
Limit the search results to the specified number. Default is 10
Expand All @@ -44,6 +47,7 @@ const parseArgs = (rawArgs) => {
'--help': Boolean,
'--user': String,
'--find': String,
'--org': String,
'--cache-dir': String,
'--limit': Number,
'--verbose': Boolean,
Expand All @@ -53,6 +57,7 @@ const parseArgs = (rawArgs) => {
'-h': '--help',
'-u': '--user',
'-f': '--find',
'-o': '--org',
'-c': '--cache-dir',
'-l': '--limit',
'-V': '--verbose',
Expand All @@ -64,6 +69,7 @@ const parseArgs = (rawArgs) => {
help: args['--help'],
user: args['--user'],
findParam: args['--find'],
organization: args['--org'],
cacheDir: args['--cache-dir'] || cachedir('starredsearch'),
limit: args['--limit'] || 10,
verbose: args['--verbose'] || false,
Expand Down
19 changes: 17 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ const fetch_starred_repos = (options, pages) => {
* @param {*} options
*/
const search = (options) => {
(options.verbose) ? console.log(chalk.bold.green(`🕵 INFO: Searching for "${options.findParam}" in "${options.user}"'s starred catalogue`)) : null;
let organizationLog = '';
if (options.verbose) {
organizationLog = options.organization ? `(belonging to org: ${options.organization})` : '';
console.log(chalk.bold.green(`🕵 INFO: Searching for "${options.findParam}" ${organizationLog} in "${options.user}"'s starred catalogue`))
}

validate_parameters(options);

return calculate_pages(options)
Expand Down Expand Up @@ -162,11 +167,21 @@ const search = (options) => {
* then we do a full-text search only in the 'full_name,
* description and homepage' fields.
*/
let flattenedData = data.flat();

// if the organization option was passed, run the search on the repos
// belonging to that organization only
if (options.organization) {
flattenedData = flattenedData.filter(repo =>
repo.owner.type == "Organization" && repo.owner.login.toLowerCase() == options.organization.toLowerCase()
);
}

const searcher = new MiniSearch({
fields: ['full_name', 'description', 'homepage'],
storeFields: ['full_name', 'description', 'homepage', 'forks', 'stargazers_count']
});
searcher.addAll(data.flat());
searcher.addAll(flattenedData);
let results = searcher.search(options.findParam);
// Limit the search results
results.splice(options.limit);
Expand Down