How to search for code in GitHub with GitHub API?
Answer a question
I'm trying to search for some piece of code using the GitHub API V3 given only the keyword, not limiting by user, organization, or repository.
For example, if I want to search for all pieces of code that contain the keyword "addClass", the results would be https://github.com/search?q=addClass&type=Code&ref=searchresults without using GitHub API.
But how can I do the same thing through GitHub API? I tried https://api.github.com/search/code?q=addClass It says "Must include at least one user, organization, or repository". How can I fix this?
Answers
2020: As detailed in Mark Z.'s answer, using an authentication (Authorization': 'Token xxxx') allows for a code search.
get /search/code
You can use:
-
either a dedicated command-line tool like
feinoujc/gh-search-clighs code --extension js "import _ from 'lodash'" -
or the official GitHub CLI
gh, (after agh auth login) as show in issue 5117:gh api --method=GET "search/code?q=filename:test+extension:yaml+org:new-org"Or even:
gh api --method=GET search/code -f q='filename:test extension:yaml org:new-org' \ --jq '.items[] | [.repository.full_name,.path,.sha] | @tsv'That would get a line-based, tab-separated list of fields in this order: repo name, file path, git sha. (see
gh help formatting)
2014 (original answer): That seems related to the new restriction "New Validation Rule for Beta Code Search API" (Oct. 2013)
In order to support the expected volume of requests, we’re applying a new validation rule to the Code Search API. Starting today, you will need to scope your code queries to a specific set of users, organizations, or repositories.
So, the example of the API search code mentions now:
Suppose you want to find the definition of the
addClassfunction insidejQuery. Your query would look something like this:
https://api.github.com/search/code?q=addClass+in:file+language:js+repo:jquery/jquery
更多推荐


所有评论(0)