How to get original forked repository with the github api?

后端 未结 1 578
说谎
说谎 2021-02-09 23:47

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 li

相关标签:
1条回答
  • 2021-02-10 00:23

    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": {
          ...
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题