16
votes

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?

3

3 Answers

10
votes

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 addClass function inside jQuery. Your query would look something like this:

https://api.github.com/search/code?q=addClass+in:file+language:js+repo:jquery/jquery

11
votes

While Gihub does not currently support code search without repo, user, or organization (see VonC's answer), codesearch does index some sources from Github via the codesearch API, albeit with an API less fully featured out than Github's.

For example, to search for wget invocations indexed from Github, call

curl "https://searchcode.com/api/codesearch_I/?q=wget&src=2"

The optional src parameter picks the code source (e.g., Github, BitBucket) that should be searched, and you can find its integer value for a source by altering the parameters of faceted search in the codesearch UI. The current value of src for Github is 2.

You can verify that the returned results from the above example come from github.com by viewing the the repo property of results items.

11
votes

You can do a code search without specifying a user/org/repo if you authenticate.

First, generate a personal access token for use for this purpose, from your Profile on GitHub's website: Settings -> Developer Settings -> Personal Access Token -> Generate New Token (you can leave all access options unticked, since you're just using to make web requests)

Now, your original GET request will work and return results, if you append the token to it:

https://api.github.com/search/code?q=addClass&access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx