-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathteam_stats.py
executable file
·325 lines (283 loc) · 9.03 KB
/
team_stats.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env python3
import os
import requests
import json
import pandas
from pprint import pprint
from datetime import datetime
# Your GitHub personal access token
TOKEN = 'YOUR_PERSONAL_ACCESS_TOKEN'
TOKEN = os.getenv('GITHUB_TOKEN', '...')
# The name of the organization
ORGANIZATIONS = {}
ORGANIZATIONS['newrelic'] = 'O_kgDNe_s'
start_date = '2022-07-01'
end_date = '2023-03-31'
def getReviews(username,org_id, start_date, end_date):
# The GraphQL query to retrieve the reviews
QUERY = """
query {
user(login: "%s") {
contributionsCollection(organizationID: "%s") {
totalPullRequestReviewContributions
pullRequestReviewContributionsByRepository {
contributions(first: 100) {
nodes {
repository {
name
}
occurredAt
pullRequest {
author {
login
}
}
}
edges {
cursor
}
}
}
}
}
}
"""
full_query = QUERY % (username, org_id)
user = {}
user[username] = {'reviewCount': 0, 'authors': {}, 'repos': {}}
#{'username': 'reviewCount': 0, 'authors': {'author1': 0, 'author2': 0}, 'repos': {'repo1': 0, 'repo2: 0}}
has_next_page = True
after_cursor = None
while has_next_page:
# Construct the variables for the GraphQL query
variables = {}
if after_cursor:
variables['after'] = after_cursor
# Send the GraphQL request to the GitHub API
response = requests.post('https://api.github.com/graphql', headers={
'Authorization': f'Bearer {TOKEN}',
'Content-Type': 'application/json'
}, json={'query': full_query, 'variables': variables})
# Check for errors in the response
if response.status_code != 200:
raise ValueError(f'Request failed with status code {response.status_code}: {response.text}')
# Parse the response JSON
response_json = json.loads(response.text)
for e in response_json['data']['user']['contributionsCollection']['pullRequestReviewContributionsByRepository']:
for node in e['contributions']['nodes']:
occurredAt = datetime.strptime(node['occurredAt'], '%Y-%m-%dT%H:%M:%SZ')
if datetime.strptime(start_date, '%Y-%m-%d') <= occurredAt <= datetime.strptime(end_date, '%Y-%m-%d'):
user[username]['reviewCount'] += 1
repo = node['repository']['name']
author = node['pullRequest']['author']['login']
if user[username]['authors'].get(author):
user[username]['authors'][author] = user[username]['authors'][author] + 1
else:
user[username]['authors'][author] = 1
if user[username]['repos'].get(repo):
user[username]['repos'][repo] = user[username]['repos'][repo] + 1
else:
user[username]['repos'][repo] = 1
cursor = e['contributions']['edges'][0]['cursor']
has_next_page = False
# has_next_page = response_json['data']['search']['pageInfo']['hasNextPage']
# after_cursor = response_json
print(user)
#
# if username in user and user.get(username).get('repos'):
# stats = user[username]['repos']
#
# if repository in stats:
# s = stats[repository]
# s['pullRequestCount'] += 1
# s['commits'] += commits
# s['files'] += files
# s['additions'] += additions
# s['deletions'] += deletions
# stats[repository] = s
# else:
# stats[repository] = {'pullRequestCount': 1, 'commits':commits,'files':files,'additions':additions,'deletions':deletions}
#
return response_json
def getPullRequests(username, org_id, start, end):
# The GraphQL query to retrieve the pull requests
QUERY = """
query {
user(login: "%s") {
contributionsCollection(organizationID: "%s") {
totalPullRequestContributions
pullRequestContributions(orderBy: {direction: DESC}, first: 1) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
pullRequest {
author {
login
}
changedFiles
deletions
additions
repository {
name
}
}
occurredAt
}
}
}
}
}
}
"""
full_query = QUERY % (username, org_id)
# Send the GraphQL request to the GitHub API
response = requests.post('https://api.github.com/graphql', headers={
'Authorization': f'Bearer {TOKEN}',
'Content-Type': 'application/json'
}, json={'query': full_query})
# Check for errors in the response
if response.status_code != 200:
raise ValueError(f'Request failed with status code {response.status_code}: {response.text}')
# Parse the response JSON
response_json = json.loads(response.text)
# if datetime.strptime(start, '%Y-%m-%d') <= merge_date <= datetime.strptime(end, '%Y-%m-%d'):
# print('yep')
# else:
# print('nope')
return response_json
def getPullRequestsBySearch(username, org_name, start, end):
QUERY = """
query ($after: String){
search(
query: "author:%s org:%s is:pr merged:%s..%s"
type: ISSUE
first: 100
after: $after
) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
... on PullRequest {
url
mergedAt
commits(first: 12) {
totalCount
}
additions
deletions
merged
repository {
name
}
files {
totalCount
}
}
}
}
}
}
"""
full_query = QUERY % (username, org_name, start, end)
user = {}
user[username] = {'pullRequestCount': 0, 'additions': 0, 'deletions': 0, 'files': 0, 'commits': 0}
has_next_page = True
after_cursor = None
while has_next_page:
# Construct the variables for the GraphQL query
variables = {}
if after_cursor:
variables['after'] = after_cursor
# Send the GraphQL request to the GitHub API
response = requests.post('https://api.github.com/graphql', headers={
'Authorization': f'Bearer {TOKEN}',
'Content-Type': 'application/json'
}, json={'query': full_query, 'variables': variables})
# Check for errors in the response
if response.status_code != 200:
raise ValueError(f'Request failed with status code {response.status_code}: {response.text}')
# Parse the response JSON
response_json = json.loads(response.text)
stats = {}
for e in response_json['data']['search']['edges']:
repository = e['node']['repository']['name']
# merge_date = datetime.strptime(e['node']['mergedAt'], '%Y-%m-%dT%H:%M:%SZ')
commits = e['node']['commits']['totalCount']
additions = e['node']['additions']
deletions = e['node']['deletions']
files = e['node']['files']['totalCount']
cursor = e['node'].get('cursor')
if username in user and user.get(username).get('repos'):
stats = user[username]['repos']
if repository in stats:
s = stats[repository]
s['pullRequestCount'] += 1
s['commits'] += commits
s['files'] += files
s['additions'] += additions
s['deletions'] += deletions
stats[repository] = s
else:
stats[repository] = {'pullRequestCount': 1, 'commits':commits,'files':files,'additions':additions,'deletions':deletions}
if username in user:
u = user[username]
u['pullRequestCount'] += 1
u['additions'] += additions
u['deletions'] += deletions
u['files'] += files
u['commits'] += commits
u['repos'] = stats
else:
user[username] = {'pullRequestCount': 1, 'additions': additions, 'deletions': deletions, 'files': files, 'commits': commits, 'repos': stats}
# print(response_json)
has_next_page = response_json['data']['search']['pageInfo']['hasNextPage']
after_cursor = response_json['data']['search']['pageInfo']['endCursor']
#"Y3Vyc29yOjEwMA=="#response_json['data']['search']['pageInfo']['endCursor']
#{'data': {'search': {'pageInfo': {'hasNextPage': False, 'endCursor': 'Y3Vyc29yOjUy'}
print(f'{username}: PRs:' + str({user[username]['pullRequestCount']}))
if user[username].get('repos'):
df = pandas.DataFrame(user[username]['repos'])
df_pivot = df.pivot_table(user[username]['repos'].keys()
, columns=['pullRequestCount', 'commits', 'files', 'additions', 'deletions'], aggfunc='sum', fill_value=0)
print(df_pivot)
return user
def readConfigJson():
# JSON file
f = open ('teams.json', "r")
# Reading from file
data = json.loads(f.read())
# Closing file
f.close()
return data
def getTeamMembers(team):
memberlist = []
query_url = "https://api.github.com/orgs/newrelic/teams"
params = {
"per_page": 100,
}
headers = {'Authorization': f'token {TOKEN}'}
r = requests.get(query_url, headers=headers, params=params)
for i in r.json():
if i['slug'] == team['team-slug']:
query_url = i['members_url'].replace('{/member}','')
teammembers = requests.get(query_url, headers=headers, params=params)
for m in teammembers.json():
memberlist.append(m['login'])
return memberlist
# The list of usernames to retrieve pull request reviews for
USERS = ['newrelic-node-agent-team']
teams = readConfigJson()
for team in teams['team']:
print(team['name'])
teammembers = getTeamMembers(team)
for team_member in teammembers:
getPullRequestsBySearch(team_member, list(ORGANIZATIONS.keys())[0], start_date, end_date)
getReviews(team_member, ORGANIZATIONS['newrelic'], start_date, end_date)
#getPullRequestsBySearch('coreyarnold', 'newrelic', start_date, end_date)
#getReviews('bizob2828', ORGANIZATIONS['newrelic'], start_date, end_date)