-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrg.py
111 lines (84 loc) · 3.45 KB
/
Org.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
# -*- coding: utf-8 -*-
import json
import requests
import os
from Repo import Repo
class Org:
REPO_URL = "https://api.github.com/search/repositories?q=org:{org_name}+archived:false+is:public&per_page=100&page=1"
def __init__(self, org_name):
self.name = org_name
# Have to auth to increase api limit from 60 -> 5000
self.USER = os.getenv('GITHUB_USERNAME')
self.AUTH_TOKEN = os.getenv('GITHUB_AUTH_TOKEN')
if not self.USER or not self.AUTH_TOKEN:
raise Exception("ERROR: GITHUB_USERNAME or GITHUB_AUTH_TOKEN not provided!")
response = requests.get(Org.REPO_URL.format(org_name=self.name), auth=(self.USER, self.AUTH_TOKEN))
# Raise errors if the request for repos failed
response.raise_for_status()
self.raw_repos = json.loads(response.text)
while 'next' in response.links.keys():
response = requests.get(response.links['next']['url'], auth=(self.USER, self.AUTH_TOKEN))
self.raw_repos.extend(json.loads(response.text))
members_url = "https://api.github.com/orgs/"+ org_name +"/members?per_page=100&page=1"
response = requests.get(members_url, auth=(self.USER, self.AUTH_TOKEN))
org_members = json.loads(response.text)
while 'next' in response.links.keys():
response = requests.get(response.links['next']['url'], auth=(self.USER, self.AUTH_TOKEN))
org_members.extend(json.loads(response.text))
self.members = self.make_member_list(org_members)
self.repo_list = self.make_repo_list()
self.open_states = 0
self.closed_states = 0
self.find_states()
# Make list of repo objects
def make_repo_list(self):
to_return = []
for x in range(self.raw_repos['total_count']):
to_return.append(Repo(self.name, self.raw_repos['items'][x]['name'], self.USER, self.AUTH_TOKEN, self.members))
return to_return
# Make string list of org members
def make_member_list(self, members):
to_return = []
for member in members:
to_return.append(member['login'])
return to_return
# Return list of repo objects
def get_repos(self):
return self.repo_list
# Return number of repos
def get_num_repos(self):
return len(self.repo_list)
# Finds states
def find_states(self):
self.open_states = 0
self.closed_states = 0
for repo in self.repo_list:
self.open_states += repo.pr_open_states
self.closed_states += repo.pr_closed_states
def get_open(self):
return self.open_states
def get_closed(self):
return self.closed_states
##################
#methods to make:
#repos with smallest and greatest avgTimes, open, closed
#total # of prs in last 6 months
# # of prs in each of the last 6 months
#call them in export_json and feed in the org_data dict
##################
def export_json(self):
# TODO: Simplify this - lots of unnecessary work here
repos = {}
x = 0
for repo in self.repo_list:
repos[x] = repo.export()
x+=1
org_data = {
'repos': repos,
'totalRepos': self.get_num_repos(),
'closedPR': self.get_closed(),
'openPR': self.get_open()
}
file_name = self.name + ".json"
with open(file_name, 'w') as outfile:
json.dump(org_data, outfile)