如何从一个GitHub存储库列表操作中获取所有fork资源? [英] How to get all fork sources from one GitHub repo list operation?

查看:77
本文介绍了如何从一个GitHub存储库列表操作中获取所有fork资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取GitHub用户仓库的所有源仓库.我必须分两个步骤来做.例如,首先,我可以使用https://api.github.com/users/nmap/repos来获取用户nmap的所有存储库.但是sourceparent不存在.只有当我为每个仓库调用https://api.github.com/repos/nmap/:repo_name时,我才能获得sourceparent:

I want to get all the source repos for a GitHub user's repos. I have to do it by two steps. For example, first, I can use https://api.github.com/users/nmap/repos to get all repos of user nmap. But the source and parent do not exist. Only when I call https://api.github.com/repos/nmap/:repo_name for each repo, I can get the source and parent:

{
  "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": "git@github.com: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": "git@github.com: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": "git@github.com: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
}

我只想知道分叉的来源.例如从nmap/libpcap知道the-tcpdump-group/libpcap.我想知道是否有一种方法可以仅一步就从GitHub存储库列表操作中获取所有fork资源?

I only want to know the source of the fork. e.g. know the-tcpdump-group/libpcap from nmap/libpcap. I want to know is there a way to get all the fork sources from a GitHub repo list operation for only one step?

推荐答案

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

You can use GraphQL API v4 to get the list of forked repo for a specific user/organization and get the parent repo name :

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

使用:

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个派生的回购,使用RepositoryConnection中的after获取给定值后的100个下一个值光标(如果hasNextPagetrue,则存储前一个endCursor的值).

To go through pagination, you can check the hasNextPage value to check if you have to send another query to grab the next 100 forked repo, Use after in RepositoryConnection to get the 100 next value after the given cursor (store the value of previous endCursor if hasNextPage is true).

第一个请求:

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

给予"hasNextPage": true& "endCursor" : "Y3Vyc29yOnYyOpHOAQ1VZg==".下一个请求将是:

gives "hasNextPage": true & "endCursor" : "Y3Vyc29yOnYyOpHOAQ1VZg==". Next request will be :

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

这篇关于如何从一个GitHub存储库列表操作中获取所有fork资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