diff --git a/ggshield/core/check_updates.py b/ggshield/core/check_updates.py index 0b5b8645e9..68e7b78f3f 100644 --- a/ggshield/core/check_updates.py +++ b/ggshield/core/check_updates.py @@ -17,6 +17,8 @@ "update_check.yaml", ) +CHECK_AT_KEY = "check-at" + # Use a short timeout to prevent blocking CHECK_TIMEOUT = 5 @@ -26,6 +28,11 @@ def _split_version(version: str) -> Tuple[int, ...]: return tuple([int(x) for x in version.split(".")]) +def _save_check_time(check_at: float) -> None: + """Store `check_at` in our cache""" + save_yaml_dict({CHECK_AT_KEY: check_at}, CACHE_FILE) + + def check_for_updates() -> Optional[str]: """ Check for ggshield updates on GitHub. Return the latest version if available. @@ -40,7 +47,7 @@ def check_for_updates() -> Optional[str]: cached_data = None if cached_data is not None: try: - check_at = cached_data["check_at"] + check_at = cached_data[CHECK_AT_KEY] except Exception as e: logger.warning("Could not load cached latest version: %s", repr(e)) @@ -53,7 +60,7 @@ def check_for_updates() -> Optional[str]: # Save check time now so that it is saved even if the check fails. This ensures we # don't try for every command if the user does not have network access. try: - save_yaml_dict({"check_at": time.time()}, CACHE_FILE) + _save_check_time(time.time()) except Exception as e: logger.warning("Could not save time of version check to cache: %s", repr(e)) # Do not continue if we can't save check time. If we continue we are going to @@ -89,7 +96,7 @@ def check_for_updates() -> Optional[str]: return None try: - save_yaml_dict({"check_at": check_at}, CACHE_FILE) + _save_check_time(check_at) except Exception as e: logger.warning( "Could not save time of version check to cache: %s", repr(e)