From 3d13ae755aa27881a6cd38855bb4ba5c94eee28f Mon Sep 17 00:00:00 2001 From: Kumi Date: Sun, 18 Feb 2024 13:21:39 +0100 Subject: [PATCH] Enhance env var handling in chore script Refactored the chores script to improve the robustness of environment variable handling. This includes mandatory checks for the FORGEJO_TOKEN and default values or prompts for missing optional environment variables. Added assertion to ensure the token is present after retrieval which, combined with the KeyError, will provide better error handling. Optional variables now use a fallback value directly in the get method for concise expression. Resolves issue with silent failures when required environment settings are not provided. --- scripts/chores.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/scripts/chores.py b/scripts/chores.py index deebe4f..ec8efdf 100644 --- a/scripts/chores.py +++ b/scripts/chores.py @@ -11,18 +11,21 @@ FORGEJO_URL = os.environ.get("FORGEJO_URL", "https://git.private.coffee") try: TOKEN = os.environ["FORGEJO_TOKEN"] -except KeyError: + assert TOKEN +except (KeyError, AssertionError): logging.error("FORGEJO_TOKEN environment variable is required") exit(1) -ASSIGNEES = os.environ.get("CHORES_ASSIGNEES", "").split() -REPO_OWNER = os.environ.get("CHORES_REPO_OWNER", "PrivateCoffee") -REPO_NAME = os.environ.get("CHORES_REPO_NAME", "chores") -EXPIRY_DAYS = int(os.environ.get("CHORES_EXPIRY_DAYS", 2)) -CHORES = os.environ.get( - "CHORES_TEXT", "Looks like you forgot to set the CHORES_TEXT environment variable." +ASSIGNEES = os.environ.get("CHORES_ASSIGNEES").split() +REPO_OWNER = os.environ.get("CHORES_REPO_OWNER") or "PrivateCoffee" +REPO_NAME = os.environ.get("CHORES_REPO_NAME") or "chores" +EXPIRY_DAYS = int(os.environ.get("CHORES_EXPIRY_DAYS") or 2) +CHORES = ( + os.environ.get("CHORES_TEXT") + or "Looks like you forgot to set the CHORES_TEXT environment variable." ) + due_date = (datetime.utcnow() + timedelta(days=EXPIRY_DAYS)).isoformat() payload = {