如何从一个GitHub repo列表操作获取所有fork源?

问题描述 投票:1回答:1

我想获得GitHub用户的repos的所有源代码库。我必须分两步完成。例如,首先,我可以使用https://api.github.com/users/nmap/repos获取用户nmap的所有回购。但sourceparent不存在。只有当我为每个回购打电话给https://api.github.com/repos/nmap/:repo_name时,我才能得到sourceparent

{
  "id": 63894766,
  "name": "libpcap",
  "full_name": "nmap/libpcap",
  "owner": {
    "login": "nmap",
    "id": 63385,
    "avatar_url": "https://avatars3.githubusercontent.com/u/63385?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/nmap",
    "html_url": "https://github.com/nmap",
    "followers_url": "https://api.github.com/users/nmap/followers",
    "following_url": "https://api.github.com/users/nmap/following{/other_user}",
    "gists_url": "https://api.github.com/users/nmap/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/nmap/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/nmap/subscriptions",
    "organizations_url": "https://api.github.com/users/nmap/orgs",
    "repos_url": "https://api.github.com/users/nmap/repos",
    "events_url": "https://api.github.com/users/nmap/events{/privacy}",
    "received_events_url": "https://api.github.com/users/nmap/received_events",
    "type": "Organization",
    "site_admin": false
  },
  "private": false,
  "html_url": "https://github.com/nmap/libpcap",
  "description": "the LIBpcap interface to various kernel packet capture mechanism",
  "fork": true,
  "url": "https://api.github.com/repos/nmap/libpcap",
  "forks_url": "https://api.github.com/repos/nmap/libpcap/forks",
  "keys_url": "https://api.github.com/repos/nmap/libpcap/keys{/key_id}",
  "collaborators_url": "https://api.github.com/repos/nmap/libpcap/collaborators{/collaborator}",
  "teams_url": "https://api.github.com/repos/nmap/libpcap/teams",
  "hooks_url": "https://api.github.com/repos/nmap/libpcap/hooks",
  "issue_events_url": "https://api.github.com/repos/nmap/libpcap/issues/events{/number}",
  "events_url": "https://api.github.com/repos/nmap/libpcap/events",
  "assignees_url": "https://api.github.com/repos/nmap/libpcap/assignees{/user}",
  "branches_url": "https://api.github.com/repos/nmap/libpcap/branches{/branch}",
  "tags_url": "https://api.github.com/repos/nmap/libpcap/tags",
  "blobs_url": "https://api.github.com/repos/nmap/libpcap/git/blobs{/sha}",
  "git_tags_url": "https://api.github.com/repos/nmap/libpcap/git/tags{/sha}",
  "git_refs_url": "https://api.github.com/repos/nmap/libpcap/git/refs{/sha}",
  "trees_url": "https://api.github.com/repos/nmap/libpcap/git/trees{/sha}",
  "statuses_url": "https://api.github.com/repos/nmap/libpcap/statuses/{sha}",
  "languages_url": "https://api.github.com/repos/nmap/libpcap/languages",
  "stargazers_url": "https://api.github.com/repos/nmap/libpcap/stargazers",
  "contributors_url": "https://api.github.com/repos/nmap/libpcap/contributors",
  "subscribers_url": "https://api.github.com/repos/nmap/libpcap/subscribers",
  "subscription_url": "https://api.github.com/repos/nmap/libpcap/subscription",
  "commits_url": "https://api.github.com/repos/nmap/libpcap/commits{/sha}",
  "git_commits_url": "https://api.github.com/repos/nmap/libpcap/git/commits{/sha}",
  "comments_url": "https://api.github.com/repos/nmap/libpcap/comments{/number}",
  "issue_comment_url": "https://api.github.com/repos/nmap/libpcap/issues/comments{/number}",
  "contents_url": "https://api.github.com/repos/nmap/libpcap/contents/{+path}",
  "compare_url": "https://api.github.com/repos/nmap/libpcap/compare/{base}...{head}",
  "merges_url": "https://api.github.com/repos/nmap/libpcap/merges",
  "archive_url": "https://api.github.com/repos/nmap/libpcap/{archive_format}{/ref}",
  "downloads_url": "https://api.github.com/repos/nmap/libpcap/downloads",
  "issues_url": "https://api.github.com/repos/nmap/libpcap/issues{/number}",
  "pulls_url": "https://api.github.com/repos/nmap/libpcap/pulls{/number}",
  "milestones_url": "https://api.github.com/repos/nmap/libpcap/milestones{/number}",
  "notifications_url": "https://api.github.com/repos/nmap/libpcap/notifications{?since,all,participating}",
  "labels_url": "https://api.github.com/repos/nmap/libpcap/labels{/name}",
  "releases_url": "https://api.github.com/repos/nmap/libpcap/releases{/id}",
  "deployments_url": "https://api.github.com/repos/nmap/libpcap/deployments",
  "created_at": "2016-07-21T18:55:31Z",
  "updated_at": "2017-11-12T14:13:21Z",
  "pushed_at": "2016-10-27T03:27:01Z",
  "git_url": "git://github.com/nmap/libpcap.git",
  "ssh_url": "[email protected]:nmap/libpcap.git",
  "clone_url": "https://github.com/nmap/libpcap.git",
  "svn_url": "https://github.com/nmap/libpcap",
  "homepage": "http://www.tcpdump.org/",
  "size": 7706,
  "stargazers_count": 10,
  "watchers_count": 10,
  "language": "C",
  "has_issues": false,
  "has_projects": true,
  "has_downloads": true,
  "has_wiki": false,
  "has_pages": false,
  "forks_count": 6,
  "mirror_url": null,
  "archived": false,
  "open_issues_count": 0,
  "license": null,
  "forks": 6,
  "open_issues": 0,
  "watchers": 10,
  "default_branch": "master",
  "organization": {
    "login": "nmap",
    "id": 63385,
    "avatar_url": "https://avatars3.githubusercontent.com/u/63385?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/nmap",
    "html_url": "https://github.com/nmap",
    "followers_url": "https://api.github.com/users/nmap/followers",
    "following_url": "https://api.github.com/users/nmap/following{/other_user}",
    "gists_url": "https://api.github.com/users/nmap/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/nmap/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/nmap/subscriptions",
    "organizations_url": "https://api.github.com/users/nmap/orgs",
    "repos_url": "https://api.github.com/users/nmap/repos",
    "events_url": "https://api.github.com/users/nmap/events{/privacy}",
    "received_events_url": "https://api.github.com/users/nmap/received_events",
    "type": "Organization",
    "site_admin": false
  },
  "parent": {
    "id": 9435887,
    "name": "libpcap",
    "full_name": "the-tcpdump-group/libpcap",
    "owner": {
      "login": "the-tcpdump-group",
      "id": 4154908,
      "avatar_url": "https://avatars0.githubusercontent.com/u/4154908?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/the-tcpdump-group",
      "html_url": "https://github.com/the-tcpdump-group",
      "followers_url": "https://api.github.com/users/the-tcpdump-group/followers",
      "following_url": "https://api.github.com/users/the-tcpdump-group/following{/other_user}",
      "gists_url": "https://api.github.com/users/the-tcpdump-group/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/the-tcpdump-group/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/the-tcpdump-group/subscriptions",
      "organizations_url": "https://api.github.com/users/the-tcpdump-group/orgs",
      "repos_url": "https://api.github.com/users/the-tcpdump-group/repos",
      "events_url": "https://api.github.com/users/the-tcpdump-group/events{/privacy}",
      "received_events_url": "https://api.github.com/users/the-tcpdump-group/received_events",
      "type": "Organization",
      "site_admin": false
    },
    "private": false,
    "html_url": "https://github.com/the-tcpdump-group/libpcap",
    "description": "the LIBpcap interface to various kernel packet capture mechanism",
    "fork": false,
    "url": "https://api.github.com/repos/the-tcpdump-group/libpcap",
    "forks_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/forks",
    "keys_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/keys{/key_id}",
    "collaborators_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/collaborators{/collaborator}",
    "teams_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/teams",
    "hooks_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/hooks",
    "issue_events_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/issues/events{/number}",
    "events_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/events",
    "assignees_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/assignees{/user}",
    "branches_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/branches{/branch}",
    "tags_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/tags",
    "blobs_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/git/blobs{/sha}",
    "git_tags_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/git/tags{/sha}",
    "git_refs_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/git/refs{/sha}",
    "trees_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/git/trees{/sha}",
    "statuses_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/statuses/{sha}",
    "languages_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/languages",
    "stargazers_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/stargazers",
    "contributors_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/contributors",
    "subscribers_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/subscribers",
    "subscription_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/subscription",
    "commits_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/commits{/sha}",
    "git_commits_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/git/commits{/sha}",
    "comments_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/comments{/number}",
    "issue_comment_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/issues/comments{/number}",
    "contents_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/contents/{+path}",
    "compare_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/compare/{base}...{head}",
    "merges_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/merges",
    "archive_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/{archive_format}{/ref}",
    "downloads_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/downloads",
    "issues_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/issues{/number}",
    "pulls_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/pulls{/number}",
    "milestones_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/milestones{/number}",
    "notifications_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/notifications{?since,all,participating}",
    "labels_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/labels{/name}",
    "releases_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/releases{/id}",
    "deployments_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/deployments",
    "created_at": "2013-04-14T21:46:36Z",
    "updated_at": "2017-12-13T16:20:18Z",
    "pushed_at": "2017-12-11T20:56:20Z",
    "git_url": "git://github.com/the-tcpdump-group/libpcap.git",
    "ssh_url": "[email protected]:the-tcpdump-group/libpcap.git",
    "clone_url": "https://github.com/the-tcpdump-group/libpcap.git",
    "svn_url": "https://github.com/the-tcpdump-group/libpcap",
    "homepage": "https://www.tcpdump.org/",
    "size": 10255,
    "stargazers_count": 686,
    "watchers_count": 686,
    "language": "C",
    "has_issues": true,
    "has_projects": false,
    "has_downloads": true,
    "has_wiki": false,
    "has_pages": false,
    "forks_count": 328,
    "mirror_url": null,
    "archived": false,
    "open_issues_count": 97,
    "license": {
      "key": "other",
      "name": "Other",
      "spdx_id": null,
      "url": null
    },
    "forks": 328,
    "open_issues": 97,
    "watchers": 686,
    "default_branch": "master"
  },
  "source": {
    "id": 9435887,
    "name": "libpcap",
    "full_name": "the-tcpdump-group/libpcap",
    "owner": {
      "login": "the-tcpdump-group",
      "id": 4154908,
      "avatar_url": "https://avatars0.githubusercontent.com/u/4154908?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/the-tcpdump-group",
      "html_url": "https://github.com/the-tcpdump-group",
      "followers_url": "https://api.github.com/users/the-tcpdump-group/followers",
      "following_url": "https://api.github.com/users/the-tcpdump-group/following{/other_user}",
      "gists_url": "https://api.github.com/users/the-tcpdump-group/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/the-tcpdump-group/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/the-tcpdump-group/subscriptions",
      "organizations_url": "https://api.github.com/users/the-tcpdump-group/orgs",
      "repos_url": "https://api.github.com/users/the-tcpdump-group/repos",
      "events_url": "https://api.github.com/users/the-tcpdump-group/events{/privacy}",
      "received_events_url": "https://api.github.com/users/the-tcpdump-group/received_events",
      "type": "Organization",
      "site_admin": false
    },
    "private": false,
    "html_url": "https://github.com/the-tcpdump-group/libpcap",
    "description": "the LIBpcap interface to various kernel packet capture mechanism",
    "fork": false,
    "url": "https://api.github.com/repos/the-tcpdump-group/libpcap",
    "forks_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/forks",
    "keys_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/keys{/key_id}",
    "collaborators_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/collaborators{/collaborator}",
    "teams_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/teams",
    "hooks_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/hooks",
    "issue_events_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/issues/events{/number}",
    "events_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/events",
    "assignees_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/assignees{/user}",
    "branches_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/branches{/branch}",
    "tags_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/tags",
    "blobs_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/git/blobs{/sha}",
    "git_tags_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/git/tags{/sha}",
    "git_refs_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/git/refs{/sha}",
    "trees_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/git/trees{/sha}",
    "statuses_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/statuses/{sha}",
    "languages_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/languages",
    "stargazers_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/stargazers",
    "contributors_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/contributors",
    "subscribers_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/subscribers",
    "subscription_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/subscription",
    "commits_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/commits{/sha}",
    "git_commits_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/git/commits{/sha}",
    "comments_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/comments{/number}",
    "issue_comment_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/issues/comments{/number}",
    "contents_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/contents/{+path}",
    "compare_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/compare/{base}...{head}",
    "merges_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/merges",
    "archive_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/{archive_format}{/ref}",
    "downloads_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/downloads",
    "issues_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/issues{/number}",
    "pulls_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/pulls{/number}",
    "milestones_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/milestones{/number}",
    "notifications_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/notifications{?since,all,participating}",
    "labels_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/labels{/name}",
    "releases_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/releases{/id}",
    "deployments_url": "https://api.github.com/repos/the-tcpdump-group/libpcap/deployments",
    "created_at": "2013-04-14T21:46:36Z",
    "updated_at": "2017-12-13T16:20:18Z",
    "pushed_at": "2017-12-11T20:56:20Z",
    "git_url": "git://github.com/the-tcpdump-group/libpcap.git",
    "ssh_url": "[email protected]:the-tcpdump-group/libpcap.git",
    "clone_url": "https://github.com/the-tcpdump-group/libpcap.git",
    "svn_url": "https://github.com/the-tcpdump-group/libpcap",
    "homepage": "https://www.tcpdump.org/",
    "size": 10255,
    "stargazers_count": 686,
    "watchers_count": 686,
    "language": "C",
    "has_issues": true,
    "has_projects": false,
    "has_downloads": true,
    "has_wiki": false,
    "has_pages": false,
    "forks_count": 328,
    "mirror_url": null,
    "archived": false,
    "open_issues_count": 97,
    "license": {
      "key": "other",
      "name": "Other",
      "spdx_id": null,
      "url": null
    },
    "forks": 328,
    "open_issues": 97,
    "watchers": 686,
    "default_branch": "master"
  },
  "network_count": 328,
  "subscribers_count": 8
}

