Globally searching code by filename via APIv3

Hello,

I’m currently trying to create an application that automatically searches github repositories for a specific filename and grab data from repos that have that file.

I’m currently using APIv3 because GraphQL does not properly support searching for filenames in code, only repositories.

According to this doc, it states that searching by filename is exempt from the user/org/repository requirement, but when I send the following GET request, it returns a 422 Unprocessable Entity validation error:

curl -i 'https://api.github.com/search/code?q=filename:mmur.yaml&client_id=xxxx&client_secret=yyyy'

I would greatly appreicate help on this issue, or a workaround solution.

Are you replacing xxxx and yyyy with your server application’s client ID and client secret that you got when you followed the instructions for building an OAuth app? When I don’t include a client ID and client secret, I get the 422 status like you describe.

Yes, the client ID and client secret are substituted with the proper values. It still requires me to specify a user, repo, or org for the search to be successful.

I just tried it using a Personal Access Token:

curl -i 'https://api.github.com/search/code?q=filename:mmur.yaml&access_token=redacted'

and got the following response:

HTTP/1.1 200 OK
Date: Mon, 11 Jun 2018 22:01:04 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 5556
Server: GitHub.com
Status: 200 OK
X-RateLimit-Limit: 30
X-RateLimit-Remaining: 29
X-RateLimit-Reset: 1528754524
Cache-Control: no-cache
X-OAuth-Scopes:
X-Accepted-OAuth-Scopes:
X-GitHub-Media-Type: github.v3; format=json
Access-Control-Expose-Headers: ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy: default-src 'none'
X-Runtime-rack: 1.139346
Vary: Accept-Encoding
X-GitHub-Request-Id: AC94:6E19:8FAB2:BBE15:5B1EF11F

{
  "total_count": 1,
  "incomplete_results": false,
  "items": [
    {
      "name": "mmur.yaml",
      "path": "mmur.yaml",
      "sha": "13ffa930d4d917ae076906a14390637f71587de9",
      "url": "https://api.github.com/repositories/94132730/contents/mmur.yaml?ref=c93e2e6d4d4ca510358a00543f5a0fa453c1781e",
      "git_url": "https://api.github.com/repositories/94132730/git/blobs/13ffa930d4d917ae076906a14390637f71587de9",
      "html_url": "https://github.com/edward-shen/MMM-MBTA/blob/c93e2e6d4d4ca510358a00543f5a0fa453c1781e/mmur.yaml",
      "repository": {
        "id": 94132730,
        "node_id": "MDEwOlJlcG9zaXRvcnk5NDEzMjczMA==",
        "name": "MMM-MBTA",
        "full_name": "edward-shen/MMM-MBTA",
        "owner": {
          "login": "edward-shen",
          "id": 6173958,
          "node_id": "MDQ6VXNlcjYxNzM5NTg=",
          "avatar_url": "https://avatars1.githubusercontent.com/u/6173958?v=4",
          "gravatar_id": "",
          "url": "https://api.github.com/users/edward-shen",
          "html_url": "https://github.com/edward-shen",
          "followers_url": "https://api.github.com/users/edward-shen/followers",
          "following_url": "https://api.github.com/users/edward-shen/following{/other_user}",
          "gists_url": "https://api.github.com/users/edward-shen/gists{/gist_id}",
          "starred_url": "https://api.github.com/users/edward-shen/starred{/owner}{/repo}",
          "subscriptions_url": "https://api.github.com/users/edward-shen/subscriptions",
          "organizations_url": "https://api.github.com/users/edward-shen/orgs",
          "repos_url": "https://api.github.com/users/edward-shen/repos",
          "events_url": "https://api.github.com/users/edward-shen/events{/privacy}",
          "received_events_url": "https://api.github.com/users/edward-shen/received_events",
          "type": "User",
          "site_admin": false
        },
        "private": false,
        "html_url": "https://github.com/edward-shen/MMM-MBTA",
        "description": "MagicMirror² module for the Massachusetts Bay Transportation Authority",
        "fork": false,
        "url": "https://api.github.com/repos/edward-shen/MMM-MBTA",
        "forks_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/forks",
        "keys_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/keys{/key_id}",
        "collaborators_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/collaborators{/collaborator}",
        "teams_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/teams",
        "hooks_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/hooks",
        "issue_events_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/issues/events{/number}",
        "events_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/events",
        "assignees_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/assignees{/user}",
        "branches_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/branches{/branch}",
        "tags_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/tags",
        "blobs_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/git/blobs{/sha}",
        "git_tags_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/git/tags{/sha}",
        "git_refs_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/git/refs{/sha}",
        "trees_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/git/trees{/sha}",
        "statuses_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/statuses/{sha}",
        "languages_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/languages",
        "stargazers_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/stargazers",
        "contributors_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/contributors",
        "subscribers_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/subscribers",
        "subscription_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/subscription",
        "commits_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/commits{/sha}",
        "git_commits_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/git/commits{/sha}",
        "comments_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/comments{/number}",
        "issue_comment_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/issues/comments{/number}",
        "contents_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/contents/{+path}",
        "compare_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/compare/{base}...{head}",
        "merges_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/merges",
        "archive_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/{archive_format}{/ref}",
        "downloads_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/downloads",
        "issues_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/issues{/number}",
        "pulls_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/pulls{/number}",
        "milestones_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/milestones{/number}",
        "notifications_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/notifications{?since,all,participating}",
        "labels_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/labels{/name}",
        "releases_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/releases{/id}",
        "deployments_url": "https://api.github.com/repos/edward-shen/MMM-MBTA/deployments"
      },
      "score": 17.562763
    }
  ]
}

so the API is working as documented. I’m not sure what is going wrong. You may want to reach out to private support at https://github.com/contact so they can take a closer look at your private configuration.

Thank you for helping with my issue. I’ll use a PAT for now, but I’ll follow up with Github support to see what’s going on.

1 Like