-
Notifications
You must be signed in to change notification settings - Fork 22
/
github_metrics.py
213 lines (202 loc) · 9.29 KB
/
github_metrics.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import argparse
from github import Github
import requests
import json
import csv
import os
import datetime
import socket
import pandas as pd
socket.setdefaulttimeout(60 * 60)
today = datetime.date.today()
todaystr = str(today)
def setup():
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--org", help="choose what company you what to see", required=True)
parser.add_argument("-t", "--token", help="OAuth token from GitHub", required=True)
args = parser.parse_args()
return args
def list_orgs(authToken):
g = Github(authToken)
orgslist = []
for orgs in g.get_user().get_orgs():
orgslist.append(orgs.login)
return orgslist
def list_org_members(org, authToken):
s = requests.Session()
s.headers.update({'Authorization': 'token ' + authToken})
g = Github(authToken)
namesmembers = []
try:
filename = "memblist_" + org + ".txt"
text_file = open(filename, "r")
loginmembers = text_file.read().split(',')
except:
loginmembers = []
for orgs in g.get_user().get_orgs():
if orgs.login == org:
for memb in orgs.get_members():
if memb.login not in loginmembers:
loginmembers.append(memb.login)
else:
next
if loginmembers[0] == "":
loginmembers.pop(0)
for member in loginmembers:
r = s.get("https://api.github.com/users/" + member)
r_user = json.loads(r.text)
try:
if r_user["name"] != None:
namesmembers.append(r_user["name"])
else:
namesmembers.append(r_user["login"])
except:
next
return loginmembers, namesmembers
def export_code_frequency(directory, organization, authToken):
totalrepos = 0
g = Github(authToken)
today = str(datetime.date.today())
today = today.replace("-", "")
allorgs = g.get_user().get_orgs()
for orgs in allorgs:
if orgs.login == organization:
for repo in orgs.get_repos():
if repo.fork == False and repo.private == False:
totalrepos +=1
with open(directory + "/github_code_frequency_" + organization + "_" + today+ ".csv", 'w', encoding='utf-8') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',')
csvwriter.writerow(
["count", "org", "repo", "week", "additions", "deletions", "commits", "author", "is a member"])
loginmembers, namesmembers = list_org_members(organization, authToken)
for orgs in allorgs:
if orgs.login == organization:
print("Gathering code frequency for all repos on", orgs.login)
count = 0
for repo in orgs.get_repos():
controws = 0
if repo.fork == False and repo.private == False:
count += 1
reponame = repo.name
try:
stats = repo.get_stats_contributors()
for stat in stats:
author = str(stat.author)
author = (author.replace('NamedUser(login="', "")).replace('")', "")
for week in stat.weeks:
if week.c != 0:
date = str(week.w)
date = date[:10]
if author in loginmembers:
controws+=1
try:
csvwriter.writerow(
[count, orgs.login, reponame, date, week.a, week.d, week.c, author,
"yes"])
except:
print("error")
else:
controws += 1
try:
csvwriter.writerow(
[count, orgs.login, reponame, date, week.a, week.d, week.c, author,
"no"])
except:
print("error2")
print("[", str(count).zfill(2), "|", totalrepos, "] ", orgs.login, " | ", repo.name, " | ", controws, " rows in the file")
except:
print("[", str(count).zfill(2), "|", totalrepos, "] ", orgs.login, " | ", repo.name, "| none")
csvwriter.writerow([count, orgs.login, reponame, 0, 0, 0, 0, 0, "n/a"])
else:
next
def export_community_engagement(directory, organization, authToken):
g = Github(authToken)
today = str(datetime.date.today())
today = today.replace("-", "")
totalrepos = 0
allorgs = g.get_user().get_orgs()
for orgs in allorgs:
if orgs.login == organization:
for repo in orgs.get_repos():
if repo.fork == False and repo.private == False:
totalrepos += 1
with open(directory + "/github_community_engagement_" + organization + "_" + today+ ".csv", 'w', encoding='utf-8') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',')
csvwriter.writerow(
["date", "org", "repo", "forks", "stars", "commits", "collaborators"])
for orgs in allorgs:
if orgs.login == organization:
print("Gathering community metrics for", orgs.login)
count = 0
for repo in orgs.get_repos():
try:
hascommits = repo.get_commits()[0]
except:
hascommits = False
print(repo.name, 'is empty')
countcommit = 0
countcollab = 0
if repo.fork == False and repo.private == False and hascommits != False:
count += 1
for commits in repo.get_commits():
countcommit += 1
for collab in repo.get_contributors():
countcollab += 1
print("[", str(count).zfill(2), "|", totalrepos, "]", repo.name, "|", countcommit, "commits |", repo.forks_count, "forks |",
repo.stargazers_count, "stars |", countcollab, "contributors")
csvwriter.writerow(
[todaystr, organization, repo.name, repo.forks_count, repo.stargazers_count, countcommit,
countcollab])
def list_unique_collaborators(directory, organization, authToken):
g = Github(authToken)
with open(directory + "/github_unique_collaborators_" + organization + ".csv", "w", encoding="utf-8") as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',')
csvwriter.writerow(["name", "login", "name", "member of the org?"])
loginmembers, namesmembers = list_org_members(organization, authToken)
userslist = []
nameslist = []
allorgs = g.get_user().get_orgs()
for orgs in allorgs:
if orgs.login == organization:
print("Gathering unique collaborators for", orgs.login)
count = 0
for repo in orgs.get_repos():
if repo.fork == False and repo.private == False:
for collab in repo.get_contributors():
if collab.login not in userslist:
userslist.append(collab.login)
if collab.name != None:
nameslist.append(collab.name)
else:
nameslist.append(collab.login)
count += 1
collablogin = collab.login
if collab.name == None:
collabname = collab.login
else:
collabname = collab.name
if collablogin in loginmembers:
member = "yes"
else:
member = "no"
print(str(count).zfill(2), "|", member, "|", collablogin, "|", collabname)
csvwriter.writerow([count, collablogin, collabname, member])
def main():
args = setup()
organization = args.org
authToken = args.token
g = Github(authToken)
directory = "output/" + organization
if not os.path.exists(directory):
os.makedirs(directory)
try:
print("Valid token. Starting process. \n")
list_org_members(organization, authToken)
print("")
export_code_frequency(directory, organization, authToken)
print("")
export_community_engagement(directory, organization, authToken)
except Exception as e:
print(e)
if __name__ == '__main__':
main()