forked from coreyarnold/newrelic-monthlymetrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
173 lines (154 loc) · 5.4 KB
/
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
import calendar
import datetime
import operator
import requests
import os
import json
from pprint import pprint
from datetime import date
from datetime import datetime
from dateutil.relativedelta import relativedelta
token = os.getenv('GITHUB_TOKEN', '...')
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
def getUnreviewedBugCounts(team):
repos = team['repos']
openBugCount = 0
print("Gathering Unreviewed Bug Counts")
for repo in repos:
query_url = f"https://api.github.com/repos/newrelic/{repo}/issues"
params = {
"status": "open",
"labels": "bug",
"labels": "needs-triage",
}
headers = {'Authorization': f'token {token}'}
r = requests.get(query_url, headers=headers, params=params)
bugs = r.json()
if (len(bugs) > 0):
for b in bugs:
labels = b.get('labels')
bug_check = False
triage_check = False
for label in labels:
if label.get('name') == 'needs-triage':
triage_check = True
if label.get('name') == 'bug':
bug_check = True
if (triage_check and bug_check):
print(' found unreviewed bug %s in %s' % (b.get('number'), repo))
openBugCount += 1
print("Open Bugs: ", openBugCount)
def getOpenExternalPRCounts(team):
repos = team['repos']
print("Gathering Open External PR Counts")
prcount = 0
teammembers = getTeamMembers(team)
for repo in repos:
query_url = f"https://api.github.com/repos/newrelic/{repo}/pulls"
params = {
"state": "open",
}
headers = {'Authorization': f'token {token}'}
r = requests.get(query_url, headers=headers, params=params)
#this has all pull requests. need to filter out ones where the author is on the team
prs = r.json()
prs.sort(key=operator.itemgetter('created_at'),reverse=False)
for pr in prs:
if pr['user']['login'] not in teammembers :
print(' found external pr in %s from: %s opened %s' % (repo, pr['user']['login'], datetime.strptime(pr['created_at'], '%Y-%m-%dT%H:%M:%SZ')))
prcount += 1
# else:
# print(' found pr for a team member: ', pr['user']['login'])
print("Open External PRs: ", prcount)
def get_month_day_range(date):
first_day = datetime.combine(date.replace(day = 1), datetime.min.time())
last_day = datetime.combine(date.replace(day = calendar.monthrange(date.year, date.month)[1]), datetime.max.time())
return first_day, last_day
def getReleaseNumbers(repos):
print('Gathering Release Counts')
#gets the number of releases across all team repos for the current and previous month
#GET /repos/{owner}/{repo}/releases
lastmonthreleasecount = 0
thismonthreleasecount = 0
now = date.today()
thismonth = get_month_day_range(now)
lastmonth = get_month_day_range(now + relativedelta(months=-1))
for repo in repos:
reporeleasecount_lastmonth = 0
reporeleasecount_thismonth = 0
query_url = f"https://api.github.com/repos/newrelic/{repo}/releases"
params = {
"state": "open",
}
headers = {'Authorization': f'token {token}'}
r = requests.get(query_url, headers=headers, params=params)
#this has all pull requests. need to filter out ones where the author is on the team
releases = r.json()
#Remove any unpublished releases since can't sort them when 'published_at' is None
for release in releases:
if release['published_at'] == None:
releases.remove(release)
releases.sort(key=operator.itemgetter('published_at'),reverse=False)
for release in releases:
releasepublishedat = datetime.strptime(release['published_at'], '%Y-%m-%dT%H:%M:%SZ')
if(releasepublishedat > lastmonth[0] and releasepublishedat < lastmonth[1]):
print(" Release %s last month (%s) in %s" % (release.get("name"), releasepublishedat, repo))
reporeleasecount_lastmonth += 1
elif(releasepublishedat > thismonth[0] and releasepublishedat < thismonth[1]):
print(" Release %s this month (%s) in %s" % (release.get("name"), releasepublishedat, repo))
reporeleasecount_thismonth += 1
lastmonthreleasecount += reporeleasecount_lastmonth
thismonthreleasecount += reporeleasecount_thismonth
print("Total Release Count Last Month: ", lastmonthreleasecount)
print("Total Release Count This Month: ", thismonthreleasecount)
def getOpenBugs(team):
repos = team['repos']
openBugCount = 0
for repo in repos:
query_url = f"https://api.github.com/repos/newrelic/{repo}/issues"
params = {
"status": "open",
"labels": "bug",
}
headers = {'Authorization': f'token {token}'}
r = requests.get(query_url, headers=headers, params=params)
bugs = r.json()
if (len(bugs) > 0):
openBugCount += len(bugs)
else:
openBugCount += 0
return openBugCount
teams = readConfigJson()
aggregatebugs = 0
for team in teams['team']:
aggregatebugs += getOpenBugs(team)
print('Total Open Bugs: ',aggregatebugs)
for team in teams['team']:
print(team['name'].upper())
print(team)
getUnreviewedBugCounts(team)
getOpenExternalPRCounts(team)
getReleaseNumbers(team['repos'])
print()