1

I am trying to get the readme file in a bunch of repos (that I don't own), and the problem I came across is that sometimes people name the readme file in different way (ex. README.md, README.MD, Readme.md or readme.md)

what I am doing now

GET https://api.github.com/repos/REPO_NAME/contents/README.md

the problem is that it doesn't return the readme file if it's named in a different way.

Also tried to use

GET https://api.github.com/search/code?q=repo:REPO_NAME+filename:readme

But I keep hitting the API limit for search (because I have lots of repos I am using)

welly4412
  • 53
  • 6

1 Answers1

3

You can use Github GraphQL API and use aliases for each possible filename, for example:

readme1: object(expression: "master:README.md") {
   ...
}
readme2: object(expression: "master:README.MD") {
   ...
}

The idea is that you perform only one request for the search and content. Also you can add more repository aliases and the cost remains at 1 point for each request which drastically reduces chance to reach rate limit.

For example the following will get the readme with 5 filenames possibilities for 2 repositories and this request only cost 1 point:

{
  repo1: repository(name: "material-ui", owner: "mui-org") {
    ...RepoFragment
  }
  repo2: repository(name: "linux", owner: "torvalds") {
    ...RepoFragment
  }
  rateLimit {
    cost
    remaining
    limit
  }
}

fragment RepoFragment on Repository {
  readme1: object(expression: "master:README.md") {
    ...ReadmeText
  }
  readme2: object(expression: "master:README.MD") {
    ...ReadmeText
  }
  readme3: object(expression: "master:readme.md") {
    ...ReadmeText
  }
  readme4: object(expression: "master:Readme.md") {
    ...ReadmeText
  }
  readme5: object(expression: "master:README") {
    ...ReadmeText
  }
}

fragment ReadmeText on GitObject {
  ... on Blob {
    text
  }
}

try this in the explorer

In the output:

"rateLimit": {
   "cost": 1,
   "remaining": 4977,
   "limit": 5000
}

This means that you can take advantage of aliases to slice your repository array into graphql aliases like above by building the request dynamically. You are only limited by the time it takes to process the query, so you must have a max number of repository per request. See this post for an example to build the query dynamically. Another post which maybe is more simple

Also, see graphql resource limitations doc

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
  • 1
    Thank you for your answer. I ended up using `octokit`. they have a `getReadme` function and I used it like this `octokit.rest.repos.getReadme({ owner, repo,path})` – welly4412 Apr 28 '21 at 13:09