Reputation: 2919
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": "[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
}
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?
Upvotes: 2
Views: 1027
Reputation: 45503
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
}
}
}
}
}
}
An example using go :
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)
}
}
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
).
First request :
{
organization(login: "appcelerator-forks") {
repositories(first: 100, isFork: true) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
parent {
nameWithOwner
}
}
}
}
}
}
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
}
}
}
}
}
}
Upvotes: 2