5
5
# from jwt import JWT
6
6
import os
7
7
from dotenv import load_dotenv
8
- import logging
9
8
import sys
10
9
import time
11
10
12
11
from sqlalchemy import NullPool
13
12
from sqlalchemy .ext .asyncio import create_async_engine , AsyncSession
14
13
from sqlalchemy .orm import sessionmaker
15
14
from utils .jwt_generator import GenerateJWT
15
+ from utils .logging_file import logger
16
16
from utils .new_jwt_generator import NewGenerateJWT
17
17
18
18
sys .path .append (os .path .dirname (os .path .abspath (__file__ )))
@@ -52,7 +52,7 @@ def get_github_jwt(self):
52
52
pem_file .close ()
53
53
return encoded_jwt
54
54
except Exception as e :
55
- logging .error (f"In get_github_jwt: { e } " )
55
+ logger .error (f"In get_github_jwt: { e } " )
56
56
return None
57
57
58
58
@@ -79,7 +79,8 @@ async def get_installations(self, token_headers: dict):
79
79
elif installations_response .status_code == 401 :
80
80
if installations_response .json ().get ("message" ,
81
81
None ) == '\' Expiration time\' claim (\' exp\' ) must be a numeric value representing the future time at which the assertion expires' :
82
- logging .info ("JWT expired at get_installation stage" )
82
+
83
+ logger .info ("JWT expired at get_installation stage" )
83
84
return - 1
84
85
85
86
@@ -97,7 +98,8 @@ async def get_access_token(self, token_headers: dict, installation_id: str):
97
98
elif access_token_response .status_code == 401 :
98
99
if access_token_response .json ().get ("message" ,
99
100
None ) == '\' Expiration time\' claim (\' exp\' ) must be a numeric value representing the future time at which the assertion expires' :
100
- logging .info ("JWT expired at get_access_token stage" )
101
+
102
+ logger .info ("JWT expired at get_access_token stage" )
101
103
return - 1
102
104
else :
103
105
return None
@@ -117,7 +119,8 @@ async def get_repos(self, token: str):
117
119
repo_data = repo_response .json ()
118
120
return repo_data .get ('repositories' , [])
119
121
120
- async def get_issues (self , token : str , since : datetime , repo_fullname : str ):
122
+
123
+ async def get_issues (self , token : str , since : datetime , repo_fullname : str , to_date = None ):
121
124
page = 1
122
125
all_issues = []
123
126
while True :
@@ -127,50 +130,70 @@ async def get_issues(self, token: str, since: datetime, repo_fullname: str):
127
130
"Authorization" : f"Bearer { token } " ,
128
131
"X-GitHub-Api-Version" : "2022-11-28"
129
132
}
130
- payload = {"labels" : "c4gt community" ,
131
- "since" : since .isoformat ()}
133
+
134
+ payload = {
135
+ "labels" : "c4gt community" ,
136
+ "since" : since .isoformat (),
137
+ "direction" : "asc"
138
+ }
132
139
async with httpx .AsyncClient () as client :
133
- issues_response = await client .get (url = get_issue_url ,
134
- headers = token_headers ,
135
- params = payload
136
- )
140
+ issues_response = await client .get (url = get_issue_url , headers = token_headers , params = payload )
137
141
page_issues = issues_response .json ()
138
- if len (page_issues )> 0 :
142
+
143
+ # Filter based on created_at if to_date is provided
144
+ if to_date :
145
+ page_issues = [issue for issue in page_issues if
146
+ datetime .fromisoformat (issue ['created_at' ].replace ('Z' , '+00:00' )) <= to_date ]
147
+
148
+ if len (page_issues ) > 0 :
149
+ all_issues += page_issues
139
150
page += 1
140
- all_issues = all_issues + page_issues
141
151
else :
142
152
break
143
- rate_limit = await self .get_rate_limits (token )
144
- print (rate_limit )
153
+
154
+ rate_limit = await self .get_rate_limits (token )
155
+ print (rate_limit )
156
+
145
157
return all_issues
146
158
147
- async def get_issue_comments (self , issue_comment_url , since : datetime , ** kwargs ):
148
- page = 1
159
+ async def get_issue_comments (self , issue_comment_url , since : datetime , to_date = None , ** kwargs ):
160
+ page = 1
161
+
149
162
all_comments = []
150
163
token = kwargs .get ("token" , None )
151
164
while True :
152
165
comments_url = f"{ issue_comment_url } ?state=all&page={ page } &per_page=100"
153
- payload = {"since" : since .isoformat ()}
154
-
166
+ payload = {
167
+ "since" : since .isoformat (),
168
+ "direction" : "asc"
169
+ }
170
+
155
171
async with httpx .AsyncClient () as client :
156
- if token is not None :
172
+ if token :
157
173
token_headers = {
158
174
"Accept" : "application/vnd.github+json" ,
159
175
"Authorization" : f"Bearer { token } " ,
160
176
"X-GitHub-Api-Version" : "2022-11-28"
161
177
}
162
- issue_comment_response = await client .get (url = comments_url ,
163
- headers = token_headers ,
164
- params = payload )
178
+ response = await client .get (url = comments_url , headers = token_headers , params = payload )
165
179
else :
166
- issue_comment_response = await client .get (url = comments_url ,
167
- )
168
- issue_comments_data = issue_comment_response .json ()
180
+ response = await client .get (url = comments_url , params = payload )
181
+
182
+ issue_comments_data = response .json ()
183
+
184
+ # Filter based on created_at if to_date is provided
185
+ if to_date :
186
+ issue_comments_data = [
187
+ comment for comment in issue_comments_data
188
+ if datetime .fromisoformat (comment ['created_at' ].replace ('Z' , '+00:00' )) <= to_date
189
+ ]
190
+
169
191
if len (issue_comments_data ) > 0 :
170
- page += 1
171
192
all_comments += issue_comments_data
193
+ page += 1
172
194
else :
173
195
break
196
+
174
197
return all_comments
175
198
176
199
async def get_pull_requests (self , token : str , repo_fullname , since : datetime ):
@@ -198,119 +221,122 @@ async def get_pull_requests(self, token: str, repo_fullname, since: datetime):
198
221
return all_prs
199
222
200
223
201
- async def main (self ):
224
+ async def main (self , from_date = None , to_date = None ):
202
225
start_time = time .time ()
203
- action_types = [ "labeled" ]
226
+ logger . info ( f"Cron triggered" )
204
227
engine = create_async_engine (get_postgres_uri (), echo = False , poolclass = NullPool )
205
- async_session = sessionmaker (autocommit = False , autoflush = False , bind = engine , class_ = AsyncSession )
206
- issue_handler = IssuesHandler ()
207
- pr_handler = Pull_requestHandler ()
208
228
jwt_token = self .jwt_generator .__call__ ()
209
- # jwt_token = self.get_github_jwt()
229
+
210
230
jwt_headers = {
211
231
"Accept" : "application/vnd.github+json" ,
212
232
"Authorization" : f"Bearer { jwt_token } " ,
213
233
"X-GitHub-Api-Version" : "2022-11-28"
214
234
}
235
+
215
236
installations = await self .get_installations (jwt_headers )
216
237
access_tokens = {installation .get ('id' ): await self .get_access_token (jwt_headers , installation .get ('id' )) for
217
- installation in installations }
218
- # print(access_tokens)
238
+ installation in installations }
239
+
240
+
219
241
all_issue_ids = set ()
220
242
all_comment_ids = set ()
221
243
all_pr_id = set ()
222
244
223
- original_issue = await self .postgres_client .readAll ("issues" )
224
- original_prs = await self .postgres_client .readAll ("pr_history" )
225
- original_orgs = await self .postgres_client .readAll ("community_orgs" )
226
-
245
+ # Parse date params
246
+ since = datetime .now () - timedelta (days = 1 ) # Default to 1 day before
247
+ if from_date :
248
+ since = datetime .fromisoformat (from_date )
249
+ to_date = datetime .fromisoformat (to_date ).replace (tzinfo = timezone .utc ) if to_date else None
227
250
228
251
for installation in installations :
229
- time .sleep (1 )
230
- # token = await self.get_access_token(jwt_headers, installation.get('id'))
252
+ logger .info (f"Installation: " , installation )
231
253
token = access_tokens .get (installation .get ('id' ))
232
254
if not token :
233
- print (f"Error in " )
255
+ print (f"Error in installation { installation . get ( 'id' ) } " )
234
256
continue
257
+
235
258
repos = await self .get_repos (token )
236
259
for repo in repos :
237
260
repo_name = repo .get ("full_name" )
238
- since = (datetime .now () - timedelta (days = 1 ))
239
- issues = await self .get_issues (token , since , repo_name )
240
-
241
- # process issues
242
- processed_issues = await self .process_cron_issues (issues ,
243
- all_issue_ids ,
244
- all_comment_ids ,
245
- token = token )
246
-
247
- #process prs
248
- pull_requests = await self .get_pull_requests (token ,
249
- repo_name ,
250
- since )
261
+ logger .info (f"Repository: { repo_name } " )
262
+ issues = await self .get_issues (token , since , repo_name , to_date )
263
+
264
+ # Pass from_date and to_date to process_cron_issues
265
+ processed_issues = await self .process_cron_issues (
266
+ issues ,
267
+ all_issue_ids ,
268
+ all_comment_ids ,
269
+ from_date = since ,
270
+ to_date = to_date ,
271
+ token = token
272
+ )
273
+
274
+ # pull_requests = await self.get_pull_requests(token, repo_name, since)
275
+ # processed_prs = await self.process_cron_prs(pull_requests, all_pr_id)
251
276
252
- processed_prs = await self .process_cron_prs (pull_requests , all_pr_id )
253
- print ('finished cron' )
254
-
255
-
256
- #purge remaining issues, comments
257
277
await self .purge_issues_comments (all_issue_ids , all_comment_ids )
258
278
259
-
260
- new_issues_length = len (all_issue_ids )
261
- new_prs_length = len (all_pr_id )
262
- new_orgs = await self .postgres_client .readAll ("community_orgs" )
263
- new_orgs_length = len (new_orgs )
264
- #share report
265
-
266
- original_issue_length = len (original_issue )
267
- original_pr_length = len (original_prs )
268
- original_orgs_length = len (original_orgs )
269
279
end_time = time .time ()
270
-
271
280
time_taken = end_time - start_time
272
- # await self.send_discord_report(original_issue_length, new_issues_length, original_pr_length, new_prs_length, original_orgs_length, new_orgs_length, time_taken)
273
-
274
- async def process_cron_issues (self , issues , issue_ids_list , all_comment_ids , ** kwargs ):
281
+ # await self.send_discord_report(
282
+ # len(original_issue),
283
+ # len(all_issue_ids),
284
+ # len(original_prs),
285
+ # len(all_pr_id),
286
+ # len(original_orgs),
287
+ # len(await self.postgres_client.readAll("community_orgs")),
288
+ # time_taken
289
+ # )
290
+
291
+ async def process_cron_issues (self , issues , issue_ids_list , all_comment_ids , from_date = None , to_date = None ,
292
+ ** kwargs ):
275
293
try :
276
294
token = kwargs .get ("token" , None )
277
295
issue_handler = IssuesHandler ()
278
-
296
+
279
297
for issue in issues :
280
298
try :
281
- time . sleep ( 1 )
299
+ logger . info ( f"Issue: { issue . get ( 'html_url' ) } " )
282
300
issue_ids_list .add (issue ["id" ])
283
301
state = f'{ issue ["state" ]} ed'
284
302
state = state .replace ('eded' , 'ed' )
285
- data = {
286
- "action" :state ,
287
- "issue" :issue
303
+ data = {
304
+ "action" : state ,
305
+ "issue" : issue
288
306
}
289
307
if token is not None :
290
- await issue_handler .handle_event (data = data ,
291
- postgres_client = 'client' ,
292
- token = token )
308
+ await issue_handler .handle_event (
309
+ data = data ,
310
+ postgres_client = 'client' ,
311
+ token = token
312
+ )
293
313
else :
294
- await issue_handler .handle_event (data = data ,
295
- postgres_client = 'client' )
296
-
297
-
298
- #process issue comments
299
- since = (datetime .now () - timedelta (days = 1 ))
300
- all_comments = await self .get_issue_comments (issue ["comments_url" ],
301
- since = since ,
302
- token = token )
303
- processed_comments = await self .process_cron_issue_comments (issue , all_comments , all_comment_ids )
304
- # rate_limts = await self.get_rate_limits(token)
305
- # print(rate_limts)
314
+ await issue_handler .handle_event (
315
+ data = data ,
316
+ postgres_client = 'client'
317
+ )
318
+
319
+ # Process issue comments with from_date and to_date
320
+ since = from_date or (datetime .now () - timedelta (days = 1 ))
321
+ all_comments = await self .get_issue_comments (
322
+ issue ["comments_url" ],
323
+ since = since ,
324
+ to_date = to_date ,
325
+ token = token
326
+ )
327
+ time .sleep (1 )
328
+ processed_comments = await self .process_cron_issue_comments (
329
+ issue , all_comments , all_comment_ids
330
+ )
331
+
306
332
except Exception as e :
307
- print ("Exeption in issue - " , issue )
333
+ print ("Exception in issue - " , issue , e )
308
334
continue
309
-
335
+
310
336
return 'issues processed'
311
337
312
338
except Exception as e :
313
- print ('Exception occured in process_cron_issues ' , e )
339
+ print ('Exception occurred in process_cron_issues: ' , e )
314
340
return e
315
341
316
342
async def get_issue_data (self , issue_url ):
@@ -381,6 +407,7 @@ async def process_cron_prs(self, pull_requests, all_pr_id):
381
407
pr_handler = Pull_requestHandler ()
382
408
for pr in pull_requests :
383
409
try :
410
+ logger .info (f"PR: { pr .get ('html_url' )} " )
384
411
all_pr_id .add (pr ["id" ])
385
412
await pr_handler .handle_event (
386
413
data = {"action" : "closed" if pr ["state" ] == "close" else "opened" ,
@@ -466,4 +493,7 @@ async def send_discord_report(self, original_issue_length, new_issues_length, or
466
493
467
494
if __name__ == '__main__' :
468
495
cronjob = CronJob ()
469
- asyncio .run (cronjob .main ())
496
+ from_date = "2025-03-01T00:00:00"
497
+ to_date = "2025-03-11T00:00:00"
498
+ asyncio .run (cronjob .main (from_date ,to_date ))
499
+
0 commit comments