mirror of
https://github.com/qodo-ai/pr-agent.git
synced 2025-07-04 21:00:40 +08:00
Add exception protection for unexpected conditions during request handling
This commit is contained in:
@ -37,50 +37,54 @@ async def polling_loop():
|
|||||||
raise ValueError("User token must be set to get notifications")
|
raise ValueError("User token must be set to get notifications")
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
while True:
|
while True:
|
||||||
headers = {
|
try:
|
||||||
"Accept": "application/vnd.github.v3+json",
|
headers = {
|
||||||
"Authorization": f"Bearer {token}"
|
"Accept": "application/vnd.github.v3+json",
|
||||||
}
|
"Authorization": f"Bearer {token}"
|
||||||
params = {
|
}
|
||||||
"participating": "true"
|
params = {
|
||||||
}
|
"participating": "true"
|
||||||
if since[0]:
|
}
|
||||||
params["since"] = since[0]
|
if since[0]:
|
||||||
if last_modified[0]:
|
params["since"] = since[0]
|
||||||
headers["If-Modified-Since"] = last_modified[0]
|
if last_modified[0]:
|
||||||
async with session.get(NOTIFICATION_URL, headers=headers, params=params) as response:
|
headers["If-Modified-Since"] = last_modified[0]
|
||||||
if response.status == 200:
|
async with session.get(NOTIFICATION_URL, headers=headers, params=params) as response:
|
||||||
if 'Last-Modified' in response.headers:
|
if response.status == 200:
|
||||||
last_modified[0] = response.headers['Last-Modified']
|
if 'Last-Modified' in response.headers:
|
||||||
since[0] = None
|
last_modified[0] = response.headers['Last-Modified']
|
||||||
notifications = await response.json()
|
since[0] = None
|
||||||
for notification in notifications:
|
notifications = await response.json()
|
||||||
if 'id' in notification and notification['id'] in handled_ids:
|
for notification in notifications:
|
||||||
continue
|
if 'id' in notification and notification['id'] in handled_ids:
|
||||||
handled_ids.add(notification['id'])
|
continue
|
||||||
if 'reason' in notification and notification['reason'] == 'mention':
|
handled_ids.add(notification['id'])
|
||||||
if 'subject' in notification and notification['subject']['type'] == 'PullRequest':
|
if 'reason' in notification and notification['reason'] == 'mention':
|
||||||
pr_url = notification['subject']['url']
|
if 'subject' in notification and notification['subject']['type'] == 'PullRequest':
|
||||||
latest_comment = notification['subject']['latest_comment_url']
|
pr_url = notification['subject']['url']
|
||||||
async with session.get(latest_comment, headers=headers) as comment_response:
|
latest_comment = notification['subject']['latest_comment_url']
|
||||||
if comment_response.status == 200:
|
async with session.get(latest_comment, headers=headers) as comment_response:
|
||||||
comment = await comment_response.json()
|
if comment_response.status == 200:
|
||||||
if 'user' in comment and 'login' in comment['user']:
|
comment = await comment_response.json()
|
||||||
if comment['user']['login'] == user_id:
|
if 'user' in comment and 'login' in comment['user']:
|
||||||
|
if comment['user']['login'] == user_id:
|
||||||
|
continue
|
||||||
|
comment_body = comment['body'] if 'body' in comment else ''
|
||||||
|
commenter_github_user = comment['user']['login'] if 'user' in comment else ''
|
||||||
|
logging.info(f"Commenter: {commenter_github_user}\nComment: {comment_body}")
|
||||||
|
user_tag = "@" + user_id
|
||||||
|
if user_tag not in comment_body:
|
||||||
continue
|
continue
|
||||||
comment_body = comment['body'] if 'body' in comment else ''
|
rest_of_comment = comment_body.split(user_tag)[1].strip()
|
||||||
commenter_github_user = comment['user']['login'] if 'user' in comment else ''
|
agent = PRAgent()
|
||||||
logging.info(f"Commenter: {commenter_github_user}\nComment: {comment_body}")
|
await agent.handle_request(pr_url, rest_of_comment)
|
||||||
user_tag = "@" + user_id
|
elif response.status != 304:
|
||||||
if user_tag not in comment_body:
|
print(f"Failed to fetch notifications. Status code: {response.status}")
|
||||||
continue
|
|
||||||
rest_of_comment = comment_body.split(user_tag)[1].strip()
|
|
||||||
agent = PRAgent()
|
|
||||||
await agent.handle_request(pr_url, rest_of_comment)
|
|
||||||
elif response.status != 304:
|
|
||||||
print(f"Failed to fetch notifications. Status code: {response.status}")
|
|
||||||
|
|
||||||
await asyncio.sleep(5)
|
await asyncio.sleep(5)
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Exception during processing of a notification: {e}")
|
||||||
|
await asyncio.sleep(5)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
asyncio.run(polling_loop())
|
asyncio.run(polling_loop())
|
||||||
|
Reference in New Issue
Block a user