-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvelocity.py
executable file
·207 lines (175 loc) · 5.87 KB
/
velocity.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
#!/usr/bin/env python3
import os
import requests
import json
import pandas
import operator
from pprint import pprint
from datetime import date
from datetime import datetime
from dateutil.relativedelta import relativedelta
# 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 getPullRequestsBySearch(repo, org_name, start, end):
QUERY = """
query ($after: String){
search(
query: "repo:%s/%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 % (org_name, repo, start, end)
# print(full_query)
has_next_page = True
after_cursor = None
TotalPRCount = 0
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)
TotalPRCount += len(response_json['data']['search']['edges'])
# print("%s %s: %s" % (repo, end, len(response_json['data']['search']['edges'])))
# 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')
has_next_page = response_json['data']['search']['pageInfo']['hasNextPage']
after_cursor = response_json['data']['search']['pageInfo']['endCursor']
# 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
return TotalPRCount
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 getPullRequestsByTeam(team_obj):
print("pull requests for " + team_obj['name'])
now = datetime.today()
since = datetime(2023, 6, 5, 0, 0, 0)
until = since + relativedelta(days=+7)
while (until < now):
TeamPRCount = 0
for repo in team_obj['repos']:
TeamPRCount += getPullRequestsBySearch(repo, 'newrelic', since.strftime("%Y-%m-%d"), until.strftime("%Y-%m-%d"))
# TeamPRCount += getPullRequestsBySearch(repo, 'newrelic', "2023-06-05", "2023-06-12")
print("%s %s: %s" % (team_obj['name'], until.strftime("%Y/%m/%d"), TeamPRCount))
since = since + relativedelta(days=+7)
until = since + relativedelta(days=+7)
def getReleasesByTeam(team_obj):
print("deploys for " + team_obj['name'])
repos = team_obj['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
totalreleasecount = 0
now = datetime.today()
since = datetime(2023, 6, 5, 0, 0, 0)
until = since + relativedelta(days=+7)
while (until < now):
for repo in repos:
reporeleasecount = 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)
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 >= since and releasepublishedat <= until):
print(" Release %s published (%s) in %s" % (release.get("name"), releasepublishedat, repo))
reporeleasecount += 1
totalreleasecount += reporeleasecount
print("%s - Total Release Count: %s" % (until.strftime("%m/%d/%Y"), totalreleasecount))
since = since + relativedelta(days=+7)
until = since + relativedelta(days=+7)
totalreleasecount = 0
# 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'])
getPullRequestsByTeam(team)
getReleasesByTeam(team)