added github action support

This commit is contained in:
mrT23
2024-02-16 14:49:01 +02:00
parent 40fbd55da4
commit c98e736e3b
10 changed files with 31 additions and 18 deletions

View File

@ -444,7 +444,7 @@ class AzureDevopsProvider(GitProvider):
"Azure DevOps provider does not support issue comments yet" "Azure DevOps provider does not support issue comments yet"
) )
def add_eyes_reaction(self, issue_comment_id: int) -> Optional[int]: def add_eyes_reaction(self, issue_comment_id: int, disable_eyes: bool = False) -> Optional[int]:
return True return True
def remove_reaction(self, issue_comment_id: int, reaction_id: int) -> bool: def remove_reaction(self, issue_comment_id: int, reaction_id: int) -> bool:

View File

@ -298,7 +298,7 @@ class BitbucketProvider(GitProvider):
"Bitbucket provider does not support issue comments yet" "Bitbucket provider does not support issue comments yet"
) )
def add_eyes_reaction(self, issue_comment_id: int) -> Optional[int]: def add_eyes_reaction(self, issue_comment_id: int, disable_eyes: bool = False) -> Optional[int]:
return True return True
def remove_reaction(self, issue_comment_id: int, reaction_id: int) -> bool: def remove_reaction(self, issue_comment_id: int, reaction_id: int) -> bool:

View File

@ -288,7 +288,7 @@ class BitbucketServerProvider(GitProvider):
"Bitbucket provider does not support issue comments yet" "Bitbucket provider does not support issue comments yet"
) )
def add_eyes_reaction(self, issue_comment_id: int) -> Optional[int]: def add_eyes_reaction(self, issue_comment_id: int, disable_eyes: bool = False) -> Optional[int]:
return True return True
def remove_reaction(self, issue_comment_id: int, reaction_id: int) -> bool: def remove_reaction(self, issue_comment_id: int, reaction_id: int) -> bool:

View File

@ -297,7 +297,7 @@ class CodeCommitProvider(GitProvider):
settings_filename = ".pr_agent.toml" settings_filename = ".pr_agent.toml"
return self.codecommit_client.get_file(self.repo_name, settings_filename, self.pr.source_commit, optional=True) return self.codecommit_client.get_file(self.repo_name, settings_filename, self.pr.source_commit, optional=True)
def add_eyes_reaction(self, issue_comment_id: int) -> Optional[int]: def add_eyes_reaction(self, issue_comment_id: int, disable_eyes: bool = False) -> Optional[int]:
get_logger().info("CodeCommit provider does not support eyes reaction yet") get_logger().info("CodeCommit provider does not support eyes reaction yet")
return True return True

View File

@ -212,7 +212,7 @@ class GerritProvider(GitProvider):
raise NotImplementedError( raise NotImplementedError(
'Getting labels is not implemented for the gerrit provider') 'Getting labels is not implemented for the gerrit provider')
def add_eyes_reaction(self, issue_comment_id: int): def add_eyes_reaction(self, issue_comment_id: int, disable_eyes: bool = False):
raise NotImplementedError( raise NotImplementedError(
'Adding reactions is not implemented for the gerrit provider') 'Adding reactions is not implemented for the gerrit provider')

View File

@ -162,7 +162,7 @@ class GitProvider(ABC):
pass pass
@abstractmethod @abstractmethod
def add_eyes_reaction(self, issue_comment_id: int) -> Optional[int]: def add_eyes_reaction(self, issue_comment_id: int, disable_eyes: bool = False) -> Optional[int]:
pass pass
@abstractmethod @abstractmethod

View File

@ -452,7 +452,9 @@ class GithubProvider(GitProvider):
except Exception: except Exception:
return "" return ""
def add_eyes_reaction(self, issue_comment_id: int) -> Optional[int]: def add_eyes_reaction(self, issue_comment_id: int, disable_eyes: bool = False) -> Optional[int]:
if disable_eyes:
return None
try: try:
reaction = self.pr.get_issue_comment(issue_comment_id).create_reaction("eyes") reaction = self.pr.get_issue_comment(issue_comment_id).create_reaction("eyes")
return reaction.id return reaction.id

View File

@ -368,7 +368,7 @@ class GitLabProvider(GitProvider):
except Exception: except Exception:
return "" return ""
def add_eyes_reaction(self, issue_comment_id: int) -> Optional[int]: def add_eyes_reaction(self, issue_comment_id: int, disable_eyes: bool = False) -> Optional[int]:
return True return True
def remove_reaction(self, issue_comment_id: int, reaction_id: int) -> bool: def remove_reaction(self, issue_comment_id: int, reaction_id: int) -> bool:

View File

