Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

Commit

Permalink
feat(repo): Add search feature to repo command
Browse files Browse the repository at this point in the history
Enables using vanilla GitHub search syntax to search repos

Re #451
  • Loading branch information
Saulo Vallory authored and Ryan Garant committed Oct 11, 2019
1 parent f4a80f9 commit 0050a3e
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,44 @@ gh re --label --update bug --color color --repo gh
gh re --label --update bug --color color --user eduardolundgren --repo gh
```

### 11. Search

Search repositories for the terms in name, description or readme.
You can filter the results using GitHub's [search qualifiers](https://help.github.com/articles/searching-for-repositories/)

| Option | Usage | Type |
| ------------------ | ------------ | ----------------------------------------------- |
| `-s`, `--search` | **Required** | `Boolean` |
| `-d`, `--detailed` | _Optional_ | `Boolean` |
| `-u`, `--user` | _Optional_ | `String` |
| `-t`, `--type` | _Optional_ | [`all`, `owner`, `public`, `private`, `member`] |

#### Examples

- Search private repositories you have access to with the term "framework".

```
gh re --search framework --type private
```

or

```
gh re --search framework is:private
```

- Search for repositories mentioning "hapi" on tokilabs organization and show detailed info

```
gh re -d -o tokilabs --search hapi
```

or

```
gh re -d --search hapi org:tokilabs
```

## Gists

```
Expand Down
63 changes: 62 additions & 1 deletion src/cmds/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const name = 'Repo'
export const DETAILS = {
alias: 're',
description: 'Provides a set of util commands to work with Repositories.',
commands: ['browser', 'clone', 'delete', 'fork', 'list', 'new', 'update'],
commands: ['browser', 'clone', 'delete', 'fork', 'list', 'new', 'update', 'search'],
options: {
browser: Boolean,
clone: Boolean,
Expand All @@ -48,6 +48,7 @@ export const DETAILS = {
private: Boolean,
protocol: String,
repo: String,
search: String,
type: ['all', 'forks', 'member', 'owner', 'public', 'private', 'source'],
update: String,
user: String,
Expand All @@ -66,6 +67,7 @@ export const DETAILS = {
p: ['--private'],
P: ['--protocol'],
r: ['--repo'],
s: ['--search'],
t: ['--type'],
U: ['--update'],
u: ['--user'],
Expand Down Expand Up @@ -307,6 +309,29 @@ export async function run(options, done) {
listCallback_(options, listData)

await afterHooks('repo.list', { options })
} else if (options.search) {
if (options.organization) {
user = options.organization
options.type = options.type || TYPE_ALL
} else {
user = options.user
options.type = options.type || TYPE_OWNER
}

if (options.isTTY.out) {
logger.log(
'Searching ' +
logger.colors.green(options.type) +
' repos in ' +
logger.colors.green(user)
)
}

try {
await search(options, user)
} catch (error) {
logger.error(`Can't list repos.\n${error}`)
}
} else if (options.new && !options.label) {
if (!options.new.trim()) {
options = produce(options, draft => {
Expand Down Expand Up @@ -562,6 +587,42 @@ function updateLabel(options, user): Promise<Octokit.Response<Octokit.IssuesUpda
return options.GitHub.issues.updateLabel(payload)
}

async function search(options, user) {
let terms = [options.search]

// @fix currently options.user is always set, so I'm checking argv
if (options.argv.original.some(arg => arg === '-u' || arg === '--user')) {
terms.push(`user:${options.user}`)
}

if (options.organization) {
terms.push(`org:${options.organization}`)
}

if (options.type === 'public' || options.type === 'private') {
terms.push(`is:${options.type}`)
delete options.type
}

// add remaining search terms
terms = terms.concat(options.argv.remain.slice(1))

const payload = {
user,
q: terms.join(' '),
// type: options.type,
per_page: 100,
}

const { data } = await options.github.search.repos(payload)

// console.log(repos)
// process.exit()
// const re = new RegExp(options.search, 'i')
// repos = repos.filter(r => re.test(r.name))
listCallback_(options, data.items.slice(0, 10))
}

function normalizeColor(color) {
return color.includes('#') ? color.replace('#', '') : color
}

0 comments on commit 0050a3e

Please sign in to comment.