我只想知道fork的来源。例如从the-tcpdump-group/libpcap了解nmap/libpcap。我想知道有没有办法从GitHub repo列表操作中获取所有fork源仅一步?

github github-api
1个回答
1
投票

您可以使用GraphQL API v4获取特定用户/组织的分叉回购列表,并获取父回购名称:

{
  organization(login: "nmap") {
    repositories(first: 100, isFork: true) {
      edges {
        node {
          parent {
            nameWithOwner
          }
        }
      }
    }
  }
}

Try it in the explorer

使用的示例:

package main

import (
    "fmt"
    "encoding/json"
    "net/http"
    "bytes"
    "io/ioutil"
)

var token string = "YOUR_ACCESS_TOKEN"

func main() {
    jsonMap := map[string]string{"query": `{
      organization(login: "nmap") {
        repositories(first: 100, isFork: true) {
          pageInfo {
            hasNextPage
            endCursor
          }
          edges {
            node {
              parent {
                nameWithOwner
              }
            }
          }
        }
      }
    }
    `}
    jsonData, _ := json.Marshal(jsonMap)

    client := &http.Client{}
    req, _ := http.NewRequest("POST", "https://api.github.com/graphql", bytes.NewBuffer(jsonData))
    req.Header.Add("Authorization",  fmt.Sprint("Token ", token))
    req.Header.Add("Content-Type", "application/json")
    resp, _ := client.Do(req)

    fmt.Println(resp.Status)
    if resp.StatusCode == http.StatusOK {
        bodyBytes, _ := ioutil.ReadAll(resp.Body)
        bodyString := string(bodyBytes)
        fmt.Println(bodyString)
    }
}

要进行分页,您可以检查hasNextPage值以检查是否必须发送另一个查询以获取下一个100分叉回购,使用after中的RepositoryConnection获取给定游标后的100下一个值(存储前一个endCursor的值,如果hasNextPagetrue)。

第一个要求:

{
  organization(login: "appcelerator-forks") {
    repositories(first: 100, isFork: true) {
      pageInfo {
        hasNextPage
        endCursor
      }
      edges {
        node {
          parent {
            nameWithOwner
          }
        }
      }
    }
  }
}

"hasNextPage": true"endCursor" : "Y3Vyc29yOnYyOpHOAQ1VZg=="。下一个请求将是:

{
  organization(login: "appcelerator-forks") {
    repositories(first: 100, isFork: true, after: "Y3Vyc29yOnYyOpHOAQ1VZg==") {
      pageInfo {
        hasNextPage
        endCursor
      }
      edges {
        node {
          parent {
            nameWithOwner
          }
        }
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.