"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.
This commit is contained in:
Ori Kotek
2023-11-29 11:47:52 +02:00
parent 580eede021
commit b67d06ae59

View File

@ -21,6 +21,14 @@ def is_true(value: Union[str, bool]) -> bool:
return False 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(): async def run_action():
# Get environment variables # Get environment variables
GITHUB_EVENT_NAME = os.environ.get('GITHUB_EVENT_NAME') GITHUB_EVENT_NAME = os.environ.get('GITHUB_EVENT_NAME')
@ -74,13 +82,13 @@ async def run_action():
if action in ["opened", "reopened"]: if action in ["opened", "reopened"]:
pr_url = event_payload.get("pull_request", {}).get("url") pr_url = event_payload.get("pull_request", {}).get("url")
if pr_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): if auto_review is None or is_true(auto_review):
await PRReviewer(pr_url).run() 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): if is_true(auto_describe):
await PRDescription(pr_url).run() 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): if is_true(auto_improve):
await PRCodeSuggestions(pr_url).run() await PRCodeSuggestions(pr_url).run()
@ -108,4 +116,4 @@ async def run_action():
if __name__ == '__main__': if __name__ == '__main__':
asyncio.run(run_action()) asyncio.run(run_action())