Reputation: 7301
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
Upvotes: 10
Views: 1905
Reputation: 6945
This is actually much easier to do with the GraphQL API. For example, this is how you query for a list of your own repos showing their name, whether they're a fork and their parent repo URL. You can paste it as-is into the GraphQL Explorer
query {
viewer {
repositories(first:10) {
nodes {
name,
isFork,
parent {
url
}
}
}
}
}
Output
{
"data": {
"viewer": {
"repositories": {
"nodes": [
{
"name": "cucumber",
"isFork": true,
"parent": {
"url": "https://github.com/cucumber/cucumber-ruby"
}
},
...
]
}
}
}
}
The parent.url
is the URL of the original repo the fork was cloned from.
Upvotes: 0
Reputation: 16354
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"
...
}
]
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:
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": {
...
}
}
}
Upvotes: 8