-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.py
442 lines (392 loc) · 13.4 KB
/
app.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import os
import psycopg2
from dotenv import load_dotenv
import graphene
import requests
from flask import Flask,render_template,url_for,request,redirect, make_response, json
from flask_dance.contrib.github import make_github_blueprint, github
from flask_login import logout_user
from datetime import datetime, timedelta
load_dotenv()
app = Flask(__name__)
app.config["SECRET_KEY"]="abcde"
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
github_blueprint = make_github_blueprint(client_id=os.environ['GITHUB_CLIENT_ID'],
client_secret=os.environ['GITHUB_CLIENT_SECRET'])
app.register_blueprint(github_blueprint, url_prefix='/github_login')
def get_db_connection():
conn = psycopg2.connect(
host=os.environ["DB_URL"],
database=os.environ["DB_DB"],
user=os.environ["DB_USERNAME"],
password=os.environ["DB_PASSWORD"],
)
return conn
query = """
{
viewer {
login
}
}
"""
query = """
{
viewer {
pullRequests(first: 100, orderBy: {field: UPDATED_AT, direction: DESC}) {
nodes {
repository {
name
isPrivate
}
}
}
}
}
"""
query_to_issues = """
{
search(first: 100, type: ISSUE, query: "label:kg-foss state:closed") {
issueCount
pageInfo {
hasNextPage
endCursor
}
edges {
node {
... on Issue {
id
createdAt
title
url
repository {
name
}
assignees(first: 10) {
nodes {
login
}
}
}
}
}
}
}
"""
GITHUB_AUTH_TOKEN = os.environ["GITHUB_AUTH_TOKEN"]
headers = {"Authorization": "Bearer " + GITHUB_AUTH_TOKEN }
@app.route("/")
def index():
conn = get_db_connection()
cur = conn.cursor()
cur.execute("SELECT * FROM books;")
books = cur.fetchall()
cur.close()
conn.close()
return render_template("index.html", books=books)
@app.route('/login')
def github_login():
if not github.authorized:
return redirect(url_for('github.login'))
else:
account_info = github.get('/user')
if account_info.ok:
account_info_json = account_info.json()
print(account_info_json)
return '<h1>Your Github name is {}'.format(account_info_json['login'])
return '<h1>Request failed!</h1>'
@app.route("/logout")
def github_logout():
del github_blueprint.token
# print(github_blueprint.token)
return 'logged out'
@app.route("/git")
def gitget():
request = requests.post(
"https://api.github.com/graphql", json={"query": query_to_issues}, headers=headers
)
if request.status_code == 200:
return request.json()
else:
raise Exception(
"Query failed to run by returning code of {}. {}".format(
request.status_code, query
)
)
@app.route("/issues", methods=["POST","GET"])
def add_issues():
if not github.authorized:
return redirect(url_for('github.login'))
if request.method == "POST":
conn = get_db_connection()
cur = conn.cursor()
sql = """INSERT INTO UserIssues(issue_id,user_id,repo_id,status,created_at, updated_at) VALUES(%s,%s,%s,%s,%s,%s)"""
values = (request.form["issue_id"],request.form["user_id"],request.form["repo_id"],request.form["status"],request.form["created_at"],request.form["updated_at"])
cur.execute(sql,values)
conn.commit()
cur.close()
conn.close()
return redirect(url_for("index"))
gql_query="""query {
viewer{
repositories(isFork:true,first:10) {
totalCount
edges {
node {
id
name
url
parent {
id
nameWithOwner
issues(first:20,) {
edges {
node {
id
body
bodyUrl
assignees(first:10) {
edges {
node {
id
login
isViewer
name
}
}
}
}
}
}
}
}
}
}
}
}
"""
response = requests.post("https://api.github.com/graphql", json= {"query":gql_query}, headers=headers)
if response.status_code == 200:
# return render_template("issues.html",issues=response.content)
return render_template("issues.html",issues=response.json()['data']['viewer']['repositories']['edges'])
return redirect(url_for("index"))
@app.route("/git/repos")
def gitRepos():
jsonItems = """
[{
"createdAt": "2022-07-09T21:23:49Z",
"description": null,
"issues": {
"nodes": [],
"totalCount": 0
},
"name": "Login-Register",
"owner": {
"login": "rakshitmehra"
},
"updatedAt": "2022-07-09T21:30:29Z"
},
{
"createdAt": "2022-07-17T15:57:34Z",
"description": null,
"issues": {
"nodes": [],
"totalCount": 0
},
"name": "Calculator",
"owner": {
"login": "rakshitmehra"
},
"updatedAt": "2022-07-17T17:12:52Z"
},
{
"createdAt": "2022-07-19T22:33:57Z",
"description": "This repository is to allow Hactoberfest 2022 participants to make PR's. This repository aims to help beginners \ud83e\udd14 with their first successful Pull Request and Open Source Contribution\ud83d\ude0a\ud83d\ude0a",
"issues": {
"nodes": [
{
"title": "longest palindromic substring"
},
{
"title": "[OpenCV] live sketch program"
},
{
"title": "Summation of diagonal elements in CPP"
},
{
"title": "Adding questions for Programming under Binary Trees Topic"
},
{
"title": "Add any programs related to C++ or Java or Python in following folders"
},
{
"title": "Put Atleast 30 questions Related to Programming"
},
{
"title": "Make this Website Dynamic, So that We can put the questions directly, so that not changing the code again nd again."
},
{
"title": "Adding animation"
},
{
"title": "Adding Top Interview Questions"
},
{
"title": "A try to laugh challenge webpage "
}
],
"totalCount": 10
},
"name": "Quiz-application",
"owner": {
"login": "rakshitmehra"
},
"updatedAt": "2023-01-03T15:17:12Z"
}]
"""
repo = json.loads(jsonItems)
return render_template("Repos.html",repos=repo)
@app.route('/leaderboard2')
def lederboard2():
query_to_leaderboard = """
{
repository(owner: "kgex", name: "kgfoss") {
pullRequests(last: 10, states: [MERGED]) {
nodes {
title
mergedAt
author{
login
}
mergedBy {
login
}
mergeCommit {
oid
message
}
}
}
}
}
"""
request = requests.post(
"https://api.github.com/graphql", json={"query": query_to_leaderboard}, headers=headers
)
data = json.dumps( [1.0,2.0,3.0] )
if request.status_code == 200:
return render_template("leaderboard.html",data=request.json()['data']['repository']['pullRequests']['nodes'])
else:
raise Exception(
"Query failed to run by returning code of {}. {}".format(
request.status_code, query
)
)
# usersz = ["Brijesh-m-14", "Ashwin-d-27", "GnanaChandruKR", "Thanush2412", "ELAKIYA-SEKAR", "vasanthakumar2004", "AkshayaVarshieni14", "JasferI", "vikrantvikaasa27"]
usersz = ["Brijesh-m-14","Ashwin-d-27","GnanaChandruKR","Thanush2412","ELAKIYA-SEKAR","vasanthakumar2004","AkshayaVarshieni14","JasferI","vikrantvikaasa27","Anand934","Rohithvasan2871","SarathSjr","markjacksonj","CiRiLj","Aakash158","haridarsan01","Rathnapraba","Mathew1327","MOHAMEDTHABITH","fazmila-fathima","B-a-b-u","Deenadayalan3","naveenmac7","jayandhan007","KNVIKRAM","Elamathiselvan","vikashmurugesh","Adhi2407","RACKSAN","IniyalPalanisamy","ManjusriArumugam","HariHaran1214","Harikrish360","karthickraja-shanmugaguru","sabareeshwaran-sureshkumar","gnana angelin s","RSDeenu123","Sudharshini Jothikumar","Sudharshini Jothikumar","Lakshmiprabha14","GnanaAngelinS","JubileeTitusJM","HarisNihaal","shyamkumar05","sreelekha2272003","sudheesanamp2003","deva209","SwethaSathish1027","ArunKarthick2021","Dharsan12","Alwin7704","hariprasanth05","Kowsalyasriganesh","dayanithi400","Veeshavanachu","Manojkumar192002","sk-vishal-28","Aishu-5","RaghulVishal0604","VijayaDharshini Sivalkumar","PranikaBaby","hariprasanth05","yuvan-s-96","sanjeevidev","JJanany","jeban-2003","[email protected]","Saanthosh24","gobalakrishnan21cb17","SivaRamana-H-V","Thejeswini-V","lavanya787","Rakesh-B-2003","JeyaSuryaRavi","KalaiKathir-SJ","Sidharth Babu","DeenuRSD","Manojkumardheenadhayalan","sridevithanigai","umar120044","santhoshkannan5206","sankaraxi","Natheesh-N"]
queries = []
print(len(usersz))
for i, use in enumerate(usersz):
query = f'''
user{i}: user(login: "{use}") {{
name
pullRequests(last: 10, states: MERGED, orderBy: {{field: UPDATED_AT, direction: DESC}}) {{
nodes {{
title
url
state
updatedAt
repository {{
nameWithOwner
}}
}}
}}
}}
'''
queries.append(query)
print(len(queries))
graphql_query = "query {" + "\n".join(queries) + "}"
print(graphql_query)
@app.route('/leaderboard')
def lederboard():
request = requests.post(
"https://api.github.com/graphql", json={"query": graphql_query}, headers=headers
)
if request.status_code == 200:
resp = request.json()['data']
data = []
for key in resp.keys():
if resp[key] != None:
name = resp[key]['name']
nodes = len(resp[key]['pullRequests']['nodes'])
prs = []
for pr in resp[key]['pullRequests']['nodes']:
prs.append(pr['title'])
data.append({"name": name, "nodes": nodes, "prs": prs})
return render_template("realboard.html",data=data)
else:
raise Exception(
"Query failed to run by returning code of {}. {}".format(
request.status_code, query
)
)
CONTRIBUTORS = ["nivu", "PranikaBaby", "nivu", "nivu"]
QUERY = """
query ($userLogins:[String!]!, $since:DateTime!, $after:String) {
search(query: "is:pr is:merged author:{userLogins} merged:>{since} state:closed", type: ISSUE, first: 100, after: $after) {
edges {
cursor
node {
... on PullRequest {
author {
login
}
repository {
nameWithOwner
}
mergedAt
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
"""
def run_query(query, variables):
# headers = {"Authorization": f"Bearer {API_TOKEN}"}
response = requests.post("https://api.github.com/graphql", json={"query": query, "variables": variables}, headers=headers)
if response.status_code == 200:
return json.loads(response.text)
else:
raise Exception(f"Query failed to run: {response.text}")
@app.route("/leader")
def leader():
# Calculate the start date for the query
since = datetime.now() - timedelta(days=2)
# Retrieve pull requests from the GitHub GraphQL API for each contributor
pr_counts = {contributor: 0 for contributor in CONTRIBUTORS}
for contributor in CONTRIBUTORS:
end_cursor = None
while True:
query = QUERY % (",".join([f'"{contributor}"']), since.strftime("%Y-%m-%dT%H:%M:%SZ"), end_cursor)
result = run_query(query, {})
pulls = result["data"]["search"]["edges"]
# Count the number of successful pull request merges by the contributor
for pull in pulls:
if pull["node"]["author"] and pull["node"]["author"]["login"] == contributor and pull["node"]["mergedAt"] is not None:
pr_counts[contributor] += 1
# Check if there are more results to retrieve
if result["data"]["search"]["pageInfo"]["hasNextPage"]:
end_cursor = result["data"]["search"]["pageInfo"]["endCursor"]
else:
break
# Sort contributors by number of successful pull request merges
sorted_contributors = sorted(pr_counts, key=pr_counts.get, reverse=True)
# Create a list of (username, successful_pr_merge_count) tuples for the top 4 contributors
leaderboard = [(contributor, pr_counts[contributor]) for contributor in sorted_contributors[:4]]
# Render the leaderboard template with the leaderboard data
return render_template("leader.html", leaderboard=leaderboard)
if __name__ == "__main__":
app.run(debug=True)