2017-12-13 6 views
1

Ich möchte alle Quell-Repos für die Repos eines GitHub-Benutzers abrufen. Ich muss es in zwei Schritten tun. Zum Beispiel kann ich zuerst https://api.github.com/users/nmap/repos verwenden, um alle Repos des Benutzers nmap zu erhalten. Aber die source und parent existieren nicht. Nur wenn ich https://api.github.com/repos/nmap/:repo_name für jeden Repo nennen, kann ich die source und parent erhalten:Wie erhalten Sie alle Gabelquellen von einer GitHub Repo-Liste?

{ 
    "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 
} 

ich nur die Quelle der Gabel wissen wollen. z.B. kennen the-tcpdump-group/libpcap von nmap/libpcap. Ich möchte wissen, gibt es eine Möglichkeit, alle Gabelquellen aus einer GitHub Repo-Liste Operation für nur einen Schritt zu erhalten?

Antwort

1

Sie können GraphQL API v4 verwenden, um die Liste der gegabelten Repo für eine bestimmte Benutzer/Organisation zu erhalten und die Eltern Repo-Namen erhalten:

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

Try it in the explorer

ein Beispiel unter Verwendung :

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) 
    } 
} 

Um die Paginierung zu durchlaufen, können Sie den Wert hasNextPage überprüfen, um zu prüfen, ob Sie einen anderen senden müssen Er fragt nach dem nächsten 100 gegabelten Repo, Verwenden Sie after in RepositoryConnection, um den 100 nächsten Wert nach dem angegebenen Cursor zu erhalten (speichern Sie den Wert des vorherigen endCursor, wenn hasNextPagetrue ist).

erste Anfrage:

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

gibt "hasNextPage": true & "endCursor" : "Y3Vyc29yOnYyOpHOAQ1VZg==". Nächste Anfrage wird sein:

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

Ich habe es getestet. Es funktioniert gut !. Aber ich benutze Golang und es ist schwer einen Golqag GraphQL Client zu entwickeln. Gibt es eine Möglichkeit, dies mit der traditionellen REST API v3 zu tun? – hsluoyz

+0

@hsluoyz Ich habe mit einem Beispiel aktualisiert mit go –

+0

dieser Code ist erstaunlich! Es kann meine Aufgabe in einer Anfrage erledigen. Aber ich habe herausgefunden, dass es nur 100 Datensätze bekommen kann. Wie bekomme ich dann 101-200 Datensätze? – hsluoyz

Verwandte Themen