Skip to content

Commit

Permalink
chore: fprmat
Browse files Browse the repository at this point in the history
  • Loading branch information
xingwanying committed Aug 18, 2024
1 parent df18fa9 commit 6c65cd1
Showing 1 changed file with 18 additions and 34 deletions.
52 changes: 18 additions & 34 deletions server/event_handler/discussion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import requests
from typing import Any
from github import Github, Auth
Expand All @@ -7,7 +6,8 @@

from petercat_utils.data_class import ChatData, Message, TextContentBlock

class DiscussionEventHandler():

class DiscussionEventHandler:
event: Any
auth: Auth.AppAuth
installation_id: int
Expand All @@ -20,12 +20,12 @@ def __init__(self, payload: Any, auth: Auth.AppAuth, installation_id: int) -> No
self.installation_id = installation_id
self.g: Github = Github(auth=auth)
self.graph_url = "https://api.github.com/graphql"

async def get_discussion_id(self, owner: str, repo: str, discussion_number: int):
access_token= self.auth.token
access_token = self.auth.token
headers = {
"Authorization": f"Bearer {access_token}",
"Accept": "application/vnd.github.v3+json"
"Accept": "application/vnd.github.v3+json",
}
query = """
query($owner: String!, $repo: String!, $discussionNumber: Int!) {
Expand All @@ -39,27 +39,23 @@ async def get_discussion_id(self, owner: str, repo: str, discussion_number: int)
variables = {
"owner": owner,
"repo": repo,
"discussionNumber": discussion_number
}
json_data={
"query": query,
"variables": variables
"discussionNumber": discussion_number,
}
json_data = {"query": query, "variables": variables}
response = requests.post(self.graph_url, headers=headers, json=json_data)

if response.status_code == 200:
print("获取讨论成功!")
return response.json()['data']['repository']['discussion']['id']
return response.json()["data"]["repository"]["discussion"]["id"]
else:
print(f"出现错误:{response.status_code}")
print(response.json())


async def create_discussion_comment(self, discussion_id: int, comment_body: str):
access_token= self.auth.token
access_token = self.auth.token
headers = {
"Authorization": f"Bearer {access_token}",
"Accept": "application/vnd.github.v3+json"
"Accept": "application/vnd.github.v3+json",
}
query = """
mutation($discussionId: ID!, $body: String!) {
Expand All @@ -72,15 +68,9 @@ async def create_discussion_comment(self, discussion_id: int, comment_body: str)
}
}
"""
variables = {
"discussionId": discussion_id,
"body": comment_body
}

json_data={
"query": query,
"variables": variables
}
variables = {"discussionId": discussion_id, "body": comment_body}

json_data = {"query": query, "variables": variables}
response = requests.post(self.graph_url, headers=headers, json=json_data)

if response.status_code == 200:
Expand All @@ -90,25 +80,21 @@ async def create_discussion_comment(self, discussion_id: int, comment_body: str)
print(response.json())

async def handle_discussion_event(self, action: str):
owner = self.event['organization']["login"]
repo = self.event['repository']["name"]
owner = self.event["organization"]["login"]
repo = self.event["repository"]["name"]
discussion = self.event["discussion"]
discussion_content = f"{discussion['title']}: {discussion['body']}"
text_block = TextContentBlock(
type="text",
text=discussion_content
)
text_block = TextContentBlock(type="text", text=discussion_content)
discussion_number = discussion["number"]
message = Message(role="user", content=[text_block])

analysis_result = await agent_chat(ChatData(messages=[message]))
discussion_id = await self.get_discussion_id(owner, repo, discussion_number)
await self.create_discussion_comment(discussion_id, analysis_result['output'])
await self.create_discussion_comment(discussion_id, analysis_result["output"])


async def execute(self):
try:
action = self.event['action']
action = self.event["action"]
if action in ["opened", "reopened"]:
await self.handle_discussion_event(action)
return {"success": True}
Expand All @@ -119,5 +105,3 @@ async def execute(self):
except GithubException as e:
print(f"处理 GitHub 请求时出错: {e}")
return {"success": False, "error": str(e)}


0 comments on commit 6c65cd1

Please sign in to comment.