-
Notifications
You must be signed in to change notification settings - Fork 6
/
findforks.py
executable file
·114 lines (84 loc) · 2.99 KB
/
findforks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python3
import argparse
import json
import subprocess
import urllib.error
import urllib.parse
import urllib.request
def find_forks(remote):
"""
Query the GitHub API for all forks of a repository.
"""
resp_json = []
repo_url = subprocess.run(
["git", "remote", "get-url", remote],
stdout=subprocess.PIPE
)
repo_url_stdout = repo_url.stdout.decode()
(username, project) = parse_git_remote_output(repo_url_stdout)
GITHUB_FORK_URL = u"https://api.github.com/repos/{username}/{project}/forks"
try:
resp = urllib.request.urlopen(GITHUB_FORK_URL.format(username=username, project=project))
except urllib.error.HTTPError as e:
if e.code == 404:
raise StopIteration
resp_json += json.loads(resp.read())
while github_resp_next_page(resp):
resp = urllib.request.urlopen(github_resp_next_page(resp))
resp_json += json.loads(resp.read())
for fork in resp_json:
yield (fork['owner']['login'], fork['ssh_url'])
def github_resp_next_page(resp):
"""
Check to see if the GitHub response has a next link.
If the response, look for the 'link' header and see if
there is a value pointed to by next.
"""
link_header = resp.getheader(u"link")
if not link_header:
return None
rel_next = u'rel="next"'
for link in link_header.split(u","):
if rel_next in link:
return link[link.find(u"<") + 1:link.rfind(u">")]
return None
def parse_git_remote_output(repo_url):
"""
Given a repository URL, split it into its component parts.
convert [email protected]:akumria/all_forks.git to
service: [email protected]
username: akumria
project = all_forks
convert https://github.com/akumria/all_forks.git to
service: [email protected]
username: akumria
project = all_forks
"""
if repo_url.startswith("[email protected]"):
(service, repo) = repo_url.split(":")
(username, project_git) = repo.split("/")
project = project_git[:project_git.find(".")]
return (username, project)
if repo_url.startswith("http"):
o = urllib.parse.urlparse(repo_url)
(_, username, project_git) = o.path.split("/")
# also handle the case where there is no '.git'
if project_git.find(".") < 0:
project = project_git
else:
project = project_git[:project_git.find(".")]
return (username, project)
def setup_remote(remote, repository_url):
"""
Configure a remote with a specific repository.
"""
print("{}: {}".format(remote, repository_url))
subprocess.run(["git", "remote", "add", remote, repository_url])
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--remote", help="Which remote to use", default="origin")
args = parser.parse_args()
for (remote, repository) in find_forks(args.remote):
setup_remote(remote, repository)
if __name__ == "__main__":
main()