@ -103,31 +103,38 @@ async def run_action():
await PRCodeSuggestions(pr_url).run() await PRCodeSuggestions(pr_url).run()
# Handle issue comment event # Handle issue comment event
elif GITHUB_EVENT_NAME == "issue_comment": elif GITHUB_EVENT_NAME == "issue_comment" or GITHUB_EVENT_NAME == "pull_request_review_comment":
action = event_payload.get("action") action = event_payload.get("action")
if action in ["created", "edited"]: if action in ["created", "edited"]:
comment_body = event_payload.get("comment", {}).get("body") comment_body = event_payload.get("comment", {}).get("body")
try:
if GITHUB_EVENT_NAME == "pull_request_review_comment":
if '/ask' in comment_body:
comment_body = handle_line_comments(event_payload, comment_body)
except Exception as e:
get_logger().error(f"Failed to handle line comments: {e}")
return
if comment_body: if comment_body:
is_pr = False is_pr = False
disable_eyes = False
# check if issue is pull request # check if issue is pull request
if event_payload.get("issue", {}).get("pull_request"): if event_payload.get("issue", {}).get("pull_request"):
url = event_payload.get("issue", {}).get("pull_request", {}).get("url") url = event_payload.get("issue", {}).get("pull_request", {}).get("url")
is_pr = True is_pr = True
elif event_payload.get("comment", {}).get("pull_request_url"): # for 'pull_request_review_comment
url = event_payload.get("comment", {}).get("pull_request_url")
is_pr = True
disable_eyes = True
else: else:
url = event_payload.get("issue", {}).get("url") url = event_payload.get("issue", {}).get("url")
try:
if 'subject_type' in event_payload["comment"] and event_payload["comment"]["subject_type"] == "line":
comment_body = handle_line_comments(event_payload, comment_body)
except Exception as e:
get_logger().error(f"Failed to handle line comments: {e}")
if url: if url:
body = comment_body.strip().lower() body = comment_body.strip().lower()
comment_id = event_payload.get("comment", {}).get("id") comment_id = event_payload.get("comment", {}).get("id")
provider = get_git_provider()(pr_url=url) provider = get_git_provider()(pr_url=url)
if is_pr: if is_pr:
await PRAgent().handle_request(url, body, notify=lambda: provider.add_eyes_reaction(comment_id)) await PRAgent().handle_request(url, body,
notify=lambda: provider.add_eyes_reaction(comment_id, disable_eyes=disable_eyes))
else: else:
await PRAgent().handle_request(url, body) await PRAgent().handle_request(url, body)

View File

@ -96,13 +96,16 @@ async def handle_request(body: Dict[str, Any], event: str):
get_logger().info(f"Ignoring comment from {bot_user} user") get_logger().info(f"Ignoring comment from {bot_user} user")
return {} return {}
get_logger().info(f"Processing comment from {sender} user") get_logger().info(f"Processing comment from {sender} user")
disable_eyes = False
if "issue" in body and "pull_request" in body["issue"] and "url" in body["issue"]["pull_request"]: if "issue" in body and "pull_request" in body["issue"] and "url" in body["issue"]["pull_request"]:
api_url = body["issue"]["pull_request"]["url"] api_url = body["issue"]["pull_request"]["url"]
elif "comment" in body and "pull_request_url" in body["comment"]: elif "comment" in body and "pull_request_url" in body["comment"]:
api_url = body["comment"]["pull_request_url"] api_url = body["comment"]["pull_request_url"]
try: try:
if 'subject_type' in body["comment"] and body["comment"]["subject_type"] == "line": if ('/ask' in comment_body and
'subject_type' in body["comment"] and body["comment"]["subject_type"] == "line"):
comment_body = handle_line_comments(body, comment_body) comment_body = handle_line_comments(body, comment_body)
disable_eyes = True
except Exception as e: except Exception as e:
get_logger().error(f"Failed to handle line comments: {e}") get_logger().error(f"Failed to handle line comments: {e}")
@ -114,7 +117,8 @@ async def handle_request(body: Dict[str, Any], event: str):
comment_id = body.get("comment", {}).get("id") comment_id = body.get("comment", {}).get("id")
provider = get_git_provider()(pr_url=api_url) provider = get_git_provider()(pr_url=api_url)
with get_logger().contextualize(**log_context): with get_logger().contextualize(**log_context):
await agent.handle_request(api_url, comment_body, notify=lambda: provider.add_eyes_reaction(comment_id)) await agent.handle_request(api_url, comment_body,
notify=lambda: provider.add_eyes_reaction(comment_id, disable_eyes=disable_eyes))
# handle pull_request event: # handle pull_request event:
# automatically review opened/reopened/ready_for_review PRs as long as they're not in draft, # automatically review opened/reopened/ready_for_review PRs as long as they're not in draft,