diff --git a/pyproject.toml b/pyproject.toml index aad2b4dbc..28633465b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -60,6 +60,7 @@ warn_unreachable = true [[tool.mypy.overrides]] module = [ "apiai.*", + "backports.datetime_fromisoformat.*", "feedparser.*", "gitlint.*", "google.auth.*", diff --git a/zulip/integrations/openshift/post_deploy b/zulip/integrations/openshift/post_deploy index da2bdb912..4384f98c1 100755 --- a/zulip/integrations/openshift/post_deploy +++ b/zulip/integrations/openshift/post_deploy @@ -37,6 +37,8 @@ def get_deployment_details() -> Dict[str, str]: url=os.environ["OPENSHIFT_APP_DNS"], branch=splits[2], commit_id=splits[3], + dep_id=splits[1], + dep_time=splits[0], ) diff --git a/zulip/integrations/openshift/zulip_openshift_config.py b/zulip/integrations/openshift/zulip_openshift_config.py index ef71bfc57..98e5b1de0 100644 --- a/zulip/integrations/openshift/zulip_openshift_config.py +++ b/zulip/integrations/openshift/zulip_openshift_config.py @@ -1,6 +1,8 @@ # https://github.com/python/mypy/issues/1141 from typing import Dict, Optional +import zulip + # Change these values to configure authentication for the plugin ZULIP_USER = "openshift-bot@example.com" ZULIP_API_KEY = "0123456789abcdef0123456789abcdef" @@ -28,6 +30,14 @@ def deployment_notice_destination(branch: str) -> Optional[Dict[str, str]]: return None +# To utilize Zulip's global times (https://zulip.com/help/global-times) +# call this function from format_deployment_message, and use the +# returned string in your message template. +def get_global_time(dt_str: str) -> str: + dt = zulip.datetime_fromisoformat(dt_str) + return zulip.datetime_to_global_time(dt) + + # Modify this function to change how deployments are displayed # # It takes the following arguments: @@ -39,9 +49,6 @@ def deployment_notice_destination(branch: str) -> Optional[Dict[str, str]]: # * commit_id = hash of the commit that triggered the deployment # * dep_id = deployment id # * dep_time = deployment timestamp -# -# To utilize Zulip's global times (https://zulip.com/help/global-times), -# use the syntax ``. def format_deployment_message( app_name: str = "", url: str = "", diff --git a/zulip/setup.py b/zulip/setup.py index 137fc29ee..097bc684b 100755 --- a/zulip/setup.py +++ b/zulip/setup.py @@ -64,6 +64,7 @@ def recur_expand(target_root: Any, dir: Any) -> Generator[Tuple[str, List[str]], ], }, install_requires=[ + "backports-datetime-fromisoformat; python_version < '3.11'", "requests[security]>=0.12.1", "distro", "click", diff --git a/zulip/zulip/__init__.py b/zulip/zulip/__init__.py index c8d6955de..93fbc896f 100644 --- a/zulip/zulip/__init__.py +++ b/zulip/zulip/__init__.py @@ -11,6 +11,7 @@ import types import urllib.parse from configparser import ConfigParser +from datetime import datetime from typing import ( IO, Any, @@ -34,6 +35,13 @@ # Ensure the Python version is supported assert sys.version_info >= (3, 6) +if sys.version_info < (3, 11): + from backports.datetime_fromisoformat import ( + datetime_fromisoformat as datetime_fromisoformat, # noqa: PLC0414 + ) +else: + datetime_fromisoformat = datetime.fromisoformat + logger = logging.getLogger(__name__) API_VERSTRING = "v1/" @@ -351,6 +359,12 @@ def validate_boolean_field(field: Optional[str]) -> Union[bool, None]: return None +def datetime_to_global_time(dt: datetime) -> str: + if dt.tzinfo is None: + raise TimeZoneMissingError(f"Datetime {dt} does not have a time zone.") + return f"" + + class ZulipError(Exception): pass @@ -367,6 +381,10 @@ class UnrecoverableNetworkError(ZulipError): pass +class TimeZoneMissingError(ZulipError): + pass + + class Client: def __init__( self,