2

I am trying to use the search code from Github as presented in the following link:

Search code

They gave an example to search a "String" inside Github without the API as the following:

search "amazing" in language:go

Is there a way to apply the last query to get results from the Rest API they are presenting?

Community
  • 1
  • 1

1 Answers1

3

The help page mentions:

The REST API supports the same qualifiers as GitHub.com.
To learn more about the format of the query, see Constructing a search query.
See "Searching code" for a detailed list of qualifiers.

In your case:

curl -G https://api.github.com/search/code          \
--data-urlencode 'q=amazing language:go org:github' \
--data-urlencode 'sort=indexed'                   \
--data-urlencode 'order=desc'                     \
-H 'Accept: application/vnd.github.preview' 

But: since 2013, for an API search (not Web search), a user, organization, or repository qualifier is now required, as confirmed here.

This thread mentions:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • it gives me back this error "message": "Must include at least one user, organization, or repository" – Amine Barrak Mar 05 '20 at 05:36
  • 1
    @AmineBarrak Yes, I have edited the answer accordingly. – VonC Mar 05 '20 at 05:42
  • But why the result is not the same as the result in the URL: https://github.com/search?utf8=%E2%9C%93&q=amazing%20language%3Ago&type=Code – Amine Barrak Mar 05 '20 at 05:57
  • @AmineBarrak Yes, that is the point of my edit: through API, you cannot have the same result because of that constraint. – VonC Mar 05 '20 at 06:00
  • Though that you add a organisation "org:github", even with that we don't have results in the item. So the solution here is to parse the UI web page? – Amine Barrak Mar 05 '20 at 06:10
  • 1
    @AmineBarrak If you really need the same result than the Web UI, yes. But a GraphQL query would be more accurate. – VonC Mar 05 '20 at 06:43
  • I found something little bit similar ==> curl --user "username" https://api.github.com/search/code?q=amazing+language:go – Amine Barrak Mar 05 '20 at 06:57
  • Can you please share a solution with GraphQL? – Amine Barrak Mar 05 '20 at 06:57
  • 1
    @AmineBarrak Your similar solution would have the same limitation (adding at least one user, organization, or repository) – VonC Mar 05 '20 at 06:58
  • 1
    @AmineBarrak I have edited the answer with additional information. – VonC Mar 05 '20 at 07:07
  • The request `api.github.com/search/code?q=filename:makefile` fails today with the same "at least one" error. The considerations say: "Except with filename searches, you must always include at least one search term when searching source code." Which I guess is not quite the same thing? – Nick K9 Dec 20 '22 at 19:16
  • @NickK9 Exactly, and it must be limited to a set of repositories or, as I just mentioned, the result would be incomplete. – VonC Dec 20 '22 at 19:19
  • I'm just pointing out that IIUC your second bullet point says that you can search by filename without providing the scope, but that doesn't seem to be the case. – Nick K9 Dec 20 '22 at 19:21
  • @NickK9 OK I will edit to add the search term criteria. – VonC Dec 20 '22 at 19:30
  • Sorry, but that's still not accurate. This fails with the same "at least one" error: `https://api.github.com/search/code?q=filename:makefile%20PHONY`. I don't think any code searches are exempt from the "at least one user/org/repo" requirement. – Nick K9 Dec 20 '22 at 19:46
  • @NickK9 Good point. I have edited the answer accordingly. – VonC Dec 20 '22 at 19:50