Sub topic 12
Searching repositories, issues, and pull requests
This chapter assumes you can run gh while signed in (see the authentication chapter) and that you know how --json and --jq work from the structured output chapter. You also need to know what GitHub issues and pull requests are.
Two ways to search
gh gives you two search tools.
gh searchqueries all of GitHub through its search API.- The
--searchflag ongh issue listandgh pr listfilters inside the current repository.
Use gh search when you don't know which repository holds the answer. Use --search when you already work in one repository and want a narrower list.
Searching repositories
gh search repos takes keywords and flags that map to GitHub's search qualifiers.
gh search repos "command line" --language go --stars ">1000" --limit 5Quote values that contain > or <. Otherwise the shell treats them as redirects. Useful flags include --owner, --topic, --sort (such as stars), and --order.
Searching issues and pull requests
gh search issues finds issues. gh search prs finds pull requests. Both accept --repo, --author, --label, --state, and date filters such as --created.
gh search issues --repo cli/cli --label bug --state open --limit 10
gh search prs --author @me --state open
gh search prs --review-requested @me --state open@me stands for the signed-in user. The last command lists open pull requests waiting for your review across every repository.
To exclude results, put -- before the query. This stops gh from reading the leading - as a flag.
gh search issues --repo cli/cli --state open -- -label:bugBy default a query matches the title, body, and comments. Add --match title to restrict it to titles.
Filtering inside one repository
Inside a clone, --search accepts the same qualifier syntax as the GitHub web search box.
gh issue list --search "timeout in:title sort:updated-desc"
gh pr list --search "status:success review:approved"Combining search with JSON output
Search commands support --json and --jq. Search results include the repository, so your output can show where each hit lives.
gh search issues --repo cli/cli --label bug --state open --limit 10 \
--json number,title,url \
--jq '.[] | "\(.number)\t\(.title)\t\(.url)"'
gh search repos "terminal ui" --language go --limit 5 \
--json fullName,stargazersCount \
--jq '.[] | "\(.fullName)\t\(.stargazersCount)"'Run a search command with --json and no field list to see the fields available for that command.
Limits and common errors
- Result count. The default is 30 results. Raise it with
--limit. GitHub never returns more than 1,000 results for one query. - Rate limit. The search API allows far fewer requests per minute than the rest of the API, about 30 for authenticated users. A script that runs many searches in a loop can hit this limit and fail.
- Shell quoting. An unquoted
>creates a file named after your value and silently changes the query. - Wrong command.
gh search issuesdoes not return pull requests. Usegh search prsfor those. - Private repositories. Results include private repositories only if your token can access them.
Exercise
Write one command that lists the number and title of up to 20 open pull requests that you authored, across all of GitHub, one per line separated by a tab.
Show answer
gh search prs --author @me --state open --limit 20 --json number,title --jq '.[] | "\(.number)\t\(.title)"'
Exercise
Why would gh search issues --repo cli/cli --state open -label:bug fail, and how do you fix it?
Show answer
gh reads -label:bug as a flag. Put -- before the query: gh search issues --repo cli/cli --state open -- -label:bug.