Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get original forked repository with the github api?

I am wondering how to get the original repository from my forked repositories.

I am using https://api.github.com/users/:user/repos?type=forks to get the list of forked repositories. It returns various links on the forks, but I cannot find how to get the original repository.

Any idea ?

Note: something else that looping over https://api.github.com/users/:user/events

like image 828
poussma Avatar asked Apr 09 '15 21:04

poussma


1 Answers

You can load a list of the forked repositories using just the endpoint you mentioned:

curl https://api.github.com/users/:user/repos?type=forks

which will return something like the following JSON feed (irrelevant informations omitted):

[
  {
    "id": 24328834,
    "name": "repo_name",
    "full_name": ":user/repo_name",
    "owner": {
    }
    ...
    "url": "https://api.github.com/repos/:user/repo_name",
    ...
  }
  {
    "id": 24328835,
    "name": "repo_name_2",
    "full_name": ":user/repo_name_2",
    "owner": {
      ...
    }
    ...
    "url": "https://api.github.com/repos/:user/repo_name_2"
    ...
  }
]

EDITED based on @jasonrudolph suggestions

Then, out from the list returned, you pull out the repo_name under the name element key and use it to construct the repository url, or just refer to the url element to issue a GET request against as follows:

curl https://api.github.com/repos/:user/repo_name

that in return will give you the requested user repository informations (note that is the simplest of queried urls) from which you can retrieve:

  • The parent JSON element that holds the repository, from which the issued one was forked, informations including its id / name / url...
  • The source JSON element which holds the original repository information, in case the issued repository is a fork of another fork.

Something like the following (here the parent is nothing but the source, i.e. the repository was forked from the original one):

{
  "id": 24328834,
  "name": "repo_name",
  "full_name": "user/repo_name",
  "owner": {
    ...
  },
  ...
  "parent": {
    "id": 354448,
    "name": "repo_name",
    "full_name": "real_owner_name/repo_name",
    "owner": {
      ...
    }
  }
  "source": {
    "id": 354448,
    "name": "repo_name",
    "full_name": "real_owner_name/repo_name",
    "owner": {
      ...
    }
  }
}
like image 180
tmarwen Avatar answered Nov 30 '22 23:11

tmarwen