From 580eede021a3fcfc2f1e8fc1a403b6639539352c Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Wed, 29 Nov 2023 10:33:12 +0200 Subject: [PATCH] Add utility function to handle boolean type conversion A utility function (`is_true`) was added to take care of validating and converting boolean values from string or boolean types. This function is used in three parts of the `run_action` method where automatic PR review, description, and improvement actions are triggered based on environment settings. This change makes the condition checks cleaner and prevents code duplication. --- pr_agent/servers/github_action_runner.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pr_agent/servers/github_action_runner.py b/pr_agent/servers/github_action_runner.py index 697b8f0d..e81ad5b7 100644 --- a/pr_agent/servers/github_action_runner.py +++ b/pr_agent/servers/github_action_runner.py @@ -1,6 +1,7 @@ import asyncio import json import os +from typing import Union from pr_agent.agent.pr_agent import PRAgent from pr_agent.config_loader import get_settings @@ -12,6 +13,14 @@ from pr_agent.tools.pr_description import PRDescription from pr_agent.tools.pr_reviewer import PRReviewer +def is_true(value: Union[str, bool]) -> bool: + if isinstance(value, bool): + return value + if isinstance(value, str): + return value.lower() == 'true' + return False + + async def run_action(): # Get environment variables GITHUB_EVENT_NAME = os.environ.get('GITHUB_EVENT_NAME') @@ -66,13 +75,13 @@ async def run_action(): pr_url = event_payload.get("pull_request", {}).get("url") if pr_url: auto_review = get_settings().get('GITHUB_ACTION.AUTO_REVIEW', None) - if auto_review is None or (isinstance(auto_review, str) and auto_review.lower() == 'true'): + if auto_review is None or is_true(auto_review): await PRReviewer(pr_url).run() auto_describe = get_settings().get('GITHUB_ACTION.AUTO_DESCRIBE', None) - if isinstance(auto_describe, str) and auto_describe.lower() == 'true': + if is_true(auto_describe): await PRDescription(pr_url).run() auto_improve = get_settings().get('GITHUB_ACTION.AUTO_IMPROVE', None) - if isinstance(auto_improve, str) and auto_improve.lower() == 'true': + if is_true(auto_improve): await PRCodeSuggestions(pr_url).run() # Handle issue comment event