From b67d06ae59dc74772121140b5f0d13646e34bf19 Mon Sep 17 00:00:00 2001 From: Ori Kotek Date: Wed, 29 Nov 2023 11:47:52 +0200 Subject: [PATCH] "Add fallback to environment variables in GitHub Action Runner" A new function `get_setting_or_env` was implemented to facilitate fetching of either settings or environmental variables in the GitHub Action Runner. This was necessary to resolve an issue where a certain undefined behaviour occurs in GitHub Actions, leading to an attribute error. The new function also provides a default value parameter to ensure the return of a value even in failed attempts to fetch from either settings or environment variables. --- pr_agent/servers/github_action_runner.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pr_agent/servers/github_action_runner.py b/pr_agent/servers/github_action_runner.py index e81ad5b7..3a884407 100644 --- a/pr_agent/servers/github_action_runner.py +++ b/pr_agent/servers/github_action_runner.py @@ -21,6 +21,14 @@ def is_true(value: Union[str, bool]) -> bool: return False +def get_setting_or_env(key: str, default: Union[str, bool] = None) -> Union[str, bool]: + try: + value = get_settings().get(key, default) + except AttributeError: # TBD still need to debug why this happens on GitHub Actions + value = os.getenv(key, None) or os.getenv(key.upper(), None) or os.getenv(key.lower(), None) + return value + + async def run_action(): # Get environment variables GITHUB_EVENT_NAME = os.environ.get('GITHUB_EVENT_NAME') @@ -74,13 +82,13 @@ async def run_action(): if action in ["opened", "reopened"]: pr_url = event_payload.get("pull_request", {}).get("url") if pr_url: - auto_review = get_settings().get('GITHUB_ACTION.AUTO_REVIEW', None) + auto_review = get_setting_or_env("GITHUB_ACTION.AUTO_REVIEW", None) 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) + auto_describe = get_setting_or_env("GITHUB_ACTION.AUTO_DESCRIBE", None) if is_true(auto_describe): await PRDescription(pr_url).run() - auto_improve = get_settings().get('GITHUB_ACTION.AUTO_IMPROVE', None) + auto_improve = get_setting_or_env("GITHUB_ACTION.AUTO_IMPROVE", None) if is_true(auto_improve): await PRCodeSuggestions(pr_url).run() @@ -108,4 +116,4 @@ async def run_action(): if __name__ == '__main__': - asyncio.run(run_action()) \ No newline at end of file + asyncio.run(run_action())