-
Notifications
You must be signed in to change notification settings - Fork 1
/
export_org_repos.py
81 lines (70 loc) · 2.56 KB
/
export_org_repos.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
#!/bin/python3
# usage: python3 export_org_repos.py --token <token_file> --github_nickname <your nickname> --orgs <organizations_file>
import argparse
from github import Github
import json
import csv
import requests
from time import sleep
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--token', type=str, required=True,
dest='token',
help='file w/github token')
parser.add_argument('--github_nickname', type=str, required=True,
dest='github_nickname',
help='organization_names_file')
parser.add_argument('--orgs', type=str, required=True,
dest='orgs',
help='organization_names_file')
results = parser.parse_args()
return results
def get_token(filename):
with open(filename) as file:
token = file.readline().strip()
return token
def get_orgs(filename):
with open(filename) as file:
orgs = (org.strip() for org in file.readlines() if org.strip())
return orgs
def get_writer_rows():
return [
"repo_name",
"is_private",
"issues_count",
"permissions"
]
def get_repo_info(repo, org_name="", username=""):
users = ""
try:
for u in repo.get_collaborators():
# params = {
# "accept": "application/vnd.github.v3+json"
# }
# sleep(1)
# res = requests.get(f"https://api.github.com/repos/{org_name}/{repo.name}/collaborators/{u.login}/permission", params=params, auth=(username, args.token))
users += f"{u.login}:{str(u.permissions)},"
# print(users)
except Exception as exc:
print(f"Error getting collaborators: {exc}")
return [
str(repo.name),
str(repo.private),
str(len(list(repo.get_issues(state='all')))),
str(users)
]
if __name__ == '__main__':
args = parse_args()
g = Github(get_token(args.token))
for org_name in get_orgs(args.orgs):
print('get org [{}]'.format(org_name))
org = g.get_organization(org_name)
org_repos = org.get_repos()
with open(f'{org_name}.csv', 'w') as file:
writer = csv.writer(file, delimiter=';', quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow(get_writer_rows())
for repo in org_repos:
print(f"Handling repo [{repo.name}]")
# sleep(10)
info = get_repo_info(repo, org_name, args.github_nickname)
writer.writerow(info)