-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
167 lines (128 loc) · 4.18 KB
/
script.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
'''
@Author: Max Nícolas de Oliveira Lima
'''
import psycopg2
import json
import os
from lib.json_to_sql import jsonToSql
from perceval.backends.core.github import GitHub
from perceval.backends.core.git import Git
def getCommits(user_owner, repo_name):
repo = Git(f"https://github.com/{user_owner}/{repo_name}.git",
f"https://github.com/{user_owner}/{repo_name}.git")
commits = repo.fetch()
return commits
def getIssues(user_owner, repo_name, tokens):
repo = GitHub(owner=user_owner, repository=repo_name,
api_token=tokens, sleep_for_rate=True)
issues = repo.fetch(category="issue")
return issues
def getPRs(user_owner, repo_name, tokens):
repo = GitHub(owner=user_owner, repository=repo_name,
api_token=tokens, sleep_for_rate=True)
prs = repo.fetch(category="pull_request")
return prs
def getColumnsTable(cursor):
cursor.execute("""SELECT *
FROM information_schema.columns
WHERE table_schema NOT IN ('information_schema', 'pg_catalog')
ORDER BY table_schema, table_name""")
tables = {}
for row in cursor:
table = row[2]
column = row[3]
type_column = row[7]
if table in tables:
tables[table].append({
"name": row[3],
"type": row[7]
})
else:
tables[table] = [{
"name": row[3],
"type": row[7]
}]
return tables
def checkRepoExists(user_owner, repo_name, cursor):
sql = f"""
SELECT EXISTS (
SELECT *
FROM information_schema.tables
WHERE table_schema = 'serg'
AND table_name = 'repositorys'
);
"""
cursor.execute(sql)
tables = cursor.fetchall()
if tables[0][0]:
sql = f"""
SELECT
*
FROM
repositorys
WHERE
owner = {user_owner} AND
repository = {repo_name}
;"""
cursor.execute(sql)
return cursor.fetchall()
return None
def generateRepository(user_owner, repo_name):
yield {'data': {
'owner': user_owner,
'repository': repo_name
}}
def createDataBase(new_db, username, password):
con = psycopg2.connect(dbname='postgres',
user=username, host='',
password=password)
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) # <-- ADD THIS LINE
cur = con.cursor()
# Use the psycopg2.sql module instead of string concatenation
# in order to avoid sql injection attacs.
cur.execute(f"CREATE DATABASE {new_db}")
def run(owner, repository):
data_base_url = os.environ.get("DATABASE_URL")
conn = psycopg2.connect(data_base_url, sslmode='require')
cursor = conn.cursor()
tokens = []
index = 1
while 1:
token = os.environ.get(f"TOKEN_{index}")
if token != None:
tokens.append(token)
index += 1
else:
break
repositorys = checkRepoExists(owner, repository, cursor)
if repositorys is None:
print("GETING DATA...")
commits = {}
issues = {}
pullrequests = {}
repository_info = list(generateRepository(owner, repository))
print("RETRIEVING COMMITS...")
commits = list(getCommits(owner, repository))
print("COMMITS RETRIEVED")
print("RETRIEVING ISSUES...")
issues = list(getIssues(owner, repository, tokens))
print("ISSUES RETRIEVED")
print("RETRIEVING PULL_REQUESTS...")
pullrequests = list(getPRs(owner, repository, tokens))
print("PULL_REQUESTS RETRIEVED")
repository = {
"repository": repository_info,
"commits": commits,
"issues": issues,
"pullrequests": pullrequests
}
print("DATA FETCHED!")
tables = getColumnsTable(cursor)
jsonToSql(conn, tables, repository)
conn.close()
# if __name__ == '__main__':
# user_owner = "ES2-UFPI"
# repo_name = "Unichat"
# # user_owner = "Mex978"
# # repo_name = "compilador"
# run(user_owner, repo_name, tokens=[])